Skip to content

Commit

Permalink
WIP: update balances to force reward slot calculations to work
Browse files Browse the repository at this point in the history
Signed-off-by: Jacinta Ferrant <[email protected]>
  • Loading branch information
jferrant committed Mar 26, 2024
1 parent f2e6c05 commit ef4aed1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 57 deletions.
38 changes: 0 additions & 38 deletions stackslib/src/chainstate/nakamoto/coordinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,44 +223,6 @@ pub fn make_all_signers_vote_for_aggregate_key(
.collect()
}

// Create a peer configured for for testing Nakamoto functionality
pub fn nakamoto_peer<'a>(
test_name: &str,
mut initial_balances: Vec<(PrincipalData, u64)>,
test_signers: &mut TestSigners,
observer: Option<&'a TestEventObserver>,
) -> TestPeer<'a> {
let aggregate_public_key = test_signers.aggregate_public_key.clone();
let mut peer_config = TestPeerConfig::new(test_name, 0, 0);
let private_key = peer_config.private_key.clone();
let addr = StacksAddress::from_public_keys(
C32_ADDRESS_VERSION_TESTNET_SINGLESIG,
&AddressHashMode::SerializeP2PKH,
1,
&vec![StacksPublicKey::from_private(&private_key)],
)
.unwrap();

// reward cycles are 5 blocks long
// first 25 blocks are boot-up
// reward cycle 6 instantiates pox-3
// we stack in reward cycle 7 so pox-3 is evaluated to find reward set participation
peer_config.aggregate_public_key = Some(aggregate_public_key.clone());
peer_config
.stacker_dbs
.push(boot_code_id(MINERS_NAME, false));
peer_config.epochs = Some(StacksEpoch::unit_test_3_0_only(37));
peer_config.initial_balances = vec![(addr.to_account_principal(), 1_000_000_000_000_000_000)];

peer_config.initial_balances.append(&mut initial_balances);
peer_config.burnchain.pox_constants.v2_unlock_height = 21;
peer_config.burnchain.pox_constants.pox_3_activation_height = 26;
peer_config.burnchain.pox_constants.v3_unlock_height = 27;
peer_config.burnchain.pox_constants.pox_4_activation_height = 31;
peer_config.test_signers = Some(test_signers.clone());
TestPeer::new_with_observer(peer_config, observer)
}

/// Make a peer and transition it into the Nakamoto epoch.
/// The node needs to be stacking and it needs to vote for an aggregate key;
/// otherwise, Nakamoto can't activate.
Expand Down
2 changes: 2 additions & 0 deletions stackslib/src/chainstate/nakamoto/signer_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ impl NakamotoSigners {
.collect()
};

debug!("PRINTING THE SIGNER LIST: {signers_list:?}");

