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

Add cycle-id ranges to synthetic pox events #4414

Merged
merged 12 commits into from
Mar 14, 2024
Prev Previous commit
Next Next commit
test: cleanup pox event test
  • Loading branch information
janniks committed Mar 12, 2024
commit cc997bfce21dbd4bad7ac9fc02cb4ceee931d06a
5 changes: 3 additions & 2 deletions pox-locking/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn create_event_info_data_code(
response: &ResponseData,
) -> String {
// If a given burn block height is in a prepare phase, then the stacker will be in the _next_ reward cycle, so bump the cycle by 1
// `prepare_offset` is 1 or 0, depending on whether current execution is in a prepare phase or not
let prepare_offset = r#"
(prepare-offset (if (<
(mod (- %height% (var-get first-burnchain-block-height)) (var-get pox-reward-cycle-length))
Expand Down Expand Up @@ -243,7 +244,7 @@ fn create_event_info_data_code(
;; Get end cycle ID
end-cycle-id: (some (burn-height-to-reward-cycle unlock-height)),
;; Get start cycle ID
start-cycle-id: (+ (burn-height-to-reward-cycle unlock-height) prepare-offset),
start-cycle-id: (+ (current-pox-reward-cycle) prepare-offset),
}}
}})
"#,
Expand All @@ -252,7 +253,7 @@ fn create_event_info_data_code(
signer_key = &args.get(2).unwrap_or(&Value::none()),
max_amount = &args.get(3).unwrap_or(&Value::none()),
auth_id = &args.get(4).unwrap_or(&Value::none()),
prepare_offset = prepare_offset.replace("%height%", "unlock-height"),
prepare_offset = prepare_offset.replace("%height%", "burn-block-height"),
)
}
"delegate-stack-increase" => {
Expand Down
74 changes: 23 additions & 51 deletions stackslib/src/chainstate/stacks/boot/pox_4_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ fn pox_3_unlocks() {
}
}

// This tests calls most of Clarity functions to check the existence of `start-cycle-id` and `end-cycle-id`
// This tests calls most pox-4 Clarity functions to check the existence of `start-cycle-id` and `end-cycle-id`
// in emitted pox events.
// In this set up, Steph is a solo stacker and invokes `stack-stx`, `stack-increase` and `stack-extend` functions
// Alice delegates to Bob via `delegate-stx`
Expand Down Expand Up @@ -1355,8 +1355,7 @@ fn pox_4_check_cycle_id_range_in_print_events() {
}

let reward_cycle = get_current_reward_cycle(&peer, &burnchain);
let steph_pox_addr = pox_addr_from(&steph_key);
let pox_addr_val = Value::Tuple(steph_pox_addr.clone().as_clarity_tuple().unwrap());
let next_reward_cycle = reward_cycle + 1;

