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

Including rewardSet in new block event #4430

Merged
merged 7 commits into from
Mar 7, 2024
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
14 changes: 2 additions & 12 deletions stackslib/src/chainstate/coordinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use stacks_common::types::chainstate::{
use stacks_common::util::get_epoch_time_secs;

pub use self::comm::CoordinatorCommunication;
use super::stacks::boot::RewardSet;
use super::stacks::boot::{RewardSet, RewardSetData};
use super::stacks::db::blocks::DummyEventDispatcher;
use crate::burnchains::affirmation::{AffirmationMap, AffirmationMapEntry};
use crate::burnchains::bitcoin::indexer::BitcoinIndexer;
Expand Down Expand Up @@ -176,6 +176,7 @@ pub trait BlockEventDispatcher {
anchored_consumed: &ExecutionCost,
mblock_confirmed_consumed: &ExecutionCost,
pox_constants: &PoxConstants,
reward_set_data: &Option<RewardSetData>,
);

/// called whenever a burn block is about to be
Expand All @@ -190,13 +191,6 @@ pub trait BlockEventDispatcher {
burns: u64,
reward_recipients: Vec<PoxAddress>,
);

fn announce_reward_set(
&self,
reward_set: &RewardSet,
block_id: &StacksBlockId,
cycle_number: u64,
);
}

pub struct ChainsCoordinatorConfig {
Expand Down Expand Up @@ -362,10 +356,6 @@ impl<'a, T: BlockEventDispatcher> RewardSetProvider for OnChainRewardSetProvider
}
}

if let Some(dispatcher) = self.0 {
dispatcher.announce_reward_set(&reward_set, block_id, cycle);
}

Ok(reward_set)
}

