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

enable batching multiple sign calls in one batch transaction #909

Merged
merged 6 commits into from
Oct 29, 2024

Conversation

ppca
Copy link
Contributor

@ppca ppca commented Oct 25, 2024

changes:

  1. mark_request_received(request) with <request, None> in sign(), so that same payloads in the same batch tx will not be able to go thru. Only one should go thru;
  2. use SignRequestIdentifier {receipt_id, epsilon, payload} to index sign requests instead of receipt_id in backend code. When sending multiple sign() in one batch tx, the receipt_id and entropy of all of them are exactly same. What should differentiate is epsilon (which reflects path + predecessor) or payload;
  3. add an integration test_batch_signature with 3 sign calls and verify the signature response in the receipt is correct for each of the payload.

@ppca ppca force-pushed the xiangyi/fix_same_receipt_id_sign_calls branch from d565657 to 0e87e27 Compare October 25, 2024 15:47
Copy link
Collaborator

@volovyks volovyks 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 overall

This PR can close https://github.com/near/mpc/issues/763 too. If so, let's add a separate test.

chain-signatures/node/src/protocol/presignature.rs Outdated Show resolved Hide resolved
@@ -166,6 +172,7 @@ impl VersionedMpcContract {
"sign: predecessor={predecessor}, payload={payload:?}, path={path:?}, key_version={key_version}",
);
env::log_str(&serde_json::to_string(&near_sdk::env::random_seed_array()).unwrap());
self.mark_request_received(&request);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume a duplicate request with the same payload, epsilon, and crypto hash will not pass the check on line 170 and throw an error on 184. Let's have a separate test for that (batch transaction with 2+ requests with the same payload and path). And it should not break the counter.

BTW, the error should not be "PayloadCollision" anymore. Should be SignRequestCollision.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Added test_batch_duplicate_signature where 2 identical requests are sent. The test checks that tx fails with RequestCollision.

Copy link
Member

Choose a reason for hiding this comment

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

hmm, I'm against changing the name, because it's going to be a breaking change now if somebody was directly matching against the name of the error

chain-signatures/node/src/protocol/message.rs Show resolved Hide resolved
@ppca ppca requested a review from volovyks October 28, 2024 22:49
Comment on lines 168 to 152
pub receipt_id: CryptoHash,
pub receipt_id: [u8; 32],
Copy link
Member

Choose a reason for hiding this comment

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

why change these to non-CryptoHash type?

Copy link
Member

Choose a reason for hiding this comment

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

ok, so talked with @volovyks about this one and it seems like we're moving to a more generic naming for these because other chains don't have this. We can change it to some other type later

Copy link
Collaborator

Choose a reason for hiding this comment

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

Have you tried using the newly introduced SignRequestIdentifier everywhere?

Comment on lines 219 to 234
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub struct SignRequestIdentifier {
pub receipt_id: [u8; 32],
pub epsilon: Vec<u8>,
pub payload: Vec<u8>,
}

impl SignRequestIdentifier {
pub fn new(receipt_id: [u8; 32], epsilon: Scalar, payload: Scalar) -> Self {
Self {
receipt_id,
epsilon: borsh::to_vec(&SerializableScalar { scalar: epsilon }).unwrap(),
payload: borsh::to_vec(&SerializableScalar { scalar: payload }).unwrap(),
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

why are we making epsilon and the payload vectors? Is it for hashing? If so, just implement hashing for SerializableScalar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will come in a refactoring PR

@@ -356,13 +357,13 @@ impl SignatureManager {
#[allow(clippy::result_large_err)]
fn retry_failed_generation(
&mut self,
receipt_id: ReceiptId,
sign_request_identifier: SignRequestIdentifier,
Copy link
Member

Choose a reason for hiding this comment

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

this is quite long now as a variable name. Let's just simplify all of them to just request_id: SignRequestIdentifier

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll do all the rafactoring in an upcoming PR. Want to prioritize making batch multiple sign possible on dev first.

@ppca
Copy link
Contributor Author

ppca commented Oct 29, 2024

Talked with @volovyks this morning, since many people have been waiting for batch tx with multiple signs to work on dev, I want to prioritize merging this fix, then in the next PR refactor the code. @ChaoticTempest

Copy link
Collaborator

@volovyks volovyks left a comment

Choose a reason for hiding this comment

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

Let's do follow up PR later and fix the issue now.

@ppca ppca merged commit e5c1507 into develop Oct 29, 2024
3 checks passed
@ppca ppca deleted the xiangyi/fix_same_receipt_id_sign_calls branch October 29, 2024 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants