Skip to content

Commit

Permalink
fix __repr__ for ConsensusConstants to use upper case names
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jan 24, 2025
1 parent e92b5f4 commit 46b8f32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
35 changes: 30 additions & 5 deletions crates/chia_py_streamable_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS
let mut py_protocol = quote! {
#[pyo3::pymethods]
impl #ident {
fn __repr__(&self) -> pyo3::PyResult<String> {
Ok(format!("{self:?}"))
}

fn __richcmp__(&self, other: pyo3::PyRef<Self>, op: pyo3::class::basic::CompareOp) -> pyo3::Py<pyo3::PyAny> {
use pyo3::class::basic::CompareOp;
let py = other.py();
Expand Down Expand Up @@ -128,6 +124,26 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS
}
});

if py_uppercase {
py_protocol.extend(quote! {
#[pyo3::pymethods]
impl #ident {
fn __repr__(&self) -> pyo3::PyResult<String> {
Ok(format!(concat!(stringify!(#ident), " {{ ", #(stringify!(#fnames_maybe_upper), ": {:?}, ",)* "}}"), #(self.#fnames,)*))
}
}
});
} else {
py_protocol.extend(quote! {
#[pyo3::pymethods]
impl #ident {
fn __repr__(&self) -> pyo3::PyResult<String> {
Ok(format!("{self:?}"))
}
}
});
}

if !named.is_empty() {
py_protocol.extend(quote! {
#[pyo3::pymethods]
Expand Down Expand Up @@ -155,7 +171,16 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS
});
}
}
syn::Fields::Unnamed(FieldsUnnamed { .. }) => {}
syn::Fields::Unnamed(FieldsUnnamed { .. }) => {
py_protocol.extend(quote! {
#[pyo3::pymethods]
impl #ident {
fn __repr__(&self) -> pyo3::PyResult<String> {
Ok(format!("{self:?}"))
}
}
});
}
syn::Fields::Unit => {
panic!("PyStreamable does not support the unit type");
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_consensus_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from run_gen import DEFAULT_CONSTANTS


def test_formatting() -> None:

assert (
f"{DEFAULT_CONSTANTS}"
== "ConsensusConstants { SLOT_BLOCKS_TARGET: 32, MIN_BLOCKS_PER_CHALLENGE_BLOCK: 16, MAX_SUB_SLOT_BLOCKS: 128, NUM_SPS_SUB_SLOT: 64, SUB_SLOT_ITERS_STARTING: 134217728, DIFFICULTY_CONSTANT_FACTOR: 147573952589676412928, DIFFICULTY_STARTING: 7, DIFFICULTY_CHANGE_MAX_FACTOR: 3, SUB_EPOCH_BLOCKS: 384, EPOCH_BLOCKS: 4608, SIGNIFICANT_BITS: 8, DISCRIMINANT_SIZE_BITS: 1024, NUMBER_ZERO_BITS_PLOT_FILTER: 9, MIN_PLOT_SIZE: 32, MAX_PLOT_SIZE: 50, SUB_SLOT_TIME_TARGET: 600, NUM_SP_INTERVALS_EXTRA: 3, MAX_FUTURE_TIME2: 120, NUMBER_OF_TIMESTAMPS: 11, GENESIS_CHALLENGE: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855, AGG_SIG_ME_ADDITIONAL_DATA: ccd5bb71183532bff220ba46c268991a3ff07eb358e8255a65c30a2dce0e5fbb, AGG_SIG_PARENT_ADDITIONAL_DATA: baf5d69c647c91966170302d18521b0a85663433d161e72c826ed08677b53a74, AGG_SIG_PUZZLE_ADDITIONAL_DATA: 284fa2ef486c7a41cc29fc99c9d08376161e93dd37817edb8219f42dca7592c4, AGG_SIG_AMOUNT_ADDITIONAL_DATA: cda186a9cd030f7a130fae45005e81cae7a90e0fa205b75f6aebc0d598e0348e, AGG_SIG_PUZZLE_AMOUNT_ADDITIONAL_DATA: 0f7d90dff0613e6901e24dae59f1e690f18b8f5fbdcf1bb192ac9deaf7de22ad, AGG_SIG_PARENT_AMOUNT_ADDITIONAL_DATA: 585796bd90bb553c0430b87027ffee08d88aba0162c6e1abbbcc6b583f2ae7f9, AGG_SIG_PARENT_PUZZLE_ADDITIONAL_DATA: 2ebfdae17b29d83bae476a25ea06f0c4bd57298faddbbc3ec5ad29b9b86ce5df, GENESIS_PRE_FARM_POOL_PUZZLE_HASH: d23da14695a188ae5708dd152263c4db883eb27edeb936178d4d988b8f3ce5fc, GENESIS_PRE_FARM_FARMER_PUZZLE_HASH: 3d8765d3a597ec1d99663f6c9816d915b9f68613ac94009884c4addaefcce6af, MAX_VDF_WITNESS_SIZE: 64, MEMPOOL_BLOCK_BUFFER: 10, MAX_COIN_AMOUNT: 18446744073709551615, MAX_BLOCK_COST_CLVM: 11000000000, COST_PER_BYTE: 12000, WEIGHT_PROOF_THRESHOLD: 2, WEIGHT_PROOF_RECENT_BLOCKS: 1000, MAX_BLOCK_COUNT_PER_REQUESTS: 32, BLOCKS_CACHE_SIZE: 5120, MAX_GENERATOR_SIZE: 1000000, MAX_GENERATOR_REF_LIST_SIZE: 512, POOL_SUB_SLOT_ITERS: 37600000000, SOFT_FORK6_HEIGHT: 0, HARD_FORK_HEIGHT: 5496000, PLOT_FILTER_128_HEIGHT: 10542000, PLOT_FILTER_64_HEIGHT: 15592000, PLOT_FILTER_32_HEIGHT: 20643000, }"
)

0 comments on commit 46b8f32

Please sign in to comment.