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(int-on-evm): pass through originating tx info in that #18

Merged
merged 14 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion common/chain_ids/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ license = "MIT"
publish = false
edition = "2021"
name = "chain_ids"
version = "1.1.0"
version = "1.2.0"
readme = "README.md"
rust-version = "1.56"
keywords = ["provable", "defi", "crypto"]
authors = ["Greg Kapka <[email protected]>"]

[features]
# NOTE: This feature was required for v3 dao messages to be passed accross chain correctly, which message
# require a higher gas limit stipend. This higher limit may eventually be present by default, but for now
# its feature gated
include-origin-tx-details = []

[dependencies]
hex = { workspace = true }
log = { workspace = true }
Expand Down
12 changes: 10 additions & 2 deletions common/chain_ids/src/eth_chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,20 @@ impl FromStr for EthChainId {
}
}

#[cfg(not(feature = "include-origin-tx-details"))]
const ERC777_MINT_WITH_DATA_GAS_LIMIT: usize = 450_000;
#[cfg(feature = "include-origin-tx-details")]
const ERC777_MINT_WITH_DATA_GAS_LIMIT: usize = 1_000_000;

#[cfg(not(feature = "include-origin-tx-details"))]
const ERC20_VAULT_PEGOUT_WITH_USER_DATA_GAS_LIMIT: usize = 450_000;
#[cfg(feature = "include-origin-tx-details")]
const ERC20_VAULT_PEGOUT_WITH_USER_DATA_GAS_LIMIT: usize = 1_000_000;

const ARBITRUM_GAS_MULTIPLIER: usize = 10;
const ERC777_CHANGE_PNETWORK_GAS_LIMIT: usize = 30_000;
const ERC20_VAULT_MIGRATE_GAS_LIMIT: usize = 2_000_000;
const ERC777_MINT_WITH_DATA_GAS_LIMIT: usize = 450_000;
const ERC777_MINT_WITH_NO_DATA_GAS_LIMIT: usize = 180_000;
const ERC20_VAULT_PEGOUT_WITH_USER_DATA_GAS_LIMIT: usize = 450_000;
const ERC20_VAULT_CHANGE_SUPPORTED_TOKEN_GAS_LIMIT: usize = 100_000;
const ERC20_VAULT_PEGOUT_WITHOUT_USER_DATA_GAS_LIMIT: usize = 250_000;

Expand Down
18 changes: 18 additions & 0 deletions scripts/test-build-with-origin-tx-details.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Exit early if non-zero exit code encountered
set -e

echo testing cores with origin chain tx pass through enabled

featureFlag="--features=include-origin-tx-details"

packages=(
"int_on_evm" # NOTE: array in case we add this feature to future cores
)

for package in "${packages[@]}"
do
echo testing cores with origin chain tx pass through enabled for package: \'$package\'...
cargo test --package=$package $featureFlag
done
10 changes: 9 additions & 1 deletion v2_bridges/int_on_evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
license = "MIT"
publish = false
edition = "2021"
version = "1.6.0"
version = "1.7.0"
name = "int_on_evm"
readme = "README.md"
rust-version = "1.56"
Expand All @@ -13,6 +13,11 @@ authors = ["Greg Kapka <[email protected]>"]
# NOTE: Turns off block header & chain validation.
non-validating = ["common_eth/non-validating"]

# NOTE: Normally the peg-in event coming from the interim chain will have origin details pertaining _to_
# that interim chain. With this feature enabled, we instead look for the original minting action on the
# interim chain (if extant) and gather originating tx details from that instead.
include-origin-tx-details = ["common_chain_ids/include-origin-tx-details"]

[dependencies]
hex = { workspace = true }
log = { workspace = true }
Expand All @@ -36,5 +41,8 @@ common_debug_signers = { workspace = true }
common_safe_addresses = { workspace = true }
common_database_utils = { workspace = true }

[dev-dependencies]
simple_logger = { workspace = true }

[lib]
doctest = false
4 changes: 3 additions & 1 deletion v2_bridges/int_on_evm/src/evm/sign_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ pub fn maybe_sign_eth_txs_and_add_to_evm_state<D: DatabaseInterface>(state: EthS
}
}

#[cfg(test)]
// NOTE: Including origin tx details provisions more gas to txs, which changes tx encoding, hence
// cause test failures.
#[cfg(all(test, not(feature = "include-origin-tx-details")))]
mod tests {
use common_eth::EthTxInfoCompatible;

Expand Down
4 changes: 3 additions & 1 deletion v2_bridges/int_on_evm/src/evm/submit_evm_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ pub fn submit_evm_blocks_to_core<D: DatabaseInterface>(db: &D, blocks: &str) ->
})
}

