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
11 changes: 8 additions & 3 deletions core/src/ibc/channel_04/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ use super::types::Packet;
use crate::ibc;
use ibc::IdentifierSlice;

pub fn set_packet<'a>(ctx: &'a mut dyn ibc::Context, packet: &Packet, tag: &str) {
pub fn set_packet<'a>(
ctx: &'a mut dyn ibc::Context,
port: IdentifierSlice,
channel: IdentifierSlice,
packet: &Packet,
tag: &str,
) {
let value = rlp::encode(packet);
let path = format!("nastylogs/{}/{}/{}/latest", packet.source_port, packet.source_channel, tag);
let path = format!("nastylogs/{}/{}/{}/latest", port, channel, tag);
let result = ctx.get_kv_store_mut().insert(&path, &value);
if result.is_some() {
panic!("Packet already exists.");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in the commit message 'remove_pcket'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it. Thanks!

}

#[allow(dead_code)]
pub fn get_packet<'a>(
ctx: &'a mut dyn ibc::Context,
port: IdentifierSlice,
Expand Down
20 changes: 14 additions & 6 deletions core/src/ibc/channel_04/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +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 super::log::{remove_packet, set_packet};
use super::log::{get_packet, remove_packet, set_packet};
use super::types::{
Acknowledgement, ChannelEnd, ChannelOrder, ChannelState, Packet, PacketCommitment, PacketCommitmentHash, Sequence,
};
Expand Down Expand Up @@ -445,7 +445,10 @@ impl<'a> Manager<'a> {
let mut next_sequence_send = self.get_sequence_send(&packet.source_port, &packet.source_channel)?;

if packet.sequence != next_sequence_send {
return Err("Packet carries invalid sequence".to_owned())
return Err(format!(
"Packet carries invalid sequence. expected: {:?}, actual: {:?}",
next_sequence_send, packet.sequence
))
}

next_sequence_send.raw += 1;
Expand All @@ -467,7 +470,7 @@ impl<'a> Manager<'a> {
);

// check log.rs to understand this statement
set_packet(self.ctx, &packet, "send");
set_packet(self.ctx, &packet.source_port, &packet.source_channel, &packet, "send");

Ok(())
}
Expand Down Expand Up @@ -522,7 +525,10 @@ impl<'a> Manager<'a> {
);

if packet.sequence != next_sequence_recv {
return Err("Packet carries invalid sequence".to_owned())
return Err(format!(
"Packet carries invalid sequence. expected: {:?} actal: {:?}",
next_sequence_recv, packet.sequence
))
}
next_sequence_recv.raw += 1;
kv_store.insert(
Expand All @@ -535,8 +541,10 @@ impl<'a> Manager<'a> {

// check log.rs to understand this statement
// Unlike send, we just overwrite an old event.
remove_packet(self.ctx, &packet.dest_port, &packet.dest_channel, "recv");
set_packet(self.ctx, &packet, "recv");
if get_packet(self.ctx, &packet.dest_port, &packet.dest_channel, "recv").is_some() {
remove_packet(self.ctx, &packet.dest_port, &packet.dest_channel, "recv");
}
set_packet(self.ctx, &packet.dest_port, &packet.dest_channel, &packet, "recv");

Ok(packet)
}
Expand Down
22 changes: 18 additions & 4 deletions core/src/ibc/channel_04/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,25 @@ use primitives::{Bytes, H256};
use rlp;
use rlp::{DecoderError, Rlp, RlpStream};

#[derive(RlpEncodable, RlpDecodable, PartialEq, Debug)]
#[derive(PartialEq, Debug)]
pub struct Sequence {
pub raw: u64,
}

impl rlp::Encodable for Sequence {
fn rlp_append(&self, s: &mut RlpStream) {
s.append_single_value(&self.raw);
}
}

impl rlp::Decodable for Sequence {
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
Ok(Self {
raw: rlp.as_val()?,
})
}
}

#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ChannelState {
Expand Down Expand Up @@ -100,12 +114,12 @@ pub struct Packet {

/// Acknowledgement and PacketCommitment's behaviors are somewhat different from other ICS data:
/// They are not saved directly in the state, but the hash PacketCommitmentHash will be.
#[derive(RlpEncodable, RlpDecodable, PartialEq, Debug)]
#[derive(RlpEncodableWrapper, RlpDecodableWrapper, PartialEq, Debug)]
pub struct AcknowledgementHash {
pub raw: H256,
}

#[derive(RlpEncodable, RlpDecodable, PartialEq, Debug)]
#[derive(RlpEncodableWrapper, RlpDecodableWrapper, PartialEq, Debug)]
pub struct Acknowledgement {
pub raw: Bytes,
}
Expand All @@ -118,7 +132,7 @@ impl Acknowledgement {
}
}

#[derive(RlpEncodable, RlpDecodable, PartialEq, Debug)]
#[derive(RlpEncodableWrapper, RlpDecodableWrapper, PartialEq, Debug)]
pub struct PacketCommitmentHash {
pub raw: H256,
}
Expand Down
22 changes: 22 additions & 0 deletions core/src/ibc/transaction_handler/datagrams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ impl Decodable for Datagram {
#[cfg(test)]
mod tests {
use super::*;
use crate::ibc::channel_04::types::Sequence;
use rlp::{self, rlp_encode_and_decode_test};

#[test]
Expand Down Expand Up @@ -542,4 +543,25 @@ mod tests {
};
rlp_encode_and_decode_test!(chan_open_init);
}

#[test]
fn send_packet() {
let send_packet = Datagram::SendPacket {
raw: SendPacket {
tag: DatagramTag::SendPacket,
packet: Packet {
sequence: Sequence {
raw: 1,
},
timeout_height: 32,
source_port: "source_port".to_owned(),
source_channel: "source_channel".to_owned(),
dest_port: "dest_port".to_owned(),
dest_channel: "dest_channel".to_owned(),
data: b"data".to_vec(),
},
},
};
rlp_encode_and_decode_test!(send_packet);
}
}
2 changes: 1 addition & 1 deletion ibc.ts/chainA/chainA.schem.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"validators": [
"0xc7db8f558a87a45d3b4d2e7422abb48a27e6eeee00a0f884ca244d1d783d6ead2ca445e9ac8f08b311d78b4b4b69d48a9daf0c98d2a19e87e482f8060138ba88"
],
"timeoutPropose": 10000,
"timeoutPropose": 3000,
"timeoutProposeDelta": 500,
"timeoutPrevote": 1000,
"timeoutPrevoteDelta": 500,
Expand Down
2 changes: 1 addition & 1 deletion ibc.ts/chainB/chainB.schem.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"validators": [
"0x521d103d95b5b684f9be089ccc5cffd6b4ff2997ce535a7053e44d811b84f38abf2f35ebfeb5458e86a0b7d7293e71bcb3a59eddeb8b63801c2c4a8081fc67ab"
],
"timeoutPropose": 10000,
"timeoutPropose": 3000,
"timeoutProposeDelta": 500,
"timeoutPrevote": 1000,
"timeoutPrevoteDelta": 500,
Expand Down
56 changes: 55 additions & 1 deletion ibc.ts/src/common/chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Datagram } from "./datagram/index";
import { Datagram, PacketJSON } from "./datagram/index";
import { SDK } from "codechain-sdk";
import { H256, PlatformAddress } from "codechain-primitives";
import { IBC } from "./foundry/transaction";
Expand Down Expand Up @@ -136,6 +136,60 @@ export class Chain {
blockNumber
]);
}

