Skip to content

Commit bffb1aa

Browse files
authored
Merge pull request #5772 from stacks-network/feat/vrf-seed-in-get-sortition-rpc-endpoint
Feat/vrf seed in get sortition rpc endpoint
2 parents 3730ca3 + 32db942 commit bffb1aa

File tree

6 files changed

+26
-3
lines changed

6 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE
99

1010
### Added
1111

12+
- Add `vrf_seed` to the `/v3/sortitions` rpc endpoint
13+
1214
### Changed
1315

1416
### Fixed

docs/rpc/api/core-node/get_sortitions.example.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"miner_pk_hash160": "0x6bc51b33e9f3626944eb879147e18111581f8f9b",
1111
"stacks_parent_ch": "0x697357c72da55b759b1d6b721676c92c69f0b490",
1212
"last_sortition_ch": "0x697357c72da55b759b1d6b721676c92c69f0b490",
13-
"committed_block_hash": "0xeea47d6d639c565027110e192e308fb11656183d5c077bcd718d830652800183"
13+
"committed_block_hash": "0xeea47d6d639c565027110e192e308fb11656183d5c077bcd718d830652800183",
14+
"vrf_seed": "0x48b754acc291a5bfad1354ee19bbc471f14af2b21dc7eccc0f929bd16798defe"
1415
}
1516
]

stacks-signer/src/tests/chainstate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ fn check_proposal_refresh() {
557557
stacks_parent_ch: Some(view.cur_sortition.parent_tenure_id),
558558
last_sortition_ch: Some(view.cur_sortition.parent_tenure_id),
559559
committed_block_hash: None,
560+
vrf_seed: None,
560561
},
561562
SortitionInfo {
562563
burn_block_hash: BurnchainHeaderHash([128; 32]),
@@ -570,6 +571,7 @@ fn check_proposal_refresh() {
570571
stacks_parent_ch: Some(view.cur_sortition.parent_tenure_id),
571572
last_sortition_ch: Some(view.cur_sortition.parent_tenure_id),
572573
committed_block_hash: None,
574+
vrf_seed: None,
573575
},
574576
];
575577

stackslib/src/net/api/getsortition.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use std::io::{Read, Seek, SeekFrom, Write};
1717
use std::{fs, io};
1818

19+
use clarity::types::chainstate::VRFSeed;
1920
use regex::{Captures, Regex};
2021
use serde::de::Error as de_Error;
22+
use serde::Serialize;
2123
use stacks_common::codec::{StacksMessageCodec, MAX_MESSAGE_LEN};
2224
use stacks_common::types::chainstate::{
2325
BlockHeaderHash, BurnchainHeaderHash, ConsensusHash, SortitionId, StacksBlockId,
@@ -111,6 +113,9 @@ pub struct SortitionInfo {
111113
/// In Stacks 2.x, this is the winning block.
112114
/// In Stacks 3.x, this is the first block of the parent tenure.
113115
pub committed_block_hash: Option<BlockHeaderHash>,
116+
#[serde(with = "prefix_opt_hex")]
117+
/// This is the VRF seed generated by this sortition
118+
pub vrf_seed: Option<VRFSeed>,
114119
}
115120

116121
impl TryFrom<(&str, &str)> for QuerySpecifier {
@@ -163,12 +168,12 @@ impl GetSortitionHandler {
163168
let is_shadow = chainstate
164169
.nakamoto_blocks_db()
165170
.is_shadow_tenure(&sortition_sn.consensus_hash)?;
166-
let (miner_pk_hash160, stacks_parent_ch, committed_block_hash, last_sortition_ch) =
171+
let (miner_pk_hash160, stacks_parent_ch, committed_block_hash, last_sortition_ch, vrf_seed) =
167172
if !sortition_sn.sortition && !is_shadow {
168173
let handle = sortdb.index_handle(&sortition_sn.sortition_id);
169174
let last_sortition =
170175
handle.get_last_snapshot_with_sortition(sortition_sn.block_height)?;
171-
(None, None, None, Some(last_sortition.consensus_hash))
176+
(None, None, None, Some(last_sortition.consensus_hash), None)
172177
} else if !sortition_sn.sortition && is_shadow {
173178
// this is a shadow tenure.
174179
let parent_tenure_ch = chainstate
@@ -191,6 +196,7 @@ impl GetSortitionHandler {
191196
parent_tenure_start_header.index_block_hash().0,
192197
)),
193198
Some(parent_tenure_ch),
199+
None,
194200
)
195201
} else {
196202
let block_commit = SortitionDB::get_block_commit(sortdb.conn(), &sortition_sn.winning_block_txid, &sortition_sn.sortition_id)?
@@ -236,6 +242,7 @@ impl GetSortitionHandler {
236242
Some(stacks_parent_sn.consensus_hash),
237243
Some(block_commit.block_header_hash),
238244
Some(last_sortition_ch),
245+
Some(block_commit.new_seed),
239246
)
240247
};
241248

@@ -251,6 +258,7 @@ impl GetSortitionHandler {
251258
stacks_parent_ch,
252259
last_sortition_ch,
253260
committed_block_hash,
261+
vrf_seed,
254262
})
255263
}
256264
}

stackslib/src/net/api/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17+
use clarity::types::chainstate::VRFSeed;
1718
use clarity::vm::costs::ExecutionCost;
1819
use stacks_common::codec::read_next;
1920
use stacks_common::types::chainstate::{
@@ -239,6 +240,7 @@ macro_rules! impl_hex_deser {
239240
impl_hex_deser!(BurnchainHeaderHash);
240241
impl_hex_deser!(StacksBlockId);
241242
impl_hex_deser!(SortitionId);
243+
impl_hex_deser!(VRFSeed);
242244
impl_hex_deser!(ConsensusHash);
243245
impl_hex_deser!(BlockHeaderHash);
244246
impl_hex_deser!(Hash160);

stackslib/src/net/api/tests/getsortition.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use std::collections::BTreeMap;
1717
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
1818

19+
use clarity::types::chainstate::VRFSeed;
1920
use stacks_common::types::chainstate::{BurnchainHeaderHash, ConsensusHash};
2021
use stacks_common::types::net::PeerHost;
2122

@@ -159,6 +160,13 @@ fn response() {
159160
let second_entry: SortitionInfo = serde_json::from_value(info_array[1].clone())
160161
.expect("Response array elements should parse to SortitionInfo");
161162
assert!(first_entry.was_sortition);
163+
assert_eq!(
164+
first_entry.vrf_seed,
165+
Some(
166+
VRFSeed::from_hex("48b754acc291a5bfad1354ee19bbc471f14af2b21dc7eccc0f929bd16798defe")
167+
.unwrap()
168+
)
169+
);
162170
assert!(second_entry.was_sortition);
163171
assert_eq!(
164172
first_entry.last_sortition_ch.as_ref().unwrap(),

0 commit comments

Comments
 (0)