-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enha: implement Consensus trait for Importer (#1608)
- Loading branch information
1 parent
8b79e7e
commit f19c442
Showing
7 changed files
with
89 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,56 @@ | ||
pub mod raft; | ||
pub mod simple_consensus; | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
|
||
use crate::eth::primitives::Bytes; | ||
use crate::eth::primitives::Hash; | ||
use crate::infra::metrics; | ||
use crate::infra::BlockchainClient; | ||
|
||
#[async_trait] | ||
pub trait Consensus: Send + Sync { | ||
async fn should_serve(&self) -> bool; | ||
fn should_forward(&self) -> bool; | ||
async fn forward(&self, transaction: Bytes) -> anyhow::Result<Hash>; | ||
/// Whether this node should serve requests. | ||
async fn should_serve(&self) -> bool { | ||
let lag = match self.lag().await { | ||
Ok(lag) => lag, | ||
Err(err) => { | ||
tracing::error!(?err, "failed to get the lag between this node and the leader"); | ||
return false; | ||
} | ||
}; | ||
|
||
let should_serve = lag <= 3; | ||
|
||
if !should_serve { | ||
tracing::info!(?lag, "validator and replica are too far appart"); | ||
} | ||
|
||
return should_serve; | ||
} | ||
|
||
/// Forward a transaction | ||
async fn forward(&self, transaction: Bytes) -> anyhow::Result<Hash> { | ||
#[cfg(feature = "metrics")] | ||
let start = metrics::now(); | ||
|
||
let blockchain_client = self.get_chain()?; | ||
|
||
let result = blockchain_client.send_raw_transaction(transaction.into()).await?; | ||
|
||
#[cfg(feature = "metrics")] | ||
metrics::inc_consensus_forward(start.elapsed()); | ||
|
||
let tx_hash = result.tx_hash; | ||
let validator_url = &blockchain_client.http_url; | ||
tracing::info!(%tx_hash, ?validator_url, "forwarded eth_sendRawTransaction to leader"); | ||
|
||
Ok(result.tx_hash) | ||
} | ||
|
||
fn get_chain(&self) -> anyhow::Result<&Arc<BlockchainClient>>; | ||
|
||
/// Get the lag between this node and the leader. | ||
async fn lag(&self) -> anyhow::Result<u64>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters