Have the Router track its deployment block

Prevents a consensus split where some nodes would drop transfers if their node
didn't think the Router was deployed, and some would handle them.
This commit is contained in:
Luke Parker
2024-09-20 01:24:28 -04:00
parent 294462641e
commit 7e4c59a0a3
3 changed files with 38 additions and 4 deletions

View File

@@ -7,6 +7,9 @@ import "Schnorr.sol";
// _ is used as a prefix for internal functions and smart-contract-scoped variables
contract Router {
// The block at which this contract was deployed.
uint256 private _deploymentBlock;
// Nonce is incremented for each command executed, preventing replays
uint256 private _nonce;
@@ -63,6 +66,8 @@ contract Router {
}
constructor(bytes32 initialSeraiKey) _updateSeraiKeyAtEndOfFn(0, initialSeraiKey) {
_deploymentBlock = block.number;
// We consumed nonce 0 when setting the initial Serai key
_nonce = 1;
// Nonces are incremented by 1 upon account creation, prior to any code execution, per EIP-161
@@ -230,6 +235,10 @@ contract Router {
return _nonce;
}
function deploymentBlock() external view returns (uint256) {
return _deploymentBlock;
}
function smartContractNonce() external view returns (uint256) {
return _smartContractNonce;
}

View File

@@ -11,7 +11,7 @@ use alloy_consensus::TxLegacy;
use alloy_sol_types::{SolValue, SolConstructor, SolCall, SolEvent};
use alloy_rpc_types_eth::Filter;
use alloy_rpc_types_eth::{TransactionInput, TransactionRequest, Filter};
use alloy_transport::{TransportErrorKind, RpcError};
use alloy_simple_request_transport::SimpleRequest;
use alloy_provider::{Provider, RootProvider};
@@ -296,6 +296,23 @@ impl Router {
self.1
}
/// Fetch the block this contract was deployed at.
pub async fn deployment_block(&self) -> Result<u64, RpcError<TransportErrorKind>> {
let call = TransactionRequest::default()
.to(self.address())
.input(TransactionInput::new(abi::deploymentBlockCall::new(()).abi_encode().into()));
let bytes = self.0.call(&call).await?;
let deployment_block = abi::deploymentBlockCall::abi_decode_returns(&bytes, true)
.map_err(|e| {
TransportErrorKind::Custom(
format!("node returned a non-u256 for function returning u256: {e:?}").into(),
)
})?
._0;
Ok(deployment_block.try_into().unwrap())
}
/// Get the message to be signed in order to update the key for Serai.
pub fn update_serai_key_message(chain_id: U256, nonce: u64, key: &PublicKey) -> Vec<u8> {
(
@@ -420,7 +437,7 @@ impl Router {
*/
if let Some(matched) = Erc20::match_top_level_transfer(&self.0, tx_hash, self.1).await? {
// Mark this log index as used so it isn't used again
transfer_check.insert(matched.log_index);
transfer_check.insert(matched.id.1);
}
// Find a matching transfer log