-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove cache from validation #646
base: validate_clvm_and_sigs
Are you sure you want to change the base?
Changes from 5 commits
34c750a
4a039a5
460f3e3
722d553
5cbed30
5d0f4d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,11 @@ use crate::gen::opcodes::{ | |
use crate::gen::owned_conditions::OwnedSpendBundleConditions; | ||
use crate::gen::validation_error::ErrorCode; | ||
use crate::spendbundle_conditions::get_conditions_from_spendbundle; | ||
use chia_bls::BlsCache; | ||
use chia_bls::PairingInfo; | ||
use chia_bls::{aggregate_verify_gt, hash_to_g2}; | ||
use chia_protocol::SpendBundle; | ||
use clvmr::sha2::Sha256; | ||
use clvmr::{ENABLE_BLS_OPS_OUTSIDE_GUARD, ENABLE_FIXED_DIV, LIMIT_HEAP}; | ||
use std::sync::{Arc, Mutex}; | ||
use std::time::{Duration, Instant}; | ||
|
||
// currently in mempool_manager.py | ||
|
@@ -24,14 +24,14 @@ pub fn validate_clvm_and_signature( | |
max_cost: u64, | ||
constants: &ConsensusConstants, | ||
height: u32, | ||
cache: &Arc<Mutex<BlsCache>>, | ||
) -> Result<(OwnedSpendBundleConditions, Vec<PairingInfo>, Duration), ErrorCode> { | ||
let start_time = Instant::now(); | ||
let mut a = make_allocator(LIMIT_HEAP); | ||
let sbc = get_conditions_from_spendbundle(&mut a, spend_bundle, max_cost, height, constants) | ||
.map_err(|e| e.1)?; | ||
let npcresult = OwnedSpendBundleConditions::from(&a, sbc); | ||
let iter = npcresult.spends.iter().flat_map(|spend| { | ||
let spends = npcresult.spends.clone(); | ||
let iter = spends.iter().flat_map(|spend| { | ||
let condition_items_pairs = [ | ||
(AGG_SIG_PARENT, &spend.agg_sig_parent), | ||
(AGG_SIG_PUZZLE, &spend.agg_sig_puzzle), | ||
|
@@ -46,32 +46,52 @@ pub fn validate_clvm_and_signature( | |
.flat_map(move |(condition, items)| { | ||
let spend_clone = spend.clone(); | ||
items.iter().map(move |(pk, msg)| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a good reason to move the spends into this lambda function? That's probably why you have to clone the spends in the first place. |
||
( | ||
pk, | ||
make_aggsig_final_message( | ||
condition, | ||
msg.as_slice(), | ||
&spend_clone, | ||
constants, | ||
), | ||
) | ||
let mut aug_msg = pk.to_bytes().to_vec(); | ||
let msg = make_aggsig_final_message( | ||
condition, | ||
msg.as_slice(), | ||
&spend_clone, | ||
constants, | ||
); | ||
aug_msg.extend_from_slice(msg.as_ref()); | ||
let aug_hash = hash_to_g2(&aug_msg); | ||
let pairing = aug_hash.pair(pk); | ||
|
||
(hash_pk_and_msg(&pk.to_bytes(), &msg), pairing) | ||
}) | ||
}) | ||
}); | ||
let unsafe_items = npcresult | ||
.agg_sig_unsafe | ||
.iter() | ||
.map(|(pk, msg)| (pk, msg.as_slice().to_vec())); | ||
let unsafes = npcresult.agg_sig_unsafe.clone(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you should clone this either |
||
let unsafe_items = unsafes.iter().map(|(pk, msg)| { | ||
let mut aug_msg = pk.to_bytes().to_vec(); | ||
aug_msg.extend_from_slice(msg.as_ref()); | ||
let aug_hash = hash_to_g2(&aug_msg); | ||
let pairing = aug_hash.pair(pk); | ||
(hash_pk_and_msg(&pk.to_bytes(), msg), pairing) | ||
}); | ||
let iter = iter.chain(unsafe_items); | ||
|
||
// Verify aggregated signature | ||
let (result, added) = cache | ||
.lock() | ||
.unwrap() | ||
.aggregate_verify(iter, &spend_bundle.aggregated_signature); | ||
let result = aggregate_verify_gt( | ||
&spend_bundle.aggregated_signature, | ||
iter.clone().map(|tuple| tuple.1), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cloning the iterator here probably means you'll perform the work in the lambda twice, each time you iterate over it. It might be a good reason to |
||
); | ||
if !result { | ||
return Err(ErrorCode::BadAggregateSignature); | ||
} | ||
Ok((npcresult, added, start_time.elapsed())) | ||
Ok(( | ||
npcresult, | ||
iter.map(|tuple| (tuple.0, tuple.1.to_bytes().to_vec())) | ||
.collect(), | ||
start_time.elapsed(), | ||
)) | ||
} | ||
|
||
fn hash_pk_and_msg(pk: &[u8], msg: &[u8]) -> [u8; 32] { | ||
let mut hasher = Sha256::new(); | ||
hasher.update(pk); | ||
hasher.update(msg); | ||
hasher.finalize() | ||
} | ||
|
||
pub fn get_flags_for_height_and_constants(height: u32, constants: &ConsensusConstants) -> u32 { | ||
|
@@ -161,7 +181,6 @@ ff01\ | |
TEST_CONSTANTS.max_block_cost_clvm, | ||
&TEST_CONSTANTS, | ||
236, | ||
&Arc::new(Mutex::new(BlsCache::default())), | ||
) | ||
.expect("SpendBundle should be valid for this test"); | ||
} | ||
|
@@ -193,7 +212,6 @@ ff01\ | |
TEST_CONSTANTS.max_block_cost_clvm, | ||
&TEST_CONSTANTS, | ||
236, | ||
&Arc::new(Mutex::new(BlsCache::default())), | ||
) | ||
.expect("SpendBundle should be valid for this test"); | ||
} | ||
|
@@ -222,15 +240,13 @@ ff01\ | |
TEST_CONSTANTS.max_block_cost_clvm / 2, // same as mempool_manager default | ||
&TEST_CONSTANTS, | ||
236, | ||
&Arc::new(Mutex::new(BlsCache::default())), | ||
); | ||
assert!(matches!(result, Ok(..))); | ||
let result = validate_clvm_and_signature( | ||
&spend_bundle, | ||
TEST_CONSTANTS.max_block_cost_clvm / 3, // lower than mempool_manager default | ||
&TEST_CONSTANTS, | ||
236, | ||
&Arc::new(Mutex::new(BlsCache::default())), | ||
); | ||
assert!(matches!(result, Err(ErrorCode::CostExceeded))); | ||
} | ||
|
@@ -271,7 +287,6 @@ ff01\ | |
TEST_CONSTANTS.max_block_cost_clvm, | ||
&TEST_CONSTANTS, | ||
1, | ||
&Arc::new(Mutex::new(BlsCache::default())), | ||
) | ||
.expect("SpendBundle should be valid for this test"); | ||
} | ||
|
@@ -321,7 +336,6 @@ ff01\ | |
TEST_CONSTANTS.max_block_cost_clvm, | ||
&TEST_CONSTANTS, | ||
TEST_CONSTANTS.hard_fork_height + 1, | ||
&Arc::new(Mutex::new(BlsCache::default())), | ||
) | ||
.expect("SpendBundle should be valid for this test"); | ||
} | ||
|
@@ -365,7 +379,6 @@ ff01\ | |
TEST_CONSTANTS.max_block_cost_clvm, | ||
&TEST_CONSTANTS, | ||
TEST_CONSTANTS.hard_fork_height + 1, | ||
&Arc::new(Mutex::new(BlsCache::default())), | ||
) | ||
.expect("SpendBundle should be valid for this test"); | ||
} | ||
|
@@ -409,7 +422,6 @@ ff01\ | |
TEST_CONSTANTS.max_block_cost_clvm, | ||
&TEST_CONSTANTS, | ||
TEST_CONSTANTS.hard_fork_height + 1, | ||
&Arc::new(Mutex::new(BlsCache::default())), | ||
) | ||
.expect("SpendBundle should be valid for this test"); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need to clone the spends here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an unfortunate move that happens which means we can't return
npcresult
unless we clone it earlierThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect you the lambda to take the entry by reference, and return a reference