Skip to content

Commit 372cb22

Browse files
author
0xTopaz
committed
Merge branch 'governance' of https://github.com/gnoswap-labs/gnoswap into governance
2 parents a99503f + f80deca commit 372cb22

File tree

3 files changed

+284
-89
lines changed

3 files changed

+284
-89
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package staker
2+
3+
import (
4+
"std"
5+
"testing"
6+
7+
"gno.land/p/demo/avl"
8+
"gno.land/p/demo/testutils"
9+
)
10+
11+
var (
12+
testAddr1 = testutils.TestAddress("test1")
13+
testAddr = testutils.TestAddress("test")
14+
)
15+
16+
type mockEnv struct {
17+
height uint64
18+
isAdmin bool
19+
}
20+
21+
func (m *mockEnv) GetHeight() int64 {
22+
return int64(m.height)
23+
}
24+
25+
func (m *mockEnv) IsAdmin() bool {
26+
return m.isAdmin
27+
}
28+
29+
func TestCleanDelegationStatHistory(t *testing.T) {
30+
mock := &mockEnv{height: 1000, isAdmin: true}
31+
std.TestSetOrigCaller(testAddr1)
32+
delegationSnapShotHistory = avl.NewTree()
33+
34+
addr := testAddr.String()
35+
history := []DelegationSnapShotHistory{
36+
{updatedBlock: 500}, // Old
37+
{updatedBlock: 900}, // Within threshold
38+
{updatedBlock: 950}, // Latest
39+
}
40+
delegationSnapShotHistory.Set(addr, history)
41+
42+
tests := []struct {
43+
name string
44+
setupHeight uint64
45+
lastCleaned uint64
46+
threshold int64
47+
expectedLen int
48+
}{
49+
{
50+
name: "no clean needed",
51+
setupHeight: 1000,
52+
lastCleaned: 999,
53+
threshold: 100,
54+
expectedLen: 3,
55+
},
56+
{
57+
name: "clean old records",
58+
setupHeight: 1000,
59+
lastCleaned: 800,
60+
threshold: 100,
61+
expectedLen: 3,
62+
},
63+
}
64+
65+
for _, tc := range tests {
66+
t.Run(tc.name, func(t *testing.T) {
67+
mock.height = tc.setupHeight
68+
lastCleanedHeight = tc.lastCleaned
69+
thresholdVotingWeightBlockHeight = tc.threshold
70+
71+
cleanDelegationStatHistory()
72+
73+
value, exists := delegationSnapShotHistory.Get(addr)
74+
if !exists {
75+
t.Fatal("history should exist")
76+
}
77+
78+
history := value.([]DelegationSnapShotHistory)
79+
if len(history) != tc.expectedLen {
80+
t.Errorf("expected history length %d, got %d", tc.expectedLen, len(history))
81+
}
82+
})
83+
}
84+
}

gov/staker/delegate_undelegate_test.gno

Lines changed: 97 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,109 @@ package staker
33
import (
44
"std"
55
"testing"
6-
"time"
76

87
"gno.land/p/demo/avl"
98
"gno.land/p/demo/testutils"
109
)
1110

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)
8750

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")
9467
}
68+
}()
69+
undelegate(addr1, 100)
70+
})
71+
72+
t.Run("fail - insufficient amount", func(t *testing.T) {
73+
delegate(addr1, 50)
9574

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")
10078
}
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+
}

gov/staker/history_test.gno

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package staker
2+
3+
import (
4+
"std"
5+
"testing"
6+
"time"
7+
8+
"gno.land/p/demo/avl"
9+
"gno.land/p/demo/testutils"
10+
)
11+
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+
}
87+
88+
if tt.expectPanic {
89+
defer func() {
90+
if r := recover(); r == nil {
91+
t.Errorf("expected panic but got none")
92+
}
93+
}()
94+
}
95+
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)
100+
}
101+
})
102+
}
103+
}

0 commit comments

Comments
 (0)