Add ViewSlashKeeper
This commit is contained in:
parent
2672f4a1bb
commit
91b1ee393c
|
@ -354,7 +354,7 @@ func (k Keeper) clearAccUpdateValidators(ctx sdk.Context) {
|
|||
|
||||
//_____________________________________________________________________
|
||||
|
||||
// load a delegator bong
|
||||
// load a delegator bond
|
||||
func (k Keeper) GetDelegatorBond(ctx sdk.Context,
|
||||
delegatorAddr, candidateAddr sdk.Address) (bond DelegatorBond, found bool) {
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package stake
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// keeper to view information & slash validators
|
||||
// will be used by governance module
|
||||
type ViewSlashKeeper struct {
|
||||
keeper Keeper
|
||||
}
|
||||
|
||||
// NewViewSlashKeeper creates a keeper restricted to
|
||||
// viewing information & slashing validators
|
||||
func NewViewSlashKeeper(k Keeper) ViewSlashKeeper {
|
||||
return ViewSlashKeeper{k}
|
||||
}
|
||||
|
||||
// load a delegator bond
|
||||
func (v ViewSlashKeeper) GetDelegatorBond(ctx sdk.Context,
|
||||
delegatorAddr, candidateAddr sdk.Address) (bond DelegatorBond, found bool) {
|
||||
return v.keeper.GetDelegatorBond(ctx, delegatorAddr, candidateAddr)
|
||||
}
|
||||
|
||||
// load n delegator bonds
|
||||
func (v ViewSlashKeeper) GetDelegatorBonds(ctx sdk.Context,
|
||||
delegator sdk.Address, maxRetrieve int16) (bonds []DelegatorBond) {
|
||||
return v.keeper.GetDelegatorBonds(ctx, delegator, maxRetrieve)
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package stake
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// tests GetDelegatorBond, GetDelegatorBonds
|
||||
func TestViewSlashBond(t *testing.T) {
|
||||
ctx, _, keeper := createTestInput(t, false, 0)
|
||||
|
||||
//construct the candidates
|
||||
amts := []int64{9, 8, 7}
|
||||
var candidates [3]Candidate
|
||||
for i, amt := range amts {
|
||||
candidates[i] = Candidate{
|
||||
Address: addrVals[i],
|
||||
PubKey: pks[i],
|
||||
Assets: sdk.NewRat(amt),
|
||||
Liabilities: sdk.NewRat(amt),
|
||||
}
|
||||
}
|
||||
|
||||
// first add a candidates[0] to delegate too
|
||||
keeper.setCandidate(ctx, candidates[0])
|
||||
|
||||
bond1to1 := DelegatorBond{
|
||||
DelegatorAddr: addrDels[0],
|
||||
CandidateAddr: addrVals[0],
|
||||
Shares: sdk.NewRat(9),
|
||||
}
|
||||
|
||||
viewSlashKeeper := NewViewSlashKeeper(keeper)
|
||||
|
||||
bondsEqual := func(b1, b2 DelegatorBond) bool {
|
||||
return bytes.Equal(b1.DelegatorAddr, b2.DelegatorAddr) &&
|
||||
bytes.Equal(b1.CandidateAddr, b2.CandidateAddr) &&
|
||||
b1.Shares == b2.Shares
|
||||
}
|
||||
|
||||
// check the empty keeper first
|
||||
_, found := viewSlashKeeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0])
|
||||
assert.False(t, found)
|
||||
|
||||
// set and retrieve a record
|
||||
keeper.setDelegatorBond(ctx, bond1to1)
|
||||
resBond, found := viewSlashKeeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0])
|
||||
assert.True(t, found)
|
||||
assert.True(t, bondsEqual(bond1to1, resBond))
|
||||
|
||||
// modify a records, save, and retrieve
|
||||
bond1to1.Shares = sdk.NewRat(99)
|
||||
keeper.setDelegatorBond(ctx, bond1to1)
|
||||
resBond, found = viewSlashKeeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0])
|
||||
assert.True(t, found)
|
||||
assert.True(t, bondsEqual(bond1to1, resBond))
|
||||
|
||||
// add some more records
|
||||
keeper.setCandidate(ctx, candidates[1])
|
||||
keeper.setCandidate(ctx, candidates[2])
|
||||
bond1to2 := DelegatorBond{addrDels[0], addrVals[1], sdk.NewRat(9)}
|
||||
bond1to3 := DelegatorBond{addrDels[0], addrVals[2], sdk.NewRat(9)}
|
||||
bond2to1 := DelegatorBond{addrDels[1], addrVals[0], sdk.NewRat(9)}
|
||||
bond2to2 := DelegatorBond{addrDels[1], addrVals[1], sdk.NewRat(9)}
|
||||
bond2to3 := DelegatorBond{addrDels[1], addrVals[2], sdk.NewRat(9)}
|
||||
keeper.setDelegatorBond(ctx, bond1to2)
|
||||
keeper.setDelegatorBond(ctx, bond1to3)
|
||||
keeper.setDelegatorBond(ctx, bond2to1)
|
||||
keeper.setDelegatorBond(ctx, bond2to2)
|
||||
keeper.setDelegatorBond(ctx, bond2to3)
|
||||
|
||||
// test all bond retrieve capabilities
|
||||
resBonds := viewSlashKeeper.GetDelegatorBonds(ctx, addrDels[0], 5)
|
||||
require.Equal(t, 3, len(resBonds))
|
||||
assert.True(t, bondsEqual(bond1to1, resBonds[0]))
|
||||
assert.True(t, bondsEqual(bond1to2, resBonds[1]))
|
||||
assert.True(t, bondsEqual(bond1to3, resBonds[2]))
|
||||
resBonds = viewSlashKeeper.GetDelegatorBonds(ctx, addrDels[0], 3)
|
||||
require.Equal(t, 3, len(resBonds))
|
||||
resBonds = viewSlashKeeper.GetDelegatorBonds(ctx, addrDels[0], 2)
|
||||
require.Equal(t, 2, len(resBonds))
|
||||
resBonds = viewSlashKeeper.GetDelegatorBonds(ctx, addrDels[1], 5)
|
||||
require.Equal(t, 3, len(resBonds))
|
||||
assert.True(t, bondsEqual(bond2to1, resBonds[0]))
|
||||
assert.True(t, bondsEqual(bond2to2, resBonds[1]))
|
||||
assert.True(t, bondsEqual(bond2to3, resBonds[2]))
|
||||
|
||||
}
|
Loading…
Reference in New Issue