#[cfg(test)]
// NOTE: Including origin tx details provisions more gas to txs, which changes tx encoding, hence
// cause test failures.
#[cfg(all(test, not(feature = "include-origin-tx-details")))]
mod tests {
use std::{fs::read_to_string, str::FromStr};

Expand Down
3 changes: 2 additions & 1 deletion v2_bridges/int_on_evm/src/int/evm_tx_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use common::{
types::{Byte, Bytes, Result},
utils::convert_bytes_to_string,
};
use common_eth::{convert_eth_address_to_string, convert_eth_hash_to_string};
use common_eth::{convert_eth_address_to_string, convert_eth_hash_to_string, PTokensRouterMetadataEvent};
use common_metadata::MetadataChainId;
use common_safe_addresses::SAFE_ETH_ADDRESS_STR;
use derive_more::{Constructor, Deref};
Expand All @@ -25,6 +25,7 @@ pub struct IntOnEvmEvmTxInfo {
pub eth_token_address: EthAddress,
pub origin_chain_id: MetadataChainId,
pub destination_chain_id: MetadataChainId,
pub(crate) metadata_event: Option<PTokensRouterMetadataEvent>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Constructor, Deref, Serialize, Deserialize)]
Expand Down
20 changes: 17 additions & 3 deletions v2_bridges/int_on_evm/src/int/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,32 @@ use crate::int::evm_tx_info::IntOnEvmEvmTxInfo;
impl IntOnEvmEvmTxInfo {
fn to_metadata(&self) -> Result<Metadata> {
let user_data = if self.user_data.len() > MAX_BYTES_FOR_ETH_USER_DATA {
// TODO Test for this case!
info!(
"`user_data` redacted from `Metadata` ∵ it's > {} bytes",
"`user_data` redacted from `Metadata` ∵ it's > {} bytes",
MAX_BYTES_FOR_ETH_USER_DATA
);
vec![]
} else {
self.user_data.clone()
};

// NOTE: In this case the token sender is the router address, and the origin chain id
// is that of the interim chain, IE 0xffffffff
let mut origin_address = MetadataAddress::new_from_eth_address(&self.token_sender, &self.origin_chain_id)?;

if cfg!(feature = "include-origin-tx-details") && self.metadata_event.is_some() {
// NOTE: Here we use the router's metadata event to get the original sender & chain ID details
let metadata = self
.metadata_event
.as_ref()
.expect("this not to fail due to preceeding line");

origin_address = MetadataAddress::new(metadata.origin_address(), metadata.origin_chain_id())?;
};

Ok(Metadata::new_v2(
&user_data,
&MetadataAddress::new_from_eth_address(&self.token_sender, &self.origin_chain_id)?,
&origin_address,
&MetadataAddress::new_from_eth_address(
&safely_convert_str_to_eth_address(&self.destination_address),
&self.destination_chain_id,
Expand Down
2 changes: 2 additions & 0 deletions v2_bridges/int_on_evm/src/int/parse_tx_infos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use common_eth::{
EthReceipt,
EthState,
EthSubmissionMaterial,
PTokensRouterMetadataEvent,
ERC20_VAULT_PEG_IN_EVENT_TOPIC_V2,
};
use ethereum_types::Address as EthAddress;
Expand Down Expand Up @@ -55,6 +56,7 @@ impl IntOnEvmEvmTxInfos {
origin_chain_id: event_params.get_origin_chain_id()?,
destination_address: event_params.destination_address.clone(),
destination_chain_id: event_params.get_destination_chain_id()?,
metadata_event: PTokensRouterMetadataEvent::try_from(receipt).ok(),
evm_token_address: dictionary.get_evm_address_from_eth_address(&event_params.token_address)?,
};
info!("✔ Parsed tx info: {:?}", tx_info);
Expand Down
3 changes: 1 addition & 2 deletions v2_bridges/int_on_evm/src/int/sign_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ mod tests {
let expected_num_results = 1;
assert_eq!(signed_txs.len(), expected_num_results);
let tx_hex = signed_txs[0].eth_tx_hex().unwrap();
let expected_tx_hex = "f902ab808504a817c800830493e094dd9f905a34a6c507c7d68384985905cf5eb032e980b90244dcdc7dd0000000000000000000000000fedfe2616eb3661cb8fed2782f5f0cc91d59dcac000000000000000000000000000000000000000000000000000000000000053900000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000000000000000000018002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100ffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000e1c8524b1d1891b201ffc7bb58a82c96f8fc4f60069c32200000000000000000000000000000000000000000000000000000000000000000000000000000000fedfe2616eb3661cb8fed2782f5f0cc91d59dcac000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000003c0ffee00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002ba0352bf595d0e951217a4dc438b12e6d83a74d1393b250637cb3022645f4405092a01ac93750c7431df963f9db304b044512f8cba56f38e5e247c9846277115c6af9"
;
let expected_tx_hex = "f902ab808504a817c800830493e094dd9f905a34a6c507c7d68384985905cf5eb032e980b90244dcdc7dd0000000000000000000000000fedfe2616eb3661cb8fed2782f5f0cc91d59dcac000000000000000000000000000000000000000000000000000000000000053900000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000000000000000000018002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100ffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000e1c8524b1d1891b201ffc7bb58a82c96f8fc4f60069c32200000000000000000000000000000000000000000000000000000000000000000000000000000000fedfe2616eb3661cb8fed2782f5f0cc91d59dcac000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000003c0ffee00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002ba0352bf595d0e951217a4dc438b12e6d83a74d1393b250637cb3022645f4405092a01ac93750c7431df963f9db304b044512f8cba56f38e5e247c9846277115c6af9";
assert_eq!(tx_hex, expected_tx_hex);
}
}
123 changes: 122 additions & 1 deletion v2_bridges/int_on_evm/src/int/submit_int_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn submit_int_blocks_to_core<D: DatabaseInterface>(db: &D, blocks: &str) ->
})
}

#[cfg(test)]
#[cfg(all(test, not(feature = "include-origin-tx-details")))]
mod tests {
use std::fs::read_to_string;

Expand Down Expand Up @@ -129,6 +129,7 @@ mod tests {
get_sample_vault_address,
},
};

#[test]
fn should_submit_int_block_successfully() {
let db = get_test_database();
Expand Down Expand Up @@ -211,3 +212,123 @@ mod tests {
assert_eq!(result, expected_result);
}
}

#[cfg(all(test, feature = "include-origin-tx-details"))]
mod tests {
use common::{dictionaries::eth_evm::EthEvmTokenDictionary, test_utils::get_test_database};
use common_chain_ids::EthChainId;
use common_eth::{
convert_hex_to_eth_address,
initialize_eth_core_with_vault_and_router_contracts_and_return_state,
initialize_evm_core_with_no_contract_tx,
EthDbUtilsExt,
EthPrivateKey,
EvmDbUtils,
VaultUsingCores,
};
use ethereum_types::Address as EthAddress;
use serde_json::json;

use super::*;
use crate::{
int::get_int_output_json::IntOutput,
test_utils::{
get_sample_evm_init_block_json_string,
get_sample_peg_in_with_origin_tx_details,
get_sample_peg_in_with_origin_tx_details_init_block,
get_sample_router_address,
get_sample_token_dictionary_entry_2,
},
};

#[test]
fn should_pass_through_origin_chain_tx_details() {
let db = get_test_database();
let router_address = get_sample_router_address();
let vault_address = EthAddress::from_str("857831740fa65f22eabdbc703a5b512edf9fa4df").unwrap();
let confirmations = 0;
let gas_price = 20_000_000_000;
// NOTE: Initialize the INT side of the core...
initialize_eth_core_with_vault_and_router_contracts_and_return_state(
&get_sample_peg_in_with_origin_tx_details_init_block(),
&EthChainId::InterimChain,
gas_price,
confirmations,
EthState::init(&db),
&vault_address,
&router_address,
&VaultUsingCores::IntOnEvm,
true, // NOTE: is_native
)
.unwrap();
// NOTE: Initialize the EVM side of the core...
initialize_evm_core_with_no_contract_tx(
&get_sample_evm_init_block_json_string(),
&EthChainId::Ropsten,
gas_price,
confirmations,
EthState::init(&db),
false, // NOTE: is_native
)
.unwrap();
// NOTE: Overwrite the INT address & private key since it's generated randomly above...
let address = convert_hex_to_eth_address("0x969c70bccf47406e6d27ec91a12e66aedc7ef23e").unwrap();
let private_key = EthPrivateKey::from_slice(
&hex::decode("f39d9bfba0555500b8b2c89cc46e90ae75fa80c23752ebae1ff31e3123d459dd").unwrap(),
)
.unwrap();
let db_utils = EvmDbUtils::new(&db);
db_utils
.put_eth_address_in_db(&db_utils.get_eth_address_key(), &address)
.unwrap();
db_utils.put_eth_private_key_in_db(&private_key).unwrap();
// NOTE: Set the nonce to match that used during the test...
let evm_nonce = 1;
db_utils.put_eth_account_nonce_in_db(evm_nonce).unwrap();
assert_eq!(db_utils.get_public_eth_address_from_db().unwrap(), address);
assert_eq!(db_utils.get_eth_private_key_from_db().unwrap(), private_key);
assert_eq!(db_utils.get_eth_account_nonce_from_db().unwrap(), evm_nonce);
// NOTE Save the token dictionary into the db...
EthEvmTokenDictionary::new(vec![])
.add_and_update_in_db(get_sample_token_dictionary_entry_2(), &db)
.unwrap();
let submission_string = get_sample_peg_in_with_origin_tx_details();
// NOTE: Finally, submit the block containing the peg in....
let output = submit_int_block_to_core(&db, &submission_string).unwrap();
let expected_result_json = json!({
"int_latest_block_number": 22601142,
"evm_signed_transactions": [{
"_id":"pint-on-evm-evm-1",
"broadcast":false,
"evm_tx_hash":"0xee4a0e15940ac81045e84138073ced34ebddbf28b30f56729383773b603e5eab",
"evm_tx_amount":"999000000000000000",
"evm_tx_recipient":"0xa41657bf225f8ec7e2010c89c3f084172948264d",
"witnessed_timestamp":1708707488,
"host_token_address":"0x0259461eed4d76d4f0f900f9035f6c4dfb39159a",
"originating_tx_hash":"0x4b881458a053de16e9a7a76dc7e8251da6376d1179d71c55dbb2bcd701168471",
"originating_address":"0x54d5a0638f23f0b89053f86eed60237bbc56e98c",
"destination_chain_id":"0x00f1918e",
"native_token_address":"0xeeef86a5598a48c568cca576d9e0c15c370b50a0",
"evm_signed_tx":"f902ab018504a817c800830f4240940259461eed4d76d4f0f900f9035f6c4dfb39159a80b90244dcdc7dd0000000000000000000000000a41657bf225f8ec7e2010c89c3f084172948264d0000000000000000000000000000000000000000000000000ddd2935029d800000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000000000000000000018002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100005fe7f900000000000000000000000000000000000000000000000000000000000000000000000000000000a41657bf225f8ec7e2010c89c3f084172948264d00f1918e00000000000000000000000000000000000000000000000000000000000000000000000000000000a41657bf225f8ec7e2010c89c3f084172948264d000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000008463030444241424500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000029a05e6a1c855a2a8405d2316532b9388d9705070964e1bb8cd7cedc6eaddcd07e79a04a673c69a2d70fbb2552357f03aaa985d59b410ba1fb2d3c406cd7cab041f0e7",
"any_sender_nonce":null,
"evm_account_nonce":1,
"evm_latest_block_number":11571205,
"broadcast_tx_hash":null,
"broadcast_timestamp":null,
"any_sender_tx":null
}]
});
let expected_result = IntOutput::from_str(&expected_result_json.to_string()).unwrap();
let result = IntOutput::from_str(&output).unwrap();
assert_eq!(result, expected_result);
let signed_tx = expected_result.evm_signed_transactions[0]
.evm_signed_tx
.clone()
.unwrap();

// NOTE: Assert that there's no mention of the interim chain in the tx.
assert!(!signed_tx.contains("ffffffff"));
// NOTE: Assert that the expected origin address exists in the tx.
assert!(signed_tx.contains("a41657bf225f8ec7e2010c89c3f084172948264d"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"block":{"difficulty":"2","extraData":"0xd883010a12846765746888676f312e31382e31856c696e757800000000000000e8534a36e69498f5a6f61fd0014f082bd82e2bcf9678568978c197eec3d4febb134738074c7c6017f54cb9bb66c80256573dcf69ed65a07944850d42d7280fa600","gasLimit":29999972,"gasUsed":0,"hash":"0xf635240c2ef51bbbb3886da7d78c77c1d0859ebaf401da86c7b296d2b69e3468","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":22601141,"parentHash":"0xbbc8366d29d64b00ad86e381a40d5897034915e86a466f625e82dc1797d837ad","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":611,"stateRoot":"0x41e2560d87460a8b532067960bc3f9dce859c1b84c9491a8eef17ed38fdbcc37","timestamp":1708621150,"totalDifficulty":"39424653","transactions":[],"transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","uncles":[]},"receipts":[]}
Loading
Loading