Skip to content

Commit

Permalink
correctly treat Program as a bytes buffer, for purposes of converting…
Browse files Browse the repository at this point in the history
… it from python structures
  • Loading branch information
arvidn committed Dec 15, 2023
1 parent 04dc70d commit f9c8dbd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
- name: Build
run: |
python -m pip install clvm_tools colorama blspy chia-blockchain==2.1.0
python -m pip install clvm_tools colorama blspy chia-blockchain==2.1.2 clvm==0.9.8
maturin develop --release -m wheel/Cargo.toml
- name: python mypy
Expand Down
9 changes: 6 additions & 3 deletions chia-protocol/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ fn clvm_convert(a: &mut Allocator, o: &PyAny) -> PyResult<NodePtr> {
// None
if o.is_none() {
Ok(a.null())
// Program itself
} else if let Ok(prg) = o.extract::<Program>() {
Ok(node_from_bytes_backrefs(a, prg.0.as_slice())?)
// bytes
} else if let Ok(buffer) = o.extract::<&[u8]>() {
a.new_atom(buffer)
Expand Down Expand Up @@ -148,6 +145,12 @@ fn clvm_convert(a: &mut Allocator, o: &PyAny) -> PyResult<NodePtr> {
a.new_atom(atom.extract::<&[u8]>()?)
.map_err(|e| PyMemoryError::new_err(e.to_string()))
}
// Program itself. This is interpreted as a program in serialized form, and
// just a buffer of that serialization. This is an optimization to finding
// __bytes__() and calling it
} else if let Ok(prg) = o.extract::<Program>() {
a.new_atom(prg.0.as_slice())
.map_err(|e| PyMemoryError::new_err(e.to_string()))
// anything convertible to bytes
} else if let Ok(fun) = o.getattr("__bytes__") {
let bytes = fun.call0()?;
Expand Down
5 changes: 4 additions & 1 deletion tests/test_program_fidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ def rand_list(rnd: Random) -> List:
def rand_program(rnd: Random) -> ChiaProgram:
return ChiaProgram.from_bytes(b"\xff\x01\xff\x04\x01")

def rand_rust_program(rnd: Random) -> chia_rs.Program:
return chia_rs.Program.from_bytes(b"\xff\x01\xff\x04\x01")

def rand_optional(rnd: Random) -> Optional[object]:
if rnd.randint(0, 1) == 0:
return None
return rand_object(rnd)

def rand_object(rnd: Random) -> object:
types = [rand_optional, rand_int, rand_string, rand_bytes, rand_program, rand_list]
types = [rand_optional, rand_int, rand_string, rand_bytes, rand_program, rand_list, rand_rust_program]
return rnd.sample(types, 1)[0](rnd)

def test_run_program() -> None:
Expand Down

0 comments on commit f9c8dbd

Please sign in to comment.