@@ -3,101 +3,109 @@ package staker
3
3
import (
4
4
"std"
5
5
"testing"
6
- "time"
7
6
8
7
"gno.land/p/demo/avl"
9
8
"gno.land/p/demo/testutils"
10
9
)
11
10
12
- func TestGetDelegatedCumulative(t *testing.T) {
13
- delegationSnapShotHistory = avl.NewTree()
14
-
15
- addr1 := testutils.TestAddress("test1")
16
- now := uint64(time.Now().Unix())
17
-
18
- tests := []struct {
19
- name string
20
- setupHistory []DelegationSnapShotHistory
21
- delegator std.Address
22
- endTimestamp uint64
23
- expectAmount uint64
24
- expectPanic bool
25
- }{
26
- {
27
- name: "no history returns zero",
28
- delegator: addr1,
29
- endTimestamp: now,
30
- expectAmount: 0,
31
- },
32
- {
33
- name: "single history before timestamp",
34
- setupHistory: []DelegationSnapShotHistory{
35
- {
36
- to: addr1,
37
- amount: 100,
38
- updatedBlock: 1,
39
- updatedAt: now - 100,
40
- },
41
- },
42
- delegator: addr1,
43
- endTimestamp: now,
44
- expectAmount: 100,
45
- },
46
- {
47
- name: "multiple histories returns latest before timestamp",
48
- setupHistory: []DelegationSnapShotHistory{
49
- {
50
- to: addr1,
51
- amount: 100,
52
- updatedBlock: 1,
53
- updatedAt: now - 200,
54
- },
55
- {
56
- to: addr1,
57
- amount: 150,
58
- updatedBlock: 2,
59
- updatedAt: now - 100,
60
- },
61
- {
62
- to: addr1,
63
- amount: 200,
64
- updatedBlock: 3,
65
- updatedAt: now + 100, // Future update
66
- },
67
- },
68
- delegator: addr1,
69
- endTimestamp: now,
70
- expectAmount: 150,
71
- },
72
- {
73
- name: "future timestamp panics",
74
- delegator: addr1,
75
- endTimestamp: now + 1000,
76
- expectPanic: true,
77
- },
78
- }
79
-
80
- for _, tt := range tests {
81
- t.Run(tt.name, func(t *testing.T) {
82
- delegationSnapShotHistory = avl.NewTree()
83
-
84
- if len(tt.setupHistory) > 0 {
85
- delegationSnapShotHistory.Set(tt.delegator.String(), tt.setupHistory)
86
- }
11
+ func TestDelegate(t *testing.T) {
12
+ t.Skip("Must running separately. because this test depends on TestUndelegate's state")
13
+ std.TestSetOrigCaller(testAddr1)
14
+ resetState()
15
+
16
+ addr1 := testAddr1
17
+
18
+ t.Run("success - first delegation", func(t *testing.T) {
19
+ delegate(addr1, 100)
20
+
21
+ if totalDelegated != 100 {
22
+ t.Errorf("expected totalDelegated 100, got %d", totalDelegated)
23
+ }
24
+
25
+ value, exists := delegatorAmount.Get(addr1.String())
26
+ if !exists || value.(uint64) != 100 {
27
+ t.Error("delegator amount not updated correctly")
28
+ }
29
+
30
+ innerTree, exists := delegatedFromTo.Get(addr1.String())
31
+ if !exists {
32
+ t.Error("delegatedFromTo not updated")
33
+ }
34
+
35
+ inner := innerTree.(*avl.Tree)
36
+ delegatedAmount, exists := inner.Get(addr1.String())
37
+ if !exists {
38
+ t.Error("delegatedFromTo amount incorrect")
39
+ }
40
+
41
+ if delegatedAmount.(uint64) != 100 {
42
+ t.Error("delegatedFromTo amount incorrect")
43
+ }
44
+ })
45
+
46
+ t.Run("success - additional delegation", func(t *testing.T) {
47
+ resetState()
48
+ delegate(addr1, 100)
49
+ delegate(addr1, 50)
87
50
88
- if tt.expectPanic {
89
- defer func() {
90
- if r := recover(); r == nil {
91
- t.Errorf("expected panic but got none")
92
- }
93
- }()
51
+ if totalDelegated != 150 {
52
+ t.Errorf("expected totalDelegated 150, got %d", totalDelegated)
53
+ }
54
+ })
55
+ }
56
+
57
+ func TestUndelegate(t *testing.T) {
58
+ t.Skip("Must running separately. because this test depends on TestUndelegate's state")
59
+ addr1 := testAddr1
60
+ std.TestSetOrigCaller(addr1)
61
+ resetState()
62
+
63
+ t.Run("fail - no delegation", func(t *testing.T) {
64
+ defer func() {
65
+ if r := recover(); r == nil {
66
+ t.Error("expected panic for no delegation")
94
67
}
68
+ }()
69
+ undelegate(addr1, 100)
70
+ })
71
+
72
+ t.Run("fail - insufficient amount", func(t *testing.T) {
73
+ delegate(addr1, 50)
95
74
96
- result := GetDelegatedCumulative(tt.delegator, tt.endTimestamp)
97
-
98
- if !tt.expectPanic && result != tt.expectAmount {
99
- t.Errorf("expected amount %d but got %d", tt.expectAmount, result)
75
+ defer func() {
76
+ if r := recover(); r == nil {
77
+ t.Error("expected panic for insufficient amount")
100
78
}
101
- })
102
- }
103
- }
79
+ }()
80
+ undelegate(addr1, 100)
81
+ })
82
+
83
+ t.Run("success - partial undelegate", func(t *testing.T) {
84
+ resetState()
85
+ delegate(addr1, 100)
86
+ undelegate(addr1, 30)
87
+
88
+ if totalDelegated != 70 {
89
+ t.Errorf("expected totalDelegated 70, got %d", totalDelegated)
90
+ }
91
+ })
92
+
93
+ t.Run("success - full undelegate", func(t *testing.T) {
94
+ resetState()
95
+ delegate(addr1, 100)
96
+ undelegate(addr1, 100)
97
+
98
+ if totalDelegated != 0 {
99
+ t.Errorf("expected totalDelegated 0, got %d", totalDelegated)
100
+ }
101
+ })
102
+ }
103
+
104
+ func resetState() {
105
+ totalDelegated = 0
106
+ delegatorAmount = avl.NewTree()
107
+ delegatedFromTo = avl.NewTree()
108
+ delegatedTo = avl.NewTree()
109
+ delegationHistory = avl.NewTree()
110
+ delegationSnapShotHistory = avl.NewTree()
111
+ }
0 commit comments