Skip to content

Commit 28b0a4a

Browse files
Joon-Mo YangJoon Mo Yang
authored andcommitted
Implement HitHandler
1 parent fd9eeba commit 28b0a4a

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

codechain/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use clap::ArgMatches;
7070
use clogger::LoggerConfig;
7171
use cnetwork::{NetworkConfig, NetworkControl, NetworkControlError, NetworkService, SocketAddr};
7272
use creactor::EventLoop;
73+
use cstate::{ActionHandler, HitHandler};
7374
use csync::{BlockSyncExtension, ParcelSyncExtension, SnapshotService};
7475
use ctrlc::CtrlC;
7576
use fdlimit::raise_fd_limit;
@@ -266,7 +267,7 @@ fn run_node(matches: ArgMatches) -> Result<(), String> {
266267
let config = load_config(&matches)?;
267268

268269
// Add handlers here to accept additional custom actions
269-
let custom_action_handlers = vec![];
270+
let custom_action_handlers: Vec<Arc<ActionHandler>> = vec![Arc::new(HitHandler::new())];
270271
let spec = config.operating.chain.spec(custom_action_handlers)?;
271272

272273
let instance_id = config.operating.instance_id.unwrap_or(

state/src/action_handler/hit.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2018 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 ccrypto::blake256;
18+
use ctypes::invoice::Invoice;
19+
use ctypes::parcel::Outcome;
20+
use primitives::{Bytes, H256};
21+
use rlp::{self, Decodable, DecoderError, Encodable, UntrustedRlp};
22+
use trie::TrieMut;
23+
24+
use super::super::{StateResult, TopLevelState, TopState, TopStateInfo};
25+
use super::ActionHandler;
26+
27+
const ACTION_ID: u8 = 0;
28+
29+
pub struct HitAction {
30+
increase: u8,
31+
}
32+
33+
impl Decodable for HitAction {
34+
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
35+
if rlp.item_count()? != 2 {
36+
return Err(DecoderError::RlpIncorrectListLen)
37+
}
38+
if rlp.val_at::<u8>(0)? != ACTION_ID {
39+
return Err(DecoderError::Custom("Unknown message id detected"))
40+
}
41+
Ok(Self {
42+
increase: rlp.val_at(1)?,
43+
})
44+
}
45+
}
46+
47+
pub struct HitHandler {}
48+
49+
impl HitHandler {
50+
pub fn new() -> Self {
51+
Self {}
52+
}
53+
54+
fn address(&self) -> H256 {
55+
let mut hash: H256 = blake256(&b"metadata hit");
56+
hash[0] = b'M';
57+
hash
58+
}
59+
}
60+
61+
impl ActionHandler for HitHandler {
62+
fn init(&self, state: &mut TrieMut) -> StateResult<()> {
63+
let r = state.insert(&self.address(), &0u32.rlp_bytes());
64+
debug_assert_eq!(Ok(None), r);
65+
r?;
66+
Ok(())
67+
}
68+
69+
fn is_target(&self, bytes: &Bytes) -> bool {
70+
HitAction::decode(&UntrustedRlp::new(bytes)).is_ok()
71+
}
72+
73+
/// `bytes` must be valid encoding of HitAction
74+
fn execute(&self, bytes: &Bytes, state: &mut TopLevelState) -> Option<StateResult<Outcome>> {
75+
HitAction::decode(&UntrustedRlp::new(bytes)).ok().map(|action| {
76+
let prev_counter: u32 = rlp::decode(&state.action_data(&self.address())?);
77+
let increase = action.increase as u32;
78+
state.update_action_data(&self.address(), (prev_counter + increase).rlp_bytes().to_vec())?;
79+
Ok(Outcome::Single {
80+
invoice: Invoice::Success,
81+
error: None,
82+
})
83+
})
84+
}
85+
}

state/src/action_handler/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
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+
mod hit;
18+
1719
use ctypes::parcel::Outcome;
1820
use primitives::Bytes;
1921
use trie::TrieMut;
@@ -25,3 +27,5 @@ pub trait ActionHandler: Send + Sync {
2527
fn is_target(&self, bytes: &Bytes) -> bool;
2628
fn execute(&self, bytes: &Bytes, state: &mut TopLevelState) -> Option<StateResult<Outcome>>;
2729
}
30+
31+
pub use self::hit::HitHandler;

state/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mod traits;
5151
#[cfg(test)]
5252
pub mod tests;
5353

54-
pub use action_handler::ActionHandler;
54+
pub use action_handler::{ActionHandler, HitHandler};
5555
pub use backend::{Backend, ShardBackend, TopBackend};
5656
pub use checkpoint::{CheckpointId, StateWithCheckpoint};
5757
pub use db::StateDB;

0 commit comments

Comments
 (0)