Skip to content

Commit

Permalink
[Sui native bridge] - Add update_node_url function to bridge (#18091)
Browse files Browse the repository at this point in the history
## Description 

as titled

## Test plan 

How did you test the new or updated feature?

Move unit tests

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
  • Loading branch information
patrickkuo authored Jun 10, 2024
1 parent 3b99082 commit fc4712d
Show file tree
Hide file tree
Showing 16 changed files with 1,243 additions and 222 deletions.
25 changes: 25 additions & 0 deletions crates/sui-framework/docs/bridge/bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ title: Module `0xb::bridge`
- [Function `create`](#0xb_bridge_create)
- [Function `init_bridge_committee`](#0xb_bridge_init_bridge_committee)
- [Function `committee_registration`](#0xb_bridge_committee_registration)
- [Function `update_node_url`](#0xb_bridge_update_node_url)
- [Function `register_foreign_token`](#0xb_bridge_register_foreign_token)
- [Function `send_token`](#0xb_bridge_send_token)
- [Function `approve_token_transfer`](#0xb_bridge_approve_token_transfer)
Expand Down Expand Up @@ -780,6 +781,30 @@ title: Module `0xb::bridge`



</details>

<a name="0xb_bridge_update_node_url"></a>

## Function `update_node_url`



<pre><code><b>public</b> <b>fun</b> <a href="bridge.md#0xb_bridge_update_node_url">update_node_url</a>(<a href="bridge.md#0xb_bridge">bridge</a>: &<b>mut</b> <a href="bridge.md#0xb_bridge_Bridge">bridge::Bridge</a>, new_url: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;, ctx: &<a href="../sui-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="bridge.md#0xb_bridge_update_node_url">update_node_url</a>(<a href="bridge.md#0xb_bridge">bridge</a>: &<b>mut</b> <a href="bridge.md#0xb_bridge_Bridge">Bridge</a>, new_url: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;, ctx: &TxContext) {
<a href="bridge.md#0xb_bridge_load_inner_mut">load_inner_mut</a>(<a href="bridge.md#0xb_bridge">bridge</a>).<a href="committee.md#0xb_committee">committee</a>.<a href="bridge.md#0xb_bridge_update_node_url">update_node_url</a>(new_url, ctx);
}
</code></pre>



</details>

<a name="0xb_bridge_register_foreign_token"></a>
Expand Down
43 changes: 43 additions & 0 deletions crates/sui-framework/docs/bridge/committee.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ title: Module `0xb::committee`
- [Function `try_create_next_committee`](#0xb_committee_try_create_next_committee)
- [Function `execute_blocklist`](#0xb_committee_execute_blocklist)
- [Function `committee_members`](#0xb_committee_committee_members)
- [Function `update_node_url`](#0xb_committee_update_node_url)
- [Function `check_uniqueness_bridge_keys`](#0xb_committee_check_uniqueness_bridge_keys)


Expand Down Expand Up @@ -298,6 +299,15 @@ title: Module `0xb::committee`



<a name="0xb_committee_ESenderIsNotInBridgeCommittee"></a>



<pre><code><b>const</b> <a href="committee.md#0xb_committee_ESenderIsNotInBridgeCommittee">ESenderIsNotInBridgeCommittee</a>: u64 = 9;
</code></pre>



<a name="0xb_committee_ESenderNotActiveValidator"></a>


Expand Down Expand Up @@ -630,6 +640,39 @@ title: Module `0xb::committee`



</details>

<a name="0xb_committee_update_node_url"></a>

## Function `update_node_url`



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="committee.md#0xb_committee_update_node_url">update_node_url</a>(self: &<b>mut</b> <a href="committee.md#0xb_committee_BridgeCommittee">committee::BridgeCommittee</a>, new_url: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;, ctx: &<a href="../sui-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<a href="../sui-framework/package.md#0x2_package">package</a>) <b>fun</b> <a href="committee.md#0xb_committee_update_node_url">update_node_url</a>(self: &<b>mut</b> <a href="committee.md#0xb_committee_BridgeCommittee">BridgeCommittee</a>, new_url: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;, ctx: &TxContext) {
<b>let</b> <b>mut</b> idx = 0;
<b>while</b> (idx &lt; self.members.size()) {
<b>let</b> (_, member) = self.members.get_entry_by_idx_mut(idx);
<b>if</b> (member.sui_address == ctx.sender()) {
member.http_rest_url = new_url;
<b>return</b>
};
idx = idx + 1;
};
<b>abort</b> <a href="committee.md#0xb_committee_ESenderIsNotInBridgeCommittee">ESenderIsNotInBridgeCommittee</a>
}
</code></pre>



</details>

<a name="0xb_committee_check_uniqueness_bridge_keys"></a>
Expand Down
6 changes: 5 additions & 1 deletion crates/sui-framework/packages/bridge/sources/bridge.move
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ module bridge::bridge {
.register(system_state, bridge_pubkey_bytes, http_rest_url, ctx);
}

public fun update_node_url(bridge: &mut Bridge, new_url: vector<u8>, ctx: &TxContext) {
load_inner_mut(bridge).committee.update_node_url(new_url, ctx);
}

public fun register_foreign_token<T>(
bridge: &mut Bridge,
tc: TreasuryCap<T>,
Expand Down Expand Up @@ -714,7 +718,7 @@ module bridge::bridge {
): u8 {
bridge.get_token_transfer_action_status(source_chain, bridge_seq_num)
}

#[test_only]
public fun test_get_token_transfer_action_signatures(
bridge: &mut Bridge,
Expand Down
20 changes: 19 additions & 1 deletion crates/sui-framework/packages/bridge/sources/committee.move
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module bridge::committee {
use sui::event::emit;
use sui::vec_map::{Self, VecMap};
use sui::vec_set;
use sui_system::sui_system;
use sui_system::sui_system::SuiSystemState;

use bridge::crypto;
Expand All @@ -22,6 +21,7 @@ module bridge::committee {
const EInvalidPubkeyLength: u64 = 6;
const ECommitteeAlreadyInitiated: u64 = 7;
const EDuplicatePubkey: u64 = 8;
const ESenderIsNotInBridgeCommittee: u64 = 9;

const SUI_MESSAGE_PREFIX: vector<u8> = b"SUI_BRIDGE_MESSAGE";

Expand Down Expand Up @@ -268,6 +268,19 @@ module bridge::committee {
&self.members
}

public(package) fun update_node_url(self: &mut BridgeCommittee, new_url: vector<u8>, ctx: &TxContext) {
let mut idx = 0;
while (idx < self.members.size()) {
let (_, member) = self.members.get_entry_by_idx_mut(idx);
if (member.sui_address == ctx.sender()) {
member.http_rest_url = new_url;
return
};
idx = idx + 1;
};
abort ESenderIsNotInBridgeCommittee
}

// Assert if `bridge_pubkey_bytes` is duplicated in `member_registrations`.
// Dupicate keys would cause `try_create_next_committee` to fail and,
// in consequence, an end of epoch transaction to fail (safe mode run).
Expand Down Expand Up @@ -300,6 +313,11 @@ module bridge::committee {
member.voting_power
}

#[test_only]
public(package) fun http_rest_url(member: &CommitteeMember): vector<u8> {
member.http_rest_url
}

#[test_only]
public(package) fun member_registrations(
self: &BridgeCommittee,
Expand Down
85 changes: 85 additions & 0 deletions crates/sui-framework/packages/bridge/tests/commitee_test.move
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,91 @@ module bridge::committee_test {
test_scenario::end(scenario);
}

#[test]
fun test_update_node_url() {
let mut scenario = test_scenario::begin(@0x0);
let ctx = test_scenario::ctx(&mut scenario);
let mut committee = create(ctx);

let validators = vector[
create_validator_for_testing(@0xA, 100, ctx),
];
create_sui_system_state_for_testing(validators, 0, 0, ctx);
advance_epoch_with_reward_amounts(0, 0, &mut scenario);
test_scenario::next_tx(&mut scenario, @0x0);

let mut system_state = test_scenario::take_shared<SuiSystemState>(&scenario);

// validator registration
committee.register(
&mut system_state,
hex::decode(VALIDATOR1_PUBKEY),
b"test url 1",
&tx(@0xA, 0),
);

let ctx = test_scenario::ctx(&mut scenario);
let voting_powers = system_state.validator_voting_powers_for_testing();
committee.try_create_next_committee(voting_powers, 6000, ctx);

let members = committee.members();
assert!(members.size() == 1);
let (_, member) = members.get_entry_by_idx(0);
assert_eq(member.http_rest_url(), b"test url 1");

// Update URL
committee.update_node_url(
b"test url 2",
&tx(@0xA, 0),
);

let members = committee.members();
let (_, member) = members.get_entry_by_idx(0);
assert_eq(member.http_rest_url(), b"test url 2");

test_utils::destroy(committee);
test_scenario::return_shared(system_state);
test_scenario::end(scenario);
}

#[test]
#[expected_failure(abort_code = bridge::committee::ESenderIsNotInBridgeCommittee)]
fun test_update_node_url_not_validator() {
let mut scenario = test_scenario::begin(@0x0);
let ctx = test_scenario::ctx(&mut scenario);
let mut committee = create(ctx);

let validators = vector[
create_validator_for_testing(@0xA, 100, ctx),
];
create_sui_system_state_for_testing(validators, 0, 0, ctx);
advance_epoch_with_reward_amounts(0, 0, &mut scenario);
test_scenario::next_tx(&mut scenario, @0x0);

let mut system_state = test_scenario::take_shared<SuiSystemState>(&scenario);

// validator registration
committee.register(
&mut system_state,
hex::decode(VALIDATOR1_PUBKEY),
b"test url 1",
&tx(@0xA, 0),
);

let ctx = test_scenario::ctx(&mut scenario);
let voting_powers = system_state.validator_voting_powers_for_testing();
committee.try_create_next_committee(voting_powers, 6000, ctx);

// Update URL should fail for validator @0xB
committee.update_node_url(
b"test url",
&tx(@0xB, 0),
);

// test should have failed, abort
abort 0
}

#[test]
#[expected_failure(abort_code = bridge::committee::ENotSystemAddress)]
fun test_init_non_system_sender() {
Expand Down
6 changes: 6 additions & 0 deletions crates/sui-framework/published_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3907,6 +3907,9 @@ execute_blocklist
committee_members
public(package) fun
0xb::committee
update_node_url
public(package) fun
0xb::committee
check_uniqueness_bridge_keys
fun
0xb::committee
Expand Down Expand Up @@ -3949,6 +3952,9 @@ init_bridge_committee
committee_registration
public fun
0xb::bridge
update_node_url
public fun
0xb::bridge
register_foreign_token
public fun
0xb::bridge
Expand Down
Loading

0 comments on commit fc4712d

Please sign in to comment.