Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update bcs structs for versioned contracts #166

Merged
merged 16 commits into from
Oct 9, 2024
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
5 changes: 5 additions & 0 deletions .changeset/friendly-chefs-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@axelar-network/axelar-cgp-sui': patch
---

feat: added structs for versioned contracts
12 changes: 6 additions & 6 deletions move/relayer_discovery/sources/discovery.move
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module relayer_discovery::discovery;

use axelar_gateway::channel::Channel;
use relayer_discovery::relayer_discovery_v0::{Self, RelayerDiscoveryV0};
use relayer_discovery::relayer_discovery_v0::{Self, RelayerDiscovery_v0};
use relayer_discovery::transaction::Transaction;
use std::ascii;
use sui::versioned::{Self, Versioned};
Expand All @@ -18,7 +18,7 @@ use version_control::version_control::{Self, VersionControl};
/// -------
/// This is the version of the package that should change every package upgrade.
const VERSION: u64 = 0;
/// This is the version of the data that should change when we need to migrate `Versioned` type (e.g. from `RelayerDiscoveryV0` to `RelayerDiscoveryV1`)
/// This is the version of the data that should change when we need to migrate `Versioned` type (e.g. from `RelayerDiscovery_v0` to `RelayerDiscoveryV1`)
const DATA_VERSION: u64 = 0;