Expand Down
9 changes: 1 addition & 8 deletions stackslib/src/chainstate/coordinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ impl BlockEventDispatcher for NullEventDispatcher {
_anchor_block_cost: &ExecutionCost,
_confirmed_mblock_cost: &ExecutionCost,
_pox_constants: &PoxConstants,
_reward_set_data: &Option<RewardSetData>,
) {
assert!(
false,
Expand All @@ -442,14 +443,6 @@ impl BlockEventDispatcher for NullEventDispatcher {
_slot_holders: Vec<PoxAddress>,
) {
}

fn announce_reward_set(
&self,
_reward_set: &RewardSet,
_block_id: &StacksBlockId,
_cycle_number: u64,
) {
}
}

pub fn make_coordinator<'a>(
Expand Down
40 changes: 35 additions & 5 deletions stackslib/src/chainstate/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use super::burn::db::sortdb::{
};
use super::burn::operations::{DelegateStxOp, StackStxOp, TransferStxOp};
use super::stacks::boot::{
PoxVersions, RawRewardSetEntry, RewardSet, BOOT_TEST_POX_4_AGG_KEY_CONTRACT,
PoxVersions, RawRewardSetEntry, RewardSet, RewardSetData, BOOT_TEST_POX_4_AGG_KEY_CONTRACT,
BOOT_TEST_POX_4_AGG_KEY_FNAME, SIGNERS_MAX_LIST_SIZE, SIGNERS_NAME, SIGNERS_PK_LEN,
};
use super::stacks::db::accounts::MinerReward;
Expand Down Expand Up @@ -1402,7 +1402,7 @@ impl NakamotoChainState {
return Err(e);
};

let (receipt, clarity_commit) = ok_opt.expect("FATAL: unreachable");
let (receipt, clarity_commit, reward_set_data) = ok_opt.expect("FATAL: unreachable");

assert_eq!(
receipt.header.anchored_header.block_hash(),
Expand Down Expand Up @@ -1458,6 +1458,7 @@ impl NakamotoChainState {
&receipt.anchored_block_cost,
&receipt.parent_microblocks_cost,
&pox_constants,
&reward_set_data,
);
}

Expand Down Expand Up @@ -2684,7 +2685,14 @@ impl NakamotoChainState {
block_size: u64,
burnchain_commit_burn: u64,
burnchain_sortition_burn: u64,
) -> Result<(StacksEpochReceipt, PreCommitClarityBlock<'a>), ChainstateError> {
) -> Result<
(
StacksEpochReceipt,
PreCommitClarityBlock<'a>,
Option<RewardSetData>,
),
ChainstateError,
> {
debug!(
"Process block {:?} with {} transactions",
&block.header.block_hash().to_hex(),
Expand Down Expand Up @@ -3007,8 +3015,30 @@ impl NakamotoChainState {
// NOTE: miner and proposal evaluation should not invoke this because
// it depends on knowing the StacksBlockId.
let signers_updated = signer_set_calc.is_some();
let mut reward_set_data = None;
if let Some(signer_calculation) = signer_set_calc {
Self::write_reward_set(chainstate_tx, &new_block_id, &signer_calculation.reward_set)?
Self::write_reward_set(chainstate_tx, &new_block_id, &signer_calculation.reward_set)?;

let cycle_number = if let Some(cycle) = pox_constants.reward_cycle_of_prepare_phase(
first_block_height.into(),
chain_tip_burn_header_height.into(),
) {
Some(cycle)
} else {
pox_constants
.block_height_to_reward_cycle(
first_block_height.into(),
chain_tip_burn_header_height.into(),
)
.map(|cycle| cycle + 1)
};

if let Some(cycle) = cycle_number {
reward_set_data = Some(RewardSetData::new(
signer_calculation.reward_set.clone(),
cycle,
));
}
}

monitoring::set_last_block_transaction_count(u64::try_from(block.txs.len()).unwrap());
Expand Down Expand Up @@ -3050,7 +3080,7 @@ impl NakamotoChainState {
signers_updated,
};

Ok((epoch_receipt, clarity_commit))
Ok((epoch_receipt, clarity_commit, reward_set_data))
}

/// Create a StackerDB config for the .miners contract.
Expand Down
14 changes: 14 additions & 0 deletions stackslib/src/chainstate/stacks/boot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ pub struct RewardSet {
pub signers: Option<Vec<NakamotoSignerEntry>>,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct RewardSetData {
pub reward_set: RewardSet,
pub cycle_number: u64,
}
const POX_CYCLE_START_HANDLED_VALUE: &'static str = "1";

impl PoxStartCycleInfo {
Expand Down Expand Up @@ -269,6 +274,15 @@ impl RewardSet {
}
}

impl RewardSetData {
pub fn new(reward_set: RewardSet, cycle_number: u64) -> RewardSetData {
RewardSetData {
reward_set,
cycle_number,
}
}
}

impl StacksChainState {
/// Return the MARF key used to store whether or not a given PoX
/// cycle's "start" has been handled by the Stacks fork yet. This
Expand Down
54 changes: 37 additions & 17 deletions stackslib/src/chainstate/stacks/db/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl BlockEventDispatcher for DummyEventDispatcher {
_anchor_block_cost: &ExecutionCost,
_confirmed_mblock_cost: &ExecutionCost,
_pox_constants: &PoxConstants,
_reward_set_data: &Option<RewardSetData>,
) {
assert!(
false,
Expand All @@ -201,18 +202,6 @@ impl BlockEventDispatcher for DummyEventDispatcher {
"We should never try to announce to the dummy dispatcher"
);
}

fn announce_reward_set(
&self,
_reward_set: &RewardSet,
_block_id: &StacksBlockId,
_cycle_number: u64,
) {
assert!(
false,
"We should never try to announce to the dummy dispatcher"
);
}
}

impl MemPoolRejection {
Expand Down Expand Up @@ -5209,7 +5198,14 @@ impl StacksChainState {
burnchain_sortition_burn: u64,
affirmation_weight: u64,
do_not_advance: bool,
) -> Result<(StacksEpochReceipt, PreCommitClarityBlock<'a>), Error> {
) -> Result<
(
StacksEpochReceipt,
PreCommitClarityBlock<'a>,
Option<RewardSetData>,
),
Error,
> {
debug!(
"Process block {:?} with {} transactions",
&block.block_hash().to_hex(),
Expand Down Expand Up @@ -5596,7 +5592,7 @@ impl StacksChainState {
signers_updated: false,
};

return Ok((epoch_receipt, clarity_commit));
return Ok((epoch_receipt, clarity_commit, None));
}

let parent_block_header = parent_chain_tip
Expand Down Expand Up @@ -5632,13 +5628,36 @@ impl StacksChainState {
// NOTE: miner and proposal evaluation should not invoke this because
// it depends on knowing the StacksBlockId.
let signers_updated = signer_set_calc.is_some();
let mut reward_set_data = None;
if let Some(signer_calculation) = signer_set_calc {
let new_block_id = new_tip.index_block_hash();
NakamotoChainState::write_reward_set(
chainstate_tx,
&new_block_id,
&signer_calculation.reward_set,
)?
)?;

let first_block_height = burn_dbconn.get_burn_start_height();
let cycle_number = if let Some(cycle) = pox_constants.reward_cycle_of_prepare_phase(
first_block_height.into(),
parent_burn_block_height.into(),
) {
Some(cycle)
} else {
pox_constants
.block_height_to_reward_cycle(
first_block_height.into(),
parent_burn_block_height.into(),
)
.map(|cycle| cycle + 1)
};

if let Some(cycle) = cycle_number {
reward_set_data = Some(RewardSetData::new(
signer_calculation.reward_set.clone(),
cycle,
));
}
}

set_last_block_transaction_count(
Expand All @@ -5661,7 +5680,7 @@ impl StacksChainState {
signers_updated,
};

Ok((epoch_receipt, clarity_commit))
Ok((epoch_receipt, clarity_commit, reward_set_data))
}

/// Verify that a Stacks anchored block attaches to its parent anchored block.
Expand Down Expand Up @@ -5986,7 +6005,7 @@ impl StacksChainState {
// Execute the confirmed microblocks' transactions against the chain state, and then
// execute the anchored block's transactions against the chain state.
let pox_constants = sort_tx.context.pox_constants.clone();
let (epoch_receipt, clarity_commit) = match StacksChainState::append_block(
let (epoch_receipt, clarity_commit, reward_set_data) = match StacksChainState::append_block(
&mut chainstate_tx,
clarity_instance,
sort_tx,
Expand Down Expand Up @@ -6122,6 +6141,7 @@ impl StacksChainState {
&epoch_receipt.anchored_block_cost,
&epoch_receipt.parent_microblocks_cost,
&pox_constants,
&reward_set_data,
);
}

Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1717,7 +1717,7 @@ fn replay_block(stacks_path: &str, index_block_hash_hex: &str) {
block_am.weight(),
true,
) {
Ok((_receipt, _)) => {
Ok((_receipt, _, _)) => {
info!("Block processed successfully! block = {index_block_hash}");
}
Err(e) => {
Expand Down
10 changes: 1 addition & 9 deletions stackslib/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,7 @@ pub mod test {
_anchor_block_cost: &ExecutionCost,
_confirmed_mblock_cost: &ExecutionCost,
pox_constants: &PoxConstants,
reward_set_data: &Option<RewardSetData>,
) {
self.blocks.lock().unwrap().push(TestEventObserverBlock {
block: block.clone(),
Expand All @@ -1922,15 +1923,6 @@ pub mod test {
) {
// pass
}

fn announce_reward_set(
&self,
_reward_set: &RewardSet,
_block_id: &StacksBlockId,
_cycle_number: u64,
) {
// pass
}
}

// describes a peer's initial configuration
Expand Down
5 changes: 0 additions & 5 deletions testnet/stacks-node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2466,7 +2466,6 @@ pub enum EventKeyType {
MinedMicroblocks,
StackerDBChunks,
BlockProposal,
StackerSet,
}

impl EventKeyType {
Expand Down Expand Up @@ -2499,10 +2498,6 @@ impl EventKeyType {
return Some(EventKeyType::BlockProposal);
}

if raw_key == "stacker_set" {
return Some(EventKeyType::StackerSet);
}

let comps: Vec<_> = raw_key.split("::").collect();
if comps.len() == 1 {
let split: Vec<_> = comps[0].split('.').collect();
Expand Down
Loading