info!(
"Block height: {}",
Expand All @@ -1367,7 +1366,7 @@ fn pox_4_check_cycle_id_range_in_print_events() {
let block_height = get_tip(peer.sortdb.as_ref()).block_height;
let min_ustx = get_stacking_minimum(&mut peer, &latest_block.unwrap());

//stack-stx
// stack-stx
let steph_stack_stx_nonce = steph_nonce;
let signature = make_signer_key_signature(
&steph_pox_addr,
Expand All @@ -1392,7 +1391,7 @@ fn pox_4_check_cycle_id_range_in_print_events() {
);
steph_nonce += 1;

//stack-increase
// stack-increase
let steph_stack_increase_nonce = steph_nonce;
let signature = make_signer_key_signature(
&steph_pox_addr,
Expand All @@ -1414,7 +1413,7 @@ fn pox_4_check_cycle_id_range_in_print_events() {
);
steph_nonce += 1;

//stack-extend
// stack-extend
let steph_stack_extend_nonce = steph_nonce;
let stack_extend_signature = make_signer_key_signature(
&steph_pox_addr,
Expand All @@ -1425,7 +1424,6 @@ fn pox_4_check_cycle_id_range_in_print_events() {
u128::MAX,
1,
);

let steph_stack_extend = make_pox_4_extend(
&steph_key,
steph_stack_extend_nonce,
Expand All @@ -1438,13 +1436,12 @@ fn pox_4_check_cycle_id_range_in_print_events() {
);
steph_nonce += 1;

// alice delegates STX to Bob
let alice_delegation_amount_min_ustx = get_stacking_minimum(&mut peer, &latest_block.unwrap());
// alice delegates STX to bob
let target_height = get_tip(peer.sortdb.as_ref()).block_height + 10;
let alice_delegate = make_pox_4_delegate_stx(
&alice,
alice_nonce,
alice_delegation_amount_min_ustx,
min_ustx,
bob_principal.clone(),
Some(target_height as u128),
Some(bob_pox_addr.clone()),
Expand All @@ -1458,16 +1455,13 @@ fn pox_4_check_cycle_id_range_in_print_events() {
&bob,
bob_nonce,
alice_principal.clone(),
alice_delegation_amount_min_ustx,
min_ustx,
bob_pox_addr.clone(),
curr_height as u128,
lock_period,
);
bob_nonce += 1;

let reward_cycle = get_current_reward_cycle(&peer, &burnchain);
let next_reward_cycle = reward_cycle + 1;

let bob_aggregation_commit_nonce = bob_nonce;
let signature = make_signer_key_signature(
&bob_pox_addr,
Expand Down Expand Up @@ -1502,6 +1496,13 @@ fn pox_4_check_cycle_id_range_in_print_events() {
&mut coinbase_nonce,
));

let tip = get_tip(peer.sortdb.as_ref());
let tipId = StacksBlockId::new(&tip.consensus_hash, &tip.canonical_stacks_tip_hash);
assert_eq!(tipId, latest_block.unwrap());

let in_prepare_phase = burnchain.is_in_prepare_phase(tip.block_height);
assert_eq!(in_prepare_phase, false);

let blocks = observer.get_blocks();
let mut steph_txs = HashMap::new();
let mut alice_txs = HashMap::new();
Expand Down Expand Up @@ -1539,12 +1540,7 @@ fn pox_4_check_cycle_id_range_in_print_events() {
let steph_stacking_tx_event = &steph_stacking_tx_events[0];
let steph_stacking_op_data = HashMap::from([
("start-cycle-id", Value::UInt(22)),
(
"end-cycle-id",
Optional(OptionalData {
data: Some(Box::from(Value::UInt(24))),
}),
),
("end-cycle-id", Value::some(Value::UInt(24)).unwrap()),
]);
let common_data = PoxPrintFields {
op_name: "stack-stx".to_string(),
Expand All @@ -1560,13 +1556,9 @@ fn pox_4_check_cycle_id_range_in_print_events() {
assert_eq!(steph_stack_increase_tx_events.len() as u64, 2);
let steph_stack_increase_tx_event = &steph_stack_increase_tx_events[0];
let steph_stack_increase_op_data = HashMap::from([
("start-cycle-id", Value::UInt(24)),
(
"end-cycle-id",
Optional(OptionalData {
data: Some(Box::from(Value::UInt(24))),
}),
),
// in the same block, so we essentially want to be able to disregard the first event (stack-stx)
("start-cycle-id", Value::UInt(22)),
("end-cycle-id", Value::some(Value::UInt(24)).unwrap()),
]);
let common_data = PoxPrintFields {
op_name: "stack-increase".to_string(),
Expand All @@ -1587,12 +1579,7 @@ fn pox_4_check_cycle_id_range_in_print_events() {
let steph_stack_extend_tx_event = &steph_stack_extend_tx_events[0];
let steph_stacking_op_data = HashMap::from([
("start-cycle-id", Value::UInt(24)),
(
"end-cycle-id",
Optional(OptionalData {
data: Some(Box::from(Value::UInt(25))),
}),
),
("end-cycle-id", Value::some(Value::UInt(25)).unwrap()),
]);
let common_data = PoxPrintFields {
op_name: "stack-extend".to_string(),
Expand All @@ -1613,12 +1600,7 @@ fn pox_4_check_cycle_id_range_in_print_events() {
let alice_delegation_tx_event = &alice_delegation_tx_events[0];
let alice_delegate_stx_op_data = HashMap::from([
("start-cycle-id", Value::UInt(22)),
(
"end-cycle-id",
Optional(OptionalData {
data: Some(Box::from(Value::UInt(24))),
}),
),
("end-cycle-id", Value::some(Value::UInt(24)).unwrap()),
]);
let common_data = PoxPrintFields {
op_name: "delegate-stx".to_string(),
Expand All @@ -1639,12 +1621,7 @@ fn pox_4_check_cycle_id_range_in_print_events() {
let bob_delegate_stack_stx_tx_event = &bob_delegate_stack_stx_tx_events[0];
let bob_delegate_stack_stx_tx_op_data = HashMap::from([
("start-cycle-id", Value::UInt(22)),
(
"end-cycle-id",
Optional(OptionalData {
data: Some(Box::from(Value::UInt(24))),
}),
),
("end-cycle-id", Value::some(Value::UInt(24)).unwrap()),
]);
let common_data = PoxPrintFields {
op_name: "delegate-stack-stx".to_string(),
Expand All @@ -1665,12 +1642,7 @@ fn pox_4_check_cycle_id_range_in_print_events() {
let bob_aggregation_commit_tx_event = &bob_aggregation_commit_tx_events[0];
let bob_aggregation_commit_tx_op_data = HashMap::from([
("start-cycle-id", Value::UInt(22)),
(
"end-cycle-id",
Optional(OptionalData {
data: Some(Box::from(Value::UInt(0))), //Is this supposed to be 0?!
}),
),
("end-cycle-id", Value::some(Value::UInt(0)).unwrap()), //Is this supposed to be 0?!
]);
let common_data = PoxPrintFields {
op_name: "stack-aggregation-commit-indexed".to_string(),
Expand Down