Skip to content

Commit

Permalink
feat(signer+application): increment nonce txn
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed May 31, 2024
1 parent 841e4f0 commit ed8766a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
1 change: 1 addition & 0 deletions core/application/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl<B: Backend> State<B> {
UpdateMethod::UpdateContentRegistry { updates } => {
self.update_content_registry(txn.payload.sender, updates)
},
UpdateMethod::IncrementNonce {} => TransactionResponse::Success(ExecutionData::None),
};

#[cfg(debug_assertions)]
Expand Down
69 changes: 42 additions & 27 deletions core/signer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,35 +212,50 @@ impl SignerState {
self.next_nonce = self.base_nonce + 1;
// Resend all transactions in the buffer.

self.pending_transactions.retain_mut(|tx| {
if let TransactionResponse::Revert(_) =
query_runner.simulate_txn(tx.update_request.clone().into())
for tx in self.pending_transactions.iter_mut() {
if matches!(
query_runner.simulate_txn(tx.update_request.clone().into()),
TransactionResponse::Revert(_)
) || tx.tries >= MAX_RETRIES
{
// If transaction reverts, don't retry.
false
} else if tx.tries < MAX_RETRIES {
if tx.update_request.payload.nonce != self.next_nonce {
tx.update_request.payload.nonce = self.next_nonce;
tx.update_request.payload.secondary_nonce = self.next_secondary_nonce;

let digest = tx.update_request.payload.to_digest();
let signature = self.node_secret_key.sign(&digest);
tx.update_request.signature = signature.into();
}

// Update timestamp to resending time.
tx.timestamp = SystemTime::now();
if self.base_timestamp.is_none() {
self.base_timestamp = Some(tx.timestamp);
}

self.next_nonce += 1;
self.next_secondary_nonce += 1;
true
} else {
false
// If transaction reverts or we reached the maximum number of retries, don't
// retry again.
// To prevent invalidating the nonces of the following pending transactions,
// we have to increment the nonce on the application state.
let method = UpdateMethod::IncrementNonce {};
let update_payload = UpdatePayload {
sender: TransactionSender::NodeMain(self.node_public_key),
method,
nonce: self.next_nonce,
secondary_nonce: self.next_secondary_nonce,
chain_id: self.chain_id.unwrap(),
};
let digest = update_payload.to_digest();
let signature = self.node_secret_key.sign(&digest);
let update_request = UpdateRequest {
signature: signature.into(),
payload: update_payload,
};
tx.update_request = update_request;
} else if tx.update_request.payload.nonce != self.next_nonce {
// Note: with the change to using the increment nonce txn, this check
// should not be necessary anymore.
tx.update_request.payload.nonce = self.next_nonce;
tx.update_request.payload.secondary_nonce = self.next_secondary_nonce;

let digest = tx.update_request.payload.to_digest();
let signature = self.node_secret_key.sign(&digest);
tx.update_request.signature = signature.into();
}
// Update timestamp to resending time.
tx.timestamp = SystemTime::now();
if self.base_timestamp.is_none() {
self.base_timestamp = Some(tx.timestamp);
}
});

self.next_nonce += 1;
self.next_secondary_nonce += 1;
}

for pending_tx in self.pending_transactions.iter_mut() {
if let Err(e) = self
Expand Down
5 changes: 5 additions & 0 deletions core/types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ pub enum UpdateMethod {
/// provided by the network and the corresponding nodes that
/// are providing that content.
UpdateContentRegistry { updates: Vec<ContentUpdate> },
/// Increment the node nonce.
IncrementNonce {},
}

impl ToDigest for UpdatePayload {
Expand Down Expand Up @@ -642,6 +644,9 @@ impl ToDigest for UpdatePayload {
.with("remove", &(update.remove as u8));
}
},
UpdateMethod::IncrementNonce {} => {
transcript_builder = transcript_builder.with("transaction_name", &"inc_nonce");
},
}

transcript_builder
Expand Down

0 comments on commit ed8766a

Please sign in to comment.