Skip to content

Commit

Permalink
[bridge 65/n] change tokenId from enum to u8, to prepare for the add …
Browse files Browse the repository at this point in the history
…token flow (#16939)

## Description 

To unblock token flow, in this PR we change the usage of TokenID enum to
u8.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and do not break anything, you can
skip the following section. Otherwise, please briefly describe what has
changed under the Release Notes section.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
longbowlu authored and patrickkuo committed May 9, 2024
1 parent c020c89 commit 72e2a73
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 133 deletions.
10 changes: 5 additions & 5 deletions crates/sui-bridge/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use ethers::{
};
use serde::{Deserialize, Serialize};
use sui_types::base_types::SuiAddress;
use sui_types::bridge::{BridgeChainId, TokenId};
use sui_types::bridge::BridgeChainId;

// TODO: write a macro to handle variants

Expand Down Expand Up @@ -139,7 +139,7 @@ pub struct EthToSuiTokenBridgeV1 {
pub eth_chain_id: BridgeChainId,
pub sui_address: SuiAddress,
pub eth_address: EthAddress,
pub token_id: TokenId,
pub token_id: u8,
pub sui_adjusted_amount: u64,
}

Expand All @@ -152,7 +152,7 @@ impl TryFrom<&TokensDepositedFilter> for EthToSuiTokenBridgeV1 {
eth_chain_id: BridgeChainId::try_from(event.source_chain_id)?,
sui_address: SuiAddress::from_bytes(event.recipient_address.as_ref())?,
eth_address: event.sender_address,
token_id: TokenId::try_from(event.token_id)?,
token_id: event.token_id,
sui_adjusted_amount: event.sui_adjusted_amount,
})
}
Expand Down Expand Up @@ -232,7 +232,7 @@ mod tests {
types::{BlocklistType, EmergencyActionType},
};
use fastcrypto::encoding::{Encoding, Hex};
use sui_types::crypto::ToFromBytes;
use sui_types::{bridge::TOKEN_ID_ETH, crypto::ToFromBytes};

#[test]
fn test_eth_message_conversion_emergency_action_regression() -> anyhow::Result<()> {
Expand Down Expand Up @@ -316,7 +316,7 @@ mod tests {
let action = AssetPriceUpdateAction {
nonce: 2,
chain_id: BridgeChainId::EthSepolia,
token_id: TokenId::ETH,
token_id: TOKEN_ID_ETH,
new_usd_price: 80000000,
};
let message: eth_bridge_limiter::Message = action.into();
Expand Down
10 changes: 5 additions & 5 deletions crates/sui-bridge/src/client/bridge_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl BridgeClient {
BridgeAction::AssetPriceUpdateAction(a) => {
let chain_id = (a.chain_id as u8).to_string();
let nonce = a.nonce.to_string();
let token_id = (a.token_id as u8).to_string();
let token_id = a.token_id.to_string();
let new_usd_price = a.new_usd_price.to_string();
format!("sign/update_asset_price/{chain_id}/{nonce}/{token_id}/{new_usd_price}")
}
Expand Down Expand Up @@ -182,7 +182,7 @@ mod tests {
use fastcrypto::hash::{HashFunction, Keccak256};
use fastcrypto::traits::KeyPair;
use prometheus::Registry;
use sui_types::bridge::{BridgeChainId, TokenId};
use sui_types::bridge::{BridgeChainId, TOKEN_ID_BTC, TOKEN_ID_USDT};
use sui_types::{base_types::SuiAddress, crypto::get_key_pair, digests::TransactionDigest};

#[tokio::test]
Expand Down Expand Up @@ -378,7 +378,7 @@ mod tests {
sui_address: SuiAddress::random_for_testing_only(),
eth_chain_id: BridgeChainId::EthSepolia,
eth_address: EthAddress::random(),
token_id: TokenId::USDT,
token_id: TOKEN_ID_USDT,
amount_sui_adjusted: 1,
},
});
Expand All @@ -401,7 +401,7 @@ mod tests {
eth_address: EthAddress::random(),
sui_chain_id: BridgeChainId::SuiDevnet,
sui_address: SuiAddress::random_for_testing_only(),
token_id: TokenId::USDT,
token_id: TOKEN_ID_USDT,
sui_adjusted_amount: 1,
},
});
Expand Down Expand Up @@ -473,7 +473,7 @@ mod tests {
let action = BridgeAction::AssetPriceUpdateAction(crate::types::AssetPriceUpdateAction {
chain_id: BridgeChainId::SuiDevnet,
nonce: 8,
token_id: TokenId::BTC,
token_id: TOKEN_ID_BTC,
new_usd_price: 100_000_000,
});
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-bridge/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ mod tests {
use std::str::FromStr;
use std::sync::Arc;
use sui_types::base_types::SuiAddress;
use sui_types::bridge::{BridgeChainId, TokenId};
use sui_types::bridge::{BridgeChainId, TOKEN_ID_ETH};
use sui_types::crypto::get_key_pair;
use sui_types::digests::TransactionDigest;

Expand Down Expand Up @@ -342,7 +342,7 @@ mod tests {
eth_chain_id: BridgeChainId::EthSepolia,
eth_address: EthAddress::from_str("0xb18f79Fe671db47393315fFDB377Da4Ea1B7AF96")
.unwrap(),
token_id: TokenId::ETH,
token_id: TOKEN_ID_ETH,
amount_sui_adjusted: 100000u64,
},
});
Expand Down
12 changes: 6 additions & 6 deletions crates/sui-bridge/src/e2e_tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sui_sdk::wallet_context::WalletContext;
use sui_sdk::SuiClient;
use sui_types::base_types::{ObjectRef, SuiAddress};
use sui_types::bridge::{
BridgeChainId, MoveTypeBridgeMessageKey, MoveTypeBridgeRecord, TokenId, BRIDGE_MODULE_NAME,
BridgeChainId, MoveTypeBridgeMessageKey, MoveTypeBridgeRecord, BRIDGE_MODULE_NAME, TOKEN_ID_ETH,
};
use sui_types::collection_types::LinkedTableNode;
use sui_types::committee::TOTAL_VOTING_POWER;
Expand Down Expand Up @@ -139,7 +139,7 @@ async fn test_bridge_from_eth_to_sui_to_eth() {
sui_chain_id,
amount,
sui_amount,
TokenId::ETH,
TOKEN_ID_ETH,
0,
)
.await;
Expand Down Expand Up @@ -529,7 +529,7 @@ async fn deposit_eth_to_sui_package(
BRIDGE_PACKAGE_ID,
BRIDGE_MODULE_NAME.to_owned(),
ident_str!("send_token").to_owned(),
vec![get_sui_token_type_tag(TokenId::ETH)],
vec![get_sui_token_type_tag(TOKEN_ID_ETH).unwrap()],
vec![arg_bridge, arg_target_chain, arg_target_address, arg_token],
);

Expand Down Expand Up @@ -587,7 +587,7 @@ async fn init_eth_to_sui_bridge(
sui_chain_id: u8,
amount: u64,
sui_amount: u64,
token_id: TokenId,
token_id: u8,
nonce: u64,
) {
let eth_tx = deposit_native_eth_to_sol_contract(
Expand Down Expand Up @@ -615,7 +615,7 @@ async fn init_eth_to_sui_bridge(
assert_eq!(eth_bridge_event.source_chain_id, eth_chain_id);
assert_eq!(eth_bridge_event.nonce, nonce);
assert_eq!(eth_bridge_event.destination_chain_id, sui_chain_id);
assert_eq!(eth_bridge_event.token_id, token_id as u8);
assert_eq!(eth_bridge_event.token_id, token_id);
assert_eq!(eth_bridge_event.sui_adjusted_amount, sui_amount);
assert_eq!(eth_bridge_event.sender_address, eth_address);
assert_eq!(eth_bridge_event.recipient_address, sui_address.to_vec());
Expand Down Expand Up @@ -670,7 +670,7 @@ async fn init_sui_to_eth_bridge(
);
assert_eq!(bridge_event.sui_bridge_event.sui_address, sui_address);
assert_eq!(bridge_event.sui_bridge_event.eth_address, eth_address);
assert_eq!(bridge_event.sui_bridge_event.token_id, TokenId::ETH);
assert_eq!(bridge_event.sui_bridge_event.token_id, TOKEN_ID_ETH);
assert_eq!(
bridge_event.sui_bridge_event.amount_sui_adjusted,
sui_amount
Expand Down
23 changes: 11 additions & 12 deletions crates/sui-bridge/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl BridgeMessageEncoding for SuiToEthBridgeAction {
bytes.extend_from_slice(e.eth_address.as_bytes());

// Add token id
bytes.push(e.token_id as u8);
bytes.push(e.token_id);

// Add token amount
bytes.extend_from_slice(&e.amount_sui_adjusted.to_be_bytes());
Expand Down Expand Up @@ -116,7 +116,7 @@ impl BridgeMessageEncoding for EthToSuiBridgeAction {
bytes.extend_from_slice(&e.sui_address.to_vec());

// Add token id
bytes.push(e.token_id as u8);
bytes.push(e.token_id);

// Add token amount
bytes.extend_from_slice(&e.sui_adjusted_amount.to_be_bytes());
Expand Down Expand Up @@ -239,7 +239,7 @@ impl BridgeMessageEncoding for AssetPriceUpdateAction {
fn as_payload_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
// Add token id
bytes.push(self.token_id as u8);
bytes.push(self.token_id);
// Add new usd limit
bytes.extend_from_slice(&self.new_usd_price.to_be_bytes());
bytes
Expand Down Expand Up @@ -302,11 +302,10 @@ mod tests {
use fastcrypto::traits::ToFromBytes;
use prometheus::Registry;
use std::str::FromStr;
use sui_types::base_types::{SuiAddress, TransactionDigest};
use sui_types::bridge::BridgeChainId;
use sui_types::{
base_types::{SuiAddress, TransactionDigest},
bridge::TokenId,
};
use sui_types::bridge::TOKEN_ID_BTC;
use sui_types::bridge::TOKEN_ID_USDC;

use super::*;

Expand All @@ -322,7 +321,7 @@ mod tests {
let eth_chain_id = BridgeChainId::EthSepolia;
let sui_address = SuiAddress::random_for_testing_only();
let eth_address = EthAddress::random();
let token_id = TokenId::USDC;
let token_id = TOKEN_ID_USDC;
let amount_sui_adjusted = 1_000_000;

let sui_bridge_event = EmittedSuiToEthTokenBridgeV1 {
Expand Down Expand Up @@ -355,7 +354,7 @@ mod tests {
let eth_address_length_bytes = vec![EthAddress::len_bytes() as u8]; // len: 1
let eth_address_bytes = eth_address.as_bytes().to_vec(); // len: 20

let token_id_bytes = vec![token_id as u8]; // len: 1
let token_id_bytes = vec![token_id]; // len: 1
let token_amount_bytes = amount_sui_adjusted.to_be_bytes().to_vec(); // len: 8

let mut combined_bytes = Vec::new();
Expand Down Expand Up @@ -401,7 +400,7 @@ mod tests {
.unwrap();
let eth_address =
EthAddress::from_str("0x00000000000000000000000000000000000000c8").unwrap();
let token_id = TokenId::USDC;
let token_id = TOKEN_ID_USDC;
let amount_sui_adjusted = 12345;

let sui_bridge_event = EmittedSuiToEthTokenBridgeV1 {
Expand Down Expand Up @@ -610,7 +609,7 @@ mod tests {
let action = BridgeAction::AssetPriceUpdateAction(AssetPriceUpdateAction {
nonce: 266,
chain_id: BridgeChainId::SuiLocalTest,
token_id: TokenId::BTC,
token_id: TOKEN_ID_BTC,
new_usd_price: 100_000 * USD_MULTIPLIER, // $100k USD
});
let bytes = action.to_bytes();
Expand Down Expand Up @@ -774,7 +773,7 @@ mod tests {
.unwrap();
let eth_address =
EthAddress::from_str("0x00000000000000000000000000000000000000c8").unwrap();
let token_id = TokenId::USDC;
let token_id = TOKEN_ID_USDC;
let sui_adjusted_amount = 12345;

let eth_bridge_event = EthToSuiTokenBridgeV1 {
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-bridge/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum BridgeError {
TransientProviderError(String),
// Ethereum provider error
ProviderError(String),
// TokenId is unknown
UnknownTokenId(u8),
// Invalid BridgeCommittee
InvalidBridgeCommittee(String),
// Invalid Bridge authority signature
Expand Down
19 changes: 7 additions & 12 deletions crates/sui-bridge/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use sui_json_rpc_types::SuiEvent;
use sui_types::base_types::SuiAddress;
use sui_types::bridge::{BridgeChainId, TokenId};
use sui_types::bridge::BridgeChainId;

use sui_types::digests::TransactionDigest;
use sui_types::BRIDGE_PACKAGE_ID;
Expand All @@ -47,7 +47,7 @@ pub struct EmittedSuiToEthTokenBridgeV1 {
pub eth_chain_id: BridgeChainId,
pub sui_address: SuiAddress,
pub eth_address: EthAddress,
pub token_id: TokenId,
pub token_id: u8,
// The amount of tokens deposited with decimal points on Sui side
pub amount_sui_adjusted: u64,
}
Expand All @@ -63,13 +63,7 @@ impl TryFrom<MoveTokenBridgeEvent> for EmittedSuiToEthTokenBridgeV1 {
event.message_type
)));
}
let token_id = TokenId::try_from(event.token_type).map_err(|_e| {
BridgeError::Generic(format!(
"Failed to convert MoveTokenBridgeEvent to EmittedSuiToEthTokenBridgeV1. Failed to convert token type {} to TokenId",
event.token_type,
))
})?;

let token_id = event.token_type;
let sui_chain_id = BridgeChainId::try_from(event.source_chain).map_err(|_e| {
BridgeError::Generic(format!(
"Failed to convert MoveTokenBridgeEvent to EmittedSuiToEthTokenBridgeV1. Failed to convert source chain {} to BridgeChainId",
Expand Down Expand Up @@ -204,7 +198,8 @@ pub mod tests {
use sui_json_rpc_types::SuiEvent;
use sui_types::base_types::ObjectID;
use sui_types::base_types::SuiAddress;
use sui_types::bridge::{BridgeChainId, TokenId};
use sui_types::bridge::BridgeChainId;
use sui_types::bridge::TOKEN_ID_SUI;
use sui_types::digests::TransactionDigest;
use sui_types::event::EventID;
use sui_types::Identifier;
Expand All @@ -217,7 +212,7 @@ pub mod tests {
sui_address: SuiAddress::random_for_testing_only(),
eth_chain_id: BridgeChainId::EthSepolia,
eth_address: EthAddress::random(),
token_id: TokenId::Sui,
token_id: TOKEN_ID_SUI,
amount_sui_adjusted: 100,
};
let emitted_event = MoveTokenBridgeEvent {
Expand All @@ -227,7 +222,7 @@ pub mod tests {
sender_address: sanitized_event.sui_address.to_vec(),
target_chain: sanitized_event.eth_chain_id as u8,
target_address: sanitized_event.eth_address.as_bytes().to_vec(),
token_type: sanitized_event.token_id as u8,
token_type: sanitized_event.token_id,
amount_sui_adjusted: sanitized_event.amount_sui_adjusted,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/sui-bridge/src/server/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ mod tests {
};
use ethers::types::{Address as EthAddress, TransactionReceipt};
use sui_json_rpc_types::SuiEvent;
use sui_types::bridge::{BridgeChainId, TokenId};
use sui_types::bridge::{BridgeChainId, TOKEN_ID_USDC};
use sui_types::{base_types::SuiAddress, crypto::get_key_pair};

#[tokio::test]
Expand Down Expand Up @@ -419,7 +419,7 @@ mod tests {
sender_address: SuiAddress::random_for_testing_only().to_vec(),
target_chain: BridgeChainId::EthLocalTest as u8,
target_address: EthAddress::random().as_bytes().to_vec(),
token_type: TokenId::USDC as u8,
token_type: TOKEN_ID_USDC,
amount_sui_adjusted: 12345,
};

Expand Down
9 changes: 4 additions & 5 deletions crates/sui-bridge/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use fastcrypto::{
};
use std::net::SocketAddr;
use std::sync::Arc;
use sui_types::bridge::{BridgeChainId, TokenId};
use sui_types::bridge::BridgeChainId;

pub mod governance_verifier;
pub mod handler;
Expand Down Expand Up @@ -200,9 +200,6 @@ async fn handle_asset_price_update_action(
let chain_id = BridgeChainId::try_from(chain_id).map_err(|err| {
BridgeError::InvalidBridgeClientRequest(format!("Invalid chain id: {:?}", err))
})?;
let token_id = TokenId::try_from(token_id).map_err(|err| {
BridgeError::InvalidBridgeClientRequest(format!("Invalid token id: {:?}", err))
})?;
let action = BridgeAction::AssetPriceUpdateAction(AssetPriceUpdateAction {
chain_id,
nonce,
Expand Down Expand Up @@ -265,6 +262,8 @@ async fn handle_evm_contract_upgrade(

#[cfg(test)]
mod tests {
use sui_types::bridge::TOKEN_ID_BTC;

use super::*;
use crate::client::bridge_client::BridgeClient;
use crate::server::mock_handler::BridgeRequestMockHandler;
Expand Down Expand Up @@ -321,7 +320,7 @@ mod tests {
let action = BridgeAction::AssetPriceUpdateAction(AssetPriceUpdateAction {
nonce: 266,
chain_id: BridgeChainId::SuiLocalTest,
token_id: TokenId::BTC,
token_id: TOKEN_ID_BTC,
new_usd_price: 100_000_0000, // $100k USD
});
client.request_sign_bridge_action(action).await.unwrap();
Expand Down
Loading

0 comments on commit 72e2a73

Please sign in to comment.