/// -------
Expand Down Expand Up @@ -50,19 +50,19 @@ fun init(ctx: &mut TxContext) {
macro fun value(
$self: &RelayerDiscovery,
$function_name: vector<u8>,
): &RelayerDiscoveryV0 {
): &RelayerDiscovery_v0 {
let relayer_discovery = $self;
let value = relayer_discovery.inner.load_value<RelayerDiscoveryV0>();
let value = relayer_discovery.inner.load_value<RelayerDiscovery_v0>();
value.version_control().check(VERSION, ascii::string($function_name));
value
}

macro fun value_mut(
$self: &mut RelayerDiscovery,
$function_name: vector<u8>,
): &mut RelayerDiscoveryV0 {
): &mut RelayerDiscovery_v0 {
let relayer_discovery = $self;
let value = relayer_discovery.inner.load_value_mut<RelayerDiscoveryV0>();
let value = relayer_discovery.inner.load_value_mut<RelayerDiscovery_v0>();
value.version_control().check(VERSION, ascii::string($function_name));
value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use version_control::version_control::VersionControl;
/// A central shared object that stores discovery configuration for the
/// Relayer. The Relayer will use this object to discover and execute the
/// transactions when a message is targeted at specific channel.
public struct RelayerDiscoveryV0 has store {
public struct RelayerDiscovery_v0 has store {
/// A map of channel IDs to the target that needs to be executed by the
/// relayer. There can be only one configuration per channel.
configurations: Table<ID, Transaction>,
Expand All @@ -30,15 +30,15 @@ const EChannelNotFound: vector<u8> = b"channel not found";
public(package) fun new(
version_control: VersionControl,
ctx: &mut TxContext,
): RelayerDiscoveryV0 {
RelayerDiscoveryV0 {
): RelayerDiscovery_v0 {
RelayerDiscovery_v0 {
configurations: table::new<ID, Transaction>(ctx),
version_control,
}
}

public(package) fun set_transaction(
self: &mut RelayerDiscoveryV0,
self: &mut RelayerDiscovery_v0,
id: ID,
transaction: Transaction,
) {
Expand All @@ -49,23 +49,23 @@ public(package) fun set_transaction(
}

public(package) fun remove_transaction(
self: &mut RelayerDiscoveryV0,
self: &mut RelayerDiscovery_v0,
id: ID,
): Transaction {
assert!(self.configurations.contains(id), EChannelNotFound);
self.configurations.remove(id)
}

public(package) fun get_transaction(
self: &RelayerDiscoveryV0,
self: &RelayerDiscovery_v0,
id: ID,
): Transaction {
assert!(self.configurations.contains(id), EChannelNotFound);
self.configurations[id]
}

public(package) fun version_control(
self: &RelayerDiscoveryV0,
self: &RelayerDiscovery_v0,
): &VersionControl {
&self.version_control
}
Expand All @@ -74,13 +74,13 @@ public(package) fun version_control(
// Test Only
// ---------
#[test_only]
public(package) fun destroy_for_testing(self: RelayerDiscoveryV0) {
public(package) fun destroy_for_testing(self: RelayerDiscovery_v0) {
sui::test_utils::destroy(self);
}

#[test_only]
fun dummy(ctx: &mut TxContext): RelayerDiscoveryV0 {
RelayerDiscoveryV0 {
fun dummy(ctx: &mut TxContext): RelayerDiscovery_v0 {
RelayerDiscovery_v0 {
configurations: table::new<ID, Transaction>(ctx),
version_control: version_control::version_control::new(vector[]),
}
Expand Down
97 changes: 90 additions & 7 deletions src/bcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,23 @@ function getCommonStructs() {
};
}

function getVersionControlStructs() {
const VecSet = bcs.struct('VecSet', {
contents: bcs.vector(bcs.string()),
});

const VersionControl = bcs.struct('VersionControl', {
allowed_functions: bcs.vector(VecSet),
});

return {
VersionControl,
};
}

function getGatewayStructs() {
const { Bytes32, Bag } = getCommonStructs();
const { Bytes32, Bag, Table } = getCommonStructs();
const { VersionControl } = getVersionControlStructs();

const Message = bcs.struct('Message', {
source_chain: bcs.String,
Expand Down Expand Up @@ -124,6 +139,28 @@ function getGatewayStructs() {
payload: bcs.vector(bcs.U8),
});

const AxelarSigners = bcs.struct('AxelarSigners', {
epoch: bcs.U64,
epoch_by_signers_hash: Table,
domain_separator: Bytes32,
minimum_rotation_delay: bcs.U64,
last_rotation_timestamp: bcs.U64,
previous_signers_retention: bcs.U64,
});

const GatewayV0 = bcs.struct('Gateway_v0', {
operator: bcs.Address,
messages: Table,
signers: AxelarSigners,
version_control: VersionControl,
});

const Gateway = bcs.struct('Gateway', {
id: UID,
name: bcs.U64,
value: GatewayV0,
});

return {
Bytes32,
Message,
Expand All @@ -140,11 +177,13 @@ function getGatewayStructs() {
Operators,
ExecuteData,
ApprovedMessage,
Gateway,
};
}

function getSquidStructs() {
const { Channel, CoinBag } = getCommonStructs();
const { VersionControl } = getVersionControlStructs();

const DeepbookV2SwapData = bcs.struct('DeepbookV2SwapData', {
swap_type: bcs.U8,
Expand Down Expand Up @@ -172,10 +211,16 @@ function getSquidStructs() {
metadata: bcs.vector(bcs.U8),
});

const Squid = bcs.struct('Squid', {
id: UID,
const SquidV0 = bcs.struct('Squid_v0', {
channel: Channel,
coin_bag: CoinBag,
version_control: VersionControl,
});

const Squid = bcs.struct('Squid', {
id: UID,
name: bcs.U64,
value: SquidV0,
});

return {
Expand All @@ -186,8 +231,29 @@ function getSquidStructs() {
};
}

function getRelayerDiscoveryStructs() {
const { Table } = getCommonStructs();
const { VersionControl } = getVersionControlStructs();

const RelayerDiscoveryV0 = bcs.struct('RelayerDiscovery_v0', {
configurations: Table,
version_control: VersionControl,
});

const RelayerDiscovery = bcs.struct('RelayerDiscovery', {
id: UID,
name: bcs.U64,
value: RelayerDiscoveryV0,
});

return {
RelayerDiscovery,
};
}

function getITSStructs() {
const { Table, Bag, Channel } = getCommonStructs();
const { VersionControl } = getVersionControlStructs();

const InterchainAddressTracker = bcs.struct('InterchainAddressTracker', {
trusted_addresses: Table,
Expand All @@ -198,14 +264,21 @@ function getITSStructs() {
trusted_addresses: bcs.vector(bcs.string()),
});

const ITS = bcs.struct('ITS', {
id: UID,
const ITSv0 = bcs.struct('ITS_v0', {
channel: Channel,
address_tracker: InterchainAddressTracker,
unregistered_coin_types: Table,
unregistered_coin_info: Bag,
unregistered_coins: Bag,
registered_coin_types: Table,
registered_coins: Bag,
relayer_discovery_id: bcs.Address,
version_control: VersionControl,
});

const ITS = bcs.struct('ITS', {
id: UID,
name: bcs.u64(),
value: ITSv0,
});

return {
Expand All @@ -229,9 +302,17 @@ function getGMPStructs() {
}

function getGasServiceStructs() {
const { VersionControl } = getVersionControlStructs();

const GasServiceV0 = bcs.struct('GasService_v0', {
balance: bcs.U64,
version_control: VersionControl,
});

const GasService = bcs.struct('GasService', {
id: UID,
balance: bcs.U64,
name: bcs.U64,
value: GasServiceV0,
});

return {
Expand All @@ -244,6 +325,8 @@ export const bcsStructs = {
gateway: getGatewayStructs(),
squid: getSquidStructs(),
gmp: getGMPStructs(),
versionControl: getVersionControlStructs(),
gasService: getGasServiceStructs(),
its: getITSStructs(),
relayerDiscovery: getRelayerDiscoveryStructs(),
};
Loading