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(refiner): Include EVM logs for bridge transactions #111

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 9 additions & 9 deletions Cargo.lock

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

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ license = "CC0-1.0"
[workspace.dependencies]
actix = "0.13"
anyhow = "1"
aurora-engine = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "4527320c940d33a7ce40ce422b4d2fb77dc8e74e", default-features = false, features = ["std", "tracing", "log", "impl-serde"] }
aurora-engine-transactions = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "4527320c940d33a7ce40ce422b4d2fb77dc8e74e", default-features = false, features = ["std", "impl-serde"] }
aurora-engine-types = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "4527320c940d33a7ce40ce422b4d2fb77dc8e74e", default-features = false, features = ["std", "impl-serde"] }
aurora-engine-sdk = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "4527320c940d33a7ce40ce422b4d2fb77dc8e74e", default-features = false, features = ["std"] }
aurora-engine-modexp = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "4527320c940d33a7ce40ce422b4d2fb77dc8e74e", default-features = false, features = ["std"] }
aurora-engine-hashchain = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "4527320c940d33a7ce40ce422b4d2fb77dc8e74e", default-features = false, features = ["std"] }
engine-standalone-storage = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "4527320c940d33a7ce40ce422b4d2fb77dc8e74e", default-features = false }
engine-standalone-tracing = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "4527320c940d33a7ce40ce422b4d2fb77dc8e74e", default-features = false, features = ["impl-serde"] }
aurora-engine = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "7fff9ff2825483005f958108f767a91fd2422b0b", default-features = false, features = ["std", "tracing", "log", "impl-serde"] }
aurora-engine-transactions = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "7fff9ff2825483005f958108f767a91fd2422b0b", default-features = false, features = ["std", "impl-serde"] }
aurora-engine-types = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "7fff9ff2825483005f958108f767a91fd2422b0b", default-features = false, features = ["std", "impl-serde"] }
aurora-engine-sdk = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "7fff9ff2825483005f958108f767a91fd2422b0b", default-features = false, features = ["std"] }
aurora-engine-modexp = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "7fff9ff2825483005f958108f767a91fd2422b0b", default-features = false, features = ["std"] }
aurora-engine-hashchain = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "7fff9ff2825483005f958108f767a91fd2422b0b", default-features = false, features = ["std"] }
engine-standalone-storage = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "7fff9ff2825483005f958108f767a91fd2422b0b", default-features = false }
engine-standalone-tracing = { git = "https://github.com/aurora-is-near/aurora-engine.git", rev = "7fff9ff2825483005f958108f767a91fd2422b0b", default-features = false, features = ["impl-serde"] }
borsh = "0.10"
byteorder = "1"
clap = { version = "4", features = ["derive"] }
Expand Down
7 changes: 7 additions & 0 deletions refiner-lib/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ lazy_static! {
"refiner_tx_type_mirror_erc20_token_callback",
"Number of transactions of type: mirror_erc20_token_callback"
);
pub static ref TRANSACTION_TYPE_WITHDRAW_WNEAR_TO_ROUTER: IntCounter = counter(
"refiner_tx_type_withdraw_wnear_to_router",
"Number of transactions of type: withdraw_wnear_to_router"
);
}

pub fn record_metric(tx_kind: &TransactionKindTag) {
Expand Down Expand Up @@ -370,6 +374,9 @@ pub fn record_metric(tx_kind: &TransactionKindTag) {
TransactionKindTag::MirrorErc20TokenCallback => {
TRANSACTION_TYPE_MIRROR_ERC20_TOKEN_CALLBACK.inc();
}
TransactionKindTag::WithdrawWnearToRouter => {
TRANSACTION_TYPE_WITHDRAW_WNEAR_TO_ROUTER.inc();
}
}
}

Expand Down
47 changes: 47 additions & 0 deletions refiner-lib/src/near_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,53 @@ pub mod tests {
assert_eq!(target_aurora_tx.nonce, expected_nonce);
}

// Tests processing the transaction https://explorer.mainnet.near.org/transactions/964KbgjnkCfyUS1kaHVJNGuXAMsahdNHiP1jWkMnx1Bk
// which is a bridge transfer of some tokens into Aurora. The ERC-20 logs should be present
// based on the tokens that were minted from th bridging.
#[tokio::test]
async fn test_block_75306841_bridge_tx() {
// load state snapshot and main objects
let db_dir = tempfile::tempdir().unwrap();
let mut ctx = TestContext::new(&db_dir);
ctx.init_with_snapshot("tests/res/state_EVVnmqiPm6efCJGWLS5DgMTq3spVnevvh4fEgvc2e2Hz.json")
.await;
let mut stream = ctx.create_stream();

// parameters of the test
let block = read_block("tests/res/block-75306841.json");

// run and assert
let mut aurora_block = stream.next_block(&block).await.pop().unwrap();

assert_eq!(aurora_block.transactions.len(), 1);
let mut target_aurora_tx = aurora_block.transactions.pop().unwrap();

assert_eq!(target_aurora_tx.logs.len(), 1);
let log = target_aurora_tx.logs.pop().unwrap();

// ERC-20 event hex signature for `Transfer(address,address,uint256)`
// https://www.4byte.directory/event-signatures/?bytes_signature=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
assert_eq!(
hex::encode(log.topics[0]),
"ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
);

// Transfer from 0x00 (i.e. mint)
assert_eq!(log.topics[1], [0u8; 32]);

// Transfer to user's address (given in the `ft_on_transfer` message).
assert_eq!(
hex::encode(log.topics[2]),
"000000000000000000000000852285d421bb5682470ad46e2eb99adf001ab9f1"
);

// Transfer amount equal to value specified in `ft_on_transfer`
assert_eq!(
aurora_engine_types::U256::from_big_endian(&log.data),
13870504203340000000000_u128.into()
);
}

