Skip to content

Commit 203779d

Browse files
Seulgi Kimsgkim126
authored andcommitted
Create a new RPC category named mempool
It contains 1. mempool_sendSignedTransaction 2. mempool_getErrorHint 3. mempool_getTransactionResultsByTracker 4. mempool_getPendingTransactions 5. mempool_getPendingTransactionsCount
1 parent 18f66ba commit 203779d

File tree

10 files changed

+322
-246
lines changed

10 files changed

+322
-246
lines changed

codechain/rpc_apis.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ pub struct ApiDependencies {
3333
impl ApiDependencies {
3434
pub fn extend_api(&self, enable_devel_api: bool, handler: &mut MetaIoHandler<(), impl Middleware<()>>) {
3535
use crpc::v1::*;
36-
handler.extend_with(ChainClient::new(Arc::clone(&self.client), Arc::clone(&self.miner)).to_delegate());
36+
handler.extend_with(ChainClient::new(Arc::clone(&self.client)).to_delegate());
37+
handler.extend_with(MempoolClient::new(Arc::clone(&self.client), Arc::clone(&self.miner)).to_delegate());
3738
if enable_devel_api {
3839
handler.extend_with(
3940
DevelClient::new(Arc::clone(&self.client), Arc::clone(&self.miner), self.block_sync.clone())

rpc/src/v1/impls/chain.rs

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,30 @@
1717
use std::sync::Arc;
1818

1919
use ccore::{
20-
AssetClient, BlockId, EngineInfo, ExecuteClient, MinerService, MiningBlockChainClient, RegularKey, RegularKeyOwner,
21-
Shard, SignedTransaction, TextClient,
20+
AssetClient, BlockId, EngineInfo, ExecuteClient, MiningBlockChainClient, RegularKey, RegularKeyOwner, Shard,
21+
TextClient,
2222
};
2323
use ccrypto::Blake;
24-
use cjson::bytes::Bytes;
2524
use cjson::uint::Uint;
2625
use ckey::{public_to_address, NetworkId, PlatformAddress, Public};
2726
use cstate::FindActionHandler;
2827
use ctypes::transaction::{Action, ShardTransaction as ShardTransactionType};
2928
use ctypes::{BlockNumber, ShardId};
3029
use primitives::{Bytes as BytesArray, H160, H256};
31-
use rlp::UntrustedRlp;
3230

3331
use jsonrpc_core::Result;
3432

3533
use super::super::errors;
3634
use super::super::traits::Chain;
37-
use super::super::types::{
38-
AssetScheme, Block, BlockNumberAndHash, OwnedAsset, PendingTransactions, Text, Transaction, UnsignedTransaction,
39-
};
35+
use super::super::types::{AssetScheme, Block, BlockNumberAndHash, OwnedAsset, Text, Transaction, UnsignedTransaction};
4036

41-
pub struct ChainClient<C, M>
37+
pub struct ChainClient<C>
4238
where
43-
C: AssetClient + MiningBlockChainClient + Shard + RegularKey + RegularKeyOwner + ExecuteClient + EngineInfo,
44-
M: MinerService, {
39+
C: AssetClient + MiningBlockChainClient + Shard + RegularKey + RegularKeyOwner + ExecuteClient + EngineInfo, {
4540
client: Arc<C>,
46-
miner: Arc<M>,
4741
}
4842

49-
impl<C, M> ChainClient<C, M>
43+
impl<C> ChainClient<C>
5044
where
5145
C: AssetClient
5246
+ MiningBlockChainClient
@@ -56,17 +50,15 @@ where
5650
+ ExecuteClient
5751
+ EngineInfo
5852
+ TextClient,
59-
M: MinerService,
6053
{
61-
pub fn new(client: Arc<C>, miner: Arc<M>) -> Self {
54+
pub fn new(client: Arc<C>) -> Self {
6255
ChainClient {
6356
client,
64-
miner,
6557
}
6658
}
6759
}
6860

69-
impl<C, M> Chain for ChainClient<C, M>
61+
impl<C> Chain for ChainClient<C>
7062
where
7163
C: AssetClient
7264
+ MiningBlockChainClient
@@ -78,20 +70,7 @@ where
7870
+ FindActionHandler
7971
+ TextClient
8072
+ 'static,
81-
M: MinerService + 'static,
8273
{
83-
fn send_signed_transaction(&self, raw: Bytes) -> Result<H256> {
84-
UntrustedRlp::new(&raw.into_vec())
85-
.as_val()
86-
.map_err(|e| errors::rlp(&e))
87-
.and_then(|tx| SignedTransaction::try_new(tx).map_err(errors::transaction_core))
88-
.and_then(|signed| {
89-
let hash = signed.hash();
90-
self.miner.import_own_transaction(&*self.client, signed).map_err(errors::transaction_core).map(|_| hash)
91-
})
92-
.map(Into::into)
93-
}
94-
9574
fn get_transaction(&self, transaction_hash: H256) -> Result<Option<Transaction>> {
9675
let id = transaction_hash.into();
9776
Ok(self.client.transaction(&id).map(From::from))
@@ -105,15 +84,6 @@ where
10584
Ok(self.client.transaction_by_tracker(&tracker).map(From::from))
10685
}
10786

108-
fn get_transaction_results_by_tracker(&self, tracker: H256) -> Result<Vec<bool>> {
109-
Ok(self
110-
.client
111-
.error_hints_by_tracker(&tracker)
112-
.into_iter()
113-
.map(|(_hash, error_hint)| error_hint.is_none())
114-
.collect())
115-
}
116-
11787
fn get_asset_scheme_by_tracker(
11888
&self,
11989
tracker: H256,
@@ -183,10 +153,6 @@ where
183153
Ok(self.client.balance(address, block_id.into()).map(Into::into))
184154
}
185155

186-
fn get_error_hint(&self, transaction_hash: H256) -> Result<Option<String>> {
187-
Ok(self.client.error_hint(&transaction_hash))
188-
}
189-
190156
fn get_regular_key(&self, address: PlatformAddress, block_number: Option<u64>) -> Result<Option<Public>> {
191157
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
192158
let address = address.try_address().map_err(errors::core)?;
@@ -269,14 +235,6 @@ where
269235
Ok(self.client.block(&BlockId::Hash(block_hash)).map(|block| block.transactions_count()))
270236
}
271237

272-
fn get_pending_transactions(&self, from: Option<u64>, to: Option<u64>) -> Result<PendingTransactions> {
273-
Ok(self.client.ready_transactions(from.unwrap_or(0)..to.unwrap_or(::std::u64::MAX)).into())
274-
}
275-
276-
fn get_pending_transactions_count(&self, from: Option<u64>, to: Option<u64>) -> Result<usize> {
277-
Ok(self.client.count_pending_transactions(from.unwrap_or(0)..to.unwrap_or(::std::u64::MAX)))
278-
}
279-
280238
fn get_mining_reward(&self, block_number: u64) -> Result<Option<u64>> {
281239
Ok(self.client.mining_reward(block_number))
282240
}

rpc/src/v1/impls/mempool.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2019 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use std::sync::Arc;
18+
19+
use ccore::{MinerService, MiningBlockChainClient, SignedTransaction};
20+
use cjson::bytes::Bytes;
21+
use primitives::H256;
22+
use rlp::UntrustedRlp;
23+
24+
use jsonrpc_core::Result;
25+
26+
use super::super::errors;
27+
use super::super::traits::Mempool;
28+
use super::super::types::PendingTransactions;
29+
30+
pub struct MempoolClient<C, M>
31+
where
32+
C: MiningBlockChainClient,
33+
M: MinerService, {
34+
client: Arc<C>,
35+
miner: Arc<M>,
36+
}
37+
38+
impl<C, M> MempoolClient<C, M>
39+
where
40+
C: MiningBlockChainClient,
41+
M: MinerService,
42+
{
43+
pub fn new(client: Arc<C>, miner: Arc<M>) -> Self {
44+
MempoolClient {
45+
client,
46+
miner,
47+
}
48+
}
49+
}
50+
51+
impl<C, M> Mempool for MempoolClient<C, M>
52+
where
53+
C: MiningBlockChainClient + 'static,
54+
M: MinerService + 'static,
55+
{
56+
fn send_signed_transaction(&self, raw: Bytes) -> Result<H256> {
57+
UntrustedRlp::new(&raw.into_vec())
58+
.as_val()
59+
.map_err(|e| errors::rlp(&e))
60+
.and_then(|tx| SignedTransaction::try_new(tx).map_err(errors::transaction_core))
61+
.and_then(|signed| {
62+
let hash = signed.hash();
63+
self.miner.import_own_transaction(&*self.client, signed).map_err(errors::transaction_core).map(|_| hash)
64+
})
65+
.map(Into::into)
66+
}
67+
68+
fn get_transaction_results_by_tracker(&self, tracker: H256) -> Result<Vec<bool>> {
69+
Ok(self
70+
.client
71+
.error_hints_by_tracker(&tracker)
72+
.into_iter()
73+
.map(|(_hash, error_hint)| error_hint.is_none())
74+
.collect())
75+
}
76+
77+
fn get_error_hint(&self, transaction_hash: H256) -> Result<Option<String>> {
78+
Ok(self.client.error_hint(&transaction_hash))
79+
}
80+
81+
fn get_pending_transactions(&self, from: Option<u64>, to: Option<u64>) -> Result<PendingTransactions> {
82+
Ok(self.client.ready_transactions(from.unwrap_or(0)..to.unwrap_or(::std::u64::MAX)).into())
83+
}
84+
85+
fn get_pending_transactions_count(&self, from: Option<u64>, to: Option<u64>) -> Result<usize> {
86+
Ok(self.client.count_pending_transactions(from.unwrap_or(0)..to.unwrap_or(::std::u64::MAX)))
87+
}
88+
}

rpc/src/v1/impls/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ mod account;
1818
mod chain;
1919
mod devel;
2020
mod engine;
21+
mod mempool;
2122
mod miner;
2223
mod net;
2324

2425
pub use self::account::AccountClient;
2526
pub use self::chain::ChainClient;
2627
pub use self::devel::DevelClient;
2728
pub use self::engine::EngineClient;
29+
pub use self::mempool::MempoolClient;
2830
pub use self::miner::MinerClient;
2931
pub use self::net::NetClient;

rpc/src/v1/traits/chain.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,17 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use cjson::bytes::Bytes;
1817
use cjson::uint::Uint;
1918
use ckey::{NetworkId, PlatformAddress, Public};
2019
use ctypes::{BlockNumber, ShardId};
2120
use primitives::{Bytes as BytesArray, H160, H256};
2221

2322
use jsonrpc_core::Result;
2423

25-
use super::super::types::{
26-
AssetScheme, Block, BlockNumberAndHash, OwnedAsset, PendingTransactions, Text, Transaction, UnsignedTransaction,
27-
};
24+
use super::super::types::{AssetScheme, Block, BlockNumberAndHash, OwnedAsset, Text, Transaction, UnsignedTransaction};
2825

2926
build_rpc_trait! {
3027
pub trait Chain {
31-
/// Sends signed transaction, returning its hash.
32-
# [rpc(name = "chain_sendSignedTransaction")]
33-
fn send_signed_transaction(&self, Bytes) -> Result<H256>;
34-
3528
/// Gets transaction with given hash.
3629
# [rpc(name = "chain_getTransaction")]
3730
fn get_transaction(&self, H256) -> Result<Option<Transaction>>;
@@ -44,10 +37,6 @@ build_rpc_trait! {
4437
# [rpc(name = "chain_getTransactionByTracker")]
4538
fn get_transaction_by_tracker(&self, H256) -> Result<Option<Transaction>>;
4639

47-
/// Gets transaction results with given transaction tracker.
48-
# [rpc(name = "chain_getTransactionResultsByTracker")]
49-
fn get_transaction_results_by_tracker(&self, H256) -> Result<Vec<bool>>;
50-
5140
/// Gets asset scheme with given transaction tracker.
5241
# [rpc(name = "chain_getAssetSchemeByTracker")]
5342
fn get_asset_scheme_by_tracker(&self, H256, ShardId, Option<u64>) -> Result<Option<AssetScheme>>;
@@ -76,10 +65,6 @@ build_rpc_trait! {
7665
# [rpc(name = "chain_getBalance")]
7766
fn get_balance(&self, PlatformAddress, Option<u64>) -> Result<Option<Uint>>;
7867

79-
/// Gets a hint to find out why the transaction failed.
80-
# [rpc(name = "chain_getErrorHint")]
81-
fn get_error_hint(&self, H256) -> Result<Option<String>>;
82-
8368
/// Gets regular key with given account
8469
# [rpc(name = "chain_getRegularKey")]
8570
fn get_regular_key(&self, PlatformAddress, Option<u64>) -> Result<Option<Public>>;
@@ -136,14 +121,6 @@ build_rpc_trait! {
136121
# [rpc(name = "chain_getBlockTransactionCountByHash")]
137122
fn get_block_transaction_count_by_hash(&self, H256) -> Result<Option<usize>>;
138123

139-
/// Gets transactions in the current mem pool.
140-
# [rpc(name = "chain_getPendingTransactions")]
141-
fn get_pending_transactions(&self, Option<u64>, Option<u64>) -> Result<PendingTransactions>;
142-
143-
/// Gets the count of transactions in the current mem pool.
144-
# [rpc(name = "chain_getPendingTransactionsCount")]
145-
fn get_pending_transactions_count(&self, Option<u64>, Option<u64>) -> Result<usize>;
146-
147124
/// Gets the mining given block number
148125
# [rpc(name = "chain_getMiningReward")]
149126
fn get_mining_reward(&self, u64) -> Result<Option<u64>>;

rpc/src/v1/traits/mempool.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use cjson::bytes::Bytes;
18+
use primitives::H256;
19+
20+
use jsonrpc_core::Result;
21+
22+
use super::super::types::PendingTransactions;
23+
24+
build_rpc_trait! {
25+
pub trait Mempool {
26+
/// Sends signed transaction, returning its hash.
27+
# [rpc(name = "mempool_sendSignedTransaction")]
28+
fn send_signed_transaction(&self, Bytes) -> Result<H256>;
29+
30+
/// Gets transaction results with given transaction tracker.
31+
# [rpc(name = "mempool_getTransactionResultsByTracker")]
32+
fn get_transaction_results_by_tracker(&self, H256) -> Result<Vec<bool>>;
33+
34+
/// Gets a hint to find out why the transaction failed.
35+
# [rpc(name = "mempool_getErrorHint")]
36+
fn get_error_hint(&self, H256) -> Result<Option<String>>;
37+
38+
/// Gets transactions in the current mem pool.
39+
# [rpc(name = "mempool_getPendingTransactions")]
40+
fn get_pending_transactions(&self, Option<u64>, Option<u64>) -> Result<PendingTransactions>;
41+
42+
/// Gets the count of transactions in the current mem pool.
43+
# [rpc(name = "mempool_getPendingTransactionsCount")]
44+
fn get_pending_transactions_count(&self, Option<u64>, Option<u64>) -> Result<usize>;
45+
}
46+
}

rpc/src/v1/traits/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ mod account;
1818
mod chain;
1919
mod devel;
2020
mod engine;
21+
mod mempool;
2122
mod miner;
2223
mod net;
2324

2425
pub use self::account::Account;
2526
pub use self::chain::Chain;
2627
pub use self::devel::Devel;
2728
pub use self::engine::Engine;
29+
pub use self::mempool::Mempool;
2830
pub use self::miner::Miner;
2931
pub use self::net::Net;

0 commit comments

Comments
 (0)