Skip to content

Commit

Permalink
address PR review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kantai committed Sep 20, 2022
1 parent 6956f00 commit ad1ffc1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
26 changes: 15 additions & 11 deletions src/chainstate/stacks/boot/pox-2.clar
Original file line number Diff line number Diff line change
Expand Up @@ -768,21 +768,20 @@
;; not at first cycle to process yet
(some { first-cycle: first-cycle, reward-cycle: (+ u1 reward-cycle), stacker: (get stacker data), add-amount: (get add-amount data) })
(let ((existing-entry (unwrap-panic (map-get? reward-cycle-pox-address-list { reward-cycle: reward-cycle, index: reward-cycle-index })))
(existing-total (unwrap-panic (map-get? reward-cycle-total-stacked { reward-cycle: reward-cycle }))))
(existing-total (unwrap-panic (map-get? reward-cycle-total-stacked { reward-cycle: reward-cycle })))
(total-ustx (+ (get total-ustx existing-total) (get add-amount data))))
;; stacker must match
(asserts! (is-eq (get stacker existing-entry) (some (get stacker data))) none)
;; update the pox-address list
(map-set reward-cycle-pox-address-list
{ reward-cycle: reward-cycle, index: reward-cycle-index }
{ pox-addr: (get pox-addr existing-entry),
total-ustx: (+ (get total-ustx existing-entry)
(get add-amount data)),
total-ustx: total-ustx,
stacker: (some (get stacker data)) })
;; update the total
(map-set reward-cycle-total-stacked
{ reward-cycle: reward-cycle }
{ total-ustx: (+ (get total-ustx existing-total)
(get add-amount data)) })
{ total-ustx: total-ustx })
(some { first-cycle: first-cycle,
reward-cycle: (+ u1 reward-cycle),
stacker: (get stacker data),
Expand All @@ -791,6 +790,7 @@
(define-public (stack-increase (increase-by uint))
(let ((stacker-info (stx-account tx-sender))
(amount-stacked (get locked stacker-info))
(amount-unlocked (get unlocked stacker-info))
(unlock-height (get unlock-height stacker-info))
(unlock-in-cycle (burn-height-to-reward-cycle unlock-height))
(cur-cycle (current-pox-reward-cycle))
Expand All @@ -802,7 +802,7 @@
(err ERR_STACK_EXTEND_NOT_LOCKED))
(asserts! (>= increase-by u1)
(err ERR_STACKING_INVALID_AMOUNT))
(asserts! (>= (get unlocked stacker-info) increase-by)
(asserts! (>= amount-unlocked increase-by)
(err ERR_STACKING_INSUFFICIENT_FUNDS))
(asserts! (check-caller-allowed)
(err ERR_STACKING_PERMISSION_DENIED))
Expand Down Expand Up @@ -921,20 +921,24 @@
(err ERR_STACKING_INSUFFICIENT_FUNDS))

;; stacker must have delegated to the caller
(let ((delegation-info (unwrap! (get-check-delegation stacker) (err ERR_STACKING_PERMISSION_DENIED))))
(let ((delegation-info (unwrap! (get-check-delegation stacker) (err ERR_STACKING_PERMISSION_DENIED)))
(delegated-to (get delegated-to delegation-info))
(delegated-amount (get amount-ustx delegation-info))
(delegated-pox-addr (get pox-addr delegation-info))
(delegated-until (get until-burn-ht delegation-info)))
;; must have delegated to tx-sender
(asserts! (is-eq (get delegated-to delegation-info) tx-sender)
(asserts! (is-eq delegated-to tx-sender)
(err ERR_STACKING_PERMISSION_DENIED))
;; must have delegated enough stx
(asserts! (>= (get amount-ustx delegation-info) new-total-locked)
(asserts! (>= delegated-amount new-total-locked)
(err ERR_DELEGATION_TOO_MUCH_LOCKED))
;; if pox-addr is set, must be equal to pox-addr
(asserts! (match (get pox-addr delegation-info)
(asserts! (match delegated-pox-addr
specified-pox-addr (is-eq pox-addr specified-pox-addr)
true)
(err ERR_DELEGATION_POX_ADDR_REQUIRED))
;; delegation must not expire before lock period
(asserts! (match (get until-burn-ht delegation-info)
(asserts! (match delegated-until
until-burn-ht
(>= until-burn-ht unlock-height)
true)
Expand Down
46 changes: 19 additions & 27 deletions src/chainstate/stacks/boot/pox_2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ fn delegate_stack_increase() {
cycle_number,
&bob_principal,
);
assert_eq!(partial_stacked, 512 * POX_THRESHOLD_STEPS_USTX);
assert_eq!(partial_stacked, alice_first_lock_amount);
}

for cycle_number in (EXPECTED_FIRST_V2_CYCLE + 4)..(EXPECTED_FIRST_V2_CYCLE + 6) {
Expand Down Expand Up @@ -1130,6 +1130,7 @@ fn delegate_stack_increase() {
assert_eq!(alice_txs.len() as u64, 2);
assert_eq!(bob_txs.len() as u64, 5);

// transaction should fail because Alice cannot increase her own stacking amount while delegating
assert_eq!(
&alice_txs[&fail_direct_increase_delegation]
.result
Expand Down Expand Up @@ -1209,6 +1210,10 @@ fn stack_increase() {

let mut coinbase_nonce = 0;

let first_lockup_amt = 512 * POX_THRESHOLD_STEPS_USTX;
let total_balance = 1024 * POX_THRESHOLD_STEPS_USTX;
let increase_amt = total_balance - first_lockup_amt;

// produce blocks until the epoch switch
for _i in 0..10 {
peer.tenure_with_txs(&[], &mut coinbase_nonce);
Expand All @@ -1219,13 +1224,13 @@ fn stack_increase() {

// submit an increase: this should fail, because Alice is not yet locked
let fail_no_lock_tx = alice_nonce;
let alice_increase = make_pox_2_increase(&alice, alice_nonce, 512 * POX_THRESHOLD_STEPS_USTX);
let alice_increase = make_pox_2_increase(&alice, alice_nonce, increase_amt);
alice_nonce += 1;

let alice_lockup = make_pox_2_lockup(
&alice,
alice_nonce,
512 * POX_THRESHOLD_STEPS_USTX,
first_lockup_amt,
AddressHashMode::SerializeP2PKH,
key_to_stacks_addr(&alice).bytes,
6,
Expand All @@ -1241,12 +1246,12 @@ fn stack_increase() {

assert_eq!(
get_stx_account_at(&mut peer, &latest_block, &alice_principal).amount_locked(),
512 * POX_THRESHOLD_STEPS_USTX,
first_lockup_amt,
);

assert_eq!(
get_stx_account_at(&mut peer, &latest_block, &alice_principal).get_total_balance(),
1024 * POX_THRESHOLD_STEPS_USTX,
total_balance,
);

// check that the "raw" reward set will contain entries for alice at the cycle start
Expand All @@ -1258,10 +1263,7 @@ fn stack_increase() {
reward_set_entries[0].reward_address.bytes(),
key_to_stacks_addr(&alice).bytes.0.to_vec()
);
assert_eq!(
reward_set_entries[0].amount_stacked,
512 * POX_THRESHOLD_STEPS_USTX
);
assert_eq!(reward_set_entries[0].amount_stacked, first_lockup_amt,);
}

// we'll produce blocks until the 3rd reward cycle gets through the "handled start" code
Expand All @@ -1281,10 +1283,7 @@ fn stack_increase() {
reward_set_entries[0].reward_address.bytes(),
key_to_stacks_addr(&alice).bytes.0.to_vec()
);
assert_eq!(
reward_set_entries[0].amount_stacked,
512 * POX_THRESHOLD_STEPS_USTX
);
assert_eq!(reward_set_entries[0].amount_stacked, first_lockup_amt,);
}

let mut txs_to_submit = vec![];
Expand All @@ -1293,11 +1292,7 @@ fn stack_increase() {
alice_nonce += 1;

// this stack-increase tx should work
txs_to_submit.push(make_pox_2_increase(
&alice,
alice_nonce,
512 * POX_THRESHOLD_STEPS_USTX,
));
txs_to_submit.push(make_pox_2_increase(&alice, alice_nonce, increase_amt));
alice_nonce += 1;

// increase by an amount we don't have!
Expand All @@ -1309,19 +1304,19 @@ fn stack_increase() {

assert_eq!(
get_stx_account_at(&mut peer, &latest_block, &alice_principal).amount_locked(),
1024 * POX_THRESHOLD_STEPS_USTX,
first_lockup_amt + increase_amt,
);

assert_eq!(
get_stx_account_at(&mut peer, &latest_block, &alice_principal).get_total_balance(),
1024 * POX_THRESHOLD_STEPS_USTX,
total_balance,
);

// check that the total reward cycle amounts have incremented correctly
for cycle_number in (EXPECTED_FIRST_V2_CYCLE)..(EXPECTED_FIRST_V2_CYCLE + 4) {
assert_eq!(
get_reward_cycle_total(&mut peer, &latest_block, cycle_number),
512 * POX_THRESHOLD_STEPS_USTX
first_lockup_amt,
);
let cycle_start = burnchain.reward_cycle_to_block_height(cycle_number);
let reward_set_entries = get_reward_set_entries_at(&mut peer, &latest_block, cycle_start);
Expand All @@ -1330,16 +1325,13 @@ fn stack_increase() {
reward_set_entries[0].reward_address.bytes(),
key_to_stacks_addr(&alice).bytes.0.to_vec()
);
assert_eq!(
reward_set_entries[0].amount_stacked,
512 * POX_THRESHOLD_STEPS_USTX
);
assert_eq!(reward_set_entries[0].amount_stacked, first_lockup_amt,);
}

for cycle_number in (EXPECTED_FIRST_V2_CYCLE + 4)..(EXPECTED_FIRST_V2_CYCLE + 6) {
assert_eq!(
get_reward_cycle_total(&mut peer, &latest_block, cycle_number),
1024 * POX_THRESHOLD_STEPS_USTX
first_lockup_amt + increase_amt,
);
let cycle_start = burnchain.reward_cycle_to_block_height(cycle_number);
let reward_set_entries = get_reward_set_entries_at(&mut peer, &latest_block, cycle_start);
Expand All @@ -1350,7 +1342,7 @@ fn stack_increase() {
);
assert_eq!(
reward_set_entries[0].amount_stacked,
1024 * POX_THRESHOLD_STEPS_USTX
first_lockup_amt + increase_amt,
);
}

Expand Down

0 comments on commit ad1ffc1

Please sign in to comment.