Skip to content

Commit

Permalink
feat(protocol): Add the mempool traits (#7)
Browse files Browse the repository at this point in the history
* define transaction-pool trait

* rename transaction_pool to mempool

* derive PartialEq, Eq, Hash for Hash, Address, AccountAddress

* add check_transaction interface

* reorder crate ref
  • Loading branch information
rev-chaos authored and yejiayu committed Oct 31, 2019
1 parent 197c0b9 commit 81ff4f3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
Empty file added core/mempool/src/tx_cache.rs
Empty file.
64 changes: 64 additions & 0 deletions protocol/src/traits/mempool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::collections::HashMap;

use async_trait::async_trait;

use crate::types::{Hash, SignedTransaction};
use crate::ProtocolResult;

#[allow(dead_code)]
pub type Context = HashMap<String, String>;

#[allow(dead_code)]
pub struct MixedTxHashes {
pub order_tx_hashes: Vec<Hash>,
pub propose_tx_hashes: Vec<Hash>,
}

#[async_trait]
pub trait MemPool<Adapter: MemPoolAdapter>: Send + Sync {
async fn insert(&self, ctx: Context, tx: SignedTransaction) -> ProtocolResult<()>;

async fn package(
&self,
ctx: Context,
count: u64,
cycle_limit: u64,
) -> ProtocolResult<MixedTxHashes>;

async fn flush(&self, ctx: Context, tx_hashes: Vec<Hash>) -> ProtocolResult<()>;

async fn get_full_txs(
&self,
ctx: Context,
tx_hashes: Vec<Hash>,
) -> ProtocolResult<Vec<SignedTransaction>>;

async fn ensure_order_txs(
&self,
ctx: Context,
order_tx_hashes: Vec<Hash>,
) -> ProtocolResult<()>;

async fn sync_propose_txs(
&self,
ctx: Context,
propose_tx_hashes: Vec<Hash>,
) -> ProtocolResult<()>;
}

#[async_trait]
pub trait MemPoolAdapter: Send + Sync {
async fn pull_txs(
&self,
ctx: Context,
tx_hashes: Vec<Hash>,
) -> ProtocolResult<SignedTransaction>;

async fn broadcast_txs(&self, ctx: Context, txs: Vec<SignedTransaction>) -> ProtocolResult<()>;

async fn check_signature(&self, ctx: Context, tx: SignedTransaction) -> ProtocolResult<()>;

async fn check_transaction(&self, ctx: Context, tx: SignedTransaction) -> ProtocolResult<()>;

async fn check_storage_exist(&self, ctx: Context, tx_hash: Hash) -> ProtocolResult<()>;
}
2 changes: 2 additions & 0 deletions protocol/src/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod executor;
mod mempool;
mod storage;

pub use executor::{Executor, ExecutorAdapter};
pub use mempool::{Context, MemPool, MemPoolAdapter, MixedTxHashes};
pub use storage::{Storage, StorageAdapter, StorageCategory};
6 changes: 3 additions & 3 deletions protocol/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::ProtocolResult;
/// Hash length
const HASH_LEN: usize = 32;

#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Hash([u8; HASH_LEN]);

/// Merkel root hash
Expand Down Expand Up @@ -91,11 +91,11 @@ pub enum ContractType {

/// The address consists of 21 bytes, the first of which is a magic number that
/// identifies which type the address belongs to.
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Hash)]
struct Address([u8; ADDRESS_LEN]);

/// Note: the account address here is an external account, not a contract.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct AccountAddress {
inner: Address,
}
Expand Down

0 comments on commit 81ff4f3

Please sign in to comment.