Skip to content

Commit

Permalink
refactor: nonce from bytes32 to uint256
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasRampoldi committed Sep 9, 2024
1 parent 1bd4352 commit 7672899
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
29 changes: 12 additions & 17 deletions batcher/aligned-batcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ impl BatchState {
self.batch_queue
.iter()
.map(|(entry, _)| entry)
.find(|entry| {
entry.sender == sender
&& U256::from_big_endian(entry.nonced_verification_data.nonce.as_slice())
== nonce
})
.find(|entry| entry.sender == sender && entry.nonced_verification_data.nonce == nonce)
}

/// Checks if the entry is valid
Expand All @@ -122,8 +118,7 @@ impl BatchState {
replacement_entry: BatchQueueEntry,
) -> Option<ValidityResponseMessage> {
let replacement_max_fee = replacement_entry.nonced_verification_data.max_fee;
let nonce =
U256::from_big_endian(replacement_entry.nonced_verification_data.nonce.as_slice());
let nonce = replacement_entry.nonced_verification_data.nonce;
let sender = replacement_entry.sender;

debug!(
Expand All @@ -134,7 +129,7 @@ impl BatchState {
// it is a valid entry only if there is no entry with the same sender, lower nonce and a lower fee
let is_valid = !self.batch_queue.iter().any(|(entry, _)| {
entry.sender == sender
&& U256::from_big_endian(entry.nonced_verification_data.nonce.as_slice()) < nonce
&& entry.nonced_verification_data.nonce < nonce
&& entry.nonced_verification_data.max_fee < replacement_max_fee
});

Expand Down Expand Up @@ -439,7 +434,7 @@ impl Batcher {

info!(
"Received message with nonce: {}",
U256::from_big_endian(client_msg.verification_data.nonce.as_slice())
client_msg.verification_data.nonce
);

if client_msg.verification_data.chain_id != self.chain_id {
Expand Down Expand Up @@ -495,7 +490,7 @@ impl Batcher {
}

// Nonce and max fee verification
let nonce = U256::from_big_endian(nonced_verification_data.nonce.as_slice());
let nonce = nonced_verification_data.nonce;
let max_fee = nonced_verification_data.max_fee;

if max_fee < U256::from(MIN_FEE_PER_PROOF) {
Expand Down Expand Up @@ -644,7 +639,7 @@ impl Batcher {
return false;
}

let nonce = U256::from_big_endian(&nonced_verification_data.nonce);
let nonce = nonced_verification_data.nonce;

batch_state.user_nonces.insert(addr, nonce + U256::one());
batch_state.user_min_fee.insert(addr, max_fee);
Expand Down Expand Up @@ -678,7 +673,7 @@ impl Batcher {
expected_user_nonce: U256,
) -> bool {
let replacement_max_fee = nonced_verification_data.max_fee;
let nonce = U256::from_big_endian(&nonced_verification_data.nonce);
let nonce = nonced_verification_data.nonce;

let mut replacement_entry = match batch_state.get_entry(addr, nonce) {
Some(entry) => {
Expand Down Expand Up @@ -761,7 +756,7 @@ impl Batcher {
info!("Adding verification data to batch...");

let max_fee = verification_data.max_fee;
let nonce = U256::from_big_endian(verification_data.nonce.as_slice());
let nonce = verification_data.nonce;

batch_state.batch_queue.push(
BatchQueueEntry::new(
Expand Down Expand Up @@ -1254,15 +1249,15 @@ impl Batcher {
}
};

debug!("non paying nonce: {:?}", nonpaying_nonce);
info!("non paying nonce: {:?}", nonpaying_nonce);

let nonce_value = *nonpaying_nonce;

let mut nonce_bytes = [0u8; 32];
nonpaying_nonce.to_big_endian(&mut nonce_bytes);
*nonpaying_nonce += U256::one();

NoncedVerificationData::new(
client_msg.verification_data.verification_data.clone(),
nonce_bytes,
nonce_value,
DEFAULT_MAX_FEE_PER_PROOF.into(), // 13_000 gas per proof * 100 gwei gas price (upper bound)
self.chain_id,
self.payment_service.address(),
Expand Down
6 changes: 1 addition & 5 deletions batcher/aligned-sdk/src/communication/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,14 @@ pub async fn send_messages(

let mut ws_write = ws_write.lock().await;

let mut nonce_bytes = [0u8; 32];

let mut response_stream = response_stream.lock().await;

let chain_id = U256::from(wallet.chain_id());

for (idx, verification_data) in verification_data.iter().enumerate() {
nonce.to_big_endian(&mut nonce_bytes);

let verification_data = NoncedVerificationData::new(
verification_data.clone(),
nonce_bytes,
nonce,
max_fees[idx],
chain_id,
payment_service_addr,
Expand Down
10 changes: 6 additions & 4 deletions batcher/aligned-sdk/src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::errors::VerifySignatureError;
// we don't have the fields of VerificationData, we only have the hash of the VerificationData.
// chain_id is not included in the type because it is now part of the domain.
const NONCED_VERIFICATION_DATA_TYPE: &[u8] =
b"NoncedVerificationData(bytes32 verification_data_hash,bytes32 nonce,uint256 max_fee)";
b"NoncedVerificationData(bytes32 verification_data_hash,uint256 nonce,uint256 max_fee)";

#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq)]
#[repr(u8)]
Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct VerificationData {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct NoncedVerificationData {
pub verification_data: VerificationData,
pub nonce: [u8; 32],
pub nonce: U256,
pub max_fee: U256,
pub chain_id: U256,
pub payment_service_addr: Address,
Expand All @@ -56,7 +56,7 @@ pub struct NoncedVerificationData {
impl NoncedVerificationData {
pub fn new(
verification_data: VerificationData,
nonce: [u8; 32],
nonce: U256,
max_fee: U256,
chain_id: U256,
payment_service_addr: Address,
Expand Down Expand Up @@ -218,7 +218,9 @@ impl Eip712 for NoncedVerificationData {
hasher.update(NONCED_VERIFICATION_DATA_TYPE);
let nonced_verification_data_type_hash = hasher.finalize_reset();

hasher.update(self.nonce);
let mut nonce_bytes = [0u8; 32];
self.nonce.to_big_endian(&mut nonce_bytes);
hasher.update(nonce_bytes);
let nonce_hash = hasher.finalize_reset();

let mut max_fee_bytes = [0u8; 32];
Expand Down
4 changes: 1 addition & 3 deletions batcher/aligned-sdk/src/eth/batcher_payment_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn batcher_payment_service(
}

impl SignatureData {
pub fn new(signature: &Signature, nonce: [u8; 32], max_fee: U256) -> Self {
pub fn new(signature: &Signature, nonce: U256, max_fee: U256) -> Self {
let mut signature_bytes = [0u8; 65];

signature.r.to_big_endian(&mut signature_bytes[0..32]);
Expand All @@ -33,8 +33,6 @@ impl SignatureData {

signature_bytes[64] = signature.v as u8;

let nonce = U256::from_big_endian(nonce.as_slice());

let signature_bytes = Bytes::from(signature_bytes);

SignatureData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"owner": "0x14dC79964da2C08b23698B3D3cc7Ca32193d9955"
},
"eip712": {
"noncedVerificationDataTypeHash": "2194895b48bd1793cdc112d96441a9e5291ef6151d5aa0e8e2386c81bdc61624"
"noncedVerificationDataTypeHash": "41817b5c5b0c3dcda70ccb43ba175fdcd7e586f9e0484422a2c6bba678fdf4a3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"owner": "<owner_address>"
},
"eip712": {
"noncedVerificationDataTypeHash": "2194895b48bd1793cdc112d96441a9e5291ef6151d5aa0e8e2386c81bdc61624"
"noncedVerificationDataTypeHash": "41817b5c5b0c3dcda70ccb43ba175fdcd7e586f9e0484422a2c6bba678fdf4a3"
}
}

0 comments on commit 7672899

Please sign in to comment.