Skip to content

Commit

Permalink
fix: BridgeContract: get_bridge_transfer_details correct struct formats
Browse files Browse the repository at this point in the history
  • Loading branch information
andygolay committed Oct 14, 2024
1 parent c368bb7 commit 3d99f27
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 231 deletions.
270 changes: 135 additions & 135 deletions Cargo.lock

Large diffs are not rendered by default.

64 changes: 32 additions & 32 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,40 +114,40 @@ serde_yaml = "0.9.34"
## Aptos dependencies
### We use a forked version so that we can override dependency versions. This is required
### to be avoid dependency conflicts with other Sovereign Labs crates.
aptos-api = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-api-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-bitvec = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-block-executor = { git = "https://github.com/movementlabsxyz/aptos-core.git", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-cached-packages = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-config = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-consensus-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-crypto = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af", features = [
aptos-api = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-api-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-bitvec = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-block-executor = { git = "https://github.com/movementlabsxyz/aptos-core.git", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-cached-packages = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-config = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-consensus-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-crypto = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48", features = [
"cloneable-private-keys",
] }
aptos-db = { git = "https://github.com/movementlabsxyz/aptos-core.git", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-executor = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-executor-test-helpers = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-executor-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-faucet-core = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-framework = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-language-e2e-tests = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-mempool = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-proptest-helpers = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-sdk = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-state-view = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-storage-interface = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-temppath = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-vm = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-vm-genesis = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-vm-logging = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-vm-validator = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-logger = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-vm-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-indexer = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-indexer-grpc-fullnode = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-indexer-grpc-table-info = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-protos = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "620db441bd6e5900ca4f2317170ced52571a90af" }
aptos-db = { git = "https://github.com/movementlabsxyz/aptos-core.git", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-executor = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-executor-test-helpers = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-executor-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-faucet-core = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-framework = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-language-e2e-tests = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-mempool = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-proptest-helpers = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-sdk = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-state-view = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-storage-interface = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-temppath = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-vm = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-vm-genesis = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-vm-logging = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-vm-validator = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-logger = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-vm-types = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-indexer = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-indexer-grpc-fullnode = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-indexer-grpc-table-info = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }
aptos-protos = { git = "https://github.com/movementlabsxyz/aptos-core", rev = "1284b8c662100fcf7e39bc271ff59afdd1e73f48" }

