Skip to content

Commit

Permalink
implement more of arvid's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-o-how committed Jun 18, 2024
1 parent 37808e5 commit 3a78f66
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 63 deletions.
6 changes: 3 additions & 3 deletions crates/chia-consensus/src/gen/condition_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ pub fn u64_to_bytes(val: u64) -> Bytes {
#[cfg(test)]
mod tests {
use super::*;
use clvmr::Allocator;
use crate::allocator::make_allocator;
use clvmr::chia_dialect::LIMIT_HEAP;
use clvmr::Allocator;

#[test]
fn test_validate_u64() {
let mut a: Allocator = make_allocator(LIMIT_HEAP);
for v in 0..10000{
for v in 0..10000 {
let ptr = a.new_small_number(v).expect("valid u64");
assert_eq!(a.atom(ptr).as_ref(), u64_to_bytes(v as u64).as_slice())
}
Expand All @@ -97,4 +97,4 @@ mod tests {
assert_eq!(a.atom(ptr).as_ref(), u64_to_bytes(v).as_slice())
}
}
}
}
58 changes: 19 additions & 39 deletions crates/chia-consensus/src/multiprocess_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,8 @@ mod tests {
#[test]
fn test_validate_no_pks() {
let test_coin = Coin::new(
Bytes32::new(hex!(
"4444444444444444444444444444444444444444444444444444444444444444"
)),
Bytes32::new(hex!(
"3333333333333333333333333333333333333333333333333333333333333333"
)),
hex!("4444444444444444444444444444444444444444444444444444444444444444").into(),
hex!("3333333333333333333333333333333333333333333333333333333333333333").into(),
1,
);

Expand All @@ -168,11 +164,7 @@ ff01\
)
.to_vec(),
);
let spend = CoinSpend::new(
test_coin,
Program::new(vec![1_u8].into()),
solution.into(),
);
let spend = CoinSpend::new(test_coin, Program::new(vec![1_u8].into()), solution.into());
let coin_spends: Vec<CoinSpend> = vec![spend];
let spend_bundle = SpendBundle {
coin_spends: coin_spends,
Expand All @@ -195,22 +187,18 @@ ff01\
//let pk: PublicKey = sk.public_key(); //0x997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2
// panic!("{:?}", pk);
let test_coin = Coin::new(
Bytes32::new(hex!(
"4444444444444444444444444444444444444444444444444444444444444444"
)),
Bytes32::new(hex!(
"3333333333333333333333333333333333333333333333333333333333333333"
)),
hex!("4444444444444444444444444444444444444444444444444444444444444444").into(),
hex!("3333333333333333333333333333333333333333333333333333333333333333").into(),
1,
);

let solution = Bytes::new(hex!("ffff31ffb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec());
let solution = hex!("ffff31ffb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec();
// ((49 0x997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2 "hello"))

let spend = CoinSpend::new(
test_coin,
Program::new(vec![1_u8].into()),
Program::new(solution),
Program::new(solution.into()),
);
let msg = b"hello";
let sig = sign(&sk, msg);
Expand Down Expand Up @@ -238,20 +226,18 @@ ff01\

let full_puz = Bytes32::new(tree_hash_atom(&[1_u8]).to_bytes());
let test_coin = Coin::new(
Bytes32::new(hex!(
"4444444444444444444444444444444444444444444444444444444444444444"
)),
hex!("4444444444444444444444444444444444444444444444444444444444444444").into(),
full_puz,
1,
);

let solution = Bytes::new(hex!("ffff32ffb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec());
let solution = hex!("ffff32ffb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec();
// ((50 0x997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2 "hello"))

let spend = CoinSpend::new(
test_coin,
Program::new(vec![1_u8].into()),
Program::new(solution),
Program::new(solution.into()),
);
let msg = b"hello";
let mut result = msg.to_vec();
Expand Down Expand Up @@ -287,20 +273,18 @@ ff01\

let full_puz = Bytes32::new(tree_hash_atom(&[1_u8]).to_bytes());
let test_coin = Coin::new(
Bytes32::new(hex!(
"4444444444444444444444444444444444444444444444444444444444444444"
)),
hex!("4444444444444444444444444444444444444444444444444444444444444444").into(),
full_puz,
1,
);

let solution = Bytes::new(hex!("ffff30ffb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec());
let solution = hex!("ffff30ffb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec();
// ((48 0x997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2 "hello"))

let spend = CoinSpend::new(
test_coin,
Program::new(vec![1_u8].into()),
Program::new(solution),
Program::new(solution.into()),
);
let msg = b"hello";
let mut result = msg.to_vec();
Expand Down Expand Up @@ -349,20 +333,18 @@ ff01\

let full_puz = Bytes32::new(tree_hash_atom(&[1_u8]).to_bytes());
let test_coin = Coin::new(
Bytes32::new(hex!(
"4444444444444444444444444444444444444444444444444444444444444444"
)),
hex!("4444444444444444444444444444444444444444444444444444444444444444").into(),
full_puz,
1,
);

let solution = Bytes::new(hex!("ffff2fffb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec());
let solution = hex!("ffff2fffb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec();
// ((47 0x997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2 "hello"))

let spend = CoinSpend::new(
test_coin,
Program::new(vec![1_u8].into()),
Program::new(solution),
Program::new(solution.into()),
);
let msg = b"hello";
let mut result = msg.to_vec();
Expand Down Expand Up @@ -411,20 +393,18 @@ ff01\

let full_puz = Bytes32::new(tree_hash_atom(&[1_u8]).to_bytes());
let test_coin = Coin::new(
Bytes32::new(hex!(
"4444444444444444444444444444444444444444444444444444444444444444"
)),
hex!("4444444444444444444444444444444444444444444444444444444444444444").into(),
full_puz,
1,
);

let solution = Bytes::new(hex!("ffff2effb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec());
let solution = hex!("ffff2effb0997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2ff8568656c6c6f8080").to_vec();
// ((46 0x997cc43ed8788f841fcf3071f6f212b89ba494b6ebaf1bda88c3f9de9d968a61f3b7284a5ee13889399ca71a026549a2 "hello"))

let spend = CoinSpend::new(
test_coin,
Program::new(vec![1_u8].into()),
Program::new(solution),
Program::new(solution.into()),
);
let msg = b"hello";
let mut result = msg.to_vec();
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-consensus/src/npc_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn get_name_puzzle_conditions(
let mut state = ParseState::default();
let mut cache = HashMap::<NodePtr, TreeHash>::new();

for coin_spend in spend_bundle.coin_spends.clone() {
for coin_spend in &spend_bundle.coin_spends {
// process the spend
let puz = node_from_bytes(&mut a, coin_spend.puzzle_reveal.as_slice())?;
let sol = node_from_bytes(&mut a, coin_spend.solution.as_slice())?;
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn get_name_puzzle_conditions(
}

validate_conditions(&a, &ret, state, a.nil(), flags)?;

assert!(max_cost >= cost_left);
ret.cost = max_cost - cost_left;
let osbc = OwnedSpendBundleConditions::from(&a, ret);
Ok(osbc)
Expand Down
29 changes: 10 additions & 19 deletions wheel/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,29 +384,20 @@ pub fn py_validate_clvm_and_signature(
Vec<([u8; 32], GTElement)>,
f32,
)> {
let sbc: Result<
(
OwnedSpendBundleConditions,
Vec<([u8; 32], GTElement)>,
Duration,
),
ErrorCode,
> = validate_clvm_and_signature(
let (owned_conditions, additions, duration) = validate_clvm_and_signature(
&new_spend,
max_cost,
&constants,
peak_height,
Arc::new(Mutex::new(cache)),
); // TODO: use cache properly
match sbc {
Ok((owned_conditions, additions, duration)) => Ok((
new_spend,
owned_conditions,
additions,
duration.as_secs_f32(),
)),
Err(e) => Err(PyErr::new::<PyTypeError, _>(e as u32)),
}
Arc::new(Mutex::new(cache)), // TODO: use cache properly
)
.map_err(|e| PyErr::new::<PyTypeError, _>(e as u32))?;
Ok((
new_spend,
owned_conditions,
additions,
duration.as_secs_f32(),
))
}

#[pymodule]
Expand Down

0 comments on commit 3a78f66

Please sign in to comment.