Skip to content

Commit

Permalink
extend SpendBundleConditions with the cost broken out into execution_…
Browse files Browse the repository at this point in the history
…time and condition_time (as well as the existing total cost)
  • Loading branch information
arvidn committed Jan 11, 2025
1 parent 4444a22 commit ed72a1b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 7 deletions.
9 changes: 9 additions & 0 deletions crates/chia-consensus/src/gen/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,12 @@ pub struct SpendBundleConditions {
// the total cost)
pub cost: u64,

// the cost of executing the Chialisp
pub execution_cost: u64,

// the cost of the conditions
pub condition_cost: u64,

// the sum of all values of all spent coins
pub removal_amount: u128,

Expand Down Expand Up @@ -921,6 +927,7 @@ pub fn parse_conditions<V: SpendVisitor>(
return Err(ValidationErr(c, ErrorCode::CostExceeded));
}
*max_cost -= CREATE_COIN_COST;
ret.condition_cost += CREATE_COIN_COST;
}
AGG_SIG_UNSAFE
| AGG_SIG_ME
Expand All @@ -934,6 +941,7 @@ pub fn parse_conditions<V: SpendVisitor>(
return Err(ValidationErr(c, ErrorCode::CostExceeded));
}
*max_cost -= AGG_SIG_COST;
ret.condition_cost += AGG_SIG_COST;
}
_ => (),
}
Expand Down Expand Up @@ -1212,6 +1220,7 @@ pub fn parse_conditions<V: SpendVisitor>(
return Err(ValidationErr(c, ErrorCode::CostExceeded));
}
*max_cost -= cost;
ret.condition_cost += cost;
}
Condition::SendMessage(src_mode, dst, msg) => {
decrement(&mut announce_countdown, msg)?;
Expand Down
4 changes: 4 additions & 0 deletions crates/chia-consensus/src/gen/owned_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub struct OwnedSpendBundleConditions {
// set if the aggregate signature of the block/spend bundle was
// successfully validated
pub validated_signature: bool,
pub execution_cost: u64,
pub condition_cost: u64,
}

impl OwnedSpendConditions {
Expand Down Expand Up @@ -144,6 +146,8 @@ impl OwnedSpendBundleConditions {
removal_amount: sb.removal_amount,
addition_amount: sb.addition_amount,
validated_signature: sb.validated_signature,
execution_cost: sb.execution_cost,
condition_cost: sb.condition_cost,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/chia-consensus/src/gen/run_block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ where
constants,
)?;
result.cost += max_cost - cost_left;
result.execution_cost = clvm_cost;
Ok(result)
}

Expand Down
1 change: 1 addition & 0 deletions crates/chia-consensus/src/spendbundle_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn run_spendbundle(
let amount = a.new_number(coin_spend.coin.amount.into())?;
let Reduction(clvm_cost, conditions) = run_program(a, &dialect, puz, sol, cost_left)?;

ret.execution_cost += clvm_cost;
subtract_cost(a, &mut cost_left, clvm_cost)?;

let buf = tree_hash(a, puz);
Expand Down
14 changes: 9 additions & 5 deletions tests/test_streamable.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def test_hash_spend() -> None:
def test_hash_spend_bundle_conditions() -> None:

a1 = SpendBundleConditions(
[], 1000, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False
[], 1000, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False, 4321, 8765
)
a2 = SpendBundleConditions(
[], 1001, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False
[], 1001, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False, 4321, 8765
)
b = hash(a1)
c = hash(a2)
Expand Down Expand Up @@ -391,7 +391,7 @@ def test_missing_field() -> None:
def test_json_spend_bundle_conditions() -> None:

a = SpendBundleConditions(
[], 1000, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False
[], 1000, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False, 4321, 8765
)

assert a.to_json_dict() == {
Expand All @@ -406,13 +406,15 @@ def test_json_spend_bundle_conditions() -> None:
"removal_amount": 123,
"addition_amount": 456,
"validated_signature": False,
"execution_cost": 4321,
"condition_cost": 8765,
}


def test_from_json_spend_bundle_conditions() -> None:

a = SpendBundleConditions(
[], 1000, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False
[], 1000, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False, 4321, 8765
)
b = SpendBundleConditions.from_json_dict(
{
Expand All @@ -427,6 +429,8 @@ def test_from_json_spend_bundle_conditions() -> None:
"removal_amount": 123,
"addition_amount": 456,
"validated_signature": False,
"execution_cost": 4321,
"condition_cost": 8765,
}
)
assert a == b
Expand Down Expand Up @@ -468,7 +472,7 @@ def test_copy_spend() -> None:
def test_copy_spend_bundle_conditions() -> None:

a = SpendBundleConditions(
[], 1000, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False
[], 1000, 1337, 42, None, None, [(pk, b"msg")], 12345678, 123, 456, False, 4321, 8765
)
b = copy.copy(a)

Expand Down
2 changes: 2 additions & 0 deletions wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ def __init__(
"removal_amount: int",
"addition_amount: int",
"validated_signature: bool",
"execution_cost: int",
"condition_cost: int",
],
)

Expand Down
10 changes: 8 additions & 2 deletions wheel/python/chia_rs/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ class SpendBundleConditions:
removal_amount: int
addition_amount: int
validated_signature: bool
execution_cost: int
condition_cost: int
def __init__(
self,
spends: Sequence[SpendConditions],
Expand All @@ -362,7 +364,9 @@ class SpendBundleConditions:
cost: int,
removal_amount: int,
addition_amount: int,
validated_signature: bool
validated_signature: bool,
execution_cost: int,
condition_cost: int
) -> None: ...
def __hash__(self) -> int: ...
def __repr__(self) -> str: ...
Expand Down Expand Up @@ -391,7 +395,9 @@ class SpendBundleConditions:
cost: Union[ int, _Unspec] = _Unspec(),
removal_amount: Union[ int, _Unspec] = _Unspec(),
addition_amount: Union[ int, _Unspec] = _Unspec(),
validated_signature: Union[ bool, _Unspec] = _Unspec()) -> SpendBundleConditions: ...
validated_signature: Union[ bool, _Unspec] = _Unspec(),
execution_cost: Union[ int, _Unspec] = _Unspec(),
condition_cost: Union[ int, _Unspec] = _Unspec()) -> SpendBundleConditions: ...

@final
class BlockRecord:
Expand Down

0 comments on commit ed72a1b

Please sign in to comment.