public async queryCommitment(
sequence: number,
blockNumber?: number
): Promise<IBCQueryResult<string> | null> {
return this.sdk.rpc.sendRpcRequest("ibc_query_packet_commitment", [
"DEFAULT_PORT",
this.counterpartyIdentifiers.channel,
sequence,
blockNumber
]);
}

public async queryAcknowledgement(
sequence: number,
blockNumber?: number
): Promise<IBCQueryResult<string> | null> {
return this.sdk.rpc.sendRpcRequest("ibc_query_packet_acknowledgement", [
"DEFAULT_PORT",
this.counterpartyIdentifiers.channel,
sequence,
blockNumber
]);
}

public async queryLatestSendPacket(
blockNumber?: number
): Promise<PacketJSON | null> {
return this.sdk.rpc.sendRpcRequest("ibc_query_latest_send_packet", [
"DEFAULT_PORT",
this.counterpartyIdentifiers.channel,
blockNumber
]);
}

public async queryLatestRecvPacket(
blockNumber?: number
): Promise<PacketJSON | null> {
return this.sdk.rpc.sendRpcRequest("ibc_query_latest_recv_packet", [
"DEFAULT_PORT",
this.counterpartyIdentifiers.channel,
blockNumber
]);
}

public async queryNextSequenceRecv(
blockNumber?: number
): Promise<IBCQueryResult<number> | null> {
return this.sdk.rpc.sendRpcRequest("ibc_query_next_sequence_recv", [
"DEFAULT_PORT",
this.counterpartyIdentifiers.channel,
blockNumber
]);
}
}

