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

Feat/vrf seed in get sortition rpc endpoint #5772

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions stacks-signer/src/tests/chainstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ fn check_proposal_refresh() {
stacks_parent_ch: Some(view.cur_sortition.parent_tenure_id),
last_sortition_ch: Some(view.cur_sortition.parent_tenure_id),
committed_block_hash: None,
vrf_seed: None,
},
SortitionInfo {
burn_block_hash: BurnchainHeaderHash([128; 32]),
Expand All @@ -569,6 +570,7 @@ fn check_proposal_refresh() {
stacks_parent_ch: Some(view.cur_sortition.parent_tenure_id),
last_sortition_ch: Some(view.cur_sortition.parent_tenure_id),
committed_block_hash: None,
vrf_seed: None,
},
];

Expand Down
12 changes: 10 additions & 2 deletions stackslib/src/net/api/getsortition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use std::io::{Read, Seek, SeekFrom, Write};
use std::{fs, io};

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

impl TryFrom<(&str, &str)> for QuerySpecifier {
Expand Down Expand Up @@ -163,12 +168,12 @@ impl GetSortitionHandler {
let is_shadow = chainstate
.nakamoto_blocks_db()
.is_shadow_tenure(&sortition_sn.consensus_hash)?;
let (miner_pk_hash160, stacks_parent_ch, committed_block_hash, last_sortition_ch) =
let (miner_pk_hash160, stacks_parent_ch, committed_block_hash, last_sortition_ch, vrf_seed) =
if !sortition_sn.sortition && !is_shadow {
let handle = sortdb.index_handle(&sortition_sn.sortition_id);
let last_sortition =
handle.get_last_snapshot_with_sortition(sortition_sn.block_height)?;
(None, None, None, Some(last_sortition.consensus_hash))
(None, None, None, Some(last_sortition.consensus_hash), None)
} else if !sortition_sn.sortition && is_shadow {
// this is a shadow tenure.
let parent_tenure_ch = chainstate
Expand All @@ -191,6 +196,7 @@ impl GetSortitionHandler {
parent_tenure_start_header.index_block_hash().0,
)),
Some(parent_tenure_ch),
None,
)
} else {
let block_commit = SortitionDB::get_block_commit(sortdb.conn(), &sortition_sn.winning_block_txid, &sortition_sn.sortition_id)?
Expand Down Expand Up @@ -236,6 +242,7 @@ impl GetSortitionHandler {
Some(stacks_parent_sn.consensus_hash),
Some(block_commit.block_header_hash),
Some(last_sortition_ch),
Some(block_commit.new_seed),
)
};

Expand All @@ -251,6 +258,7 @@ impl GetSortitionHandler {
stacks_parent_ch,
last_sortition_ch,
committed_block_hash,
vrf_seed,
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions stackslib/src/net/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use clarity::types::chainstate::VRFSeed;
use clarity::vm::costs::ExecutionCost;
use stacks_common::codec::read_next;
use stacks_common::types::chainstate::{
Expand Down Expand Up @@ -239,6 +240,7 @@ macro_rules! impl_hex_deser {
impl_hex_deser!(BurnchainHeaderHash);
impl_hex_deser!(StacksBlockId);
impl_hex_deser!(SortitionId);
impl_hex_deser!(VRFSeed);
impl_hex_deser!(ConsensusHash);
impl_hex_deser!(BlockHeaderHash);
impl_hex_deser!(Hash160);
8 changes: 8 additions & 0 deletions stackslib/src/net/api/tests/getsortition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use std::collections::BTreeMap;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use clarity::types::chainstate::VRFSeed;
use stacks_common::types::chainstate::{BurnchainHeaderHash, ConsensusHash};
use stacks_common::types::net::PeerHost;

Expand Down Expand Up @@ -159,6 +160,13 @@ fn response() {
let second_entry: SortitionInfo = serde_json::from_value(info_array[1].clone())
.expect("Response array elements should parse to SortitionInfo");
assert!(first_entry.was_sortition);
assert_eq!(
first_entry.vrf_seed,
Some(
VRFSeed::from_hex("48b754acc291a5bfad1354ee19bbc471f14af2b21dc7eccc0f929bd16798defe")
.unwrap()
)
);
assert!(second_entry.was_sortition);
assert_eq!(
first_entry.last_sortition_ch.as_ref().unwrap(),
Expand Down
Loading