From eabe78fd7c62a470be6fa9d5d410fb134f350fe9 Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Wed, 11 Nov 2020 08:10:02 -0600 Subject: [PATCH 1/2] add event_index for ordering events, add `locked_address` field to stx_lock_event --- src/chainstate/stacks/events.rs | 20 ++++++++++++++++++-- src/vm/functions/special.rs | 1 + testnet/stacks-node/src/event_dispatcher.rs | 8 +++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/chainstate/stacks/events.rs b/src/chainstate/stacks/events.rs index a53a3cadc3..dc83186644 100644 --- a/src/chainstate/stacks/events.rs +++ b/src/chainstate/stacks/events.rs @@ -44,58 +44,72 @@ pub enum StacksTransactionEvent { } impl StacksTransactionEvent { - pub fn json_serialize(&self, txid: &Txid, committed: bool) -> serde_json::Value { + pub fn json_serialize( + &self, + event_index: usize, + txid: &Txid, + committed: bool, + ) -> serde_json::Value { match self { StacksTransactionEvent::SmartContractEvent(event_data) => json!({ "txid": format!("0x{:?}", txid), + "event_index": event_index, "committed": committed, "type": "contract_event", "contract_event": event_data.json_serialize() }), StacksTransactionEvent::STXEvent(STXEventType::STXTransferEvent(event_data)) => json!({ "txid": format!("0x{:?}", txid), + "event_index": event_index, "committed": committed, "type": "stx_transfer_event", "stx_transfer_event": event_data.json_serialize() }), StacksTransactionEvent::STXEvent(STXEventType::STXMintEvent(event_data)) => json!({ "txid": format!("0x{:?}", txid), + "event_index": event_index, "committed": committed, "type": "stx_mint_event", "stx_mint_event": event_data.json_serialize() }), StacksTransactionEvent::STXEvent(STXEventType::STXBurnEvent(event_data)) => json!({ "txid": format!("0x{:?}", txid), + "event_index": event_index, "committed": committed, "type": "stx_burn_event", "stx_burn_event": event_data.json_serialize() }), StacksTransactionEvent::STXEvent(STXEventType::STXLockEvent(event_data)) => json!({ "txid": format!("0x{:?}", txid), + "event_index": event_index, "committed": committed, "type": "stx_lock_event", "stx_lock_event": event_data.json_serialize() }), StacksTransactionEvent::NFTEvent(NFTEventType::NFTTransferEvent(event_data)) => json!({ "txid": format!("0x{:?}", txid), + "event_index": event_index, "committed": committed, "type": "nft_transfer_event", "nft_transfer_event": event_data.json_serialize() }), StacksTransactionEvent::NFTEvent(NFTEventType::NFTMintEvent(event_data)) => json!({ "txid": format!("0x{:?}", txid), + "event_index": event_index, "committed": committed, "type": "nft_mint_event", "nft_mint_event": event_data.json_serialize() }), StacksTransactionEvent::FTEvent(FTEventType::FTTransferEvent(event_data)) => json!({ "txid": format!("0x{:?}", txid), + "event_index": event_index, "committed": committed, "type": "ft_transfer_event", "ft_transfer_event": event_data.json_serialize() }), StacksTransactionEvent::FTEvent(FTEventType::FTMintEvent(event_data)) => json!({ "txid": format!("0x{:?}", txid), + "event_index": event_index, "committed": committed, "type": "ft_mint_event", "ft_mint_event": event_data.json_serialize() @@ -160,6 +174,7 @@ impl STXMintEventData { pub struct STXLockEventData { pub locked_amount: u128, pub unlock_height: u64, + pub locked_address: PrincipalData, } impl STXLockEventData { @@ -167,6 +182,7 @@ impl STXLockEventData { json!({ "locked_amount": format!("{}",self.locked_amount), "unlock_height": format!("{}", self.unlock_height), + "locked_address": format!("{}", self.locked_address), }) } } @@ -180,7 +196,7 @@ pub struct STXBurnEventData { impl STXBurnEventData { pub fn json_serialize(&self) -> serde_json::Value { json!({ - "sender": format!("{}",self.sender), + "sender": format!("{}", self.sender), "amount": format!("{}", self.amount), }) } diff --git a/src/vm/functions/special.rs b/src/vm/functions/special.rs index db6d838f83..c784fe10f9 100644 --- a/src/vm/functions/special.rs +++ b/src/vm/functions/special.rs @@ -92,6 +92,7 @@ fn handle_pox_api_contract_call( STXEventType::STXLockEvent(STXLockEventData { locked_amount, unlock_height, + locked_address: stacker, }), )); } diff --git a/testnet/stacks-node/src/event_dispatcher.rs b/testnet/stacks-node/src/event_dispatcher.rs index 63b6406b28..91619400ab 100644 --- a/testnet/stacks-node/src/event_dispatcher.rs +++ b/testnet/stacks-node/src/event_dispatcher.rs @@ -197,7 +197,7 @@ impl EventObserver { fn send( &self, - filtered_events: Vec<&(bool, Txid, &StacksTransactionEvent)>, + filtered_events: Vec<(usize, &(bool, Txid, &StacksTransactionEvent))>, chain_tip: &ChainTip, parent_index_hash: &StacksBlockId, boot_receipts: Option<&Vec>, @@ -207,7 +207,9 @@ impl EventObserver { // Serialize events to JSON let serialized_events: Vec = filtered_events .iter() - .map(|(committed, txid, event)| event.json_serialize(txid, *committed)) + .map(|(event_index, (committed, txid, event))| { + event.json_serialize(*event_index, txid, *committed) + }) .collect(); let mut tx_index: u32 = 0; @@ -449,7 +451,7 @@ impl EventDispatcher { for (observer_id, filtered_events_ids) in dispatch_matrix.iter().enumerate() { let filtered_events: Vec<_> = filtered_events_ids .iter() - .map(|event_id| &events[*event_id]) + .map(|event_id| (*event_id, &events[*event_id])) .collect(); self.registered_observers[observer_id].send( From aeb06395ed2535fde078bcaa9f5787a82f839d80 Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Wed, 11 Nov 2020 08:13:09 -0600 Subject: [PATCH 2/2] docs: add event_index to sample --- docs/event-dispatcher.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/event-dispatcher.md b/docs/event-dispatcher.md index b1d08a7dd5..51e873cfcd 100644 --- a/docs/event-dispatcher.md +++ b/docs/event-dispatcher.md @@ -36,6 +36,7 @@ Example: "burn_block_time": 1591301733, "events": [ { + "event_index": 1, "committed": true, "stx_transfer_event": { "amount": "1000",