Skip to content

Commit

Permalink
protobuf breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Sep 6, 2023
1 parent 2343382 commit 0f38485
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 432 deletions.
8 changes: 4 additions & 4 deletions code/xcvm/cosmwasm/contracts/interpreter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ pub fn handle_execute_step(
interpret_transfer(&mut deps, &env, &tip, to, assets),
Instruction::Call { bindings, encoded } =>
interpret_call(deps.as_ref(), &env, bindings, encoded, instruction_pointer, &tip),
Instruction::Spawn { network, salt, assets, program } =>
interpret_spawn(&mut deps, &env, network, salt, assets, program),
Instruction::Exchange { give, id, want } =>
interpret_exchange(&mut deps, give, want, id, env.contract.address.clone()),
Instruction::Spawn { network_id, salt, assets, program } =>
interpret_spawn(&mut deps, &env, network_id, salt, assets, program),
Instruction::Exchange { exchange_id, give, want } =>
interpret_exchange(&mut deps, give, want, exchange_id, env.contract.address.clone()),
}?;
// Save the intermediate IP so that if the execution fails, we can recover at which
// instruction it happened.
Expand Down
118 changes: 31 additions & 87 deletions code/xcvm/lib/core/protos/xcvm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,36 @@ import "common.proto";
package cvm.xcvm;

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

// next tag: 3
}

message Packet {
Account interpreter = 1;
bytes interpreter = 1;
UserOrigin user_origin = 2;
Salt salt = 3;
bytes salt = 3;
Program program = 4;
repeated PacketAsset assets = 5;

// next tag: 6
}

message UserOrigin {
Network network = 1;
Account account = 2;
uint32 network_id = 1;
bytes account = 2;

// next tag: 3
}

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

// next tag: 3
}

message Instructions {
repeated Instruction instructions = 1;

// next tag: 2
}

message Instruction {
oneof instruction {
Transfer transfer = 1;
Expand Down Expand Up @@ -83,121 +77,71 @@ message Balance {
// next tag: 4
}

message Account {
bytes account = 1;

// next tag: 2
}

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

// next tag: 2
}

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

// next tag: 2
}

message Asset {
AssetId assetId = 1;
cvm.common.Uint128 asset_id = 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;
cvm.common.Uint128 asset_id = 1;
Balance balance = 2;

// next tag: 3
}

message IpRegister {
uint64 ip = 1;

// next tag: 2
}

message BindingValue {
oneof type {
Self self = 1;
Tip tip = 2;
Result result = 3;
AssetAmount assetAmount = 4;
AssetId assetId = 5;
IpRegister ipRegister = 6;
Register register = 1;
cvm.common.Uint128 asset_id = 2;
AssetAmount asset_amount = 3;
}

// next tag: 7
// next tag: 4
}