#[tokio::test]
async fn test_block_70834061_skip_block() {
let db_dir = tempfile::tempdir().unwrap();
Expand Down
82 changes: 54 additions & 28 deletions refiner-lib/src/refiner_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,34 @@ fn normalize_output(
execution_status: Option<&ExecutionStatusView>,
engine_outcome: Option<&TransactionIncludedOutcome>,
) -> Result<(SubmitResult, HashchainOutputKind, Vec<u8>), RefinerError> {
let engine_output = engine_outcome
.and_then(|x| x.maybe_result.as_ref().ok())
.and_then(Option::as_ref)
.map(|result| match result {
TransactionExecutionResult::Submit(result) => match result {
Ok(result) => result.clone(),
Err(err) => SubmitResult::new(
aurora_engine::parameters::TransactionStatus::Revert(
format!("{:?}", err.kind).into_bytes(),
),
err.gas_used,
Vec::new(),
),
},
TransactionExecutionResult::DeployErc20(address) => SubmitResult::new(
aurora_engine::parameters::TransactionStatus::Succeed(address.as_bytes().to_vec()),
MIN_EVM_GAS,
Vec::new(),
),
TransactionExecutionResult::Promise(p) => SubmitResult::new(
aurora_engine::parameters::TransactionStatus::Succeed(
format!("{:?}", p).into_bytes(),
),
MIN_EVM_GAS,
Vec::new(),
),
});

let near_output = match execution_status {
Some(ExecutionStatusView::Unknown | ExecutionStatusView::Failure(_)) => {
// Regardless of anything else, if the transaction failed on Near then we report an error.
Expand Down Expand Up @@ -406,6 +434,24 @@ fn normalize_output(
}
}
}
// In the case of `withdraw_wnear_to_router` we take the `SubmitResult` from the
// Standalone Engine because the promise value is only returned to properly link
// the execution of various XCC receipts, but the EVM computation still happened.
// In terms of the hashchain, we still treat it as `HashchainOutputKind::None` because
// `io.return_output` is never called since the promise is returned instead.
Some(ExecutionStatusView::SuccessReceiptId(result))
if tx_kind == TransactionKindTag::WithdrawWnearToRouter =>
{
let submit_result = engine_output.as_ref().cloned().unwrap_or_else(|| {
let bytes = result.0.to_vec();
SubmitResult::new(
aurora_engine::parameters::TransactionStatus::Succeed(bytes),
MIN_EVM_GAS,
Vec::new(),
)
});
Some((submit_result, HashchainOutputKind::None, Vec::new()))
}
Some(ExecutionStatusView::SuccessReceiptId(result)) => {
// No need to check the transaction kind in this case because transactions that
// produce a SubmitResult as output do not produce a receipt id.
Expand All @@ -423,36 +469,16 @@ fn normalize_output(
None => None,
};

let engine_output = engine_outcome
.and_then(|x| x.maybe_result.as_ref().ok())
.and_then(Option::as_ref)
.map(|result| match result {
TransactionExecutionResult::Submit(result) => match result {
Ok(result) => result.clone(),
Err(err) => SubmitResult::new(
aurora_engine::parameters::TransactionStatus::Revert(
format!("{:?}", err.kind).into_bytes(),
),
err.gas_used,
Vec::new(),
),
},
TransactionExecutionResult::DeployErc20(address) => SubmitResult::new(
aurora_engine::parameters::TransactionStatus::Succeed(address.as_bytes().to_vec()),
MIN_EVM_GAS,
Vec::new(),
),
TransactionExecutionResult::Promise(p) => SubmitResult::new(
aurora_engine::parameters::TransactionStatus::Succeed(
format!("{:?}", p).into_bytes(),
),
MIN_EVM_GAS,
Vec::new(),
),
});

match (near_output, engine_output) {
(Some(near_output), Some(engine_output)) => {
// In the case of ft_on_transfer, the bridge may mint new ERC-20 tokens.
// However, the NEP-141 protocol must still be followed, therefore the on-chain
// output cannot be a `SubmitResult`. But the Standalone Engine can still capture
// the `SubmitResult` from the execution. Hence, in this case we combine the
// Standalone's result with the the on-chain output to get a complete picture.
if tx_kind == TransactionKindTag::FtOnTransfer {
return Ok((engine_output, near_output.1, near_output.2));
}
// We have a result from both sources, so we should compare them to
// make sure they match. Log a warning and use the Near output if they don't.
if near_output.0 != engine_output {
Expand Down
Loading