Skip to content

Commit

Permalink
wip: .signers contract with basic write
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJanssen committed Jan 18, 2024
1 parent 3e21883 commit 5c8f091
Show file tree
Hide file tree
Showing 12 changed files with 739 additions and 63 deletions.
11 changes: 11 additions & 0 deletions stackslib/src/burnchains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,17 @@ impl PoxConstants {
(effective_height % u64::from(self.reward_cycle_length)) == 1
}

pub fn is_prepare_phase_start(&self, first_block_height: u64, burn_height: u64) -> bool {
if burn_height < first_block_height {
false
} else {
let effective_height = burn_height - first_block_height;
(effective_height + u64::from(self.prepare_length))
% u64::from(self.reward_cycle_length)
== 0
}
}

pub fn reward_cycle_to_block_height(&self, first_block_height: u64, reward_cycle: u64) -> u64 {
// NOTE: the `+ 1` is because the height of the first block of a reward cycle is mod 1, not
// mod 0.
Expand Down
129 changes: 92 additions & 37 deletions stackslib/src/chainstate/nakamoto/coordinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ use stacks_common::types::chainstate::{
StacksAddress, StacksBlockId, StacksPrivateKey, StacksPublicKey,
};
use stacks_common::types::{Address, StacksEpoch};
use stacks_common::util::secp256k1::Secp256k1PrivateKey;
use stacks_common::util::vrf::VRFProof;
use wsts::curve::point::Point;

use crate::chainstate::burn::db::sortdb::{SortitionDB, SortitionHandle};
use crate::chainstate::burn::operations::BlockstackOperationType;
use crate::chainstate::coordinator::tests::p2pkh_from;
use crate::chainstate::nakamoto::tests::get_account;
use crate::chainstate::nakamoto::tests::node::TestSigners;
use crate::chainstate::nakamoto::tests::node::{TestSigners, TestStacker};
use crate::chainstate::nakamoto::{NakamotoBlock, NakamotoChainState};
use crate::chainstate::stacks::address::PoxAddress;
use crate::chainstate::stacks::boot::test::{make_pox_4_aggregate_key, make_pox_4_lockup};
use crate::chainstate::stacks::boot::test::{
key_to_stacks_addr, make_pox_4_aggregate_key, make_pox_4_lockup,
};
use crate::chainstate::stacks::db::{MinerPaymentTxFees, StacksAccount, StacksChainState};
use crate::chainstate::stacks::{
CoinbasePayload, StacksTransaction, StacksTransactionSigner, TenureChangeCause,
Expand All @@ -44,18 +47,16 @@ use crate::chainstate::stacks::{
use crate::clarity::vm::types::StacksAddressExtensions;
use crate::core::StacksEpochExtension;
use crate::net::relay::Relayer;
use crate::net::test::{TestPeer, TestPeerConfig};
use crate::net::test::{TestEventObserver, TestPeer, TestPeerConfig};

/// Bring a TestPeer into the Nakamoto Epoch
fn advance_to_nakamoto(peer: &mut TestPeer) {
fn advance_to_nakamoto(
peer: &mut TestPeer,
test_signers: &TestSigners,
test_stackers: Vec<&TestStacker>,
) {
let mut peer_nonce = 0;
let private_key = peer.config.private_key.clone();
let signer_key = StacksPublicKey::from_slice(&[
0x02, 0xb6, 0x19, 0x6d, 0xe8, 0x8b, 0xce, 0xe7, 0x93, 0xfa, 0x9a, 0x8a, 0x85, 0x96, 0x9b,
0x64, 0x7f, 0x84, 0xc9, 0x0e, 0x9d, 0x13, 0xf9, 0xc8, 0xb8, 0xce, 0x42, 0x6c, 0xc8, 0x1a,
0x59, 0x98, 0x3c,
])
.unwrap();
let addr = StacksAddress::from_public_keys(
C32_ADDRESS_VERSION_TESTNET_SINGLESIG,
&AddressHashMode::SerializeP2PKH,
Expand All @@ -67,17 +68,24 @@ fn advance_to_nakamoto(peer: &mut TestPeer) {
for sortition_height in 0..11 {
// stack to pox-3 in cycle 7
let txs = if sortition_height == 6 {
// stack them all
let stack_tx = make_pox_4_lockup(
&private_key,
0,
1_000_000_000_000_000_000,
PoxAddress::from_legacy(AddressHashMode::SerializeP2PKH, addr.bytes.clone()),
12,
signer_key,
34,
);
vec![stack_tx]
// Make all the test Stackers stack
test_stackers
.iter()
.map(|test_stacker| {
make_pox_4_lockup(
&test_stacker.stacker_private_key,
0,
test_stacker.amount,
PoxAddress::from_legacy(
AddressHashMode::SerializeP2PKH,
addr.bytes.clone(),
),
12,
StacksPublicKey::from_private(&test_stacker.signer_private_key),
34,
)
})
.collect()
} else {
vec![]
};
Expand All @@ -90,11 +98,13 @@ fn advance_to_nakamoto(peer: &mut TestPeer) {

/// Make a peer and transition it into the Nakamoto epoch.
/// The node needs to be stacking; otherwise, Nakamoto won't activate.
pub fn boot_nakamoto(
pub fn boot_nakamoto<'a>(
test_name: &str,
mut initial_balances: Vec<(PrincipalData, u64)>,
aggregate_public_key: Point,
) -> TestPeer {
test_signers: &TestSigners,
test_stackers: Option<Vec<&TestStacker>>,
) -> TestPeer<'a> {
let aggregate_public_key = test_signers.aggregate_public_key.clone();
let mut peer_config = TestPeerConfig::new(test_name, 0, 0);
peer_config.aggregate_public_key = Some(aggregate_public_key.clone());
let private_key = peer_config.private_key.clone();
Expand All @@ -112,14 +122,46 @@ pub fn boot_nakamoto(
// we stack in reward cycle 7 so pox-3 is evaluated to find reward set participation
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)];

let test_stackers: Vec<TestStacker> = if let Some(stackers) = test_stackers {
stackers.into_iter().cloned().collect()
} else {
// Create a list of test Stackers and their signer keys
(0..test_signers.num_keys)
.map(|index| {
let stacker_private_key = StacksPrivateKey::from_seed(&index.to_be_bytes());
let signer_private_key = StacksPrivateKey::from_seed(&(index + 1000).to_be_bytes());
TestStacker {
stacker_private_key,
signer_private_key,
amount: 1_000_000_000_000_000_000,
}
})
.collect()
};

// Create some balances for test Stackers
let mut stacker_balances = test_stackers
.iter()
.map(|test_stacker| {
(
PrincipalData::from(key_to_stacks_addr(&test_stacker.stacker_private_key)),
u64::try_from(test_stacker.amount).expect("Stacking amount too large"),
)
})
.collect();

peer_config.initial_balances.append(&mut stacker_balances);
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;

let mut peer = TestPeer::new(peer_config);
advance_to_nakamoto(&mut peer);

advance_to_nakamoto(&mut peer, &test_signers, test_stackers.iter().collect());

peer
}

Expand All @@ -130,8 +172,20 @@ fn make_replay_peer<'a>(peer: &'a mut TestPeer<'a>) -> TestPeer<'a> {
replay_config.server_port = 0;
replay_config.http_port = 0;

let private_key = peer.config.private_key.clone();
let signer_private_key = StacksPrivateKey::from_seed(&[3]);

let mut replay_peer = TestPeer::new(replay_config);
advance_to_nakamoto(&mut replay_peer);
let observer = TestEventObserver::new();
advance_to_nakamoto(
&mut replay_peer,
&TestSigners::default(),
vec![&TestStacker {
stacker_private_key: private_key,
signer_private_key,
amount: 1_000_000_000_000_000_000,
}],
);

// sanity check
let replay_tip = {
Expand All @@ -158,7 +212,7 @@ fn make_replay_peer<'a>(peer: &'a mut TestPeer<'a>) -> TestPeer<'a> {
}

/// Make a token-transfer from a private key
fn make_token_transfer(
pub fn make_token_transfer(
chainstate: &mut StacksChainState,
sortdb: &SortitionDB,
private_key: &StacksPrivateKey,
Expand Down Expand Up @@ -246,11 +300,7 @@ fn replay_reward_cycle(
#[test]
fn test_simple_nakamoto_coordinator_bootup() {
let mut test_signers = TestSigners::default();
let mut peer = boot_nakamoto(
function_name!(),
vec![],
test_signers.aggregate_public_key.clone(),
);
let mut peer = boot_nakamoto(function_name!(), vec![], &test_signers, None);

let (burn_ops, mut tenure_change, miner_key) =
peer.begin_nakamoto_tenure(TenureChangeCause::BlockFound);
Expand Down Expand Up @@ -309,7 +359,8 @@ fn test_simple_nakamoto_coordinator_1_tenure_10_blocks() {
let mut peer = boot_nakamoto(
function_name!(),
vec![(addr.into(), 100_000_000)],
test_signers.aggregate_public_key.clone(),
&test_signers,
None,
);

let (burn_ops, mut tenure_change, miner_key) =
Expand Down Expand Up @@ -430,7 +481,8 @@ fn test_nakamoto_chainstate_getters() {
let mut peer = boot_nakamoto(
function_name!(),
vec![(addr.into(), 100_000_000)],
test_signers.aggregate_public_key.clone(),
&test_signers,
None,
);

let sort_tip = {
Expand Down Expand Up @@ -919,7 +971,8 @@ fn test_simple_nakamoto_coordinator_10_tenures_10_blocks() {
let mut peer = boot_nakamoto(
function_name!(),
vec![(addr.into(), 100_000_000)],
test_signers.aggregate_public_key.clone(),
&test_signers,
None,
);

let mut all_blocks = vec![];
Expand Down Expand Up @@ -1239,7 +1292,8 @@ fn test_simple_nakamoto_coordinator_2_tenures_3_sortitions() {
let mut peer = boot_nakamoto(
function_name!(),
vec![(addr.into(), 100_000_000)],
test_signers.aggregate_public_key.clone(),
&test_signers,
None,
);

let mut rc_burn_ops = vec![];
Expand Down Expand Up @@ -1567,7 +1621,8 @@ fn test_simple_nakamoto_coordinator_10_tenures_and_extensions_10_blocks() {
let mut peer = boot_nakamoto(
function_name!(),
vec![(addr.into(), 100_000_000)],
test_signers.aggregate_public_key.clone(),
&test_signers,
None,
);

let mut all_blocks = vec![];
Expand Down
Loading

0 comments on commit 5c8f091

Please sign in to comment.