-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'governance' of https://github.com/gnoswap-labs/gnoswap …
…into governance
- Loading branch information
Showing
3 changed files
with
284 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package staker | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
var ( | ||
testAddr1 = testutils.TestAddress("test1") | ||
testAddr = testutils.TestAddress("test") | ||
) | ||
|
||
type mockEnv struct { | ||
height uint64 | ||
isAdmin bool | ||
} | ||
|
||
func (m *mockEnv) GetHeight() int64 { | ||
return int64(m.height) | ||
} | ||
|
||
func (m *mockEnv) IsAdmin() bool { | ||
return m.isAdmin | ||
} | ||
|
||
func TestCleanDelegationStatHistory(t *testing.T) { | ||
mock := &mockEnv{height: 1000, isAdmin: true} | ||
std.TestSetOrigCaller(testAddr1) | ||
delegationSnapShotHistory = avl.NewTree() | ||
|
||
addr := testAddr.String() | ||
history := []DelegationSnapShotHistory{ | ||
{updatedBlock: 500}, // Old | ||
{updatedBlock: 900}, // Within threshold | ||
{updatedBlock: 950}, // Latest | ||
} | ||
delegationSnapShotHistory.Set(addr, history) | ||
|
||
tests := []struct { | ||
name string | ||
setupHeight uint64 | ||
lastCleaned uint64 | ||
threshold int64 | ||
expectedLen int | ||
}{ | ||
{ | ||
name: "no clean needed", | ||
setupHeight: 1000, | ||
lastCleaned: 999, | ||
threshold: 100, | ||
expectedLen: 3, | ||
}, | ||
{ | ||
name: "clean old records", | ||
setupHeight: 1000, | ||
lastCleaned: 800, | ||
threshold: 100, | ||
expectedLen: 3, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mock.height = tc.setupHeight | ||
lastCleanedHeight = tc.lastCleaned | ||
thresholdVotingWeightBlockHeight = tc.threshold | ||
|
||
cleanDelegationStatHistory() | ||
|
||
value, exists := delegationSnapShotHistory.Get(addr) | ||
if !exists { | ||
t.Fatal("history should exist") | ||
} | ||
|
||
history := value.([]DelegationSnapShotHistory) | ||
if len(history) != tc.expectedLen { | ||
t.Errorf("expected history length %d, got %d", tc.expectedLen, len(history)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package staker | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
"time" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestGetDelegatedCumulative(t *testing.T) { | ||
delegationSnapShotHistory = avl.NewTree() | ||
|
||
addr1 := testutils.TestAddress("test1") | ||
now := uint64(time.Now().Unix()) | ||
|
||
tests := []struct { | ||
name string | ||
setupHistory []DelegationSnapShotHistory | ||
delegator std.Address | ||
endTimestamp uint64 | ||
expectAmount uint64 | ||
expectPanic bool | ||
}{ | ||
{ | ||
name: "no history returns zero", | ||
delegator: addr1, | ||
endTimestamp: now, | ||
expectAmount: 0, | ||
}, | ||
{ | ||
name: "single history before timestamp", | ||
setupHistory: []DelegationSnapShotHistory{ | ||
{ | ||
to: addr1, | ||
amount: 100, | ||
updatedBlock: 1, | ||
updatedAt: now - 100, | ||
}, | ||
}, | ||
delegator: addr1, | ||
endTimestamp: now, | ||
expectAmount: 100, | ||
}, | ||
{ | ||
name: "multiple histories returns latest before timestamp", | ||
setupHistory: []DelegationSnapShotHistory{ | ||
{ | ||
to: addr1, | ||
amount: 100, | ||
updatedBlock: 1, | ||
updatedAt: now - 200, | ||
}, | ||
{ | ||
to: addr1, | ||
amount: 150, | ||
updatedBlock: 2, | ||
updatedAt: now - 100, | ||
}, | ||
{ | ||
to: addr1, | ||
amount: 200, | ||
updatedBlock: 3, | ||
updatedAt: now + 100, // Future update | ||
}, | ||
}, | ||
delegator: addr1, | ||
endTimestamp: now, | ||
expectAmount: 150, | ||
}, | ||
{ | ||
name: "future timestamp panics", | ||
delegator: addr1, | ||
endTimestamp: now + 1000, | ||
expectPanic: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
delegationSnapShotHistory = avl.NewTree() | ||
|
||
if len(tt.setupHistory) > 0 { | ||
delegationSnapShotHistory.Set(tt.delegator.String(), tt.setupHistory) | ||
} | ||
|
||
if tt.expectPanic { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Errorf("expected panic but got none") | ||
} | ||
}() | ||
} | ||
|
||
result := GetDelegatedCumulative(tt.delegator, tt.endTimestamp) | ||
|
||
if !tt.expectPanic && result != tt.expectAmount { | ||
t.Errorf("expected amount %d but got %d", tt.expectAmount, result) | ||
} | ||
}) | ||
} | ||
} |