From 166f7649d60334daae544663e0ea35740d44aa9b Mon Sep 17 00:00:00 2001 From: Naman Garg <0708ng@gmail.com> Date: Thu, 26 Sep 2024 14:50:38 +0530 Subject: [PATCH] chore(sidecar): move test to test util --- bolt-sidecar/src/crypto/bls.rs | 95 +------------------ bolt-sidecar/src/primitives/constraint.rs | 4 +- bolt-sidecar/src/primitives/mod.rs | 8 +- bolt-sidecar/src/test_util.rs | 109 +++++++++++++++++++++- 4 files changed, 115 insertions(+), 101 deletions(-) diff --git a/bolt-sidecar/src/crypto/bls.rs b/bolt-sidecar/src/crypto/bls.rs index e1b821824..618f113d4 100644 --- a/bolt-sidecar/src/crypto/bls.rs +++ b/bolt-sidecar/src/crypto/bls.rs @@ -102,11 +102,10 @@ fn sign_with_prefix(key: &BlsSecretKey, data: &[u8]) -> Signature { #[cfg(test)] mod tests { use crate::{ - crypto::bls::{SignableBLS, Signer, SignerBLS}, primitives::{ConstraintsMessage, DelegationMessage, FullTransaction, InclusionRequest, RevocationMessage, SignedConstraints, SignedDelegation, SignedRevocation}, test_util::{test_bls_secret_key, TestSignableData} + crypto::bls::{SignableBLS, Signer, SignerBLS}, + test_util::{test_bls_secret_key, TestSignableData}, }; - use blst::min_pk::SecretKey; - use ethereum_consensus::crypto::{PublicKey, Signature}; use rand::Rng; #[tokio::test] @@ -125,94 +124,4 @@ mod tests { let sig = blst::min_pk::Signature::from_bytes(signature.as_ref()).unwrap(); assert!(signer.verify(&msg, &sig, &pubkey)); } - - fn random_constraints(count: usize) -> Vec { - // Random inclusion request - let json_req = r#"{ - "slot": 10, - "txs": ["0x02f86c870c72dd9d5e883e4d0183408f2382520894d2e2adf7177b7a8afddbc12d1634cf23ea1a71020180c001a08556dcfea479b34675db3fe08e29486fe719c2b22f6b0c1741ecbbdce4575cc6a01cd48009ccafd6b9f1290bbe2ceea268f94101d1d322c787018423ebcbc87ab4"] - }"#; - - let req: InclusionRequest = serde_json::from_str(json_req).unwrap(); - - (0..count).map(|_| req.txs.first().unwrap().clone()).collect() - } - - #[tokio::test] - async fn generate_test_data() { - let sk = test_bls_secret_key(); - let pk = sk.sk_to_pk(); - let signer = Signer::new(sk); - - println!("Validator Public Key: {}", hex::encode(pk.to_bytes())); - - // Generate a delegatee's BLS secret key and public key - let delegatee_ikm: [u8; 32] = rand::thread_rng().gen(); - let delegatee_sk = SecretKey::key_gen(&delegatee_ikm, &[]).expect("Failed to generate delegatee secret key"); - let delegatee_pk = delegatee_sk.sk_to_pk(); - - // Prepare a Delegation message - let delegation_msg = DelegationMessage { - validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), - delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()).expect("Failed to convert delegatee public key"), - }; - - let digest = SignableBLS::digest(&delegation_msg); - - // Sign the Delegation message - let delegation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); - - // Create SignedDelegation - let signed_delegation = SignedDelegation { - message: delegation_msg, - signature: Signature::try_from(delegation_signature.as_ref()).expect("Failed to convert delegation signature"), - }; - - // Output SignedDelegation - println!("{}", serde_json::to_string_pretty(&signed_delegation).unwrap()); - - // Prepare a revocation message - let revocation_msg = RevocationMessage { - validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), - delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()).expect("Failed to convert delegatee public key"), - }; - - let digest = SignableBLS::digest(&revocation_msg); - - // Sign the Revocation message - let revocation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); - - // Create SignedRevocation - let signed_revocation = SignedRevocation { - message: revocation_msg, - signature: Signature::try_from(revocation_signature.as_ref()).expect("Failed to convert revocation signature"), - }; - - // Output SignedRevocation - println!("{}", serde_json::to_string_pretty(&signed_revocation).unwrap()); - - let transactions = random_constraints(2); - - // Prepare a ConstraintsMessage - let constraints_msg = ConstraintsMessage { - pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), - slot: 2, - top: false, - transactions, - }; - - let digest = SignableBLS::digest(&constraints_msg); - - // Sign the ConstraintsMessage - let constraints_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); - - // Create SignedConstraints - let signed_constraints = SignedConstraints { - message: constraints_msg, - signature: constraints_signature, - }; - - // Output SignedConstraints - println!("{}", serde_json::to_string_pretty(&signed_constraints).unwrap()); - } } diff --git a/bolt-sidecar/src/primitives/constraint.rs b/bolt-sidecar/src/primitives/constraint.rs index 6c1709aab..a3e0bc37d 100644 --- a/bolt-sidecar/src/primitives/constraint.rs +++ b/bolt-sidecar/src/primitives/constraint.rs @@ -79,8 +79,8 @@ impl SignableBLS for ConstraintsMessage { hasher.update(self.slot.to_le_bytes()); hasher.update((self.top as u8).to_le_bytes()); - for constraint in &self.transactions { - hasher.update(constraint.hash()); + for tx in &self.transactions { + hasher.update(tx.hash()); } hasher.finalize().into() diff --git a/bolt-sidecar/src/primitives/mod.rs b/bolt-sidecar/src/primitives/mod.rs index cc4597913..d9cfbec70 100644 --- a/bolt-sidecar/src/primitives/mod.rs +++ b/bolt-sidecar/src/primitives/mod.rs @@ -8,7 +8,7 @@ use std::{ use alloy::{ primitives::{Address, U256}, - signers::k256::sha2::{Digest, Sha256} + signers::k256::sha2::{Digest, Sha256}, }; use ethereum_consensus::{ crypto::KzgCommitment, @@ -440,7 +440,7 @@ impl SignableBLS for DelegationMessage { let mut hasher = Sha256::new(); hasher.update(&self.validator_pubkey.to_vec()); hasher.update(&self.delegatee_pubkey.to_vec()); - + hasher.finalize().into() } } @@ -462,7 +462,7 @@ impl SignableBLS for RevocationMessage { let mut hasher = Sha256::new(); hasher.update(&self.validator_pubkey.to_vec()); hasher.update(&self.delegatee_pubkey.to_vec()); - + hasher.finalize().into() } -} \ No newline at end of file +} diff --git a/bolt-sidecar/src/test_util.rs b/bolt-sidecar/src/test_util.rs index c03bad87e..ad599b124 100644 --- a/bolt-sidecar/src/test_util.rs +++ b/bolt-sidecar/src/test_util.rs @@ -11,13 +11,18 @@ use alloy::{ }; use alloy_node_bindings::{Anvil, AnvilInstance}; use blst::min_pk::SecretKey; +use ethereum_consensus::crypto::{PublicKey, Signature}; +use rand::Rng; use reth_primitives::PooledTransactionsElement; use secp256k1::Message; use tracing::warn; use crate::{ - crypto::{ecdsa::SignableECDSA, SignableBLS}, - primitives::{CommitmentRequest, FullTransaction, InclusionRequest}, + crypto::{bls::Signer as BlsSigner, ecdsa::SignableECDSA, SignableBLS, SignerBLS}, + primitives::{ + CommitmentRequest, ConstraintsMessage, DelegationMessage, FullTransaction, + InclusionRequest, RevocationMessage, SignedConstraints, SignedDelegation, SignedRevocation, + }, Config, }; @@ -170,3 +175,103 @@ pub(crate) async fn create_signed_commitment_request( Ok(CommitmentRequest::Inclusion(request)) } + +fn random_constraints(count: usize) -> Vec { + // Random inclusion request + let json_req = r#"{ + "slot": 10, + "txs": [ + "0x02f86c870c72dd9d5e883e4d0183408f2382520894d2e2adf7177b7a8afddbc12d1634cf23ea1a71020180c001a08556dcfea479b34675db3fe08e29486fe719c2b22f6b0c1741ecbbdce4575cc6a01cd48009ccafd6b9f1290bbe2ceea268f94101d1d322c787018423ebcbc87ab4", + "0x02f9017b8501a2140cff8303dec685012a05f2008512a05f2000830249f094843669e5220036eddbaca89d8c8b5b82268a0fc580b901040cc7326300000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000022006292538e66f0000000000000000000000005ba38f2c245618e39f6fa067bf1dec304e73ff3c00000000000000000000000092f0ee29e6e1bf0f7c668317ada78f5774a6cb7f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000003fac6482aee49bf58515be2d3fb58378a8497cc9000000000000000000000000c6cc140787b02ae479a10e41169607000c0d44f6c080a00cf74c45dbe9ee1fb923118ec5ce9db8f88cd651196ed3f9d4f8f2a65827e611a04a6bc1d49a7e18b7c92e8f3614cae116b1832ceb311c81d54b2c87de1545f68f", + "0x02f8708501a2140cff82012f800782520894b6c402298fcb88039bbfde70f5ace791f18cfac88707131d70870dc880c080a03aab1b17ecf28f85de43c7733611759b87d25ba885babacb6b4c625d715415eea03fb52cb7744ccb885906e42f6b9cf82e74b47a4b4b4072af2aa52a8dc472236e" + ] + }"#; + + let req: InclusionRequest = serde_json::from_str(json_req).unwrap(); + + req.txs.iter().cloned().take(count).collect() +} + +#[tokio::test] +async fn generate_test_data() { + let sk = test_bls_secret_key(); + let pk = sk.sk_to_pk(); + let signer = BlsSigner::new(sk); + + println!("Validator Public Key: {}", hex::encode(pk.to_bytes())); + + // Generate a delegatee's BLS secret key and public key + let delegatee_ikm: [u8; 32] = rand::thread_rng().gen(); + let delegatee_sk = + SecretKey::key_gen(&delegatee_ikm, &[]).expect("Failed to generate delegatee secret key"); + let delegatee_pk = delegatee_sk.sk_to_pk(); + + // Prepare a Delegation message + let delegation_msg = DelegationMessage { + validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()) + .expect("Failed to convert validator public key"), + delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()) + .expect("Failed to convert delegatee public key"), + }; + + let digest = SignableBLS::digest(&delegation_msg); + + // Sign the Delegation message + let delegation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); + + // Create SignedDelegation + let signed_delegation = SignedDelegation { + message: delegation_msg, + signature: Signature::try_from(delegation_signature.as_ref()) + .expect("Failed to convert delegation signature"), + }; + + // Output SignedDelegation + println!("{}", serde_json::to_string_pretty(&signed_delegation).unwrap()); + + // Prepare a revocation message + let revocation_msg = RevocationMessage { + validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()) + .expect("Failed to convert validator public key"), + delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()) + .expect("Failed to convert delegatee public key"), + }; + + let digest = SignableBLS::digest(&revocation_msg); + + // Sign the Revocation message + let revocation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); + + // Create SignedRevocation + let signed_revocation = SignedRevocation { + message: revocation_msg, + signature: Signature::try_from(revocation_signature.as_ref()) + .expect("Failed to convert revocation signature"), + }; + + // Output SignedRevocation + println!("{}", serde_json::to_string_pretty(&signed_revocation).unwrap()); + + let transactions = random_constraints(1); + + // Prepare a ConstraintsMessage + let constraints_msg = ConstraintsMessage { + pubkey: PublicKey::try_from(pk.to_bytes().as_slice()) + .expect("Failed to convert validator public key"), + slot: 32, + top: true, + transactions, + }; + + let digest = SignableBLS::digest(&constraints_msg); + + // Sign the ConstraintsMessage + let constraints_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); + + // Create SignedConstraints + let signed_constraints = + SignedConstraints { message: constraints_msg, signature: constraints_signature }; + + // Output SignedConstraints + println!("{}", serde_json::to_string_pretty(&signed_constraints).unwrap()); +}