Skip to content

Commit

Permalink
feat(sequencer): report deposit events (#1447)
Browse files Browse the repository at this point in the history
## Summary
Added creation of ABCI deposit events and record them to the state to be
included in `ExecTxResult`.

## Background
Deposit events were previously only stored app-side in `SequencerBlock`.
This will allow for outside observation of the deposit events.

## Changes
- Added helper function for the creation of ABCI deposit events.
- Added recording of these events whenever deposits are created.

## Testing
Added test to ensure deposit events are correctly recorded and returned
by `state.apply()`

## Related Issues
closes #1440

---------

Co-authored-by: Jordan Oroshiba <[email protected]>
Co-authored-by: Fraser Hutchison <[email protected]>
Co-authored-by: jesse snyder <[email protected]>
  • Loading branch information
4 people authored Sep 11, 2024
1 parent f543222 commit 81f2931
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
5 changes: 5 additions & 0 deletions crates/astria-core/src/sequencerblock/v1alpha1/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,11 @@ impl Deposit {
&self.destination_chain_address
}

#[must_use]
pub fn source_transaction_id(&self) -> &TransactionId {
&self.source_transaction_id
}

#[must_use]
pub fn source_action_index(&self) -> u64 {
self.source_action_index
Expand Down
65 changes: 63 additions & 2 deletions crates/astria-sequencer/src/app/tests_execute_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ use astria_core::{
Protobuf as _,
};
use bytes::Bytes;
use cnidarium::StateDelta;
use cnidarium::{
ArcStateDeltaExt as _,
StateDelta,
};

use super::test_utils::get_alice_signing_key;
use crate::{
Expand All @@ -41,7 +44,10 @@ use crate::{
},
ActionHandler as _,
},
assets::StateReadExt as _,
assets::{
StateReadExt as _,
StateWriteExt as _,
},
authority::StateReadExt as _,
bridge::{
StateReadExt as _,
Expand All @@ -59,6 +65,7 @@ use crate::{
InvalidChainId,
InvalidNonce,
},
utils::create_deposit_event,
};

fn proto_genesis_state() -> astria_core::generated::protocol::genesis::v1alpha1::GenesisAppState {
Expand Down Expand Up @@ -1116,3 +1123,57 @@ async fn app_execute_transaction_action_index_correctly_increments() {
starting_index_of_action + 1
);
}

#[tokio::test]
async fn transaction_execution_records_deposit_event() {
let mut app = initialize_app(None, vec![]).await;
let mut state_tx = app
.state
.try_begin_transaction()
.expect("state Arc should be present and unique");

let alice = get_alice_signing_key();
let bob_address = astria_address_from_hex_string(BOB_ADDRESS);
state_tx.put_bridge_account_rollup_id(bob_address, &[0; 32].into());
state_tx.put_allowed_fee_asset(nria());
state_tx
.put_bridge_account_ibc_asset(bob_address, nria())
.unwrap();
let tx = UnsignedTransaction {
params: TransactionParams::builder()
.nonce(0)
.chain_id("test")
.build(),
actions: vec![
BridgeLockAction {
to: bob_address,
amount: 1,
asset: nria().into(),
fee_asset: nria().into(),
destination_chain_address: "test_chain_address".to_string(),
}
.into(),
],
};

let signed_tx = Arc::new(tx.into_signed(&alice));

let expected_deposit = Deposit::new(
bob_address,
[0; 32].into(),
1,
nria().into(),
"test_chain_address".to_string(),
signed_tx.id(),
0,
);
let expected_deposit_event = create_deposit_event(&expected_deposit);

signed_tx.check_and_execute(&mut state_tx).await.unwrap();
let events = &state_tx.apply().1;
let event = events
.iter()
.find(|event| event.kind == "tx.deposit")
.expect("should have deposit event");
assert_eq!(*event, expected_deposit_event);
}
3 changes: 3 additions & 0 deletions crates/astria-sequencer/src/bridge/bridge_lock_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
StateWriteExt as _,
},
transaction::StateReadExt as _,
utils::create_deposit_event,
};

#[async_trait::async_trait]
Expand Down Expand Up @@ -90,6 +91,7 @@ impl ActionHandler for BridgeLockAction {
transaction_id,
source_action_index,
);
let deposit_abci_event = create_deposit_event(&deposit);

let byte_cost_multiplier = state
.get_bridge_lock_byte_cost_multiplier()
Expand Down Expand Up @@ -133,6 +135,7 @@ impl ActionHandler for BridgeLockAction {
.await
.context("failed to deduct fee from account balance")?;

state.record(deposit_abci_event);
state
.put_deposit_event(deposit)
.await
Expand Down
9 changes: 7 additions & 2 deletions crates/astria-sequencer/src/ibc/ics20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ use crate::{
StateReadExt as _,
StateWriteExt as _,
},
ibc,
ibc::StateReadExt as _,
ibc::{
self,
StateReadExt as _,
},
transaction::StateReadExt as _,
utils::create_deposit_event,
};

/// The maximum length of the encoded Ics20 `FungibleTokenPacketData` in bytes.
Expand Down Expand Up @@ -713,6 +716,8 @@ async fn execute_deposit<S: ibc::StateWriteExt>(
transaction_id,
index_of_action,
);
let deposit_abci_event = create_deposit_event(&deposit);
state.record(deposit_abci_event);
state
.put_deposit_event(deposit)
.await
Expand Down
32 changes: 32 additions & 0 deletions crates/astria-sequencer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ use anyhow::Context as _;
use astria_core::{
generated::astria_vendored::tendermint::abci as raw,
protocol::transaction::v1alpha1::action::ValidatorUpdate,
sequencerblock::v1alpha1::block::Deposit,
Protobuf as _,
};
use tendermint::abci::{
self,
EventAttributeIndexExt as _,
};

pub(crate) struct Hex<'a>(pub(crate) &'a [u8]);

Expand All @@ -30,6 +35,33 @@ pub(crate) fn cometbft_to_sequencer_validator(
.context("failed converting cometbft validator update to astria validator update")
}

pub(crate) fn create_deposit_event(deposit: &Deposit) -> abci::Event {
abci::Event::new(
"tx.deposit",
[
("bridgeAddress", deposit.bridge_address().to_string()).index(),
("rollupId", deposit.rollup_id().to_string()).index(),
("amount", deposit.amount().to_string()).index(),
("asset", deposit.asset().to_string()).index(),
(
"destinationChainAddress",
deposit.destination_chain_address().to_string(),
)
.index(),
(
"sourceTransactionId",
deposit.source_transaction_id().to_string(),
)
.index(),
(
"sourceActionIndex",
deposit.source_action_index().to_string(),
)
.index(),
],
)
}

pub(crate) fn sequencer_to_cometbft_validator(
value: ValidatorUpdate,
) -> anyhow::Result<tendermint::validator::Update> {
Expand Down

0 comments on commit 81f2931

Please sign in to comment.