Skip to content
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

Open
wants to merge 6 commits into
base: validate_clvm_and_sigs
Choose a base branch
from

Conversation

matt-o-how
Copy link
Contributor

Creating a new branch and PRing into validate_clvm_and_sigs as this change is rather big.

@matt-o-how matt-o-how requested a review from arvidn August 6, 2024 14:54
Copy link
Contributor

@arvidn arvidn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good. just a few comments

) -> 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();
Copy link
Contributor

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

Copy link
Contributor Author

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 earlier

Copy link
Contributor

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

Comment on lines 60 to 63
let mut hasher = Sha256::new();
hasher.update(pk.to_bytes());
hasher.update(msg.as_slice());
let hash: [u8; 32] = hasher.finalize();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would probably make sense to factor this out as a separate function, as this code is repeated.

@@ -46,32 +46,54 @@ pub fn validate_clvm_and_signature(
.flat_map(move |(condition, items)| {
let spend_clone = spend.clone();
items.iter().map(move |(pk, msg)| {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

.agg_sig_unsafe
.iter()
.map(|(pk, msg)| (pk, msg.as_slice().to_vec()));
let unsafes = npcresult.agg_sig_unsafe.clone();
Copy link
Contributor

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 should clone this either

.aggregate_verify(iter, &spend_bundle.aggregated_signature);
let result = aggregate_verify_gt(
&spend_bundle.aggregated_signature,
iter.clone().map(|tuple| tuple.1),
Copy link
Contributor

Choose a reason for hiding this comment

The 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 collect() the GTElements first. Alternatively, build the return vector as part of this lambda function. That would be the most efficient.

Copy link
Contributor

@arvidn arvidn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good. I would think the & allows you to not make GTElement derive from Copy though

@@ -13,7 +13,7 @@ use std::ops::{Mul, MulAssign};
pyo3::pyclass,
derive(chia_py_streamable_macro::PyStreamable)
)]
#[derive(Clone)]
#[derive(Clone, Copy)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the GTElement is more than 500 bytes large. I think it's pretty good to require explicit cloning, to see where we pay that cost.


// Verify aggregated signature
let result = aggregate_verify_gt(
&spend_bundle.aggregated_signature,
iter.clone().map(|tuple| tuple.1),
pairs.iter().map(|tuple| tuple.1),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pairs.iter().map(|tuple| tuple.1),
pairs.iter().map(|tuple| &tuple.1),

does this work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants