Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/hide-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos committed Nov 25, 2024
2 parents 300209f + ada6fd9 commit deb46cb
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-buttons-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@axelar-network/axelar-cgp-sui': minor
---

Add a query for version on version_control, change CreatorCap to OwnerCap and allow owner to set allowed functions in gateway
79 changes: 53 additions & 26 deletions move/axelar_gateway/sources/gateway.move
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use axelar_gateway::bytes32::{Self, Bytes32};
use axelar_gateway::channel::{Channel, ApprovedMessage};
use axelar_gateway::gateway_v0::{Self, Gateway_v0};
use axelar_gateway::message_ticket::{Self, MessageTicket};
use axelar_gateway::owner_cap::{Self, OwnerCap};
use axelar_gateway::weighted_signers;
use std::ascii::{Self, String};
use sui::clock::Clock;
Expand All @@ -62,30 +63,21 @@ public struct Gateway has key {
inner: Versioned,
}

// ------------
// Capabilities
// ------------
public struct CreatorCap has key, store {
id: UID,
}

// -----
// Setup
// -----

/// Init the module by giving a CreatorCap to the sender to allow a full
/// Init the module by giving a OwnerCap to the sender to allow a full
/// `setup`.
fun init(ctx: &mut TxContext) {
let cap = CreatorCap {
id: object::new(ctx),
};
let cap = owner_cap::create(ctx);

transfer::transfer(cap, ctx.sender());
transfer::public_transfer(cap, ctx.sender());
}

/// Setup the module by creating a new Gateway object.
entry fun setup(
cap: CreatorCap,
_: &OwnerCap,
operator: address,
domain_separator: address,
minimum_rotation_delay: u64,
Expand All @@ -94,9 +86,6 @@ entry fun setup(
clock: &Clock,
ctx: &mut TxContext,
) {
let CreatorCap { id } = cap;
id.delete();

let inner = versioned::create(
VERSION,
gateway_v0::new(
Expand Down Expand Up @@ -185,6 +174,14 @@ entry fun rotate_signers(
)
}

entry fun allow_function(self: &mut Gateway, _: &OwnerCap, version: u64, function_name: String) {
self.value_mut!(b"allow_function").allow_function(version, function_name);
}

entry fun disallow_function(self: &mut Gateway, _: &OwnerCap, version: u64, function_name: String) {
self.value_mut!(b"disallow_function").disallow_function(version, function_name);
}

// ----------------
// Public Functions
// ----------------
Expand Down Expand Up @@ -274,6 +271,8 @@ fun version_control(): VersionControl {
b"is_message_executed",
b"take_approved_message",
b"send_message",
b"allow_function",
b"disallow_function",
].map!(|function_name| function_name.to_ascii_string()),
])
}
Expand Down Expand Up @@ -338,6 +337,8 @@ fun dummy(ctx: &mut TxContext): Gateway {
b"is_message_executed",
b"take_approved_message",
b"send_message",
b"allow_function",
b"disallow_function",
b"",
].map!(|function_name| function_name.to_ascii_string()),
]),
Expand Down Expand Up @@ -376,7 +377,7 @@ fun test_init() {
init(ts.ctx());
ts.next_tx(@0x0);

let creator_cap = ts.take_from_sender<CreatorCap>();
let creator_cap = ts.take_from_sender<OwnerCap>();
ts.return_to_sender(creator_cap);
ts.end();
}
Expand All @@ -394,14 +395,12 @@ fun test_setup() {
let timestamp = rng.generate_u64();
clock.increment_for_testing(timestamp);

let creator_cap = CreatorCap {
id: object::new(ctx),
};
let owner_cap = owner_cap::create(ctx);

let mut scenario = sui::test_scenario::begin(@0x1);

setup(
creator_cap,
&owner_cap,
operator,
domain_separator,
minimum_rotation_delay,
Expand Down Expand Up @@ -451,6 +450,7 @@ fun test_setup() {
assert!(previous_signers_retention == previous_signers_retention_result);

clock.destroy_for_testing();
owner_cap.destroy_for_testing();
scenario.end();
}

Expand All @@ -468,15 +468,13 @@ fun test_setup_remaining_bytes() {
let timestamp = rng.generate_u64();
clock.increment_for_testing(timestamp);

let creator_cap = CreatorCap {
id: object::new(ctx),
};
let owner_cap = owner_cap::create(ctx);

let mut scenario = sui::test_scenario::begin(@0x1);
let mut initial_signers_bytes = bcs::to_bytes(&initial_signers);
initial_signers_bytes.push_back(0);
setup(
creator_cap,
&owner_cap,
operator,
domain_separator,
minimum_rotation_delay,
Expand Down Expand Up @@ -526,6 +524,7 @@ fun test_setup_remaining_bytes() {
assert!(previous_signers_retention == previous_signers_retention_result);

clock.destroy_for_testing();
owner_cap.destroy_for_testing();
scenario.end();
}

Expand Down Expand Up @@ -1052,9 +1051,37 @@ fun test_send_message() {

let gateway = dummy(ctx);
gateway.send_message(message_ticket);

utils::assert_event<events::ContractCall>();

sui::test_utils::destroy(gateway);
channel.destroy();
}

#[test]
fun test_allow_function() {
let ctx = &mut sui::tx_context::dummy();
let mut self = dummy(ctx);
let owner_cap = owner_cap::create(ctx);
let version = 0;
let function_name = b"function_name".to_ascii_string();

self.allow_function(&owner_cap, version, function_name);

sui::test_utils::destroy(self);
owner_cap.destroy_for_testing();
}

#[test]
fun test_disallow_function() {
let ctx = &mut sui::tx_context::dummy();
let mut self = dummy(ctx);
let owner_cap = owner_cap::create(ctx);
let version = 0;
let function_name = b"approve_messages".to_ascii_string();

self.disallow_function(&owner_cap, version, function_name);

sui::test_utils::destroy(self);
owner_cap.destroy_for_testing();
}
23 changes: 23 additions & 0 deletions move/axelar_gateway/sources/types/owner_cap.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module axelar_gateway::owner_cap;

// -----
// Types
// -----
public struct OwnerCap has key, store {
id: UID,
}

public(package) fun create(ctx: &mut TxContext): OwnerCap {
OwnerCap {
id: object::new(ctx),
}
}

/// ---------
/// Test Only
/// ---------
#[test_only]
public(package) fun destroy_for_testing(self: OwnerCap) {
let OwnerCap { id } = self;
id.delete();
}
10 changes: 9 additions & 1 deletion move/axelar_gateway/sources/versioned/gateway_v0.move
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public enum CommandType {
// -----------------
// Package Functions
// -----------------
/// Init the module by giving a CreatorCap to the sender to allow a full
/// Init the module by giving a OwnerCap to the sender to allow a full
/// `setup`.
public(package) fun new(
operator: address,
Expand Down Expand Up @@ -221,6 +221,14 @@ public(package) fun send_message(
);
}

public(package) fun allow_function(self: &mut Gateway_v0, version: u64, function_name: String) {
self.version_control.allow_function(version, function_name);
}

public(package) fun disallow_function(self: &mut Gateway_v0, version: u64, function_name: String) {
self.version_control.disallow_function(version, function_name);
}

// -----------------
// Private Functions
// -----------------
Expand Down
Loading

0 comments on commit deb46cb

Please sign in to comment.