async function waitForTx(sdk: SDK, txHash: H256) {
Expand Down
41 changes: 41 additions & 0 deletions ibc.ts/src/common/datagram/acknowledgePacket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Packet } from ".";

const RLP = require("rlp");

export class AcknowledgePacketDatagram {
private packet: Packet;
private ack: Buffer;
private proof: Buffer;
private proofHeight: number;

public constructor({
packet,
ack,
proof,
proofHeight
}: {
packet: Packet;
ack: Buffer;
proof: Buffer;
proofHeight: number;
}) {
this.packet = packet;
this.ack = ack;
this.proof = proof;
this.proofHeight = proofHeight;
}

public rlpBytes(): Buffer {
return RLP.encode(this.toEncodeObject());
}

public toEncodeObject(): any[] {
return [
15,
this.packet.toEncodeObject(),
this.ack,
this.proof,
this.proofHeight
];
}
}
50 changes: 50 additions & 0 deletions ibc.ts/src/common/datagram/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,53 @@ export interface Datagram {

export const ChannelOrdered = 0;
export const ChannelUnordered = 1;

export interface PacketJSON {
sequence: number;
timeoutHeight: number;
sourcePort: string;
sourceChannel: string;
destPort: string;
destChannel: string;
data: string;
}

export class Packet {
public readonly sequence: number;
public readonly timeoutHeight: number;
public readonly sourcePort: string;
public readonly sourceChannel: string;
public readonly destPort: string;
public readonly destChannel: string;
public readonly data: Buffer;

public constructor({
sequence,
timeoutHeight,
sourcePort,
sourceChannel,
destPort,
destChannel,
data
}: PacketJSON) {
this.sequence = sequence;
this.timeoutHeight = timeoutHeight;
this.sourcePort = sourcePort;
this.sourceChannel = sourceChannel;
this.destPort = destPort;
this.destChannel = destChannel;
this.data = Buffer.from(data, "hex");
}

public toEncodeObject(): any[] {
return [
this.sequence,
this.timeoutHeight,
this.sourcePort,
this.sourceChannel,
this.destPort,
this.destChannel,
this.data
];
}
}
41 changes: 41 additions & 0 deletions ibc.ts/src/common/datagram/recvPacket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Packet } from ".";

const RLP = require("rlp");

export class RecvPacketDatagram {
private packet: Packet;
private proof: Buffer;
private proofHeight: number;
private ack: Buffer;

public constructor({
packet,
proof,
proofHeight,
ack
}: {
packet: Packet;
proof: Buffer;
proofHeight: number;
ack: Buffer;
}) {
this.packet = packet;
this.proof = proof;
this.proofHeight = proofHeight;
this.ack = ack;
}

public rlpBytes(): Buffer {
return RLP.encode(this.toEncodeObject());
}

public toEncodeObject(): any[] {
return [
14,
this.packet.toEncodeObject(),
this.proof,
this.proofHeight,
this.ack
];
}
}
Loading