forked from quicksilver-zone/quicksilver
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add delegation plan test (quicksilver-zone#709)
* add delegation plan test * fix: dont attempt to delegate to invalid validators! * minor fixes, additional tests * fix: fix delegation test * gofumpt * dont select validators jailed for greater than 2 epochs * verbose variables * lint * linting * linting * linting * duplicate import * err is not checked --------- Co-authored-by: ducnt131 <62016666+anhductn2001@users.noreply.github.com> Co-authored-by: anhductn2001 <ntd1312000@gmail.com> Co-authored-by: ThanhNhann <thanhnhan98qh@gmail.com>
1 parent
2e91ee7
commit 108ebf4
Showing
29 changed files
with
1,121 additions
and
825 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
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
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
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
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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
package keeper_test | ||
|
||
func (s *KeeperTestSuite) TestGetSetDelete() { | ||
k := s.GetQuicksilverApp(s.chainA).ClaimsManagerKeeper | ||
ctx := s.chainA.GetContext() | ||
func (suite *KeeperTestSuite) TestGetSetDelete() { | ||
k := suite.GetQuicksilverApp(suite.chainA).ClaimsManagerKeeper | ||
ctx := suite.chainA.GetContext() | ||
|
||
_, found := k.GetSelfConsensusState(ctx, "test") | ||
s.Require().False(found) | ||
suite.Require().False(found) | ||
|
||
err := k.StoreSelfConsensusState(ctx, "test") | ||
s.Require().NoError(err) | ||
suite.Require().NoError(err) | ||
|
||
_, found = k.GetSelfConsensusState(ctx, "test") | ||
s.Require().True(found) | ||
suite.Require().True(found) | ||
|
||
k.DeleteSelfConsensusState(ctx, "test") | ||
|
||
_, found = k.GetSelfConsensusState(ctx, "test") | ||
s.Require().False(found) | ||
suite.Require().False(found) | ||
} |
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
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
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,229 @@ | ||
package types_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/quicksilver-zone/quicksilver/x/interchainstaking/types" | ||
) | ||
|
||
// Delegation Allocation Tests | ||
|
||
// Test with valid inputs and expected outputs | ||
func TestValidInputs(t *testing.T) { | ||
currentAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(100), | ||
"validator2": sdkmath.NewInt(200), | ||
} | ||
currentSum := sdkmath.NewInt(300) | ||
targetAllocations := types.ValidatorIntents{ | ||
{ValoperAddress: "validator1", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
{ValoperAddress: "validator2", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
} | ||
amount := sdk.Coins{sdk.NewCoin("token", sdk.NewInt(1000))} | ||
|
||
expectedAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(550), | ||
"validator2": sdkmath.NewInt(450), | ||
} | ||
|
||
result, err := types.DetermineAllocationsForDelegation(currentAllocations, currentSum, targetAllocations, amount) | ||
require.NoError(t, err) | ||
|
||
if !reflect.DeepEqual(result, expectedAllocations) { | ||
t.Errorf("Expected allocations %v, but got %v", expectedAllocations, result) | ||
} | ||
} | ||
|
||
// Test with minimum input values - fail on zero amount | ||
func TestMinimumInputs(t *testing.T) { | ||
currentAllocations := map[string]sdkmath.Int{} | ||
currentSum := sdkmath.ZeroInt() | ||
targetAllocations := types.ValidatorIntents{} | ||
amount := sdk.Coins{sdk.NewCoin("token", sdkmath.ZeroInt())} | ||
|
||
_, err := types.DetermineAllocationsForDelegation(currentAllocations, currentSum, targetAllocations, amount) | ||
require.Error(t, err) | ||
} | ||
|
||
// Test with maximum input values | ||
func TestMaximumInputs(t *testing.T) { | ||
currentAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(1000000000), | ||
"validator2": sdkmath.NewInt(2000000000), | ||
} | ||
currentSum := sdkmath.NewInt(3000000000) | ||
targetAllocations := types.ValidatorIntents{ | ||
{ValoperAddress: "validator1", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
{ValoperAddress: "validator2", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
} | ||
amount := sdk.Coins{sdk.NewCoin("token", sdk.NewInt(10000000000))} | ||
|
||
expectedAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(5500000000), | ||
"validator2": sdkmath.NewInt(4500000000), | ||
} | ||
|
||
result, err := types.DetermineAllocationsForDelegation(currentAllocations, currentSum, targetAllocations, amount) | ||
require.NoError(t, err) | ||
|
||
if !reflect.DeepEqual(result, expectedAllocations) { | ||
t.Errorf("Expected allocations %v, but got %v", expectedAllocations, result) | ||
} | ||
} | ||
|
||
// Test with empty currentAllocations | ||
func TestEmptyCurrentAllocations(t *testing.T) { | ||
currentAllocations := map[string]sdkmath.Int{} | ||
currentSum := sdkmath.ZeroInt() | ||
targetAllocations := types.ValidatorIntents{ | ||
{ValoperAddress: "validator1", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
{ValoperAddress: "validator2", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
} | ||
amount := sdk.Coins{sdk.NewCoin("token", sdk.NewInt(1000))} | ||
|
||
expectedAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(500), | ||
"validator2": sdkmath.NewInt(500), | ||
} | ||
|
||
result, err := types.DetermineAllocationsForDelegation(currentAllocations, currentSum, targetAllocations, amount) | ||
require.NoError(t, err) | ||
|
||
if !reflect.DeepEqual(result, expectedAllocations) { | ||
t.Errorf("Expected allocations %v, but got %v", expectedAllocations, result) | ||
} | ||
} | ||
|
||
// Test with empty targetAllocations - error on empty target list. | ||
func TestEmptyTargetAllocations(t *testing.T) { | ||
currentAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(100), | ||
"validator2": sdkmath.NewInt(200), | ||
} | ||
currentSum := sdkmath.NewInt(300) | ||
targetAllocations := types.ValidatorIntents{} | ||
amount := sdk.Coins{sdk.NewCoin("token", sdk.NewInt(1000))} | ||
|
||
_, err := types.DetermineAllocationsForDelegation(currentAllocations, currentSum, targetAllocations, amount) | ||
require.Error(t, err) | ||
} | ||
|
||
// Test non-equal targetAllocations | ||
func TestNonEqualTargetAllocations(t *testing.T) { | ||
currentAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(100), | ||
"validator2": sdkmath.NewInt(200), | ||
} | ||
currentSum := sdkmath.NewInt(300) | ||
targetAllocations := types.ValidatorIntents{ | ||
{ValoperAddress: "validator1", Weight: sdk.NewDecWithPrec(3, 1)}, | ||
{ValoperAddress: "validator2", Weight: sdk.NewDecWithPrec(7, 1)}, | ||
} | ||
amount := sdk.Coins{sdk.NewCoin("token", sdk.NewInt(1000))} | ||
|
||
expectedAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(290), | ||
"validator2": sdkmath.NewInt(710), | ||
} | ||
|
||
result, err := types.DetermineAllocationsForDelegation(currentAllocations, currentSum, targetAllocations, amount) | ||
require.NoError(t, err) | ||
|
||
if !reflect.DeepEqual(result, expectedAllocations) { | ||
t.Errorf("Expected allocations %v, but got %v", expectedAllocations, result) | ||
} | ||
} | ||
|
||
// Test with some validators having zero weight | ||
func TestValidInputsWithZeroWeight(t *testing.T) { | ||
currentAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(100), | ||
"validator2": sdkmath.NewInt(200), | ||
"validator3": sdkmath.NewInt(0), | ||
} | ||
currentSum := sdkmath.NewInt(300) | ||
targetAllocations := types.ValidatorIntents{ | ||
{ValoperAddress: "validator1", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
{ValoperAddress: "validator2", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
{ValoperAddress: "validator3", Weight: sdk.NewDec(0)}, | ||
} | ||
amount := sdk.Coins{sdk.NewCoin("token", sdk.NewInt(1000))} | ||
|
||
expectedAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(550), | ||
"validator2": sdkmath.NewInt(450), | ||
} | ||
|
||
result, err := types.DetermineAllocationsForDelegation(currentAllocations, currentSum, targetAllocations, amount) | ||
require.NoError(t, err) | ||
|
||
if !reflect.DeepEqual(result, expectedAllocations) { | ||
t.Errorf("Expected allocations %v, but got %v", expectedAllocations, result) | ||
} | ||
} | ||
|
||
// Test with targetAllocations having more validators than currentAllocations | ||
func TestTargetAllocationsMoreValidators(t *testing.T) { | ||
currentAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(100), | ||
"validator2": sdkmath.NewInt(200), | ||
} | ||
currentSum := sdkmath.NewInt(300) | ||
targetAllocations := types.ValidatorIntents{ | ||
{ValoperAddress: "validator1", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
{ValoperAddress: "validator2", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
{ValoperAddress: "validator3", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
{ValoperAddress: "validator4", Weight: sdk.NewDecWithPrec(5, 1)}, | ||
} | ||
amount := sdk.Coins{sdk.NewCoin("token", sdk.NewInt(1000))} | ||
|
||
expectedAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(241), | ||
"validator2": sdkmath.NewInt(195), | ||
"validator3": sdkmath.NewInt(282), | ||
"validator4": sdkmath.NewInt(282), | ||
} | ||
|
||
result, err := types.DetermineAllocationsForDelegation(currentAllocations, currentSum, targetAllocations, amount) | ||
require.NoError(t, err) | ||
|
||
if !reflect.DeepEqual(result, expectedAllocations) { | ||
t.Errorf("Expected allocations %v, but got %v", expectedAllocations, result) | ||
} | ||
} | ||
|
||
// Test with currentAllocations having more validators than targetAllocations | ||
func TestCurrentAllocationsMoreValidators(t *testing.T) { | ||
currentAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(100), | ||
"validator2": sdkmath.NewInt(200), | ||
"validator3": sdkmath.NewInt(300), | ||
} | ||
currentSum := sdkmath.NewInt(600) | ||
targetAllocations := types.ValidatorIntents{ | ||
{ValoperAddress: "validator1", Weight: sdk.NewDecWithPrec(3, 1)}, | ||
{ValoperAddress: "validator2", Weight: sdk.NewDecWithPrec(4, 1)}, | ||
} | ||
amount := sdk.Coins{sdk.NewCoin("token", sdk.NewInt(1000))} | ||
|
||
expectedAllocations := map[string]sdkmath.Int{ | ||
"validator1": sdkmath.NewInt(489), | ||
"validator2": sdkmath.NewInt(511), | ||
} | ||
|
||
result, err := types.DetermineAllocationsForDelegation(currentAllocations, currentSum, targetAllocations, amount) | ||
require.NoError(t, err) | ||
|
||
if !reflect.DeepEqual(result, expectedAllocations) { | ||
t.Errorf("Expected allocations %v, but got %v", expectedAllocations, result) | ||
} | ||
} | ||
|
||
// Deltas tests. |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Oops, something went wrong.