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

Implement PoX-4 Locking via Special Contract-Call Handler #4106

Merged
merged 20 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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: 14 additions & 0 deletions pox-locking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ pub fn handle_contract_call_special_cases(
result,
);
} else if *contract_id == boot_code_id(POX_3_NAME, global_context.mainnet) {
if !pox_3::is_read_only(function_name) && global_context.epoch_id >= StacksEpochId::Epoch25
{
warn!("PoX-3 function call attempted on an account after Epoch 2.5";
"v3_unlock_ht" => global_context.database.get_v3_unlock_height(),
"current_burn_ht" => global_context.database.get_current_burnchain_block_height(),
"function_name" => function_name,
"contract_id" => %contract_id
);
return Err(ClarityError::Runtime(
RuntimeErrorType::DefunctPoxContract,
None,
));
}

return pox_3::handle_contract_call(
global_context,
sender,
Expand Down
27 changes: 27 additions & 0 deletions pox-locking/src/pox_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ use crate::{LockingError, POX_3_NAME};

/////////////////////// PoX-3 /////////////////////////////////

/// is a PoX-3 function call read only?
pub(crate) fn is_read_only(func_name: &str) -> bool {
"get-pox-rejection" == func_name
|| "is-pox-active" == func_name
|| "burn-height-to-reward-cycle" == func_name
|| "reward-cycle-to-burn-height" == func_name
|| "current-pox-reward-cycle" == func_name
|| "get-stacker-info" == func_name
|| "get-check-delegation" == func_name
|| "get-reward-set-size" == func_name
|| "next-cycle-rejection-votes" == func_name
|| "get-total-ustx-stacked" == func_name
|| "get-reward-set-pox-address" == func_name
|| "get-stacking-minimum" == func_name
|| "check-pox-addr-version" == func_name
|| "check-pox-addr-hashbytes" == func_name
|| "check-pox-lock-period" == func_name
|| "can-stack-stx" == func_name
|| "minimal-can-stack-stx" == func_name
|| "get-pox-info" == func_name
|| "get-delegation-info" == func_name
|| "get-allowance-contract-callers" == func_name
|| "get-num-reward-set-pox-addresses" == func_name
|| "get-partial-stacked-by-cycle" == func_name
|| "get-total-pox-rejection" == func_name
}

/// Lock up STX for PoX for a time. Does NOT touch the account nonce.
pub fn pox_lock_v3(
db: &mut ClarityDatabase,
Expand Down
35 changes: 31 additions & 4 deletions stackslib/src/chainstate/stacks/boot/pox_4_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,14 @@ fn pox_3_defunct() {
);
burnchain.pox_constants = pox_constants.clone();

let (mut peer, keys) =
instantiate_pox_peer_with_epoch(&burnchain, function_name!(), Some(epochs.clone()), None);
let observer = TestEventObserver::new();

let (mut peer, keys) = instantiate_pox_peer_with_epoch(
&burnchain,
function_name!(),
Some(epochs.clone()),
Some(&observer),
);

assert_eq!(burnchain.pox_constants.reward_slots(), 6);
let mut coinbase_nonce = 0;
Expand Down Expand Up @@ -949,6 +955,28 @@ fn pox_3_defunct() {
info!("Submitting stacking txs with pox3");
latest_block = peer.tenure_with_txs(&txs, &mut coinbase_nonce);

info!("Checking that stackers have no STX locked");
let balances = balances_from_keys(&mut peer, &latest_block, &keys);
assert_eq!(balances[0].amount_locked(), 0);
assert_eq!(balances[1].amount_locked(), 0);

info!("Checking tx receipts, all `pox3` calls should have returned `(err ...)`");
let last_observer_block = observer
.get_blocks()
.last()
.unwrap()
.clone();

let receipts = last_observer_block.receipts
.iter()
.filter(|receipt| match &receipt.result {
Value::Response(r) => !r.committed,
_ => false,
})
jbencin marked this conversation as resolved.
Show resolved Hide resolved
.collect::<Vec<_>>();

assert_eq!(receipts.len(), 4);
jbencin marked this conversation as resolved.
Show resolved Hide resolved

// Advance to start of rewards cycle stackers are participating in
let target_height = burnchain.pox_constants.pox_4_activation_height + 5;
while get_tip(peer.sortdb.as_ref()).block_height < u64::from(target_height) {
Expand Down Expand Up @@ -985,8 +1013,7 @@ fn pox_3_defunct() {
}
}

/// Test that we can lock STX for a couple cycles after pox4 starts,
/// and that it unlocks after the desired number of cycles
// Test that STX locked in pox3 automatically unlocks at `v3_unlock_height`
#[test]
fn pox_3_unlocks() {
// Config for this test
Expand Down