message Binding {
uint32 position = 1;
BindingValue bindingValue = 2;
enum Register {
IP = 0;
TIP = 1;
THIS = 2;
RESULT = 3;

// next tag: 3
// next tag: 4
}

message Bindings {
repeated Binding bindings = 1;
message Binding {
uint32 position = 1;
BindingValue binding_value = 2;

// next tag: 2
// next tag: 3
}

message Transfer {
oneof account_type{
Account account = 1;
bytes account = 1;
Tip tip = 2;
}
repeated Asset assets = 3;

// next tag: 4
}

message Tip {
// next tag: 1
}

message Exchange {
ExchangeId id = 1;
cvm.common.Uint128 exchange_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;
uint32 network_id = 1;
bytes salt = 3;
Program program = 4;
repeated Asset assets = 5;

Expand All @@ -206,7 +150,7 @@ message Spawn {

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

// next tag: 3
}
14 changes: 7 additions & 7 deletions code/xcvm/lib/core/src/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ mod tests {
program: XcProgram {
tag: b"spawn_with_asset".to_vec(),
instructions: [Instruction::Spawn {
network: 3.into(),
network_id: 3.into(),
salt: b"spawn_with_asset".to_vec(),
assets: vec![(pica_on_osmosis, 1_000_000_000u128)].into(),
program: XcProgram {
Expand Down Expand Up @@ -378,7 +378,7 @@ mod tests {
program: XcProgram {
tag: b"spawn_with_asset".to_vec(),
instructions: [Instruction::Spawn {
network: 3.into(),
network_id: 3.into(),
salt: b"spawn_with_asset".to_vec(),
assets: vec![(pica_on_osmosis, 1_000_000_000u128)].into(),
program: XcProgram {
Expand Down Expand Up @@ -488,19 +488,19 @@ mod tests {
program: XcProgram {
tag: b"spawn_with_asset".to_vec(),
instructions: [Instruction::Spawn {
network: 3.into(),
network_id: 3.into(),
salt: b"spawn_with_asset".to_vec(),
assets: vec![(pica_on_osmosis, 1_000_000_000u128)].into(),
program: XcProgram {
tag: b"spawn_with_asset".to_vec(),
instructions: [
XcInstruction::Exchange {
id: pica_osmo_on_osmosis.into(),
exchange_id: pica_osmo_on_osmosis.into(),
give: XcFundsFilter::one(pica_on_osmosis, 1_000_000_000u128),
want: XcFundsFilter::one(osmo_on_osmosis, 1_000u128),
},
XcInstruction::Spawn {
network: 2.into(),
network_id: 2.into(),
salt: b"spawn_with_asset".to_vec(),
assets: XcFundsFilter::one(osmo_on_centauri, (100, 100)),
program: XcProgram {
Expand Down Expand Up @@ -564,7 +564,7 @@ mod tests {
"instructions": [
{
"exchange": {
"id": "237684489387467420151587012609",
"exchange_id": "237684489387467420151587012609",
"give": [
[
"237684487542793012780631851009",
Expand Down Expand Up @@ -667,7 +667,7 @@ mod tests {
program: XcProgram {
tag: b"spawn_with_asset".to_vec(),
instructions: [Instruction::Spawn {
network: 2.into(),
network_id: 2.into(),
salt: b"spawn_with_asset".to_vec(),
assets: vec![(osmo_on_centauri, 1_000_000_000u128)].into(),
program: XcProgram {
Expand Down
4 changes: 2 additions & 2 deletions code/xcvm/lib/core/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub enum Instruction<Payload, Account, Assets> {
/// The program will be spawned with the desired [`Assets`].
/// The salt is used to track the program when events are dispatched in the network.
Spawn {
network: crate::network::NetworkId,
network_id: crate::network::NetworkId,
/// If JSON, than hex encoded non prefixed lower case string.
#[serde(serialize_with = "hex::serialize", deserialize_with = "hex::deserialize")]
#[cfg_attr(feature = "std", schemars(schema_with = "String::json_schema"))]
Expand All @@ -85,7 +85,7 @@ pub enum Instruction<Payload, Account, Assets> {
program: Program<VecDeque<Self>>,
},
Exchange {
id: ExchangeId,
exchange_id: ExchangeId,
give: Assets,
want: Assets,
},
Expand Down
4 changes: 2 additions & 2 deletions code/xcvm/lib/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
builder.instructions.push_back(Instruction::Spawn {
salt: salt.into(),
assets: assets.into(),
network: SpawningNetwork::ID,
network_id: SpawningNetwork::ID,
program: f(ProgramBuilder::<SpawningNetwork, Account, Assets>::new(tag.into()))?
.build(),
});
Expand Down Expand Up @@ -190,7 +190,7 @@ mod tests {
Instruction::Call { bindings: vec![], encoded: vec![202, 254, 190, 239] },
// Move to ethereum
Instruction::Spawn {
network: Ethereum::ID,
network_id: Ethereum::ID,
salt: Vec::new(),
assets: Funds::default(),
program: Program {
Expand Down
58 changes: 58 additions & 0 deletions code/xcvm/lib/core/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,61 @@ impl core::fmt::Display for DecodeError {
}
}
}

/// Maps elements of one sequence and produces the other.
///
/// This is a convenience function for `Vec<T> → Vec<U>` operation (though it
/// works for any containers) where infallible `T → U` conversion exists.
fn from_sequence<R: core::iter::FromIterator<U>, T, U: From<T>>(
sequence: impl core::iter::IntoIterator<Item = T>,
) -> R {
sequence.into_iter().map(U::from).collect()
}

/// Tries to map elements of one sequence and produces the other.
///
/// This is a convenience function for `Vec<T> → Vec<U>` operation (though it
/// works for any containers) where fallible `T → U` conversion exists. Returns
/// error on the first conversion that fails.
fn try_from_sequence<R: core::iter::FromIterator<U>, T, U: TryFrom<T>>(
sequence: impl core::iter::IntoIterator<Item = T>,
) -> Result<R, ()> {
sequence.into_iter().map(U::try_from).collect::<Result<R, _>>().map_err(|_| ())
}

/// Trait providing method which converts ‘empty’ values to ‘Err(())’.
///
/// Useful for checking whether fields in protocol buffer messages are set.
trait NonEmptyExt: Sized {
type Output: Sized;
fn non_empty(self) -> Result<Self::Output, ()>;
}

impl NonEmptyExt for alloc::string::String {
type Output = Self;
fn non_empty(self) -> Result<Self::Output, ()> {
if self.is_empty() {
Err(())
} else {
Ok(self)
}
}
}

impl<T> NonEmptyExt for alloc::vec::Vec<T> {
type Output = Self;
fn non_empty(self) -> Result<Self::Output, ()> {
if self.is_empty() {
Err(())
} else {
Ok(self)
}
}
}

impl<T> NonEmptyExt for Option<T> {
type Output = T;
fn non_empty(self) -> Result<Self::Output, ()> {
self.ok_or(())
}
}
12 changes: 12 additions & 0 deletions code/xcvm/lib/core/src/proto/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ impl From<u128> for pb::common::Uint128 {
}
}

impl From<pb::common::Uint128> for crate::AssetId {
fn from(value: pb::common::Uint128) -> Self {
u128::from(value).into()
}
}

impl From<crate::AssetId> for pb::common::Uint128 {
fn from(value: crate::AssetId) -> Self {
u128::from(value).into()
}
}

#[test]
fn test_u128_uint128_conversion() {
let value = 0xDEAD_0000_0000_0000_BEEF_0000_0000_0000_u128;
Expand Down
Loading

0 comments on commit 0f38485

Please sign in to comment.