Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};

const ACTION_CREATE_CLIENT: u8 = 1;
const ACTION_UPDATE_CLIENT: u8 = 2;
const DATAGRAM_CREATE_CLIENT: u8 = 1;
const DATAGRAM_UPDATE_CLIENT: u8 = 2;

#[derive(Debug, PartialEq)]
pub enum Action {
pub enum Datagram {
CreateClient {
id: String,
kind: u8,
Expand All @@ -32,58 +32,58 @@ pub enum Action {
},
}

impl Encodable for Action {
impl Encodable for Datagram {
fn rlp_append(&self, s: &mut RlpStream) {
match self {
Action::CreateClient {
Datagram::CreateClient {
id,
kind,
consensus_state,
} => {
s.begin_list(4).append(&ACTION_CREATE_CLIENT).append(id).append(kind).append(consensus_state);
s.begin_list(4).append(&DATAGRAM_CREATE_CLIENT).append(id).append(kind).append(consensus_state);
}
Action::UpdateClient {
Datagram::UpdateClient {
id,
header,
} => {
s.begin_list(3).append(&ACTION_UPDATE_CLIENT).append(id).append(header);
s.begin_list(3).append(&DATAGRAM_UPDATE_CLIENT).append(id).append(header);
}
};
}
}

impl Decodable for Action {
impl Decodable for Datagram {
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
let tag = rlp.val_at(0)?;
match tag {
ACTION_CREATE_CLIENT => {
DATAGRAM_CREATE_CLIENT => {
let item_count = rlp.item_count()?;
if item_count != 4 {
return Err(DecoderError::RlpInvalidLength {
expected: 4,
got: item_count,
})
}
Ok(Action::CreateClient {
Ok(Datagram::CreateClient {
id: rlp.val_at(1)?,
kind: rlp.val_at(2)?,
consensus_state: rlp.val_at(3)?,
})
}
ACTION_UPDATE_CLIENT => {
DATAGRAM_UPDATE_CLIENT => {
let item_count = rlp.item_count()?;
if item_count != 3 {
return Err(DecoderError::RlpInvalidLength {
expected: 3,
got: item_count,
})
}
Ok(Action::UpdateClient {
Ok(Datagram::UpdateClient {
id: rlp.val_at(1)?,
header: rlp.val_at(2)?,
})
}
_ => Err(DecoderError::Custom("Unexpected IBC Action Type")),
_ => Err(DecoderError::Custom("Unexpected IBC Datagram Type")),
}
}
}
12 changes: 6 additions & 6 deletions core/src/ibc/transaction_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// 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/>.

mod actions;
mod datagrams;

use self::actions::Action;
use self::datagrams::Datagram;
use crate::ibc;
use ckey::{Address, Public};
use cstate::{StateResult, TopLevelState};
Expand All @@ -32,14 +32,14 @@ pub fn execute(
fee_payer: &Address,
_sender_public: &Public,
) -> StateResult<()> {
let action = Action::decode(&Rlp::new(bytes)).expect("Verification passed");
match action {
Action::CreateClient {
let datagram = Datagram::decode(&Rlp::new(bytes)).expect("Verification passed");
match datagram {
Datagram::CreateClient {
id,
kind,
consensus_state,
} => create_client(state, fee_payer, &id, kind, &consensus_state),
Action::UpdateClient {
Datagram::UpdateClient {
id,
header,
} => update_client(state, &id, &header),
Expand Down