Skip to content

Commit 885e334

Browse files
BowTiedRadonemoodmosaic
authored andcommitted
Add rendezvous invariants for SIP-031
1 parent c311433 commit 885e334

File tree

1 file changed

+84
-7
lines changed

1 file changed

+84
-7
lines changed

stackslib/src/chainstate/stacks/boot/sip-031.tests.clar

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
(define-constant ERR_FAILED_ASSERTION u999)
22
(define-constant ERR_UNWRAP u998)
33
(define-constant ERR_UNEXPECTED_RESULT u997)
4+
(define-constant ERR_IGNORED u996)
5+
6+
(define-constant DEPLOYER tx-sender)
47

58
(define-data-var last-iteration-claimed uint u0)
69
(define-data-var minted-initial bool false)
710

8-
;; Helper to set up the Rendezvous testing environment. This should be called
9-
;; by wallet_10, which has 200M STX. It mints the initial 200M STX to the
10-
;; contract.
11-
(define-public (test-helper-initial-mint)
11+
;; General helpers
12+
13+
;; Helper to simulate the initial balance of the SIP-031 contract. This should
14+
;; be called by wallet_10, which has 200M STX. It transfers the initial 200M
15+
;; STX to the contract.
16+
(define-private (initial-mint-helper)
1217
(ok
1318
(and
1419
;; Ensure that the caller is wallet_10 as per Devnet.toml. It was added
@@ -24,17 +29,33 @@
2429
;; Helper to transfer extra STX amounts to the contract. In combination with
2530
;; other tests, this ensures that extra transfers do not break the vesting
2631
;; schedule.
27-
(define-public (test-helper-transfer-to-contract (ustx-amount uint))
32+
(define-private (extra-transfer-to-contract-helper (ustx-amount uint))
2833
(ok
2934
(and
3035
(not (is-eq tx-sender 'ST3FFKYTTB975A3JC3F99MM7TXZJ406R3GKE6JV56))
3136
(> ustx-amount u0)
3237
(>= (stx-get-balance tx-sender) ustx-amount)
33-
(try! (stx-transfer? ustx-amount tx-sender (as-contract tx-sender)))
38+
(unwrap-panic (stx-transfer? ustx-amount tx-sender (as-contract tx-sender)))
3439
)
3540
)
3641
)
3742

43+
;; Property tests
44+
45+
;; Helper to set up the Rendezvous testing environment for the property testing
46+
;; routine. The naming adheres to Rendezvous property test convention, which
47+
;; allows this function to be picked up during property testing runs.
48+
(define-public (test-initial-mint-helper)
49+
(initial-mint-helper)
50+
)
51+
52+
;; Helper to transfer extra STX amounts to the contract. The naming adheres to
53+
;; Rendezvous property test convention, which allows this function to be picked
54+
;; up during property testing runs.
55+
(define-public (test-extra-transfer-helper (ustx-amount uint))
56+
(extra-transfer-to-contract-helper ustx-amount)
57+
)
58+
3859
;; Tests that the recipient is updated if the caller is allowed.
3960
(define-public (test-update-recipient-allowed (new-recipient principal))
4061
(ok
@@ -162,4 +183,60 @@
162183
)
163184
)
164185
)
165-
)
186+
)
187+
188+
;; Invariants
189+
190+
;; Public wrapper for initial mint setup, required for Rendezvous invariant
191+
;; testing. Rendezvous randomly calls public functions during invariant testing
192+
;; runs, so this exposes the initial mint helper as a public function that can
193+
;; be selected and called.
194+
(define-public (initial-mint-helper-invariant-runs)
195+
(if
196+
(is-eq (initial-mint-helper) (ok true))
197+
(ok true)
198+
(err ERR_IGNORED)
199+
)
200+
)
201+
202+
;; Public wrapper for extra STX transfers to the contract for Rendezvous
203+
;; invariant testing. Rendezvous randomly calls public functions during
204+
;; invariant testing, so this exposes the extra transfer helper as a public
205+
;; function that can be selected during test runs.
206+
(define-public (extra-transfer-helper-invariant-runs (ustx-amount uint))
207+
(if
208+
(is-eq (extra-transfer-to-contract-helper ustx-amount) (ok true))
209+
(ok true)
210+
(err ERR_IGNORED)
211+
)
212+
)
213+
214+
;; Tests that the recipient remains unchanged unless `update-recipient` was
215+
;; called successfully at least once.
216+
(define-read-only (invariant-recipient-unchanged)
217+
(if
218+
(is-eq
219+
u0
220+
(default-to u0 (get called (map-get? context "update-recipient")))
221+
)
222+
(is-eq (var-get recipient) DEPLOYER)
223+
true
224+
)
225+
)
226+
227+
;; Tests that the amount returned by `calc-total-vested` never exceeds
228+
;; the total initial mint amount, regardless of any extra transfers
229+
;; to the contract.
230+
(define-read-only (invariant-vested-lt-initial-mint (burn-height uint))
231+
(or
232+
(<= burn-height DEPLOY_BLOCK_HEIGHT)
233+
(<=
234+
(calc-total-vested burn-height)
235+
;; We explicitly add up the total initial mint amount rather than using
236+
;; `INITIAL_MINT_AMOUNT` directly. This ensures the invariant remains
237+
;; valid even if the constants or their relationships change in the main
238+
;; contract, making this invariant's feedback more robust.
239+
(+ INITIAL_MINT_IMMEDIATE_AMOUNT INITIAL_MINT_VESTING_AMOUNT)
240+
)
241+
)
242+
)

0 commit comments

Comments
 (0)