Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
feat(consensus): Support trsanction executor. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
yejiayu authored Sep 26, 2019
1 parent 2a7eb3c commit e1188f9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
48 changes: 39 additions & 9 deletions core/consensus/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
use std::marker::PhantomData;
use std::sync::Arc;

use async_trait::async_trait;

use protocol::traits::executor::{ExecutorExecResp, ExecutorFactory, TrieDB};
use protocol::traits::{
ConsensusAdapter, Context, Gossip, MemPool, MessageTarget, MixedTxHashes, Priority, Storage,
ConsensusAdapter, Context, CurrentConsensusStatus, Gossip, MemPool, MessageTarget,
MixedTxHashes, NodeInfo, Priority, Storage,
};
use protocol::types::{Epoch, Hash, Proof, Receipt, SignedTransaction, Validator};
use protocol::types::{Address, Epoch, Hash, Proof, Receipt, SignedTransaction, Validator};
use protocol::ProtocolResult;

pub struct OverlordConsensusAdapter<G: Gossip, M: MemPool, S: Storage> {
pub struct OverlordConsensusAdapter<
EF: ExecutorFactory<DB>,
G: Gossip,
M: MemPool,
S: Storage,
DB: TrieDB,
> {
network: Arc<G>,
mempool: Arc<M>,
storage: Arc<S>,
trie_db: Arc<DB>,

pin_ef: PhantomData<EF>,
}

#[async_trait]
impl<G, M, S> ConsensusAdapter for OverlordConsensusAdapter<G, M, S>
impl<EF, G, M, S, DB> ConsensusAdapter for OverlordConsensusAdapter<EF, G, M, S, DB>
where
EF: ExecutorFactory<DB>,
G: Gossip + Sync + Send,
M: MemPool,
S: Storage,
DB: TrieDB,
{
async fn get_txs_from_mempool(
&self,
Expand Down Expand Up @@ -71,9 +85,20 @@ where
async fn execute(
&self,
_ctx: Context,
_signed_txs: Vec<SignedTransaction>,
) -> ProtocolResult<()> {
Ok(())
node_info: NodeInfo,
status: CurrentConsensusStatus,
coinbase: Address,
signed_txs: Vec<SignedTransaction>,
) -> ProtocolResult<ExecutorExecResp> {
let mut executor = EF::from_root(
node_info.chain_id,
status.state_root,
Arc::clone(&self.trie_db),
status.epoch_id,
status.cycles_price,
coinbase,
)?;
executor.exec(signed_txs)
}

async fn flush_mempool(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()> {
Expand Down Expand Up @@ -110,17 +135,22 @@ where
}
}

impl<G, M, S> OverlordConsensusAdapter<G, M, S>
impl<EF, G, M, S, DB> OverlordConsensusAdapter<EF, G, M, S, DB>
where
EF: ExecutorFactory<DB>,
G: Gossip + Sync + Send,
M: MemPool,
S: Storage,
DB: TrieDB,
{
pub fn new(network: Arc<G>, mempool: Arc<M>, storage: Arc<S>) -> Self {
pub fn new(network: Arc<G>, mempool: Arc<M>, storage: Arc<S>, trie_db: Arc<DB>) -> Self {
OverlordConsensusAdapter {
network,
mempool,
storage,
trie_db,

pin_ef: PhantomData,
}
}
}
15 changes: 11 additions & 4 deletions protocol/src/traits/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use async_trait::async_trait;
use creep::Context;

use crate::types::{
Bloom, Epoch, Hash, MerkleRoot, Proof, Receipt, SignedTransaction, UserAddress, Validator,
Address, Bloom, Epoch, Hash, MerkleRoot, Proof, Receipt, SignedTransaction, UserAddress,
Validator,
};
use crate::{traits::mempool::MixedTxHashes, ProtocolResult};
use crate::{traits::executor::ExecutorExecResp, traits::mempool::MixedTxHashes, ProtocolResult};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MessageTarget {
Expand Down Expand Up @@ -92,8 +93,14 @@ pub trait ConsensusAdapter: Send + Sync {
) -> ProtocolResult<()>;

/// Execute some transactions.
async fn execute(&self, ctx: Context, signed_txs: Vec<SignedTransaction>)
-> ProtocolResult<()>;
async fn execute(
&self,
ctx: Context,
node_info: NodeInfo,
status: CurrentConsensusStatus,
coinbase: Address,
signed_txs: Vec<SignedTransaction>,
) -> ProtocolResult<ExecutorExecResp>;

/// Flush the given transactions in the mempool.
async fn flush_mempool(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,22 @@ async fn start(cfg: &Config) -> ProtocolResult<()> {
mempool_adapter,
));

// Init trie db
let path_state = cfg.data_path_for_state();
let trie_db = Arc::new(RocksTrieDB::new(path_state, cfg.executor.light).unwrap());

// Init Consensus
let consensus_adapter = Arc::new(OverlordConsensusAdapter::new(
let consensus_adapter = Arc::new(OverlordConsensusAdapter::<
TransactionExecutorFactory,
_,
_,
_,
_,
>::new(
Arc::new(network_service.handle()),
Arc::clone(&mempool),
Arc::clone(&storage),
Arc::clone(&trie_db),
));
let node_info = NodeInfo {
chain_id: chain_id.clone(),
Expand Down

0 comments on commit e1188f9

Please sign in to comment.