Skip to content

Migrate IBC transactions to use TopLevel transaction #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::transaction::{SignedTransaction, UnverifiedTransaction};
use crate::BlockId;
use ccrypto::BLAKE_NULL_RLP;
use ckey::Address;
use cstate::{FindActionHandler, StateDB, StateError, StateWithCache, TopLevelState};
use cstate::{FindActionHandler, IBCTransactionExecutor, StateDB, StateError, StateWithCache, TopLevelState};
use ctypes::errors::HistoryError;
use ctypes::header::{Header, Seal};
use ctypes::util::unexpected::Mismatch;
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'x> OpenBlock<'x> {
}

/// Push a transaction into the block.
pub fn push_transaction<C: ChainTimeInfo + FindActionHandler>(
pub fn push_transaction<C: ChainTimeInfo + FindActionHandler + IBCTransactionExecutor>(
&mut self,
tx: SignedTransaction,
h: Option<TxHash>,
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<'x> OpenBlock<'x> {
}

/// Push transactions onto the block.
pub fn push_transactions<C: ChainTimeInfo + FindActionHandler>(
pub fn push_transactions<C: ChainTimeInfo + FindActionHandler + IBCTransactionExecutor>(
&mut self,
transactions: &[SignedTransaction],
client: &C,
Expand Down Expand Up @@ -455,7 +455,7 @@ impl IsBlock for SealedBlock {
}

/// Enact the block given by block header, transactions and uncles
pub fn enact<C: ChainTimeInfo + EngineInfo + FindActionHandler + TermInfo>(
pub fn enact<C: ChainTimeInfo + EngineInfo + FindActionHandler + TermInfo + IBCTransactionExecutor>(
header: &Header,
transactions: &[SignedTransaction],
engine: &dyn CodeChainEngine,
Expand Down
17 changes: 16 additions & 1 deletion core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::client::{ConsensusClient, SnapshotClient, TermInfo};
use crate::consensus::{CodeChainEngine, EngineError};
use crate::encoded;
use crate::error::{BlockImportError, Error, ImportError, SchemeError};
use crate::ibc;
use crate::miner::{Miner, MinerService};
use crate::scheme::Scheme;
use crate::service::ClientIoMessage;
Expand All @@ -36,7 +37,9 @@ use cdb::{new_journaldb, Algorithm, AsHashDB, DatabaseError};
use cio::IoChannel;
use ckey::{Address, NetworkId, PlatformAddress, Public};
use cnetwork::NodeId;
use cstate::{ActionHandler, FindActionHandler, StateDB, StateResult, TopLevelState, TopStateView};
use cstate::{
ActionHandler, FindActionHandler, IBCTransactionExecutor, StateDB, StateResult, TopLevelState, TopStateView,
};
use ctimer::{TimeoutHandler, TimerApi, TimerScheduleError, TimerToken};
use ctypes::header::Header;
use ctypes::transaction::{AssetTransferInput, PartialHashing, ShardTransaction};
Expand Down Expand Up @@ -856,3 +859,15 @@ impl SnapshotClient for Client {
}
}
}

impl IBCTransactionExecutor for Client {
fn execute(
&self,
bytes: &[u8],
state: &mut TopLevelState,
fee_payer: &Address,
sender_pubkey: &Public,
) -> StateResult<()> {
ibc::execute_transaction(bytes, state, fee_payer, sender_pubkey)
}
}
2 changes: 2 additions & 0 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,5 @@ pub trait StateInfo {
pub trait SnapshotClient {
fn notify_snapshot(&self, id: BlockId);
}

pub trait IBCClient {}
4 changes: 3 additions & 1 deletion core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use cdb;
use ckey::{public_to_address, Address, Generator, KeyPair, NetworkId, PlatformAddress, Private, Public, Random};
use cnetwork::NodeId;
use cstate::tests::helpers::empty_top_state;
use cstate::{FindActionHandler, StateDB, TopLevelState};
use cstate::{FindActionHandler, IBCTransactionExecutor, StateDB, TopLevelState};
use ctimer::{TimeoutHandler, TimerToken};
use ctypes::header::Header;
use ctypes::transaction::{Action, Transaction};
Expand Down Expand Up @@ -691,3 +691,5 @@ impl StateInfo for TestBlockChainClient {
Some(top_state)
}
}

impl IBCTransactionExecutor for TestBlockChainClient {}
6 changes: 6 additions & 0 deletions core/src/codechain_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ impl CodeChainMachine {
// FIXME
0
}
Action::IBC {
..
} => {
// FIXME
0
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions core/src/consensus/solo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::client::ConsensusClient;
use crate::codechain_machine::CodeChainMachine;
use crate::consensus::{EngineError, EngineType};
use crate::error::Error;
use crate::ibc;
use ckey::Address;
use cstate::{ActionHandler, HitHandler, TopStateView};
use ctypes::{BlockHash, CommonParams, Header};
Expand All @@ -49,7 +48,6 @@ impl Solo {
action_handlers.push(Arc::new(HitHandler::new()));
}
action_handlers.push(Arc::new(stake::Stake::new(params.genesis_stakes.clone())));
action_handlers.push(Arc::new(ibc::custom_action_handler::IBC::new()));

Solo {
client: Default::default(),
Expand Down
1 change: 0 additions & 1 deletion core/src/consensus/tendermint/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ impl ConsensusEngine for Tendermint {
fn register_client(&self, client: Weak<dyn ConsensusClient>) {
*self.client.write() = Some(Weak::clone(&client));
self.stake.register_resources(Weak::clone(&client), Arc::downgrade(&self.validators));
self.ibc.register_resources(client);
}

fn is_proposal(&self, header: &Header) -> bool {
Expand Down
6 changes: 1 addition & 5 deletions core/src/consensus/tendermint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub use self::types::{Height, Step, View};
pub use super::{stake, ValidatorSet};
use crate::client::ConsensusClient;
use crate::codechain_machine::CodeChainMachine;
use crate::ibc;
use crate::snapshot_notify::NotifySender as SnapshotNotifySender;
use crate::ChainNotify;
use crossbeam_channel as crossbeam;
Expand Down Expand Up @@ -72,7 +71,6 @@ pub struct Tendermint {
action_handlers: Vec<Arc<dyn ActionHandler>>,
/// stake object to register client data later
stake: Arc<stake::Stake>,
ibc: Arc<ibc::custom_action_handler::IBC>,
/// Chain notify
chain_notify: Arc<TendermintChainNotify>,
has_signer: AtomicBool,
Expand All @@ -92,7 +90,6 @@ impl Tendermint {
pub fn new(our_params: TendermintParams, machine: CodeChainMachine) -> Arc<Self> {
let validators = Arc::clone(&our_params.validators);
let stake = Arc::new(stake::Stake::new(our_params.genesis_stakes));
let ibc = Arc::new(ibc::custom_action_handler::IBC::new());
let timeouts = our_params.timeouts;
let machine = Arc::new(machine);

Expand All @@ -104,7 +101,7 @@ impl Tendermint {
inner,
quit_tendermint,
) = worker::spawn(our_params.validators);
let action_handlers: Vec<Arc<dyn ActionHandler>> = vec![stake.clone(), ibc.clone()];
let action_handlers: Vec<Arc<dyn ActionHandler>> = vec![stake.clone()];
let chain_notify = Arc::new(TendermintChainNotify::new(inner.clone()));

Arc::new(Tendermint {
Expand All @@ -121,7 +118,6 @@ impl Tendermint {
machine,
action_handlers,
stake,
ibc,
chain_notify,
has_signer: false.into(),
})
Expand Down
11 changes: 7 additions & 4 deletions core/src/ibc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use super::kv_store;
use crate::ibc::custom_action_handler::CUSTOM_ACTION_HANDLER_ID;
use crate::ibc::KVStore;
use cstate::{ActionDataKeyBuilder, TopLevelState, TopState, TopStateView};
use ccrypto::blake256;
use cstate::{TopLevelState, TopState, TopStateView};
use primitives::H256;
use rlp::RlpStream;

pub trait Context {
fn get_kv_store(&mut self) -> &mut dyn kv_store::KVStore;
Expand Down Expand Up @@ -49,8 +50,10 @@ struct TopLevelKVStore<'a> {

impl<'a> TopLevelKVStore<'a> {
fn key(path: &str) -> H256 {
let key_builder = ActionDataKeyBuilder::new(CUSTOM_ACTION_HANDLER_ID, 1);
key_builder.append(&path.as_bytes()).into_key()
let mut rlp = RlpStream::new_list(2);
rlp.append(&"IBCData");
rlp.append(&path);
blake256(rlp.drain())
}
}

Expand Down
5 changes: 2 additions & 3 deletions core/src/ibc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ pub mod client_02;
#[allow(unused_variables)]
mod commitment_23;
pub mod context;
#[allow(dead_code)]
#[allow(unused_variables)]
pub mod custom_action_handler;
mod kv_store;
mod transaction_handler;

pub use self::client_02 as client;
pub use self::context::Context;
pub use self::kv_store::KVStore;
pub use transaction_handler::execute as execute_transaction;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Kodebox, Inc.
// Copyright 2019-2020 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
Expand All @@ -14,11 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::client::ConsensusClient;
use ctypes::errors::SyntaxError;
use ctypes::CommonParams;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use std::sync::Arc;

const ACTION_CREATE_CLIENT: u8 = 1;
const ACTION_UPDATE_CLIENT: u8 = 2;
Expand All @@ -36,24 +32,6 @@ pub enum Action {
},
}

impl Action {
pub fn verify(
&self,
current_params: &CommonParams,
client: Option<Arc<dyn ConsensusClient>>,
) -> Result<(), SyntaxError> {
match self {
Action::CreateClient {
..
} => {}
Action::UpdateClient {
..
} => {}
}
Ok(())
}
}

impl Encodable for Action {
fn rlp_append(&self, s: &mut RlpStream) {
match self {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 Kodebox, Inc.
// Copyright 2020 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
Expand All @@ -17,88 +17,39 @@
mod actions;

use self::actions::Action;
use crate::client::ConsensusClient;
use crate::ibc;
use ckey::{Address, Public};
use cstate::{ActionHandler, StateResult, TopLevelState};
use cstate::{StateResult, TopLevelState};
use ctypes::errors::RuntimeError;
use ctypes::errors::SyntaxError;
use ctypes::{CommonParams, Header};
use ibc::client_02 as ibc_client;
use ibc::client_02::foundry as ibc_foundry;
use ibc::context as ibc_context;
use parking_lot::RwLock;
use rlp::{Decodable, Rlp};
use std::sync::{Arc, Weak};

pub const CUSTOM_ACTION_HANDLER_ID: u64 = 3;

#[derive(Default)]
pub struct IBC {
client: RwLock<Option<Weak<dyn ConsensusClient>>>,
}

impl IBC {
pub fn new() -> Self {
IBC {
client: Default::default(),
}
}

pub fn register_resources(&self, client: Weak<dyn ConsensusClient>) {
*self.client.write() = Some(Weak::clone(&client));
pub fn execute(
bytes: &[u8],
state: &mut TopLevelState,
fee_payer: &Address,
_sender_public: &Public,
) -> StateResult<()> {
let action = Action::decode(&Rlp::new(bytes)).expect("Verification passed");
match action {
Action::CreateClient {
id,
kind,
consensus_state,
} => create_client(state, fee_payer, &id, kind, &consensus_state),
Action::UpdateClient {
id,
header,
} => update_client(state, &id, &header),
}
}

impl ActionHandler for IBC {
fn name(&self) -> &'static str {
"IBC handler"
}

fn handler_id(&self) -> u64 {
CUSTOM_ACTION_HANDLER_ID
}

fn init(&self, state: &mut TopLevelState) -> StateResult<()> {
Ok(())
}

fn execute(
&self,
bytes: &[u8],
state: &mut TopLevelState,
fee_payer: &Address,
sender_public: &Public,
) -> StateResult<()> {
let action = Action::decode(&Rlp::new(bytes)).expect("Verification passed");
match action {
Action::CreateClient {
id,
kind,
consensus_state,
} => create_client(state, fee_payer, &id, kind, &consensus_state),
Action::UpdateClient {
id,
header,
} => update_client(state, &id, &header),
}
}

fn verify(&self, bytes: &[u8], current_params: &CommonParams) -> Result<(), SyntaxError> {
let action =
Action::decode(&Rlp::new(bytes)).map_err(|err| SyntaxError::InvalidCustomAction(err.to_string()))?;
let client: Option<Arc<dyn ConsensusClient>> = self.client.read().as_ref().and_then(Weak::upgrade);
action.verify(current_params, client)
}

fn on_close_block(&self, _state: &mut TopLevelState, _header: &Header) -> StateResult<()> {
Ok(())
}
}

fn create_client(
state: &mut TopLevelState,
fee_payer: &Address,
_fee_payer: &Address,
id: &str,
kind: ibc_client::Kind,
consensus_state: &[u8],
Expand Down
6 changes: 6 additions & 0 deletions core/src/miner/mem_pool_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ impl MemPoolMinFees {
// FIXME
0
}
Action::IBC {
..
} => {
// FIXME
0
}
}
}
}
Loading