cosmos-sdk/x/stake/view_slash_keeper_test.go

87 lines
2.7 KiB
Go
Raw Normal View History

2018-04-23 09:17:21 -07:00
package stake
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2018-05-09 18:39:14 -07:00
// tests GetDelegation, GetDelegations
2018-04-23 09:17:21 -07:00
func TestViewSlashBond(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0)
2018-05-09 21:01:58 -07:00
//construct the validators
2018-04-23 09:17:21 -07:00
amts := []int64{9, 8, 7}
2018-05-09 21:01:58 -07:00
var validators [3]Validator
2018-04-23 09:17:21 -07:00
for i, amt := range amts {
2018-05-09 21:01:58 -07:00
validators[i] = Validator{
2018-05-15 20:59:27 -07:00
Address: addrVals[i],
PubKey: pks[i],
PShares: NewBondedShares(sdk.NewRat(amt)),
2018-05-04 12:38:25 -07:00
DelegatorShares: sdk.NewRat(amt),
2018-04-23 09:17:21 -07:00
}
}
2018-05-09 21:01:58 -07:00
// first add a validators[0] to delegate too
keeper.setValidator(ctx, validators[0])
2018-04-23 09:17:21 -07:00
2018-05-09 18:39:14 -07:00
bond1to1 := Delegation{
2018-04-23 09:17:21 -07:00
DelegatorAddr: addrDels[0],
2018-05-09 21:01:58 -07:00
ValidatorAddr: addrVals[0],
2018-04-23 09:17:21 -07:00
Shares: sdk.NewRat(9),
}
viewSlashKeeper := NewViewSlashKeeper(keeper)
// check the empty keeper first
2018-05-09 18:39:14 -07:00
_, found := viewSlashKeeper.GetDelegation(ctx, addrDels[0], addrVals[0])
2018-04-23 09:17:21 -07:00
assert.False(t, found)
// set and retrieve a record
2018-05-09 18:39:14 -07:00
keeper.setDelegation(ctx, bond1to1)
resBond, found := viewSlashKeeper.GetDelegation(ctx, addrDels[0], addrVals[0])
2018-04-23 09:17:21 -07:00
assert.True(t, found)
assert.True(t, bond1to1.equal(resBond))
2018-04-23 09:17:21 -07:00
// modify a records, save, and retrieve
bond1to1.Shares = sdk.NewRat(99)
2018-05-09 18:39:14 -07:00
keeper.setDelegation(ctx, bond1to1)
resBond, found = viewSlashKeeper.GetDelegation(ctx, addrDels[0], addrVals[0])
2018-04-23 09:17:21 -07:00
assert.True(t, found)
assert.True(t, bond1to1.equal(resBond))
2018-04-23 09:17:21 -07:00
// add some more records
2018-05-09 21:01:58 -07:00
keeper.setValidator(ctx, validators[1])
keeper.setValidator(ctx, validators[2])
2018-05-09 18:39:14 -07:00
bond1to2 := Delegation{addrDels[0], addrVals[1], sdk.NewRat(9), 0}
bond1to3 := Delegation{addrDels[0], addrVals[2], sdk.NewRat(9), 1}
bond2to1 := Delegation{addrDels[1], addrVals[0], sdk.NewRat(9), 2}
bond2to2 := Delegation{addrDels[1], addrVals[1], sdk.NewRat(9), 3}
bond2to3 := Delegation{addrDels[1], addrVals[2], sdk.NewRat(9), 4}
keeper.setDelegation(ctx, bond1to2)
keeper.setDelegation(ctx, bond1to3)
keeper.setDelegation(ctx, bond2to1)
keeper.setDelegation(ctx, bond2to2)
keeper.setDelegation(ctx, bond2to3)
2018-04-23 09:17:21 -07:00
// test all bond retrieve capabilities
2018-05-09 18:39:14 -07:00
resBonds := viewSlashKeeper.GetDelegations(ctx, addrDels[0], 5)
2018-04-23 09:17:21 -07:00
require.Equal(t, 3, len(resBonds))
assert.True(t, bond1to1.equal(resBonds[0]))
assert.True(t, bond1to2.equal(resBonds[1]))
assert.True(t, bond1to3.equal(resBonds[2]))
2018-05-09 18:39:14 -07:00
resBonds = viewSlashKeeper.GetDelegations(ctx, addrDels[0], 3)
2018-04-23 09:17:21 -07:00
require.Equal(t, 3, len(resBonds))
2018-05-09 18:39:14 -07:00
resBonds = viewSlashKeeper.GetDelegations(ctx, addrDels[0], 2)
2018-04-23 09:17:21 -07:00
require.Equal(t, 2, len(resBonds))
2018-05-09 18:39:14 -07:00
resBonds = viewSlashKeeper.GetDelegations(ctx, addrDels[1], 5)
2018-04-23 09:17:21 -07:00
require.Equal(t, 3, len(resBonds))
assert.True(t, bond2to1.equal(resBonds[0]))
assert.True(t, bond2to2.equal(resBonds[1]))
assert.True(t, bond2to3.equal(resBonds[2]))
2018-04-23 09:17:21 -07:00
}