Skip to content

Commit

Permalink
Merge pull request #223 from turbofish-org/fix-ibc-tx-encoding
Browse files Browse the repository at this point in the history
IBC fixes
  • Loading branch information
keppel authored Sep 14, 2023
2 parents 7dcc8c3 + e010e45 commit d554c73
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
29 changes: 21 additions & 8 deletions src/abci/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,22 +391,35 @@ impl<A: App> Application for InternalApp<ABCIPlugin<A>> {
store: WrappedMerk,
req: RequestBeginBlock,
) -> Result<ResponseBeginBlock> {
self.run(store, move |state| state.call(req.into()))??;
let (events, _logs) = self.run(store, move |state| -> Result<_> {
state.call(req.into())?;
Ok((
state.events.take().unwrap_or_default(),
state.logs.take().unwrap_or_default(),
))
})??;

Ok(Default::default())
Ok(ResponseBeginBlock { events })
}

fn end_block(&self, store: WrappedMerk, req: RequestEndBlock) -> Result<ResponseEndBlock> {
let mut updates = self.run(store, move |state| -> Result<_> {
let (mut updates, events, _logs) = self.run(store, move |state| -> Result<_> {
state.call(req.into())?;
Ok(state
.validator_updates
.take()
.expect("ABCI plugin did not create validator update map"))
Ok((
state
.validator_updates
.take()
.expect("ABCI plugin did not create validator update map"),
state.events.take().unwrap_or_default(),
state.logs.take().unwrap_or_default(),
))
})??;

// Write back validator updates
let mut res: ResponseEndBlock = Default::default();
let mut res = ResponseEndBlock {
events,
..Default::default()
};
updates.drain().for_each(|(_key, update)| {
if let Ok(flag) = std::env::var("ORGA_STATIC_VALSET") {
if flag != "0" && flag != "false" {
Expand Down
3 changes: 3 additions & 0 deletions src/ibc/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::encoding::{Decode, Encode};
use crate::Result;
use cosmrs::proto::cosmos::tx::v1beta1::Tx as TxProto;
use cosmrs::Tx;
use ed::Terminated;
use ibc::applications::transfer::msgs::transfer::MsgTransfer;
use ibc::core::MsgEnvelope;
use ibc_proto::google::protobuf::Any;
Expand Down Expand Up @@ -43,6 +44,8 @@ impl Decode for RawIbcTx {
}
}

impl Terminated for RawIbcTx {}

#[derive(Clone, Debug)]
pub enum IbcMessage {
Ics20(MsgTransfer),
Expand Down
14 changes: 10 additions & 4 deletions src/ibc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,17 @@ impl Encode for Adapter<IbcSigner> {
}

impl Decode for Adapter<IbcSigner> {
fn decode<R: std::io::Read>(input: R) -> ed::Result<Self> {
let bytes = input
.bytes()
.collect::<Result<Vec<_>, _>>()
fn decode<R: std::io::Read>(mut input: R) -> ed::Result<Self> {
let mut len_buf = [0; 4];
input.read_exact(&mut len_buf)?;
let len: u32 = borsh::BorshDeserialize::deserialize(&mut len_buf.as_slice())
.map_err(|_| ed::Error::UnexpectedByte(40))?;

let mut buf = vec![0; len as usize];

input.read_exact(&mut buf)?;
let bytes = [len_buf.to_vec(), buf].concat();

Ok(Self(
borsh::BorshDeserialize::deserialize(&mut bytes.as_slice())
.map_err(|_| ed::Error::UnexpectedByte(40))?,
Expand Down
31 changes: 29 additions & 2 deletions src/plugins/abci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,41 @@ impl<T: App> Call for ABCIPlugin<T> {
self.inner.init_chain(&ctx)?;
}
BeginBlock(req) => {
Context::add(Events::default());
Context::add(Logs::default());
self.events.replace(vec![]);
self.logs.replace(vec![]);
let ctx: BeginBlockCtx = req.into_inner().into();
self.time = ctx.header.clone().time;
create_time_ctx(&self.time);
self.inner.begin_block(&ctx)?;
let res = self.inner.begin_block(&ctx);
if res.is_ok() {
self.events
.replace(Context::resolve::<Events>().unwrap().events.clone());
}
self.logs
.replace(Context::resolve::<Logs>().unwrap().messages.clone());
Context::remove::<Events>();
Context::remove::<Logs>();

res?;
}
EndBlock(req) => {
Context::add(Events::default());
Context::add(Logs::default());
self.events.replace(vec![]);
self.logs.replace(vec![]);
let ctx = req.into_inner().into();
self.inner.end_block(&ctx)?;
let res = self.inner.end_block(&ctx);
if res.is_ok() {
self.events
.replace(Context::resolve::<Events>().unwrap().events.clone());
}
self.logs
.replace(Context::resolve::<Logs>().unwrap().messages.clone());
Context::remove::<Events>();
Context::remove::<Logs>();
res?;
}
DeliverTx(inner_call) => {
Context::add(Events::default());
Expand Down

0 comments on commit d554c73

Please sign in to comment.