Merge branch 'feat/check-pox-2-params' of https://github.com/stacks-network/stacks-blockchain into feat/check-pox-2-params

This commit is contained in:
Jude Nelson
2022-11-21 18:21:21 -05:00

View File

@@ -120,7 +120,7 @@
;; Delegation relationships
(define-map delegation-state
{ stacker: principal }
{
{
amount-ustx: uint, ;; how many uSTX delegated?
delegated-to: principal, ;; who are we delegating?
until-burn-ht: (optional uint), ;; how long does the delegation last?
@@ -164,7 +164,7 @@
;; this map allows stackers to stack amounts < minimum
;; by paying the cost of aggregation during the commit
(define-map partial-stacked-by-cycle
{
{
pox-addr: { version: (buff 1), hashbytes: (buff 32) },
reward-cycle: uint,
sender: principal
@@ -191,19 +191,19 @@
;; Has PoX been rejected in the given reward cycle?
(define-read-only (is-pox-active (reward-cycle uint))
(let (
(reject-votes
(reject-votes
(default-to
u0
(get amount (map-get? stacking-rejection { reward-cycle: reward-cycle }))))
)
;; (100 * reject-votes) / stx-liquid-supply < pox-rejection-fraction
(< (* u100 reject-votes)
;; (100 * reject-votes) / stx-liquid-supply < pox-rejection-fraction
(< (* u100 reject-votes)
(* (var-get pox-rejection-fraction) stx-liquid-supply)))
)
;; What's the reward cycle number of the burnchain block height?
;; Will runtime-abort if height is less than the first burnchain block (this is intentional)
(define-read-only (burn-height-to-reward-cycle (height uint))
(define-read-only (burn-height-to-reward-cycle (height uint))
(/ (- height (var-get first-burnchain-block-height)) (var-get pox-reward-cycle-length)))
;; What's the block height at the start of a given reward cycle?
@@ -231,7 +231,7 @@
(define-read-only (check-caller-allowed)
(or (is-eq tx-sender contract-caller)
(let ((caller-allowed
(let ((caller-allowed
;; if not in the caller map, return false
(unwrap! (map-get? allowance-contract-callers
{ sender: tx-sender, contract-caller: contract-caller })
@@ -366,7 +366,7 @@
;; the pox-addr was added to the given cycle. Also, `reward-set-indexes` grows to include all
;; of the `reward-cycle-index` key parts of the `reward-cycle-pox-address-list` which get added by this function.
;; This way, the caller knows which items in a given reward cycle's PoX address list got updated.
(define-private (add-pox-addr-to-ith-reward-cycle (cycle-index uint) (params (tuple
(define-private (add-pox-addr-to-ith-reward-cycle (cycle-index uint) (params (tuple
(pox-addr (tuple (version (buff 1)) (hashbytes (buff 32))))
(reward-set-indexes (list 12 uint))
(first-reward-cycle uint)
@@ -400,7 +400,7 @@
num-cycles: num-cycles,
amount-ustx: (get amount-ustx params),
stacker: (get stacker params),
reward-set-indexes: (match
reward-set-indexes: (match
reward-set-index new (unwrap-panic (as-max-len? (append (get reward-set-indexes params) new) u12))
(get reward-set-indexes params)),
i: next-i
@@ -415,8 +415,8 @@
(amount-ustx uint)
(stacker principal))
(let ((cycle-indexes (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11))
(results (fold add-pox-addr-to-ith-reward-cycle cycle-indexes
{ pox-addr: pox-addr, first-reward-cycle: first-reward-cycle, num-cycles: num-cycles,
(results (fold add-pox-addr-to-ith-reward-cycle cycle-indexes
{ pox-addr: pox-addr, first-reward-cycle: first-reward-cycle, num-cycles: num-cycles,
reward-set-indexes: (list), amount-ustx: amount-ustx, i: u0, stacker: (some stacker) }))
(reward-set-indexes (get reward-set-indexes results)))
;; For safety, add up the number of times (add-principal-to-ith-reward-cycle) returns 1.
@@ -460,7 +460,7 @@
(num-cycles uint)
(amount-ustx uint))
(let ((cycle-indexes (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11)))
(fold add-pox-partial-stacked-to-ith-cycle cycle-indexes
(fold add-pox-partial-stacked-to-ith-cycle cycle-indexes
{ pox-addr: pox-addr, reward-cycle: first-reward-cycle, num-cycles: num-cycles, amount-ustx: amount-ustx })
true))
@@ -482,12 +482,12 @@
false)))
;; Is the given lock period valid?
(define-read-only (check-pox-lock-period (lock-period uint))
(and (>= lock-period MIN_POX_REWARD_CYCLES)
(define-read-only (check-pox-lock-period (lock-period uint))
(and (>= lock-period MIN_POX_REWARD_CYCLES)
(<= lock-period MAX_POX_REWARD_CYCLES)))
;; Evaluate if a participant can stack an amount of STX for a given period.
;; This method is designed as a read-only method so that it can be used as
;; This method is designed as a read-only method so that it can be used as
;; a set of guard conditions and also as a read-only RPC call that can be
;; performed beforehand.
(define-read-only (can-stack-stx (pox-addr (tuple (version (buff 1)) (hashbytes (buff 32))))
@@ -502,10 +502,10 @@
(minimal-can-stack-stx pox-addr amount-ustx first-reward-cycle num-cycles)))
;; Evaluate if a participant can stack an amount of STX for a given period.
;; This method is designed as a read-only method so that it can be used as
;; This method is designed as a read-only method so that it can be used as
;; a set of guard conditions and also as a read-only RPC call that can be
;; performed beforehand.
(define-read-only (minimal-can-stack-stx
(define-read-only (minimal-can-stack-stx
(pox-addr (tuple (version (buff 1)) (hashbytes (buff 32))))
(amount-ustx uint)
(first-reward-cycle uint)
@@ -535,7 +535,7 @@
;; Revoke contract-caller authorization to call stacking methods
(define-public (disallow-contract-caller (caller principal))
(begin
(begin
(asserts! (is-eq tx-sender contract-caller)
(err ERR_STACKING_PERMISSION_DENIED))
(ok (map-delete allowance-contract-callers { sender: tx-sender, contract-caller: caller }))))
@@ -607,7 +607,7 @@
first-reward-cycle: first-reward-cycle,
lock-period: lock-period })
;; return the lock-up information, so the node can actually carry out the lock.
;; return the lock-up information, so the node can actually carry out the lock.
(ok { stacker: tx-sender, lock-amount: amount-ustx, unlock-burn-height: (reward-cycle-to-burn-height (+ first-reward-cycle lock-period)) }))))
(define-public (revoke-delegate-stx)
@@ -665,10 +665,10 @@
;; This ensures that each entry in the reward set returned to the stacks-node is greater than the threshold,
;; but does not require it be all locked up within a single transaction
;;
;; Returns (ok uint) on success, where the given uint is the reward address's index in the list of reward
;; Returns (ok uint) on success, where the given uint is the reward address's index in the list of reward
;; addresses allocated in this reward cycle. This index can then be passed to `stack-aggregation-increase`
;; to later increment the STX this PoX address represents, in amounts less than the stacking minimum.
;;
;;
;; *New in Stacks 2.1.*
(define-private (inner-stack-aggregation-commit (pox-addr { version: (buff 1), hashbytes: (buff 32) })
(reward-cycle uint))
@@ -722,7 +722,7 @@
;; Commit partially stacked STX to a PoX address which has already received some STX (more than the Stacking min).
;; This allows a delegator to lock up marginally more STX from new delegates, even if they collectively do not
;; exceed the Stacking minimum, so long as the target PoX address already represents at least as many STX as the
;; exceed the Stacking minimum, so long as the target PoX address already represents at least as many STX as the
;; Stacking minimum.
;;
;; The `reward-cycle-index` is emitted as a contract event from `stack-aggregation-commit` when the initial STX are
@@ -781,7 +781,7 @@
stacker: none })
;; update the total ustx in this cycle
(map-set reward-cycle-total-stacked
(map-set reward-cycle-total-stacked
{ reward-cycle: reward-cycle }
{ total-ustx: total-ustx })
@@ -856,7 +856,7 @@
reward-set-indexes: (list),
lock-period: lock-period })
;; return the lock-up information, so the node can actually carry out the lock.
;; return the lock-up information, so the node can actually carry out the lock.
(ok { stacker: stacker,
lock-amount: amount-ustx,
unlock-burn-height: unlock-burn-height })))
@@ -912,7 +912,7 @@
;; `reward-cycle-index` is the index into the `reward-cycle-pox-address-list` map for a given reward cycle number.
;; `updates`, if `(some ..)`, encodes which PoX reward cycle entry (if any) gets updated. In particular, it must have
;; `(some stacker)` as the listed stacker, and must be an upcoming reward cycle.
(define-private (increase-reward-cycle-entry
(define-private (increase-reward-cycle-entry
(reward-cycle-index uint)
(updates (optional { first-cycle: uint, reward-cycle: uint, stacker: principal, add-amount: uint })))
(let ((data (try! updates))
@@ -933,7 +933,7 @@
total-ustx: total-ustx,
stacker: (some (get stacker data)) })
;; update the total
(map-set reward-cycle-total-stacked
(map-set reward-cycle-total-stacked
{ reward-cycle: reward-cycle }
{ total-ustx: total-ustx })
(some { first-cycle: first-cycle,
@@ -953,7 +953,7 @@
(unlock-in-cycle (burn-height-to-reward-cycle unlock-height))
(cur-cycle (current-pox-reward-cycle))
(first-increased-cycle (+ cur-cycle u1))
(stacker-state (unwrap! (map-get? stacking-state
(stacker-state (unwrap! (map-get? stacking-state
{ stacker: tx-sender })
(err ERR_STACK_INCREASE_NOT_LOCKED))))
;; tx-sender must be currently locked
@@ -972,7 +972,7 @@
(asserts! (> (len (get reward-set-indexes stacker-state)) u0)
(err ERR_STACKING_ALREADY_DELEGATED))
;; update reward cycle amounts
(asserts! (is-some (fold increase-reward-cycle-entry
(asserts! (is-some (fold increase-reward-cycle-entry
(get reward-set-indexes stacker-state)
(some { first-cycle: first-increased-cycle,
reward-cycle: (get first-reward-cycle stacker-state),
@@ -1086,7 +1086,7 @@
(ok (+ u1 (- last-increase-cycle first-increase-cycle)))
(err ERR_STACKING_INVALID_LOCK_PERIOD))))
(new-total-locked (+ increase-by existing-lock))
(stacker-state
(stacker-state
(unwrap! (map-get? stacking-state { stacker: stacker })
(err ERR_STACK_INCREASE_NOT_LOCKED))))
@@ -1135,7 +1135,7 @@
;; stacking-state is unchanged, so no need to update
;; return the lock-up information, so the node can actually carry out the lock.
;; return the lock-up information, so the node can actually carry out the lock.
(ok { stacker: stacker, total-locked: new-total-locked}))))
;; As a delegator, extend an active stacking lock, issuing a "partial commitment" for the
@@ -1227,7 +1227,7 @@
first-reward-cycle: first-reward-cycle,
lock-period: lock-period })
;; return the lock-up information, so the node can actually carry out the lock.
;; return the lock-up information, so the node can actually carry out the lock.
(ok { stacker: stacker,
unlock-burn-height: new-unlock-ht }))))
@@ -1272,4 +1272,3 @@
u0
)
)