# Indexer
processor = { git = "https://github.com/movementlabsxyz/aptos-indexer-processors", rev = "8e83cde3cb75fabdade9485e0af680cc4b73ca8e", subdir = "rust" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ async fn test_movement_client_should_successfully_call_lock_and_complete(
assert_eq!(details.bridge_transfer_id.0, args.bridge_transfer_id.0);
assert_eq!(details.hash_lock.0, args.hash_lock.0);
assert_eq!(
&details.initiator_address.0 .0[32 - args.initiator.len()..],
&details.initiator_address.0,
&args.initiator,
"Initiator address does not match"
);
assert_eq!(details.recipient_address.0, args.recipient.0.to_vec());
assert_eq!(details.recipient_address.0, args.recipient);
assert_eq!(details.amount.0, AssetType::Moveth(args.amount));
assert_eq!(details.state, 1, "Bridge transfer is supposed to be locked but it's not.");

Expand All @@ -145,11 +145,11 @@ async fn test_movement_client_should_successfully_call_lock_and_complete(
assert_eq!(details.bridge_transfer_id.0, args.bridge_transfer_id.0);
assert_eq!(details.hash_lock.0, args.hash_lock.0);
assert_eq!(
&details.initiator_address.0 .0[32 - args.initiator.len()..],
&details.initiator_address.0,
&args.initiator,
"Initiator address does not match"
);
assert_eq!(details.recipient_address.0, args.recipient.0.to_vec());
assert_eq!(details.recipient_address.0, args.recipient);
assert_eq!(details.amount.0, AssetType::Moveth(args.amount));
assert_eq!(details.state, 2, "Bridge transfer is supposed to be completed but it's not.");

Expand Down Expand Up @@ -224,11 +224,11 @@ async fn test_movement_client_should_successfully_call_lock_and_abort() -> Resul
assert_eq!(details.bridge_transfer_id.0, args.bridge_transfer_id.0);
assert_eq!(details.hash_lock.0, args.hash_lock.0);
assert_eq!(
&details.initiator_address.0 .0[32 - args.initiator.len()..],
&details.initiator_address.0,
&args.initiator,
"Initiator address does not match"
);
assert_eq!(details.recipient_address.0, args.recipient.0.to_vec());
assert_eq!(details.recipient_address.0, args.recipient);
assert_eq!(details.amount.0, AssetType::Moveth(args.amount));
assert_eq!(details.state, 1, "Bridge transfer is supposed to be locked but it's not.");

Expand All @@ -251,11 +251,11 @@ async fn test_movement_client_should_successfully_call_lock_and_abort() -> Resul
assert_eq!(abort_details.bridge_transfer_id.0, args.bridge_transfer_id.0);
assert_eq!(abort_details.hash_lock.0, args.hash_lock.0);
assert_eq!(
&abort_details.initiator_address.0 .0[32 - args.initiator.len()..],
&abort_details.initiator_address.0,
&args.initiator,
"Initiator address does not match"
);
assert_eq!(abort_details.recipient_address.0, args.recipient.0.to_vec());
assert_eq!(abort_details.recipient_address.0, args.recipient);
assert_eq!(abort_details.amount.0, AssetType::Moveth(args.amount));

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ async fn test_movement_client_complete_transfer(
assert_eq!(details.bridge_transfer_id.0, args.bridge_transfer_id.0);
assert_eq!(details.hash_lock.0, args.hash_lock.0);
assert_eq!(
&details.initiator_address.0 .0[32 - args.initiator.len()..],
&details.initiator_address.0,
&args.initiator,
"Initiator address does not match"
);
assert_eq!(details.recipient_address.0, args.recipient.0.to_vec());
assert_eq!(details.recipient_address.0, args.recipient);
assert_eq!(details.amount.0, AssetType::Moveth(args.amount));
assert_eq!(details.state, 2, "Bridge transfer is supposed to be completed but it's not.");

Expand Down Expand Up @@ -243,11 +243,11 @@ async fn test_movement_client_should_abort_transfer() -> Result<(), anyhow::Erro
assert_eq!(abort_details.bridge_transfer_id.0, args.bridge_transfer_id.0);
assert_eq!(abort_details.hash_lock.0, args.hash_lock.0);
assert_eq!(
&abort_details.initiator_address.0 .0[32 - args.initiator.len()..],
&abort_details.initiator_address.0,
&args.initiator,
"Initiator address does not match"
);
assert_eq!(abort_details.recipient_address.0, args.recipient.0.to_vec());
assert_eq!(abort_details.recipient_address.0, args.recipient);
assert_eq!(abort_details.amount.0, AssetType::Moveth(args.amount));

Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod client_l2move_l1move;

use anyhow::Result;
use bridge_config::Config;
use bridge_integration_tests::utils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub trait BridgeContract<A>: Clone + Unpin + Send + Sync {
async fn get_bridge_transfer_details_counterparty(
&mut self,
bridge_transfer_id: BridgeTransferId,
) -> BridgeContractResult<Option<BridgeTransferDetails<A>>>;
) -> BridgeContractResult<Option<LockDetails<A>>>;

async fn lock_bridge_transfer(
&mut self,
Expand Down
23 changes: 16 additions & 7 deletions protocol-units/bridge/service/src/chains/ethereum/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use super::utils::{calculate_storage_slot, send_transaction, send_transaction_ru
use crate::chains::bridge_contracts::BridgeContractError;
use crate::chains::bridge_contracts::BridgeContractResult;
use crate::types::{
Amount, AssetType, BridgeAddress, BridgeTransferDetails, BridgeTransferId, HashLock,
HashLockPreImage, TimeLock,
Amount, AssetType, BridgeAddress, BridgeTransferDetails, BridgeTransferId, HashLock, HashLockPreImage, LockDetails, TimeLock
};
use alloy::primitives::{Address, FixedBytes, U256};
use alloy::providers::{Provider, ProviderBuilder};
Expand Down Expand Up @@ -68,6 +67,16 @@ struct EthBridgeTransferDetails {
pub state: u8,
}

#[derive(RlpDecodable, RlpEncodable)]
struct EthBridgeTransferDetailsCounterparty {
pub amount: U256,
pub originator:[u8; 32],
pub recipient: EthAddress,
pub hash_lock: [u8; 32],
pub time_lock: U256,
pub state: u8,
}

// We need to be able to build the client and deploy the contracts
// therfore the `initiator_contract` and `counterparty_contract`
// should be optional, as their values will be unknown at the time of building the client.
Expand Down Expand Up @@ -414,7 +423,7 @@ impl crate::chains::bridge_contracts::BridgeContract<EthAddress> for EthClient {
async fn get_bridge_transfer_details_counterparty(
&mut self,
bridge_transfer_id: BridgeTransferId,
) -> BridgeContractResult<Option<BridgeTransferDetails<EthAddress>>> {
) -> BridgeContractResult<Option<LockDetails<EthAddress>>> {
let generic_error = |desc| BridgeContractError::GenericError(String::from(desc));

let mapping_slot = U256::from(0); // the mapping is the zeroth slot in the contract
Expand All @@ -429,13 +438,13 @@ impl crate::chains::bridge_contracts::BridgeContract<EthAddress> for EthClient {

println!("storage_bytes: {:?}", storage_bytes);
let mut storage_slice = &storage_bytes[..];
let eth_details = EthBridgeTransferDetails::decode(&mut storage_slice)
let eth_details = EthBridgeTransferDetailsCounterparty::decode(&mut storage_slice)
.map_err(|_| generic_error("could not decode storage"))?;

Ok(Some(BridgeTransferDetails {
Ok(Some(LockDetails {
bridge_transfer_id,
initiator_address: BridgeAddress(eth_details.originator),
recipient_address: BridgeAddress(eth_details.recipient.to_vec()),
initiator_address: BridgeAddress(eth_details.originator.to_vec()),
recipient_address: BridgeAddress(eth_details.recipient),
hash_lock: HashLock(eth_details.hash_lock),
//@TODO unit test these wrapping to check for any nasty side effects.
time_lock: TimeLock(eth_details.time_lock.wrapping_to::<u64>()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl EthMonitoring {
amount: trlocked.amount.into(),
hash_lock: HashLock(*trlocked.hashLock),
time_lock: trlocked.timeLock.into(),
state: 8
};
BridgeContractEvent::Locked(details)
}).map_err(|err| BridgeContractError::OnChainError(err.to_string()));
Expand Down
18 changes: 9 additions & 9 deletions protocol-units/bridge/service/src/chains/movement/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::utils::{self, MovementAddress};
use crate::chains::bridge_contracts::BridgeContract;
use crate::chains::bridge_contracts::BridgeContractError;
use crate::chains::bridge_contracts::BridgeContractResult;
use crate::types::LockDetails;
use crate::types::{
Amount, AssetType, BridgeAddress, BridgeTransferDetails, BridgeTransferId, HashLock,
HashLockPreImage, TimeLock,
Expand Down Expand Up @@ -397,7 +398,7 @@ impl BridgeContract<MovementAddress> for MovementClient {
async fn get_bridge_transfer_details_counterparty(
&mut self,
bridge_transfer_id: BridgeTransferId,
) -> BridgeContractResult<Option<BridgeTransferDetails<MovementAddress>>> {
) -> BridgeContractResult<Option<LockDetails<MovementAddress>>> {
let bridge_transfer_id_hex = format!("0x{}", hex::encode(bridge_transfer_id.0));

let view_request = ViewRequest {
Expand Down Expand Up @@ -442,20 +443,19 @@ impl BridgeContract<MovementAddress> for MovementClient {
.parse::<u64>()
.map_err(|_| BridgeContractError::SerializationError)?;
let state = utils::val_as_u64_initiator(values.get(5))? as u8;

let originator_address = AccountAddress::from_hex_literal(originator)
let originator_address_bytes =
hex::decode(&originator[2..]).map_err(|_| BridgeContractError::SerializationError)?;
let recipient_address = AccountAddress::from_hex_literal(recipient)
.map_err(|_| BridgeContractError::SerializationError)?;
let recipient_address_bytes =
hex::decode(&recipient[2..]).map_err(|_| BridgeContractError::SerializationError)?;
let hash_lock_array: [u8; 32] = hex::decode(&hash_lock[2..])
let hash_lock_array: [u8; 32] = hex::decode(&hash_lock[2..])
.map_err(|_| BridgeContractError::SerializationError)?
.try_into()
.map_err(|_| BridgeContractError::SerializationError)?;

let details = BridgeTransferDetails {
let details = LockDetails {
bridge_transfer_id,
initiator_address: BridgeAddress(MovementAddress(originator_address)),
recipient_address: BridgeAddress(recipient_address_bytes),
initiator_address: BridgeAddress(originator_address_bytes),
recipient_address: BridgeAddress(MovementAddress(recipient_address)),
amount: Amount(AssetType::Moveth(amount)),
hash_lock: HashLock(hash_lock_array),
time_lock: TimeLock(time_lock),
Expand Down
Loading

0 comments on commit 3d99f27

Please sign in to comment.