Skip to content

Commit

Permalink
CHIA-1034 Rename Spend to SpendConditions and OwnedSpend to `Ow…
Browse files Browse the repository at this point in the history
…nedSpendConditions` (#656)

* initial commit

* change python binding for Spend to SpendConditions

* run generate_type_stubs

* add Spend alias for SpendConditions

* remove unused import and black spend.py

* fuzz use SpendConditions

* fix test_generators

* fmt

* LF line ending

* LF line endings for test_streamable
  • Loading branch information
matt-o-how committed Aug 13, 2024
1 parent 81cd735 commit 2e0331f
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 135 deletions.
4 changes: 2 additions & 2 deletions crates/chia-consensus/fuzz/fuzz_targets/parse-conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use libfuzzer_sys::fuzz_target;

use chia_consensus::consensus_constants::TEST_CONSTANTS;
use chia_consensus::gen::conditions::{
parse_conditions, MempoolVisitor, ParseState, Spend, SpendBundleConditions,
parse_conditions, MempoolVisitor, ParseState, SpendBundleConditions, SpendConditions,
};
use chia_consensus::gen::spend_visitor::SpendVisitor;
use chia_protocol::Bytes32;
Expand Down Expand Up @@ -41,7 +41,7 @@ fuzz_target!(|data: &[u8]| {
let mut state = ParseState::default();

for flags in &[0, STRICT_ARGS_COUNT, NO_UNKNOWN_CONDS] {
let mut coin_spend = Spend {
let mut coin_spend = SpendConditions {
parent_id,
coin_amount: amount,
puzzle_hash,
Expand Down
166 changes: 83 additions & 83 deletions crates/chia-consensus/src/gen/conditions.rs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions crates/chia-consensus/src/gen/make_aggsig_final_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use crate::gen::opcodes::{
ConditionOpcode, AGG_SIG_AMOUNT, AGG_SIG_ME, AGG_SIG_PARENT, AGG_SIG_PARENT_AMOUNT,
AGG_SIG_PARENT_PUZZLE, AGG_SIG_PUZZLE, AGG_SIG_PUZZLE_AMOUNT,
};
use crate::gen::owned_conditions::OwnedSpend;
use crate::gen::owned_conditions::OwnedSpendConditions;
use chia_protocol::Bytes;
use chia_protocol::Coin;

pub fn make_aggsig_final_message(
opcode: ConditionOpcode,
msg: &[u8],
spend: &OwnedSpend,
spend: &OwnedSpendConditions,
constants: &ConsensusConstants,
) -> Vec<u8> {
let mut result = Vec::<u8>::with_capacity(msg.len() + 96);
Expand Down Expand Up @@ -114,7 +114,7 @@ mod tests {

use chia_protocol::Bytes32;

use crate::r#gen::conditions::Spend;
use crate::r#gen::conditions::SpendConditions;

let parent_id: Vec<u8> =
hex!("4444444444444444444444444444444444444444444444444444444444444444").into();
Expand Down Expand Up @@ -178,15 +178,15 @@ mod tests {
_ => {}
};
let mut a: Allocator = make_allocator(LIMIT_HEAP);
let spend = Spend::new(
let spend = SpendConditions::new(
a.new_atom(parent_id.as_slice()).expect("should pass"),
coin_amount,
a.new_atom(puzzle_hash.as_slice())
.expect("test should pass"),
Arc::new(Bytes32::try_from(coin.coin_id()).expect("test should pass")),
);

let spend = OwnedSpend::from(&a, spend);
let spend = OwnedSpendConditions::from(&a, spend);

let result = make_aggsig_final_message(opcode, msg, &spend, &TEST_CONSTANTS);
assert_eq!(result, expected_result);
Expand Down
16 changes: 8 additions & 8 deletions crates/chia-consensus/src/gen/owned_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use chia_protocol::{Bytes, Bytes32};
use chia_streamable_macro::Streamable;
use clvmr::{Allocator, NodePtr};

use super::conditions::{Spend, SpendBundleConditions};
use super::conditions::{SpendBundleConditions, SpendConditions};

#[cfg(feature = "py-bindings")]
use chia_py_streamable_macro::{PyJsonDict, PyStreamable};

#[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)]
#[cfg_attr(
feature = "py-bindings",
pyo3::pyclass(name = "Spend", get_all, frozen),
pyo3::pyclass(name = "SpendConditions", get_all, frozen),
derive(PyJsonDict, PyStreamable)
)]
pub struct OwnedSpend {
pub struct OwnedSpendConditions {
pub coin_id: Bytes32,
pub parent_id: Bytes32,
pub puzzle_hash: Bytes32,
Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct OwnedSpend {
derive(PyJsonDict, PyStreamable)
)]
pub struct OwnedSpendBundleConditions {
pub spends: Vec<OwnedSpend>,
pub spends: Vec<OwnedSpendConditions>,
pub reserve_fee: u64,
// the highest height/time conditions (i.e. most strict)
pub height_absolute: u32,
Expand All @@ -62,8 +62,8 @@ pub struct OwnedSpendBundleConditions {
pub addition_amount: u128,
}

impl OwnedSpend {
pub fn from(a: &Allocator, spend: Spend) -> Self {
impl OwnedSpendConditions {
pub fn from(a: &Allocator, spend: SpendConditions) -> Self {
let mut create_coin =
Vec::<(Bytes32, u64, Option<Bytes>)>::with_capacity(spend.create_coin.len());
for c in spend.create_coin {
Expand Down Expand Up @@ -112,9 +112,9 @@ impl OwnedSpend {

impl OwnedSpendBundleConditions {
pub fn from(a: &Allocator, sb: SpendBundleConditions) -> Self {
let mut spends = Vec::<OwnedSpend>::new();
let mut spends = Vec::<OwnedSpendConditions>::new();
for s in sb.spends {
spends.push(OwnedSpend::from(a, s));
spends.push(OwnedSpendConditions::from(a, s));
}

let mut agg_sigs = Vec::<(PublicKey, Bytes)>::with_capacity(sb.agg_sig_unsafe.len());
Expand Down
6 changes: 4 additions & 2 deletions crates/chia-consensus/src/gen/run_puzzle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::consensus_constants::ConsensusConstants;
use crate::gen::conditions::{parse_conditions, ParseState, Spend, SpendBundleConditions};
use crate::gen::conditions::{
parse_conditions, ParseState, SpendBundleConditions, SpendConditions,
};
use crate::gen::flags::ALLOW_BACKREFS;
use crate::gen::spend_visitor::SpendVisitor;
use crate::gen::validation_error::ValidationErr;
Expand Down Expand Up @@ -51,7 +53,7 @@ pub fn run_puzzle<V: SpendVisitor>(
.coin_id(),
);

let mut spend = Spend::new(
let mut spend = SpendConditions::new(
a.new_atom(parent_id)?,
amount,
a.new_atom(&puzzle_hash)?,
Expand Down
8 changes: 4 additions & 4 deletions crates/chia-consensus/src/gen/spend_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::gen::conditions::{Condition, Spend};
use crate::gen::conditions::{Condition, SpendConditions};
use clvmr::allocator::Allocator;

// These are customization points for the condition parsing and validation. The
// mempool wants to record additional information than plain consensus
// validation, so it hooks into these.
pub trait SpendVisitor {
fn new_spend(spend: &mut Spend) -> Self;
fn condition(&mut self, spend: &mut Spend, c: &Condition);
fn post_spend(&mut self, a: &Allocator, spend: &mut Spend);
fn new_spend(spend: &mut SpendConditions) -> Self;
fn condition(&mut self, spend: &mut SpendConditions, c: &Condition);
fn post_spend(&mut self, a: &Allocator, spend: &mut SpendConditions);
}
4 changes: 2 additions & 2 deletions crates/chia-consensus/src/gen/test_generators.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::conditions::{MempoolVisitor, NewCoin, Spend, SpendBundleConditions};
use super::conditions::{MempoolVisitor, NewCoin, SpendBundleConditions, SpendConditions};
use super::run_block_generator::{run_block_generator, run_block_generator2};
use crate::allocator::make_allocator;
use crate::consensus_constants::TEST_CONSTANTS;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub(crate) fn print_conditions(a: &Allocator, c: &SpendBundleConditions) -> Stri
}
ret += "SPENDS:\n";

let mut spends: Vec<Spend> = c.spends.clone();
let mut spends: Vec<SpendConditions> = c.spends.clone();
spends.sort_by_key(|s| *s.coin_id);
for s in spends {
ret += &format!(
Expand Down
8 changes: 5 additions & 3 deletions crates/chia-tools/src/bin/test-block-generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use clap::Parser;

use chia_bls::PublicKey;
use chia_consensus::consensus_constants::TEST_CONSTANTS;
use chia_consensus::gen::conditions::{EmptyVisitor, NewCoin, Spend, SpendBundleConditions};
use chia_consensus::gen::conditions::{
EmptyVisitor, NewCoin, SpendBundleConditions, SpendConditions,
};
use chia_consensus::gen::flags::{ALLOW_BACKREFS, MEMPOOL_MODE};
use chia_consensus::gen::run_block_generator::{run_block_generator, run_block_generator2};
use chia_tools::iterate_tx_blocks;
Expand Down Expand Up @@ -75,7 +77,7 @@ fn compare_agg_sig(
}
}

fn compare_spend(a: &Allocator, lhs: &Spend, rhs: &Spend) {
fn compare_spend(a: &Allocator, lhs: &SpendConditions, rhs: &SpendConditions) {
assert_eq!(a.atom(lhs.parent_id), a.atom(rhs.parent_id));
assert_eq!(lhs.coin_amount, rhs.coin_amount);
assert_eq!(*lhs.coin_id, *rhs.coin_id);
Expand All @@ -97,7 +99,7 @@ fn compare_spend(a: &Allocator, lhs: &Spend, rhs: &Spend) {
assert_eq!(a.atom(lhs.puzzle_hash), a.atom(rhs.puzzle_hash));
}

fn compare_spends(a: &Allocator, lhs: &Vec<Spend>, rhs: &Vec<Spend>) {
fn compare_spends(a: &Allocator, lhs: &Vec<SpendConditions>, rhs: &Vec<SpendConditions>) {
assert_eq!(lhs.len(), rhs.len());

for (l, r) in std::iter::zip(lhs, rhs) {
Expand Down
28 changes: 14 additions & 14 deletions tests/test_streamable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from chia_rs import (
Spend,
SpendConditions,
SpendBundleConditions,
Coin,
G1Element,
Expand All @@ -23,7 +23,7 @@

def test_hash_spend() -> None:

a1 = Spend(
a1 = SpendConditions(
coin,
parent,
ph,
Expand All @@ -44,7 +44,7 @@ def test_hash_spend() -> None:
[],
False,
)
a2 = Spend(
a2 = SpendConditions(
coin,
parent,
ph,
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_hash_spend_bundle_conditions() -> None:

def test_json_spend() -> None:

a = Spend(
a = SpendConditions(
coin,
parent,
ph,
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_json_spend() -> None:

def test_from_json_spend() -> None:

a = Spend(
a = SpendConditions(
coin,
parent,
ph,
Expand All @@ -158,7 +158,7 @@ def test_from_json_spend() -> None:
False,
)

b = Spend.from_json_dict(
b = SpendConditions.from_json_dict(
{
"coin_id": "0x" + coin.hex(),
"parent_id": "0x" + parent.hex(),
Expand Down Expand Up @@ -186,7 +186,7 @@ def test_from_json_spend() -> None:

def test_from_json_spend_set_optional() -> None:

a = Spend(
a = SpendConditions(
coin,
parent,
ph,
Expand All @@ -208,7 +208,7 @@ def test_from_json_spend_set_optional() -> None:
False,
)

b = Spend.from_json_dict(
b = SpendConditions.from_json_dict(
{
"coin_id": "0x" + coin.hex(),
"parent_id": "0x" + parent.hex(),
Expand Down Expand Up @@ -237,7 +237,7 @@ def test_from_json_spend_set_optional() -> None:
def test_invalid_hex_prefix() -> None:

with pytest.raises(ValueError, match="bytes object is expected to start with 0x"):
a = Spend.from_json_dict(
a = SpendConditions.from_json_dict(
{
# this field is missing the 0x prefix
"coin_id": coin.hex(),
Expand Down Expand Up @@ -266,7 +266,7 @@ def test_invalid_hex_prefix() -> None:
def test_invalid_hex_prefix_bytes() -> None:

with pytest.raises(ValueError, match="bytes object is expected to start with 0x"):
a = Spend.from_json_dict(
a = SpendConditions.from_json_dict(
{
"coin_id": "0x" + coin.hex(),
"parent_id": "0x" + parent.hex(),
Expand Down Expand Up @@ -295,7 +295,7 @@ def test_invalid_hex_prefix_bytes() -> None:
def test_invalid_hex_digit() -> None:

with pytest.raises(ValueError, match="invalid hex"):
a = Spend.from_json_dict(
a = SpendConditions.from_json_dict(
{
# this field is has an invalid hex digit (the last one)
"coin_id": "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdeg",
Expand Down Expand Up @@ -324,7 +324,7 @@ def test_invalid_hex_digit() -> None:
def test_invalid_hex_length() -> None:

with pytest.raises(ValueError, match="invalid length 33 expected 32"):
a = Spend.from_json_dict(
a = SpendConditions.from_json_dict(
{
# this field is has invalid length
"coin_id": "0x" + coin.hex() + "ff",
Expand Down Expand Up @@ -353,7 +353,7 @@ def test_invalid_hex_length() -> None:
def test_missing_field() -> None:

with pytest.raises(KeyError, match="coin_id"):
a = Spend.from_json_dict(
a = SpendConditions.from_json_dict(
{
# coin_id is missing
"parent_id": "0x" + parent.hex(),
Expand Down Expand Up @@ -422,7 +422,7 @@ def test_from_json_spend_bundle_conditions() -> None:

def test_copy_spend() -> None:

a = Spend(
a = SpendConditions(
coin,
parent,
ph,
Expand Down
2 changes: 1 addition & 1 deletion wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def __init__(

print_class(
file,
"Spend",
"SpendConditions",
[
"coin_id: bytes",
"parent_id: bytes",
Expand Down
3 changes: 2 additions & 1 deletion wheel/python/chia_rs/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .chia_rs import *
from .chia_rs import *
from .spend import Spend
16 changes: 8 additions & 8 deletions wheel/python/chia_rs/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class PrivateKey:
@staticmethod
def from_json_dict(json_dict: Any) -> PrivateKey: ...

class Spend:
class SpendConditions:
coin_id: bytes
parent_id: bytes
puzzle_hash: bytes
Expand Down Expand Up @@ -302,21 +302,21 @@ class Spend:
def __hash__(self) -> int: ...
def __repr__(self) -> str: ...
def __richcmp__(self) -> Any: ...
def __deepcopy__(self) -> Spend: ...
def __copy__(self) -> Spend: ...
def __deepcopy__(self) -> SpendConditions: ...
def __copy__(self) -> SpendConditions: ...
@staticmethod
def from_bytes(bytes) -> Spend: ...
def from_bytes(bytes) -> SpendConditions: ...
@staticmethod
def from_bytes_unchecked(bytes) -> Spend: ...
def from_bytes_unchecked(bytes) -> SpendConditions: ...
@staticmethod
def parse_rust(ReadableBuffer, bool = False) -> Tuple[Spend, int]: ...
def parse_rust(ReadableBuffer, bool = False) -> Tuple[SpendConditions, int]: ...
def to_bytes(self) -> bytes: ...
def __bytes__(self) -> bytes: ...
def stream_to_bytes(self) -> bytes: ...
def get_hash(self) -> bytes32: ...
def to_json_dict(self) -> Any: ...
@staticmethod
def from_json_dict(json_dict: Any) -> Spend: ...
def from_json_dict(json_dict: Any) -> SpendConditions: ...
def replace(self, *, coin_id: Union[ bytes, _Unspec] = _Unspec(),
parent_id: Union[ bytes, _Unspec] = _Unspec(),
puzzle_hash: Union[ bytes, _Unspec] = _Unspec(),
Expand All @@ -335,7 +335,7 @@ class Spend:
agg_sig_puzzle_amount: Union[ List[Tuple[G1Element, bytes]], _Unspec] = _Unspec(),
agg_sig_parent_amount: Union[ List[Tuple[G1Element, bytes]], _Unspec] = _Unspec(),
agg_sig_parent_puzzle: Union[ List[Tuple[G1Element, bytes]], _Unspec] = _Unspec(),
flags: Union[ int, _Unspec] = _Unspec()) -> Spend: ...
flags: Union[ int, _Unspec] = _Unspec()) -> SpendConditions: ...

class SpendBundleConditions:
spends: List[Spend]
Expand Down
3 changes: 3 additions & 0 deletions wheel/python/chia_rs/spend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from chia_rs import SpendConditions

Spend = SpendConditions
4 changes: 2 additions & 2 deletions wheel/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use chia_consensus::gen::flags::{
ALLOW_BACKREFS, ANALYZE_SPENDS, DISALLOW_INFINITY_G1, MEMPOOL_MODE, NO_UNKNOWN_CONDS,
STRICT_ARGS_COUNT,
};
use chia_consensus::gen::owned_conditions::{OwnedSpend, OwnedSpendBundleConditions};
use chia_consensus::gen::owned_conditions::{OwnedSpendBundleConditions, OwnedSpendConditions};
use chia_consensus::gen::run_block_generator::setup_generator_args;
use chia_consensus::gen::run_puzzle::run_puzzle as native_run_puzzle;
use chia_consensus::gen::solution_generator::solution_generator as native_solution_generator;
Expand Down Expand Up @@ -480,7 +480,7 @@ pub fn chia_rs(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
"ELIGIBLE_FOR_FF",
chia_consensus::gen::conditions::ELIGIBLE_FOR_FF,
)?;
m.add_class::<OwnedSpend>()?;
m.add_class::<OwnedSpendConditions>()?;

// constants
m.add_class::<ConsensusConstants>()?;
Expand Down

0 comments on commit 2e0331f

Please sign in to comment.