Skip to content

Commit 47f0014

Browse files
Joon-Mo YangJoon Mo Yang
authored andcommitted
Block parcels with invalid custom actions
1 parent de2cdd6 commit 47f0014

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

core/src/client/client.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ use std::time::Instant;
2222
use cio::IoChannel;
2323
use ckey::{Address, Public};
2424
use cnetwork::NodeId;
25-
use cstate::{Asset, AssetAddress, AssetScheme, AssetSchemeAddress, StateDB, TopLevelState, TopStateInfo};
25+
use cstate::{
26+
ActionHandler, Asset, AssetAddress, AssetScheme, AssetSchemeAddress, StateDB, TopBackend, TopLevelState,
27+
TopStateInfo,
28+
};
2629
use ctypes::invoice::ParcelInvoice;
2730
use ctypes::parcel::ChangeShard;
2831
use ctypes::transaction::Transaction;
@@ -449,6 +452,10 @@ impl BlockChainClient for Client {
449452
})
450453
})
451454
}
455+
456+
fn custom_handlers(&self) -> Vec<Arc<ActionHandler>> {
457+
self.state_db.read().custom_handlers().to_vec()
458+
}
452459
}
453460

454461
pub struct Importer {

core/src/client/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::sync::Arc;
3131

3232
use ckey::{Address, Public};
3333
use cnetwork::NodeId;
34-
use cstate::{Asset, AssetScheme, AssetSchemeAddress, TopStateInfo};
34+
use cstate::{ActionHandler, Asset, AssetScheme, AssetSchemeAddress, TopStateInfo};
3535
use ctypes::invoice::{Invoice, ParcelInvoice};
3636
use ctypes::parcel::ChangeShard;
3737
use ctypes::transaction::Transaction;
@@ -209,6 +209,8 @@ pub trait BlockChainClient: Sync + Send + AccountData + BlockChain + ImportBlock
209209
fn parcel_invoice(&self, id: ParcelId) -> Option<ParcelInvoice>;
210210

211211
fn transaction_invoice(&self, id: TransactionId) -> Option<Invoice>;
212+
213+
fn custom_handlers(&self) -> Vec<Arc<ActionHandler>>;
212214
}
213215

214216
/// Result of import block operation.

core/src/client/test_client.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::sync::Arc;
3838
use ckey::{Address, Generator, Random};
3939
use cmerkle::skewed_merkle_root;
4040
use cnetwork::NodeId;
41-
use cstate::StateDB;
41+
use cstate::{ActionHandler, StateDB};
4242
use ctypes::invoice::ParcelInvoice;
4343
use ctypes::parcel::{Action, Parcel};
4444
use ctypes::BlockNumber;
@@ -508,6 +508,10 @@ impl BlockChainClient for TestBlockChainClient {
508508
fn transaction_invoice(&self, _id: TransactionId) -> Option<Invoice> {
509509
unimplemented!()
510510
}
511+
512+
fn custom_handlers(&self) -> Vec<Arc<ActionHandler>> {
513+
unimplemented!()
514+
}
511515
}
512516

513517
impl super::EngineClient for TestBlockChainClient {

rpc/src/v1/impls/chain.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ use std::sync::Arc;
1818

1919
use ccore::{
2020
AssetClient, BlockId, ExecuteClient, MinerService, MiningBlockChainClient, RegularKey, Shard, SignedParcel,
21+
UnverifiedParcel,
2122
};
2223
use ckey::{Address, Public};
2324
use cstate::{Asset, AssetScheme, AssetSchemeAddress};
2425
use ctypes::invoice::{Invoice, ParcelInvoice};
25-
use ctypes::parcel::ChangeShard;
26+
use ctypes::parcel::{Action, ChangeShard};
2627
use ctypes::transaction::Transaction;
2728
use ctypes::{BlockNumber, ShardId};
2829
use primitives::{H160, H256, U256};
29-
use rlp::UntrustedRlp;
30+
use rlp::{DecoderError, UntrustedRlp};
3031

3132
use jsonrpc_core::Result;
3233

@@ -64,6 +65,17 @@ where
6465
UntrustedRlp::new(&raw.into_vec())
6566
.as_val()
6667
.map_err(errors::rlp)
68+
.and_then(|parcel: UnverifiedParcel| {
69+
match &parcel.as_unsigned().action {
70+
Action::Custom(bytes) => {
71+
if !self.client.custom_handlers().iter().any(|c| c.is_target(bytes)) {
72+
return Err(errors::rlp(DecoderError::Custom("Invalid custom action!")))
73+
}
74+
}
75+
_ => {}
76+
}
77+
Ok(parcel)
78+
})
6779
.and_then(|parcel| SignedParcel::new(parcel).map_err(errors::parcel_core))
6880
.and_then(|signed| {
6981
let hash = signed.hash();

sync/src/block/extension.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ use std::sync::Arc;
2121

2222
use ccore::encoded::Header as EncodedHeader;
2323
use ccore::{
24-
Block, BlockChainClient, BlockId, BlockImportError, ChainNotify, Header, ImportError, Seal, UnverifiedParcel,
24+
Block, BlockChainClient, BlockId, BlockImportError, BlockInfo, ChainInfo, ChainNotify, Client, Header, ImportBlock,
25+
ImportError, Seal, UnverifiedParcel,
2526
};
2627
use cnetwork::{Api, NetworkExtension, NodeId, TimerToken};
28+
use ctypes::parcel::Action;
2729
use ctypes::BlockNumber;
2830
use primitives::{H256, U256};
2931
use rlp::{Encodable, UntrustedRlp};
@@ -41,13 +43,13 @@ pub struct Extension {
4143
requests: RwLock<HashMap<NodeId, Vec<(u64, RequestMessage)>>>,
4244
header_downloaders: RwLock<HashMap<NodeId, HeaderDownloader>>,
4345
body_downloader: Mutex<BodyDownloader>,
44-
client: Arc<BlockChainClient>,
46+
client: Arc<Client>,
4547
api: Mutex<Option<Arc<Api>>>,
4648
last_request: AtomicUsize,
4749
}
4850

4951
impl Extension {
50-
pub fn new(client: Arc<BlockChainClient>) -> Arc<Self> {
52+
pub fn new(client: Arc<Client>) -> Arc<Self> {
5153
Arc::new(Self {
5254
requests: RwLock::new(HashMap::new()),
5355
header_downloaders: RwLock::new(HashMap::new()),
@@ -394,7 +396,20 @@ impl Extension {
394396

395397
headers.first().map(|header| header.number()) == Some(*start_number)
396398
}
397-
(RequestMessage::Bodies(..), ResponseMessage::Bodies(..)) => true,
399+
(RequestMessage::Bodies(_), ResponseMessage::Bodies(bodies)) => {
400+
for body in bodies {
401+
for parcel in body {
402+
let is_valid = match &parcel.as_unsigned().action {
403+
Action::Custom(bytes) => self.client.custom_handlers().iter().any(|c| c.is_target(bytes)),
404+
_ => true,
405+
};
406+
if !is_valid {
407+
return false
408+
}
409+
}
410+
}
411+
true
412+
}
398413
(RequestMessage::StateHead(..), ResponseMessage::StateHead(..)) => unimplemented!(),
399414
(
400415
RequestMessage::StateChunk {

0 commit comments

Comments
 (0)