Skip to content

Commit f48cda5

Browse files
authored
feat: add various new nakamoto block fields to /new_block ingestion and StacksPayload (#659)
Related to #651 -- we need these new fields to make the `StackerDB` chunk messages useful.
1 parent 535226a commit f48cda5

File tree

10 files changed

+258
-21
lines changed

10 files changed

+258
-21
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/chainhook-cli/src/service/tests/helpers/mock_stacks_node.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::scan::stacks::{Record, RecordKind};
22
use crate::service::tests::helpers::mock_bitcoin_rpc::TipData;
33
use chainhook_sdk::indexer::bitcoin::NewBitcoinBlock;
4-
use chainhook_sdk::indexer::stacks::{NewBlock, NewEvent, NewTransaction};
4+
use chainhook_sdk::indexer::stacks::{NewBlock, NewEvent, NewTransaction, RewardSet, RewardSetSigner};
55
use chainhook_sdk::types::{
66
FTBurnEventData, FTMintEventData, FTTransferEventData, NFTBurnEventData, NFTMintEventData,
77
NFTTransferEventData, STXBurnEventData, STXLockEventData, STXMintEventData,
@@ -260,6 +260,26 @@ pub fn create_stacks_new_block(
260260
transactions: (0..4).map(create_stacks_new_transaction).collect(),
261261
events,
262262
matured_miner_rewards: vec![],
263+
block_time: Some(12345),
264+
signer_bitvec: Some("000800000001ff".to_owned()),
265+
signer_signature: Some(vec!["1234".to_owned(), "2345".to_owned()]),
266+
cycle_number: Some(1),
267+
reward_set: Some(RewardSet {
268+
pox_ustx_threshold: "50000".to_owned(),
269+
rewarded_addresses: vec![],
270+
signers: Some(vec![
271+
RewardSetSigner {
272+
signing_key: "0123".to_owned(),
273+
weight: 123,
274+
stacked_amt: "555555".to_owned(),
275+
},
276+
RewardSetSigner {
277+
signing_key: "2345".to_owned(),
278+
weight: 234,
279+
stacked_amt: "6677777".to_owned(),
280+
},
281+
]),
282+
}),
263283
}
264284
}
265285

components/chainhook-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ prometheus = "0.13.3"
4747
chainhook-types = { path = "../chainhook-types-rs" }
4848

4949
[dev-dependencies]
50+
assert-json-diff = "2.0.2"
5051
test-case = "3.1.0"
5152

5253
[features]

components/chainhook-sdk/src/chainhooks/tests/fixtures/stacks/testnet/occurrence.json

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
"pox_cycle_index": 415,
1515
"pox_cycle_length": 1050,
1616
"pox_cycle_position": 1033,
17-
"stacks_block_hash": "0x"
17+
"stacks_block_hash": "0x",
18+
"block_time": null,
19+
"cycle_number": null,
20+
"reward_set": null,
21+
"signer_bitvec": null,
22+
"signer_signature": null
1823
},
1924
"parent_block_identifier": {
2025
"hash": "0x",
@@ -96,7 +101,12 @@
96101
"pox_cycle_index": 415,
97102
"pox_cycle_length": 1050,
98103
"pox_cycle_position": 1033,
99-
"stacks_block_hash": "0x"
104+
"stacks_block_hash": "0x",
105+
"block_time": null,
106+
"cycle_number": null,
107+
"reward_set": null,
108+
"signer_bitvec": null,
109+
"signer_signature": null
100110
},
101111
"parent_block_identifier": {
102112
"hash": "0x",
@@ -177,7 +187,12 @@
177187
"pox_cycle_index": 415,
178188
"pox_cycle_length": 1050,
179189
"pox_cycle_position": 1033,
180-
"stacks_block_hash": "0x"
190+
"stacks_block_hash": "0x",
191+
"block_time": null,
192+
"cycle_number": null,
193+
"reward_set": null,
194+
"signer_bitvec": null,
195+
"signer_signature": null
181196
},
182197
"parent_block_identifier": {
183198
"hash": "0x",
@@ -259,7 +274,12 @@
259274
"pox_cycle_index": 415,
260275
"pox_cycle_length": 1050,
261276
"pox_cycle_position": 1033,
262-
"stacks_block_hash": "0x"
277+
"stacks_block_hash": "0x",
278+
"block_time": null,
279+
"cycle_number": null,
280+
"reward_set": null,
281+
"signer_bitvec": null,
282+
"signer_signature": null
263283
},
264284
"parent_block_identifier": {
265285
"hash": "0x",
@@ -340,7 +360,12 @@
340360
"pox_cycle_index": 415,
341361
"pox_cycle_length": 1050,
342362
"pox_cycle_position": 1033,
343-
"stacks_block_hash": "0x"
363+
"stacks_block_hash": "0x",
364+
"block_time": null,
365+
"cycle_number": null,
366+
"reward_set": null,
367+
"signer_bitvec": null,
368+
"signer_signature": null
344369
},
345370
"parent_block_identifier": {
346371
"hash": "0x",
@@ -423,7 +448,12 @@
423448
"pox_cycle_index": 415,
424449
"pox_cycle_length": 1050,
425450
"pox_cycle_position": 1033,
426-
"stacks_block_hash": "0x"
451+
"stacks_block_hash": "0x",
452+
"block_time": null,
453+
"cycle_number": null,
454+
"reward_set": null,
455+
"signer_bitvec": null,
456+
"signer_signature": null
427457
},
428458
"parent_block_identifier": {
429459
"hash": "0x",
@@ -505,7 +535,12 @@
505535
"pox_cycle_index": 415,
506536
"pox_cycle_length": 1050,
507537
"pox_cycle_position": 1033,
508-
"stacks_block_hash": "0x"
538+
"stacks_block_hash": "0x",
539+
"block_time": null,
540+
"cycle_number": null,
541+
"reward_set": null,
542+
"signer_bitvec": null,
543+
"signer_signature": null
509544
},
510545
"parent_block_identifier": {
511546
"hash": "0x",
@@ -587,7 +622,12 @@
587622
"pox_cycle_index": 415,
588623
"pox_cycle_length": 1050,
589624
"pox_cycle_position": 1033,
590-
"stacks_block_hash": "0x"
625+
"stacks_block_hash": "0x",
626+
"block_time": null,
627+
"cycle_number": null,
628+
"reward_set": null,
629+
"signer_bitvec": null,
630+
"signer_signature": null
591631
},
592632
"parent_block_identifier": {
593633
"hash": "0x",
@@ -670,7 +710,12 @@
670710
"pox_cycle_index": 415,
671711
"pox_cycle_length": 1050,
672712
"pox_cycle_position": 1033,
673-
"stacks_block_hash": "0x"
713+
"stacks_block_hash": "0x",
714+
"block_time": null,
715+
"cycle_number": null,
716+
"reward_set": null,
717+
"signer_bitvec": null,
718+
"signer_signature": null
674719
},
675720
"parent_block_identifier": {
676721
"hash": "0x",
@@ -752,7 +797,12 @@
752797
"pox_cycle_index": 415,
753798
"pox_cycle_length": 1050,
754799
"pox_cycle_position": 1033,
755-
"stacks_block_hash": "0x"
800+
"stacks_block_hash": "0x",
801+
"block_time": null,
802+
"cycle_number": null,
803+
"reward_set": null,
804+
"signer_bitvec": null,
805+
"signer_signature": null
756806
},
757807
"parent_block_identifier": {
758808
"hash": "0x",
@@ -834,7 +884,12 @@
834884
"pox_cycle_index": 415,
835885
"pox_cycle_length": 1050,
836886
"pox_cycle_position": 1033,
837-
"stacks_block_hash": "0x"
887+
"stacks_block_hash": "0x",
888+
"block_time": null,
889+
"cycle_number": null,
890+
"reward_set": null,
891+
"signer_bitvec": null,
892+
"signer_signature": null
838893
},
839894
"parent_block_identifier": {
840895
"hash": "0x",
@@ -916,7 +971,12 @@
916971
"pox_cycle_index": 415,
917972
"pox_cycle_length": 1050,
918973
"pox_cycle_position": 1033,
919-
"stacks_block_hash": "0x"
974+
"stacks_block_hash": "0x",
975+
"block_time": null,
976+
"cycle_number": null,
977+
"reward_set": null,
978+
"signer_bitvec": null,
979+
"signer_signature": null
920980
},
921981
"parent_block_identifier": {
922982
"hash": "0x",
@@ -998,7 +1058,12 @@
9981058
"pox_cycle_index": 415,
9991059
"pox_cycle_length": 1050,
10001060
"pox_cycle_position": 1033,
1001-
"stacks_block_hash": "0x"
1061+
"stacks_block_hash": "0x",
1062+
"block_time": null,
1063+
"cycle_number": null,
1064+
"reward_set": null,
1065+
"signer_bitvec": null,
1066+
"signer_signature": null
10021067
},
10031068
"parent_block_identifier": {
10041069
"hash": "0x",
@@ -1090,7 +1155,12 @@
10901155
"pox_cycle_index": 415,
10911156
"pox_cycle_length": 1050,
10921157
"pox_cycle_position": 1033,
1093-
"stacks_block_hash": "0x"
1158+
"stacks_block_hash": "0x",
1159+
"block_time": null,
1160+
"cycle_number": null,
1161+
"reward_set": null,
1162+
"signer_bitvec": null,
1163+
"signer_signature": null
10941164
},
10951165
"parent_block_identifier": {
10961166
"hash": "0x",

components/chainhook-sdk/src/chainhooks/tests/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::{
2424
},
2525
utils::AbstractStacksBlock,
2626
};
27+
use assert_json_diff::assert_json_eq;
2728
use chainhook_types::{
2829
StacksBlockUpdate, StacksChainEvent, StacksChainUpdatedWithBlocksData, StacksNetwork,
2930
StacksTransactionData, StacksTransactionEvent, StacksTransactionEventPayload,
@@ -821,11 +822,9 @@ fn test_stacks_hook_action_file_append() {
821822
handle_stacks_hook_action(trigger, &proofs, &EventObserverConfig::default(), &ctx).unwrap();
822823
if let StacksChainhookOccurrence::File(path, bytes) = occurrence {
823824
assert_eq!(path, "./".to_string());
824-
let json: JsonValue = serde_json::from_slice(&bytes).unwrap();
825-
let obj = json.as_object().unwrap();
826-
let actual = serde_json::to_string_pretty(obj).unwrap();
827-
let expected = get_expected_occurrence();
828-
assert_eq!(expected, actual);
825+
let actual: JsonValue = serde_json::from_slice(&bytes).unwrap();
826+
let expected: JsonValue = serde_json::from_str(&get_expected_occurrence()).unwrap();
827+
assert_json_eq!(expected, actual);
829828
} else {
830829
panic!("wrong occurrence type");
831830
}

components/chainhook-sdk/src/indexer/stacks/mod.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ pub struct NewBlock {
3535
pub transactions: Vec<NewTransaction>,
3636
pub events: Vec<NewEvent>,
3737
pub matured_miner_rewards: Vec<MaturedMinerReward>,
38+
39+
#[serde(skip_serializing_if = "Option::is_none")]
40+
pub block_time: Option<u64>,
41+
42+
#[serde(skip_serializing_if = "Option::is_none")]
43+
pub signer_bitvec: Option<String>,
44+
45+
#[serde(skip_serializing_if = "Option::is_none")]
46+
pub signer_signature: Option<Vec<String>>,
47+
48+
#[serde(skip_serializing_if = "Option::is_none")]
49+
pub cycle_number: Option<u64>,
50+
51+
#[serde(skip_serializing_if = "Option::is_none")]
52+
pub reward_set: Option<RewardSet>,
53+
}
54+
55+
#[derive(Deserialize, Serialize)]
56+
pub struct RewardSet {
57+
pub pox_ustx_threshold: String,
58+
pub rewarded_addresses: Vec<String>,
59+
#[serde(skip_serializing_if = "Option::is_none")]
60+
pub signers: Option<Vec<RewardSetSigner>>,
61+
}
62+
63+
#[derive(Deserialize, Serialize)]
64+
pub struct RewardSetSigner {
65+
pub signing_key: String,
66+
pub weight: u32,
67+
pub stacked_amt: String,
3868
}
3969

4070
#[derive(Deserialize, Serialize, Default, Clone)]
@@ -432,6 +462,29 @@ pub fn standardize_stacks_block(
432462
pox_cycle_length: pox_cycle_length.try_into().unwrap(),
433463
confirm_microblock_identifier,
434464
stacks_block_hash: block.block_hash.clone(),
465+
466+
block_time: block.block_time,
467+
// TODO: decode `signer_bitvec` into an easy to use bit string representation (e.g. "01010101")
468+
signer_bitvec: block.signer_bitvec.clone(),
469+
signer_signature: block.signer_signature.clone(),
470+
471+
cycle_number: block.cycle_number,
472+
reward_set: block.reward_set.as_ref().and_then(|r| {
473+
Some(StacksBlockMetadataRewardSet {
474+
pox_ustx_threshold: r.pox_ustx_threshold.clone(),
475+
rewarded_addresses: r.rewarded_addresses.clone(),
476+
signers: r.signers.as_ref().map(|signers| {
477+
signers
478+
.into_iter()
479+
.map(|signer| StacksBlockMetadataRewardSetSigner {
480+
signing_key: signer.signing_key.clone(),
481+
weight: signer.weight,
482+
stacked_amt: signer.stacked_amt.clone(),
483+
})
484+
.collect()
485+
}),
486+
})
487+
}),
435488
},
436489
transactions,
437490
};

components/chainhook-sdk/src/indexer/tests/helpers/stacks_blocks.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::BlockEvent;
22
use chainhook_types::{
3-
BlockIdentifier, StacksBlockData, StacksBlockMetadata, StacksTransactionData,
3+
BlockIdentifier, StacksBlockData, StacksBlockMetadata, StacksBlockMetadataRewardSet, StacksBlockMetadataRewardSetSigner, StacksTransactionData
44
};
55

66
pub fn generate_test_stacks_block(
@@ -72,6 +72,26 @@ pub fn generate_test_stacks_block(
7272
pox_cycle_length: 100,
7373
confirm_microblock_identifier,
7474
stacks_block_hash: String::new(),
75+
block_time: Some(12345),
76+
signer_bitvec: Some("1010101010101".to_owned()),
77+
signer_signature: Some(vec!["1234".to_owned(), "2345".to_owned()]),
78+
cycle_number: Some(1),
79+
reward_set: Some(StacksBlockMetadataRewardSet {
80+
pox_ustx_threshold: "50000".to_owned(),
81+
rewarded_addresses: vec![],
82+
signers: Some(vec![
83+
StacksBlockMetadataRewardSetSigner {
84+
signing_key: "0123".to_owned(),
85+
weight: 123,
86+
stacked_amt: "555555".to_owned(),
87+
},
88+
StacksBlockMetadataRewardSetSigner {
89+
signing_key: "2345".to_owned(),
90+
weight: 234,
91+
stacked_amt: "6677777".to_owned(),
92+
},
93+
]),
94+
}),
7595
},
7696
})
7797
}

0 commit comments

Comments
 (0)