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

Minor event dispatcher updates #2050

Merged
merged 2 commits into from
Nov 11, 2020
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
1 change: 1 addition & 0 deletions docs/event-dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Example:
"burn_block_time": 1591301733,
"events": [
{
"event_index": 1,
"committed": true,
"stx_transfer_event": {
"amount": "1000",
Expand Down
20 changes: 18 additions & 2 deletions src/chainstate/stacks/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -160,13 +174,15 @@ impl STXMintEventData {
pub struct STXLockEventData {
pub locked_amount: u128,
pub unlock_height: u64,
pub locked_address: PrincipalData,
}

impl STXLockEventData {
pub fn json_serialize(&self) -> serde_json::Value {
json!({
"locked_amount": format!("{}",self.locked_amount),
"unlock_height": format!("{}", self.unlock_height),
"locked_address": format!("{}", self.locked_address),
})
}
}
Expand All @@ -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),
})
}
Expand Down
1 change: 1 addition & 0 deletions src/vm/functions/special.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn handle_pox_api_contract_call(
STXEventType::STXLockEvent(STXLockEventData {
locked_amount,
unlock_height,
locked_address: stacker,
}),
));
}
Expand Down
8 changes: 5 additions & 3 deletions testnet/stacks-node/src/event_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StacksTransactionReceipt>>,
Expand All @@ -207,7 +207,9 @@ impl EventObserver {
// Serialize events to JSON
let serialized_events: Vec<serde_json::Value> = 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;
Expand Down Expand Up @@ -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(
Expand Down