if signers_list.len() > SIGNERS_MAX_LIST_SIZE {
panic!(
"FATAL: signers list returned by reward set calculations longer than maximum ({} > {})",
Expand Down
12 changes: 12 additions & 0 deletions stackslib/src/chainstate/stacks/boot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,11 @@ impl StacksChainState {
..
}) = addresses.pop()
{
debug!(
"Processing reward set entry";
"reward_address" => %address,
"stacked_amt" => stacked_amt,
);
let mut contributed_stackers = vec![];
if let Some(stacker) = stacker.as_ref() {
contributed_stackers.push((stacker.clone(), stacked_amt));
Expand All @@ -792,6 +797,12 @@ impl StacksChainState {
// vector are sorted by address, we know that any entry
// with the same `reward_address` as `address` will be at the end of
// the list (and therefore found by this loop)
println!("SOME ADDRESS {:?}", addresses.last().map(|x| {
match &x.reward_address {
PoxAddress::Standard(address, _) => address.to_string(),
_ => panic!("Not making sense"),
}
} ));
while addresses.last().map(|x| &x.reward_address) == Some(&address) {
let next_contrib = addresses
.pop()
Expand All @@ -805,6 +816,7 @@ impl StacksChainState {
stacked_amt = stacked_amt
.checked_add(additional_amt)
.expect("CORRUPTION: Stacker stacked > u128 max amount");
debug!("New Stacked Amount: {stacked_amt}");
}
let slots_taken = u32::try_from(stacked_amt / threshold)
.expect("CORRUPTION: Stacker claimed > u32::max() reward slots");
Expand Down
89 changes: 70 additions & 19 deletions stackslib/src/chainstate/stacks/boot/pox_4_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ use crate::chainstate::burn::db::sortdb::SortitionDB;
use crate::chainstate::burn::operations::*;
use crate::chainstate::burn::{BlockSnapshot, ConsensusHash};
use crate::chainstate::coordinator::tests::pox_addr_from;
use crate::chainstate::nakamoto::coordinator::tests::nakamoto_peer;
use crate::chainstate::nakamoto::test_signers::TestSigners;
use crate::chainstate::nakamoto::tests::node::TestStacker;
use crate::chainstate::stacks::address::{PoxAddress, PoxAddressType20, PoxAddressType32};
Expand All @@ -68,7 +67,7 @@ use crate::chainstate::stacks::boot::signers_tests::{
use crate::chainstate::stacks::boot::signers_voting_tests::{make_dummy_tx, nakamoto_tenure};
use crate::chainstate::stacks::boot::{
PoxVersions, BOOT_CODE_COST_VOTING_TESTNET as BOOT_CODE_COST_VOTING, BOOT_CODE_POX_TESTNET,
POX_2_NAME, POX_3_NAME,
MINERS_NAME, POX_2_NAME, POX_3_NAME,
};
use crate::chainstate::stacks::db::{
MinerPaymentSchedule, StacksChainState, StacksHeaderInfo, MINER_REWARD_MATURITY,
Expand All @@ -82,7 +81,7 @@ use crate::clarity_vm::clarity::{ClarityBlockConnection, Error as ClarityError};
use crate::clarity_vm::database::marf::{MarfedKV, WritableMarfStore};
use crate::clarity_vm::database::HeadersDBConn;
use crate::core::*;
use crate::net::test::{TestEventObserver, TestPeer};
use crate::net::test::{TestEventObserver, TestPeer, TestPeerConfig};
use crate::util_lib::boot::boot_code_id;
use crate::util_lib::db::{DBConn, FromRow};
use crate::util_lib::signed_structured_data::pox4::Pox4SignatureTopic;
Expand Down Expand Up @@ -6092,9 +6091,9 @@ impl StackerSignerInfo {
#[test]
fn test_scenario_five() {
// Alice service signer setup
let mut alice = StackerSignerInfo::new();
let alice = StackerSignerInfo::new();
// Bob service signer setup
let mut bob = StackerSignerInfo::new();
let bob = StackerSignerInfo::new();
// Carl solo stacker and signer setup
let mut carl = StackerSignerInfo::new();
// David stacking pool operator (delegating signing to Alice) Setup
Expand Down Expand Up @@ -6127,7 +6126,7 @@ fn test_scenario_five() {
let default_initial_balances = 1_000_000_000_000_000_000;
let observer = TestEventObserver::new();
let mut test_signers = TestSigners::default();
let initial_balances = vec![
let mut initial_balances = vec![
(alice.principal.clone(), default_initial_balances),
(bob.principal.clone(), default_initial_balances),
(carl.principal.clone(), default_initial_balances),
Expand All @@ -6140,12 +6139,36 @@ fn test_scenario_five() {
(jude.principal.clone(), default_initial_balances),
(mallory.principal.clone(), default_initial_balances),
];
let mut peer = nakamoto_peer(
function_name!(),
initial_balances,
&mut test_signers,
Some(&observer),
);
let aggregate_public_key = test_signers.aggregate_public_key.clone();
let mut peer_config = TestPeerConfig::new(function_name!(), 0, 0);
let private_key = peer_config.private_key.clone();
let addr = StacksAddress::from_public_keys(
C32_ADDRESS_VERSION_TESTNET_SINGLESIG,
&AddressHashMode::SerializeP2PKH,
1,
&vec![StacksPublicKey::from_private(&private_key)],
)
.unwrap();

// reward cycles are 5 blocks long
// first 25 blocks are boot-up
// reward cycle 6 instantiates pox-3
// we stack in reward cycle 7 so pox-3 is evaluated to find reward set participation
peer_config.aggregate_public_key = Some(aggregate_public_key.clone());
peer_config
.stacker_dbs
.push(boot_code_id(MINERS_NAME, false));
peer_config.epochs = Some(StacksEpoch::unit_test_3_0_only(141));
peer_config.initial_balances = vec![(addr.to_account_principal(), 1_000_000_000_000_000_000)];
peer_config.initial_balances.append(&mut initial_balances);
peer_config.burnchain.pox_constants.v2_unlock_height = 81;
peer_config.burnchain.pox_constants.pox_3_activation_height = 101;
peer_config.burnchain.pox_constants.v3_unlock_height = 102;
peer_config.burnchain.pox_constants.pox_4_activation_height = 105;
peer_config.test_signers = Some(test_signers.clone());
peer_config.burnchain.pox_constants.reward_cycle_length = 20;
peer_config.burnchain.pox_constants.prepare_length = 5;
let mut peer = TestPeer::new_with_observer(peer_config, Some(&observer));

let mut peer_nonce = 0;
// Advance into pox4
Expand Down Expand Up @@ -6230,7 +6253,7 @@ fn test_scenario_five() {
&carl.private_key,
carl.nonce,
default_initial_balances.saturating_sub(1) as u128,
&carl.pox_address.clone(),
&carl.pox_address,
carl_lock_period,
&carl.public_key,
burn_block_height,
Expand All @@ -6244,7 +6267,7 @@ fn test_scenario_five() {
let frank_delegate_stx_to_david_tx = make_pox_4_delegate_stx(
&frank.private_key,
frank.nonce,
min_ustx,
default_initial_balances.saturating_sub(1) as u128,
david.principal.clone(),
None,
Some(david.pox_address.clone()),
Expand All @@ -6255,7 +6278,7 @@ fn test_scenario_five() {
let grace_delegate_stx_to_david_tx = make_pox_4_delegate_stx(
&grace.private_key,
grace.nonce,
min_ustx,
default_initial_balances.saturating_sub(1) as u128,
david.principal.clone(),
None,
Some(david.pox_address.clone()),
Expand Down Expand Up @@ -6288,7 +6311,7 @@ fn test_scenario_five() {
let jude_delegate_stx_to_eve_tx = make_pox_4_delegate_stx(
&jude.private_key,
jude.nonce,
min_ustx,
default_initial_balances.saturating_sub(1) as u128,
eve.principal.clone(),
None,
Some(eve.pox_address.clone()),
Expand All @@ -6315,7 +6338,7 @@ fn test_scenario_five() {
&david.private_key,
david.nonce,
stacker.principal.clone(),
min_ustx,
default_initial_balances.saturating_sub(1) as u128,
david.pox_address.clone(),
burn_block_height as u128,
1, // Must be called every reward cycle, therefore only ever lasts for 1 lock period
Expand All @@ -6332,7 +6355,7 @@ fn test_scenario_five() {
&eve.private_key,
eve.nonce,
stacker.principal.clone(),
min_ustx,
default_initial_balances.saturating_sub(1) as u128,
eve.pox_address.clone(),
burn_block_height as u128,
1, // Must be called every reward cycle, therefore only ever lasts for 1 lock period
Expand Down Expand Up @@ -6420,6 +6443,13 @@ fn test_scenario_five() {
epoch_3_reward_cycle_boundary.saturating_sub(prepare_phase_len as u64);
let epoch_3_reward_set_calculation = epoch_3_reward_set_calculation_boundary.wrapping_add(2); // Make it to reward set calculation
let target_height = epoch_3_reward_set_calculation;
debug!("Epoch 3.0 info";
"epoch_3_start_height" => epoch_3_start_height,
"epoch_3_reward_cycle_boundary" => epoch_3_reward_cycle_boundary,
"epoch_3_boundary_reward_cycle" => epoch_3_boundary_reward_cycle,
"epoch_3_reward_set_calculation_boundary" => epoch_3_reward_set_calculation_boundary,
"epoch_3_reward_set_calculation" => epoch_3_reward_set_calculation
);
info!("Advancing to reward set calculation at burn block height: {target_height}");
let mut latest_block = None;
let mut passed_txs = txs.clone();
Expand All @@ -6430,10 +6460,25 @@ fn test_scenario_five() {
passed_txs = vec![];
if tx_block.is_none() {
tx_block = Some(observer.get_blocks().last().unwrap().clone());
for stacker in davids_stackers {
let (pox_address, first_reward_cycle, _lock_period, _indices) =
get_stacker_info_pox_4(&mut peer, &stacker.principal)
.expect("Failed to find stacker");
assert_eq!(first_reward_cycle, next_reward_cycle);
assert_eq!(pox_address, david.pox_address);
}

for stacker in eves_stackers {
let (pox_address, first_reward_cycle, _lock_period, _indices) =
get_stacker_info_pox_4(&mut peer, &stacker.principal)
.expect("Failed to find stacker");
assert_eq!(first_reward_cycle, next_reward_cycle);
assert_eq!(pox_address, eve.pox_address);
}
}
}
let latest_block = latest_block.expect("Failed to get tip");
let tx_block = tx_block.expect("Failed to get tx_block");
let mut latest_block = latest_block.expect("Failed to get latest_block");
info!("Verifying stacking txs");
let mut observed_txs = HashSet::new();
for tx_receipt in &tx_block.receipts {
Expand All @@ -6449,6 +6494,7 @@ fn test_scenario_five() {
}
}

debug!("Checking stacking set");
let cycle_id = next_reward_cycle;
// Confirm that the stacking set contains all stackers
let (pox_address, first_reward_cycle, _lock_period, _indices) =
Expand All @@ -6470,6 +6516,11 @@ fn test_scenario_five() {
assert_eq!(pox_address, eve.pox_address);
}

debug!("Current info";
"reward_cycle" => peer.get_reward_cycle(),
"burn_block_height" => peer.get_burn_block_height()
);

// create vote txs
let alice_index = get_signer_index(&mut peer, latest_block, alice.address.clone(), cycle_id);
let bob_index = get_signer_index(&mut peer, latest_block, bob.address.clone(), cycle_id);
Expand Down

0 comments on commit ef4aed1

Please sign in to comment.