Skip to content

Commit

Permalink
feat(xcvm): refactor protobuf handling (#4108)
Browse files Browse the repository at this point in the history
Firstly, move XCVM-related protocol messages to its own package.  This
will make it easier to separate protocol messages defined for other
components of the system (namely virtual wallet).  Secondly, move
stuff around in the Rust code to reflect the package structure in Rust
modules.  All of that is just refactoring and moving stuff around.

Lastly, replace Encodable trait by a Isomorphism trait which defines
mapping between Rust type and protocol message type together with
providing encoding and decoding methods.  The latter replace custom
decode functions.


Required for merge:
- [x] `pr-workflow-check / draft-release-check` is ✅ success
- Other rules GitHub shows you, or can be read in
[configuration](../terraform/github.com/branches.tf)

Makes review faster:
- [x] PR title is my best effort to provide summary of changes and has
clear text to be part of release notes
- [x] I marked PR by `misc` label if it should not be in release notes
- [x] Linked Zenhub/Github/Slack/etc reference if one exists
- [x] I was clear on what type of deployment required to release my
changes (node, runtime, contract, indexer, on chain operation, frontend,
infrastructure) if any in PR title or description
- [x] Added reviewer into `Reviewers`
- [x] I tagged(`@`) or used other form of notification of one person who
I think can handle best review of this PR
- [x] I have proved that PR has no general regressions of relevant
features and processes required to release into production
- [x] Any dependency updates made, was done according guides from
relevant dependency
- Clicking all checkboxes
- Adding detailed description of changes when it feels appropriate (for
example when PR is big)
  • Loading branch information
mina86 authored Sep 3, 2023
1 parent 21b2e44 commit 2343382
Show file tree
Hide file tree
Showing 11 changed files with 780 additions and 703 deletions.
11 changes: 4 additions & 7 deletions code/xcvm/cosmwasm/contracts/gateway/src/contract/ibc/xcvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ use cosmwasm_std::{
};
use ibc_rs_scale::core::ics24_host::identifier::{ChannelId, ConnectionId};
use xc_core::{
proto::{decode_packet, Encodable},
shared::XcPacket,
transport::ibc::ChannelInfo,
CallOrigin, XCVMAck,
proto::Isomorphism, shared::XcPacket, transport::ibc::ChannelInfo, CallOrigin, XCVMAck,
};

use super::make_ibc_failure_event;
Expand Down Expand Up @@ -94,7 +91,7 @@ pub fn ibc_packet_receive(
) -> Result<IbcReceiveResponse> {
let response = IbcReceiveResponse::default().add_event(make_event("receive"));
let msg = (|| -> Result<_> {
let packet: XcPacket = decode_packet(&msg.packet.data).map_err(ContractError::Protobuf)?;
let packet = XcPacket::try_decode(&msg.packet.data)?;
let call_origin = CallOrigin::Remote { user_origin: packet.user_origin };
let execute_program = msg::ExecuteProgramMsg {
salt: packet.salt,
Expand Down Expand Up @@ -122,7 +119,7 @@ pub fn ibc_packet_receive(
pub fn ibc_packet_ack(_deps: DepsMut, _env: Env, msg: IbcPacketAckMsg) -> Result<IbcBasicResponse> {
let ack = XCVMAck::try_from(msg.acknowledgement.data.as_slice())
.map_err(|_| ContractError::InvalidAck)?;
let _: XcPacket = decode_packet(&msg.original_packet.data).map_err(ContractError::Protobuf)?;
XcPacket::try_decode(&msg.original_packet.data)?;
Ok(IbcBasicResponse::default().add_event(make_event("ack").add_attribute("ack", ack)))
}

Expand All @@ -132,7 +129,7 @@ pub fn ibc_packet_timeout(
_env: Env,
msg: IbcPacketTimeoutMsg,
) -> Result<IbcBasicResponse> {
let _: XcPacket = decode_packet(&msg.packet.data).map_err(ContractError::Protobuf)?;
XcPacket::try_decode(&msg.packet.data)?;
// https://github.com/cosmos/ibc/pull/998
Ok(IbcBasicResponse::default())
}
Expand Down
12 changes: 9 additions & 3 deletions code/xcvm/cosmwasm/contracts/gateway/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::{IbcOrder, Response, StdError};
use ibc_rs_scale::core::ics24_host::identifier::IdentifierError;
use thiserror::Error;
use xc_core::{proto::DecodingFailure, AssetId, NetworkId};
use xc_core::{AssetId, NetworkId};

pub type Result<T = Response, E = ContractError> = core::result::Result<T, E>;

Expand Down Expand Up @@ -37,8 +37,8 @@ pub enum ContractError {
ProgramFundsDenomMappingToHostNotFound,
#[error("Program amount not equal to host amount")]
ProgramAmountNotEqualToHostAmount,
#[error("{0:?}")]
Protobuf(DecodingFailure),
#[error("{0}")]
Protobuf(xc_core::proto::DecodeError),
#[error("An invalid ACK was provided, this MUST be impossible.")]
InvalidAck,
#[error("An unknown reply ID was provided, this MUST be impossible.")]
Expand Down Expand Up @@ -80,6 +80,12 @@ pub enum ContractError {
AnonymousCallsCanDoOnlyLimitedSetOfActions,
}

impl From<xc_core::proto::DecodeError> for ContractError {
fn from(value: xc_core::proto::DecodeError) -> Self {
Self::Protobuf(value)
}
}

impl From<bech32_no_std::Error> for ContractError {
fn from(value: bech32_no_std::Error) -> Self {
Self::Bech32(value)
Expand Down
14 changes: 12 additions & 2 deletions code/xcvm/lib/core/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
extern crate prost_build;

fn main() {
prost_build::compile_protos(&["protos/xc.proto"], &["protos/"]).expect("compile time");
const PROTOS_DIR: &str = "protos";

fn main() -> std::io::Result<()> {
let mut files = Vec::new();
for entry in std::fs::read_dir(PROTOS_DIR)? {
if let Ok(name) = entry?.file_name().into_string() {
if !name.starts_with('.') && name.ends_with(".proto") {
files.push([PROTOS_DIR, "/", name.as_str()].concat())
}
}
}
prost_build::compile_protos(&files, &[PROTOS_DIR])
}
10 changes: 10 additions & 0 deletions code/xcvm/lib/core/protos/common.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package cvm.common;

message Uint128 {
uint64 highBits = 1;
uint64 lowBits = 2;

// next tag: 3
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
syntax = "proto3";

package xc;
import "common.proto";

package cvm.xcvm;

message PacketAsset {
AssetId assetId = 1;
Uint128 amount = 2;
cvm.common.Uint128 amount = 2;

// next tag: 3
}

message Packet {
Expand All @@ -13,25 +17,28 @@ message Packet {
Salt salt = 3;
Program program = 4;
repeated PacketAsset assets = 5;

// next tag: 6
}

message UserOrigin {
Network network = 1;
Account account = 2;
}

message Uint128 {
uint64 highBits = 1;
uint64 lowBits = 2;
// next tag: 3
}

message Program {
bytes tag = 1;
Instructions instructions = 2;

// next tag: 3
}

message Instructions {
repeated Instruction instructions = 1;

// next tag: 2
}

message Instruction {
Expand All @@ -41,21 +48,29 @@ message Instruction {
Call call = 3;
Exchange exchange = 4;
}

// next tag: 5
}


message Ratio {
uint64 nominator = 1;
uint64 denominator = 2;

// next tag: 3
}

message Unit {
Uint128 integer = 1;
cvm.common.Uint128 integer = 1;
Ratio ratio = 2;

// next tag: 3
}

message Absolute {
Uint128 value = 1;
cvm.common.Uint128 value = 1;

// next tag: 2
}

message Balance {
Expand All @@ -64,45 +79,64 @@ message Balance {
Absolute absolute = 2;
Unit unit = 3;
}

// next tag: 4
}

message Account {
bytes account = 1;

// next tag: 2
}

message AssetId {
Uint128 id = 1;
cvm.common.Uint128 id = 1;

// next tag: 2
}

message ExchangeId {
Uint128 id = 1;
cvm.common.Uint128 id = 1;

// next tag: 2
}

message Asset {
AssetId assetId = 1;
Balance balance = 2;

// next tag: 3
}

message Self {
uint32 self = 1;

// next tag: 2
}

message Tip {
uint32 id = 1;

// next tag: 2
}

message Result {
uint32 result = 1;
}

// next tag: 2
}

message AssetAmount {
AssetId assetId = 1;
Balance balance = 2;

// next tag: 3
}

message IpRegister {
uint64 ip = 1;

// next tag: 2
}

message BindingValue {
Expand All @@ -114,15 +148,21 @@ message BindingValue {
AssetId assetId = 5;
IpRegister ipRegister = 6;
}

// next tag: 7
}

message Binding {
uint32 position = 1;
BindingValue bindingValue = 2;

// next tag: 3
}

message Bindings {
repeated Binding bindings = 1;

// next tag: 2
}

message Transfer {
Expand All @@ -131,30 +171,42 @@ message Transfer {
Tip tip = 2;
}
repeated Asset assets = 3;

// next tag: 4
}

message Exchange {
ExchangeId id = 1;
message Exchange {
ExchangeId id = 1;
repeated Asset give = 5;
repeated Asset want = 6;

// next tag: 7
}

message Salt {
bytes salt = 1;

// next tag: 2
}

message Network {
uint32 networkId = 1;

// next tag: 2
}

message Spawn {
Network network = 1;
Salt salt = 3;
Program program = 4;
repeated Asset assets = 5;

// next tag: 6
}

message Call {
bytes payload = 1;
Bindings bindings = 2;

// next tag: 3
}
9 changes: 1 addition & 8 deletions code/xcvm/lib/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(
not(test),
deny(
clippy::disallowed_methods,
clippy::disallowed_types,
clippy::todo,
unused_imports,
unused_parens,
dead_code
)
deny(clippy::disallowed_methods, clippy::disallowed_types, clippy::todo, unused_parens,)
)]
#![feature(error_in_core)]

Expand Down
Loading

0 comments on commit 2343382

Please sign in to comment.