|
| 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 | +} |
0 commit comments