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

chore(bridge-contracts): add snapshot test for memo #1753

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/astria-bridge-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ serde_json = { workspace = true }
tendermint = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true, optional = true }

[dev-dependencies]
insta = { workspace = true }
72 changes: 50 additions & 22 deletions crates/astria-bridge-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use ethers::{
Filter,
Log,
H256,
U256,
},
};
pub use generated::*;
Expand Down Expand Up @@ -432,14 +433,10 @@ where

let transaction_hash = log
.transaction_hash
.ok_or_else(|| GetWithdrawalActionsError::log_without_transaction_hash(&log))?
.encode_hex();
.ok_or_else(|| GetWithdrawalActionsError::log_without_transaction_hash(&log))?;
let event_index = log
.log_index
.ok_or_else(|| GetWithdrawalActionsError::log_without_log_index(&log))?
.encode_hex();

let rollup_withdrawal_event_id = format!("{transaction_hash}.{event_index}");
.ok_or_else(|| GetWithdrawalActionsError::log_without_log_index(&log))?;

let event = decode_log::<Ics20WithdrawalFilter>(log)
.map_err(GetWithdrawalActionsError::decode_log)?;
Expand All @@ -454,13 +451,9 @@ where
.expect("must be set if this method is entered"),
);

let memo = memo_to_json(&memos::v1::Ics20WithdrawalFromRollup {
memo: event.memo.clone(),
rollup_block_number,
rollup_return_address: event.sender.encode_hex(),
rollup_withdrawal_event_id,
})
.map_err(GetWithdrawalActionsError::encode_memo)?;
let memo =
make_withdrawal_memo(&event, rollup_block_number, &transaction_hash, &event_index)
.map_err(GetWithdrawalActionsError::encode_memo)?;

let amount = calculate_amount(&event, self.asset_withdrawal_divisor)
.map_err(GetWithdrawalActionsError::calculate_withdrawal_amount)?;
Expand Down Expand Up @@ -494,14 +487,13 @@ where

let transaction_hash = log
.transaction_hash
.ok_or_else(|| GetWithdrawalActionsError::log_without_transaction_hash(&log))?
.encode_hex();
.ok_or_else(|| GetWithdrawalActionsError::log_without_transaction_hash(&log))?;
let event_index = log
.log_index
.ok_or_else(|| GetWithdrawalActionsError::log_without_log_index(&log))?
.encode_hex();
.ok_or_else(|| GetWithdrawalActionsError::log_without_log_index(&log))?;

let rollup_withdrawal_event_id = format!("{transaction_hash}.{event_index}");
let rollup_withdrawal_event_id =
rollup_withdrawal_event_id(&transaction_hash, &event_index);

let event = decode_log::<SequencerWithdrawalFilter>(log)
.map_err(GetWithdrawalActionsError::decode_log)?;
Expand Down Expand Up @@ -698,9 +690,31 @@ struct EncodeMemoError {
source: serde_json::Error,
}

fn memo_to_json<T: prost::Name + serde::Serialize>(memo: &T) -> Result<String, EncodeMemoError> {
serde_json::to_string(memo).map_err(|source| EncodeMemoError {
proto_message: T::full_name(),
fn rollup_withdrawal_event_id(transaction_hash: &H256, event_index: &U256) -> String {
format!(
"{}.{}",
transaction_hash.encode_hex(),
event_index.encode_hex()
)
}

fn make_withdrawal_memo(
event: &Ics20WithdrawalFilter,
rollup_block_number: u64,
transaction_hash: &H256,
event_index: &U256,
) -> Result<String, EncodeMemoError> {
use prost::Name as _;
type Memo = memos::v1::Ics20WithdrawalFromRollup;

let withdrawal = Memo {
memo: event.memo.clone(),
rollup_block_number,
rollup_return_address: event.sender.encode_hex(),
rollup_withdrawal_event_id: rollup_withdrawal_event_id(transaction_hash, event_index),
};
serde_json::to_string(&withdrawal).map_err(|source| EncodeMemoError {
proto_message: Memo::full_name(),
source,
})
}
Expand All @@ -723,9 +737,23 @@ fn timeout_in_5_min() -> u64 {

#[cfg(test)]
mod tests {
use super::max_timeout_height;
use super::*;

#[test]
fn max_timeout_height_does_not_panic() {
max_timeout_height();
}

#[test]
fn memo_format_should_not_change() {
let event = Ics20WithdrawalFilter {
sender: "14cbd075f796969cf5dbd853da492d86071f97ed".parse().unwrap(),
amount: U256::one(),
destination_chain_address: "destination address".to_string(),
memo: "<a third party memo>".to_string(),
};
let json_encoded_memo =
make_withdrawal_memo(&event, 999, &H256::from_slice(&[1; 32]), &U256::one()).unwrap();
insta::assert_snapshot!("memo_snapshot", json_encoded_memo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/astria-bridge-contracts/src/lib.rs
assertion_line: 757
expression: json_encoded_memo
---
{"rollupBlockNumber":"999","rollupWithdrawalEventId":"0x0101010101010101010101010101010101010101010101010101010101010101.0x0000000000000000000000000000000000000000000000000000000000000001","rollupReturnAddress":"0x00000000000000000000000014cbd075f796969cf5dbd853da492d86071f97ed","memo":"<a third party memo>"}
Loading