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

fix: validate tx chain id #126

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bolt-sidecar/src/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ impl RpcClient {
Self(client)
}

/// Get the chain ID.
pub async fn get_chain_id(&self) -> TransportResult<u64> {
self.0.request("eth_chainId", ()).await
}

/// Get the basefee of the latest block.
pub async fn get_basefee(&self, block_number: Option<u64>) -> TransportResult<u128> {
let tag = block_number.map_or(BlockNumberOrTag::Latest, BlockNumberOrTag::Number);
Expand Down
9 changes: 9 additions & 0 deletions bolt-sidecar/src/primitives/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ impl InclusionRequest {
}
true
}

/// Validates the transaction chain id against the provided chain id.
/// Returns true if the chain id matches, false otherwise.
pub fn validate_chain_id(&self, chain_id: u64) -> bool {
match self.tx.chain_id() {
Some(tx_chain_id) if tx_chain_id == chain_id => true,
_ => false,
}
}
}

fn deserialize_tx_signed<'de, D>(deserializer: D) -> Result<TransactionSigned, D::Error>
Expand Down
12 changes: 12 additions & 0 deletions bolt-sidecar/src/state/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub enum ValidationError {
/// Could not recover signature,
#[error("Could not recover signer")]
RecoverSigner,
/// The transaction chain ID does not match the expected chain ID.
#[error("Chain ID mismatch")]
ChainIdMismatch,
/// NOTE: this should not be exposed to the user.
#[error("Internal error: {0}")]
Internal(String),
Expand Down Expand Up @@ -79,6 +82,9 @@ pub struct ExecutionState<C> {
/// proposal duties for a single lookahead.
block_templates: HashMap<Slot, BlockTemplate>,

/// The chain ID of the chain (constant).
chain_id: u64,

/// The state fetcher client.
client: C,
}
Expand All @@ -93,6 +99,7 @@ impl<C: StateFetcher> ExecutionState<C> {
slot: 0,
account_states: HashMap::new(),
block_templates: HashMap::new(),
chain_id: client.get_chain_id().await?,
client,
})
Comment on lines 99 to 104
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another way to do this can be to pass the chain ID value from config as a parameter while initializing the execution state, which felt to me as a dirty/ un-clean way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah this is cleaner, great job

}
Expand All @@ -118,6 +125,11 @@ impl<C: StateFetcher> ExecutionState<C> {
pub async fn try_commit(&mut self, request: &CommitmentRequest) -> Result<(), ValidationError> {
let CommitmentRequest::Inclusion(req) = request;

// Validate the chain ID
if !req.validate_chain_id(self.chain_id) {
return Err(ValidationError::ChainIdMismatch);
}
namn-grg marked this conversation as resolved.
Show resolved Hide resolved

let sender = req.tx.recover_signer().ok_or(ValidationError::Internal(
"Failed to recover signer from transaction".to_string(),
))?;
Expand Down
6 changes: 6 additions & 0 deletions bolt-sidecar/src/state/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub trait StateFetcher {
address: &Address,
block_number: Option<u64>,
) -> Result<AccountState, TransportError>;

async fn get_chain_id(&self) -> Result<u64, TransportError>;
}

/// A basic state fetcher that uses an RPC client to fetch state updates.
Expand Down Expand Up @@ -177,6 +179,10 @@ impl StateFetcher for StateClient {
}
}
}

async fn get_chain_id(&self) -> Result<u64, TransportError> {
self.client.get_chain_id().await
}
}

#[cfg(test)]
Expand Down