helpers_test.gno
2.62 Kb · 101 lines
1package gns
2
3import (
4 "testing"
5
6 "gno.land/p/nt/avl/v0"
7 "gno.land/p/nt/testutils/v0"
8)
9
10// Deterministic, checksum-valid test addresses.
11var (
12 admin = testutils.TestAddress("admin")
13 alice = testutils.TestAddress("alice")
14 bob = testutils.TestAddress("bob")
15 carol = testutils.TestAddress("carol")
16 dave = testutils.TestAddress("dave")
17)
18
19// reset re-initializes all package state to a deterministic, test-friendly
20// configuration. Durations are small so lifecycle tests can advance time with
21// modest SkipHeights calls. Pricing is free by default (BasePricePerSecond=0)
22// so most functional tests need not attach payment; payment tests override it.
23func reset() {
24 names = avl.NewTree()
25 commitments = avl.NewTree()
26 reverse = avl.NewTree()
27 events = avl.NewTree()
28 byOwner = avl.NewTree()
29 byParent = avl.NewTree()
30 nextEventID = 0
31
32 config = Config{
33 Admin: admin,
34 Treasury: admin,
35 RegistrationOpen: true,
36
37 MinCommitAge: 10,
38 MaxCommitAge: 1000,
39
40 MinRegistrationDuration: 100,
41 MaxRegistrationDuration: 10_000_000,
42
43 GracePeriod: 100,
44
45 BasePricePerSecond: 0,
46 PremiumByLength: map[uint8]int64{1: 100, 2: 25, 3: 5, 4: 2},
47
48 PaymentDenom: "ugnot",
49
50 MaxTextValueBytes: 4096,
51 MaxBinaryValueBytes: 8192,
52 MaxRecordsPerName: 128,
53 MaxOperatorsPerName: 32,
54
55 PolicyRevision: 1,
56 }
57}
58
59// newTestName builds a standalone active *Name for unit-testing pure helpers
60// (authorize, statusOf, ...) without going through the registration flow.
61func newTestName(canonical string, owner address, cp ControlPolicy) *Name {
62 label, parent := splitLabel(canonical)
63 return &Name{
64 Canonical: canonical,
65 Owner: owner,
66 ExpiresAt: now() + 1_000_000,
67 Parent: parent,
68 Label: label,
69 Depth: depthOf(canonical),
70 Generation: 1,
71 Revision: 1,
72 Records: newRecords(),
73 Operators: avl.NewTree(),
74 ControlPolicy: cp,
75 }
76}
77
78// doRegister runs the full commit→reveal flow for a second-level name using the
79// free (zero-price) path. Caller identity is set to owner.
80func doRegister(cur realm, name string, owner address, duration int64) {
81 secret := "sec-" + name
82 canonical, err := Normalize(name)
83 if err != nil {
84 panic(err)
85 }
86 c := computeCommitment(canonical, owner, duration, secret, "", config.PolicyRevision)
87
88 testing.SetRealm(testing.NewUserRealm(owner))
89 Commit(cross(cur), c)
90
91 testing.SkipHeights(3) // +15s > MinCommitAge (10s)
92
93 testing.SetRealm(testing.NewUserRealm(owner))
94 Register(cross(cur), RegisterRequest{
95 Name: name,
96 Owner: owner,
97 Duration: duration,
98 Secret: secret,
99 PolicyRevision: config.PolicyRevision,
100 })
101}