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

get-entitled-stacking-reward returns u0 during current cycle after shutdown #196

Closed
whoabuddy opened this issue May 1, 2022 · 1 comment

Comments

@whoabuddy
Copy link

A small bug was discovered while researching the upgrade process.

TL;DR

If someone is stacking and their stacking cycles end in the current cycle when the core contract is shut down, they can successfully send a claim-mining-reward transaction, but need to wait until after the cycle completes in order to receive both CityCoins and uSTX.

If called during the current cycle, only the CityCoins will be returned.

Description

After the shutdown-contract function is successfully called, the contract then allows users to claim their rewards for any cycle by setting the is-shutdown variable true.

;; stop mining and stacking operations
;; in preparation for a core upgrade
(define-public (shutdown-contract (stacksHeight uint))
(begin
;; make sure block height is in the future
(asserts! (>= stacksHeight block-height) (err ERR_BLOCK_HEIGHT_IN_PAST))
;; only allow shutdown request from AUTH
(asserts! (is-authorized-auth) (err ERR_UNAUTHORIZED))
;; set variables to disable mining/stacking in CORE
(var-set activationReached false)
(var-set shutdownHeight stacksHeight)
;; set variable to allow for all stacking claims
(var-set isShutdown true)
(ok true)
)
)

This variable is used to bypass the assert in claim-stacking-reward-at-cycle that verifies the user is claiming in a previous cycle, which allows claims to process for the current and future cycles.

(entitledUstx (get-entitled-stacking-reward userId targetCycle stacksHeight))
(stackerAtCycle (get-stacker-at-cycle-or-default targetCycle userId))
(toReturn (get toReturn stackerAtCycle))
)
(asserts! (or
(is-eq true (var-get isShutdown))
(> currentCycle targetCycle))
(err ERR_REWARD_CYCLE_NOT_COMPLETED))

However, before that happens the function get-entitled-stacking-reward is called on line 770 to determine the amount of uSTX entitled to the caller. That function checks if the target cycle is before the current cycle and returns u0 by default.

During the claim process the entry for the stacker in the StackerAtCycle map is reset to 0 for CityCoins and uSTX to prevent the stacker from claiming again.

(match (get-reward-cycle stacksHeight)
currentCycle
(if (or (<= currentCycle targetCycle) (is-eq u0 userStackedThisCycle))
;; this cycle hasn't finished, or Stacker contributed nothing
u0
;; (totalUstxThisCycle * userStackedThisCycle) / totalStackedThisCycle
(/ (* totalUstxThisCycle userStackedThisCycle) totalStackedThisCycle)
)

This means that if a stacker has stacked CityCoins they can claim after the current cycle finishes:

  • the shutdown-contract call will allow the stacker to claim the CityCoins
  • if the stacker claims during the current cycle they will only receive CityCoins
  • if the stacker claims after the current cycle, they will receive both CityCoins and STX
@whoabuddy
Copy link
Author

Fixed in #200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant