2018-01-11 21:30:39 -08:00
|
|
|
package stake
|
|
|
|
|
|
|
|
import (
|
2018-01-18 00:39:16 -08:00
|
|
|
"bytes"
|
2018-04-03 11:50:31 -07:00
|
|
|
"encoding/json"
|
2018-01-11 21:30:39 -08:00
|
|
|
"testing"
|
|
|
|
|
2018-03-17 11:18:04 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-04-05 09:31:36 -07:00
|
|
|
crypto "github.com/tendermint/go-crypto"
|
2018-01-25 12:11:58 -08:00
|
|
|
|
2018-01-11 21:30:39 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-03-23 08:55:50 -07:00
|
|
|
var (
|
2018-03-30 11:23:16 -07:00
|
|
|
addrDels = []sdk.Address{
|
|
|
|
addrs[0],
|
|
|
|
addrs[1],
|
2018-03-23 08:55:50 -07:00
|
|
|
}
|
2018-03-30 11:23:16 -07:00
|
|
|
addrVals = []sdk.Address{
|
|
|
|
addrs[2],
|
|
|
|
addrs[3],
|
|
|
|
addrs[4],
|
|
|
|
addrs[5],
|
|
|
|
addrs[6],
|
2018-03-29 11:23:23 -07:00
|
|
|
}
|
2018-03-23 08:55:50 -07:00
|
|
|
)
|
2018-01-31 18:56:46 -08:00
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// This function tests GetCandidate, GetCandidates, setCandidate, removeCandidate
|
2018-03-22 09:00:45 -07:00
|
|
|
func TestCandidate(t *testing.T) {
|
2018-04-01 09:05:58 -07:00
|
|
|
ctx, _, keeper := createTestInput(t, false, 0)
|
2018-01-11 21:30:39 -08:00
|
|
|
|
2018-03-30 11:23:16 -07:00
|
|
|
//construct the candidates
|
|
|
|
var candidates [3]Candidate
|
|
|
|
amts := []int64{9, 8, 7}
|
|
|
|
for i, amt := range amts {
|
|
|
|
candidates[i] = Candidate{
|
|
|
|
Address: addrVals[i],
|
|
|
|
PubKey: pks[i],
|
|
|
|
Assets: sdk.NewRat(amt),
|
|
|
|
Liabilities: sdk.NewRat(amt),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:21:18 -07:00
|
|
|
candidatesEqual := func(c1, c2 Candidate) bool {
|
2018-01-11 21:30:39 -08:00
|
|
|
return c1.Status == c2.Status &&
|
|
|
|
c1.PubKey.Equals(c2.PubKey) &&
|
2018-03-17 11:18:04 -07:00
|
|
|
bytes.Equal(c1.Address, c2.Address) &&
|
2018-02-02 08:38:40 -08:00
|
|
|
c1.Assets.Equal(c2.Assets) &&
|
|
|
|
c1.Liabilities.Equal(c2.Liabilities) &&
|
2018-01-11 21:30:39 -08:00
|
|
|
c1.Description == c2.Description
|
|
|
|
}
|
|
|
|
|
2018-03-20 06:56:07 -07:00
|
|
|
// check the empty keeper first
|
2018-03-30 11:23:16 -07:00
|
|
|
_, found := keeper.GetCandidate(ctx, addrVals[0])
|
2018-03-20 14:21:18 -07:00
|
|
|
assert.False(t, found)
|
2018-03-27 17:17:11 -07:00
|
|
|
resCands := keeper.GetCandidates(ctx, 100)
|
|
|
|
assert.Zero(t, len(resCands))
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
// set and retrieve a record
|
2018-03-30 11:23:16 -07:00
|
|
|
keeper.setCandidate(ctx, candidates[0])
|
|
|
|
resCand, found := keeper.GetCandidate(ctx, addrVals[0])
|
2018-03-27 17:17:11 -07:00
|
|
|
require.True(t, found)
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.True(t, candidatesEqual(candidates[0], resCand), "%v \n %v", resCand, candidates[0])
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
// modify a records, save, and retrieve
|
2018-03-30 11:23:16 -07:00
|
|
|
candidates[0].Liabilities = sdk.NewRat(99)
|
|
|
|
keeper.setCandidate(ctx, candidates[0])
|
|
|
|
resCand, found = keeper.GetCandidate(ctx, addrVals[0])
|
2018-03-27 17:17:11 -07:00
|
|
|
require.True(t, found)
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.True(t, candidatesEqual(candidates[0], resCand))
|
2018-01-11 21:30:39 -08:00
|
|
|
|
2018-03-22 06:39:31 -07:00
|
|
|
// also test that the address has been added to address list
|
2018-03-27 17:17:11 -07:00
|
|
|
resCands = keeper.GetCandidates(ctx, 100)
|
|
|
|
require.Equal(t, 1, len(resCands))
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.Equal(t, addrVals[0], resCands[0].Address)
|
2018-03-22 09:00:45 -07:00
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// add other candidates
|
2018-03-30 11:23:16 -07:00
|
|
|
keeper.setCandidate(ctx, candidates[1])
|
|
|
|
keeper.setCandidate(ctx, candidates[2])
|
|
|
|
resCand, found = keeper.GetCandidate(ctx, addrVals[1])
|
2018-03-27 17:17:11 -07:00
|
|
|
require.True(t, found)
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.True(t, candidatesEqual(candidates[1], resCand), "%v \n %v", resCand, candidates[1])
|
|
|
|
resCand, found = keeper.GetCandidate(ctx, addrVals[2])
|
2018-03-27 17:17:11 -07:00
|
|
|
require.True(t, found)
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.True(t, candidatesEqual(candidates[2], resCand), "%v \n %v", resCand, candidates[2])
|
2018-03-27 17:17:11 -07:00
|
|
|
resCands = keeper.GetCandidates(ctx, 100)
|
|
|
|
require.Equal(t, 3, len(resCands))
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.True(t, candidatesEqual(candidates[0], resCands[0]), "%v \n %v", resCands[0], candidates[0])
|
|
|
|
assert.True(t, candidatesEqual(candidates[1], resCands[1]), "%v \n %v", resCands[1], candidates[1])
|
|
|
|
assert.True(t, candidatesEqual(candidates[2], resCands[2]), "%v \n %v", resCands[2], candidates[2])
|
2018-03-27 17:17:11 -07:00
|
|
|
|
|
|
|
// remove a record
|
2018-03-30 11:23:16 -07:00
|
|
|
keeper.removeCandidate(ctx, candidates[1].Address)
|
|
|
|
_, found = keeper.GetCandidate(ctx, addrVals[1])
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.False(t, found)
|
2018-03-22 09:00:45 -07:00
|
|
|
}
|
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// tests GetDelegatorBond, GetDelegatorBonds, SetDelegatorBond, removeDelegatorBond
|
2018-03-22 09:00:45 -07:00
|
|
|
func TestBond(t *testing.T) {
|
2018-04-01 09:05:58 -07:00
|
|
|
ctx, _, keeper := createTestInput(t, false, 0)
|
2018-01-11 21:30:39 -08:00
|
|
|
|
2018-03-30 11:23:16 -07:00
|
|
|
//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])
|
2018-01-11 21:30:39 -08:00
|
|
|
|
2018-03-22 09:00:45 -07:00
|
|
|
bond1to1 := DelegatorBond{
|
2018-03-30 11:23:16 -07:00
|
|
|
DelegatorAddr: addrDels[0],
|
|
|
|
CandidateAddr: addrVals[0],
|
2018-03-20 14:21:18 -07:00
|
|
|
Shares: sdk.NewRat(9),
|
2018-01-11 21:30:39 -08:00
|
|
|
}
|
|
|
|
|
2018-03-20 14:21:18 -07:00
|
|
|
bondsEqual := func(b1, b2 DelegatorBond) bool {
|
|
|
|
return bytes.Equal(b1.DelegatorAddr, b2.DelegatorAddr) &&
|
|
|
|
bytes.Equal(b1.CandidateAddr, b2.CandidateAddr) &&
|
2018-01-22 10:09:05 -08:00
|
|
|
b1.Shares == b2.Shares
|
2018-01-11 21:30:39 -08:00
|
|
|
}
|
|
|
|
|
2018-03-22 09:00:45 -07:00
|
|
|
// check the empty keeper first
|
2018-03-30 11:23:16 -07:00
|
|
|
_, found := keeper.getDelegatorBond(ctx, addrDels[0], addrVals[0])
|
2018-03-20 14:21:18 -07:00
|
|
|
assert.False(t, found)
|
2018-01-11 21:30:39 -08:00
|
|
|
|
2018-03-22 09:00:45 -07:00
|
|
|
// set and retrieve a record
|
|
|
|
keeper.setDelegatorBond(ctx, bond1to1)
|
2018-03-30 11:23:16 -07:00
|
|
|
resBond, found := keeper.getDelegatorBond(ctx, addrDels[0], addrVals[0])
|
2018-03-20 14:21:18 -07:00
|
|
|
assert.True(t, found)
|
2018-03-22 09:00:45 -07:00
|
|
|
assert.True(t, bondsEqual(bond1to1, resBond))
|
2018-01-11 21:30:39 -08:00
|
|
|
|
2018-03-22 09:00:45 -07:00
|
|
|
// modify a records, save, and retrieve
|
|
|
|
bond1to1.Shares = sdk.NewRat(99)
|
|
|
|
keeper.setDelegatorBond(ctx, bond1to1)
|
2018-03-30 11:23:16 -07:00
|
|
|
resBond, found = keeper.getDelegatorBond(ctx, addrDels[0], addrVals[0])
|
2018-03-20 14:21:18 -07:00
|
|
|
assert.True(t, found)
|
2018-03-22 09:00:45 -07:00
|
|
|
assert.True(t, bondsEqual(bond1to1, resBond))
|
|
|
|
|
|
|
|
// add some more records
|
2018-03-30 11:23:16 -07:00
|
|
|
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)}
|
2018-03-22 09:00:45 -07:00
|
|
|
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
|
2018-03-30 11:23:16 -07:00
|
|
|
resBonds := keeper.getDelegatorBonds(ctx, addrDels[0], 5)
|
2018-03-22 09:00:45 -07:00
|
|
|
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]))
|
2018-03-30 11:23:16 -07:00
|
|
|
resBonds = keeper.getDelegatorBonds(ctx, addrDels[0], 3)
|
2018-03-22 09:00:45 -07:00
|
|
|
require.Equal(t, 3, len(resBonds))
|
2018-03-30 11:23:16 -07:00
|
|
|
resBonds = keeper.getDelegatorBonds(ctx, addrDels[0], 2)
|
2018-03-22 09:00:45 -07:00
|
|
|
require.Equal(t, 2, len(resBonds))
|
2018-03-30 11:23:16 -07:00
|
|
|
resBonds = keeper.getDelegatorBonds(ctx, addrDels[1], 5)
|
2018-03-22 09:00:45 -07:00
|
|
|
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]))
|
2018-03-27 17:17:11 -07:00
|
|
|
|
|
|
|
// delete a record
|
|
|
|
keeper.removeDelegatorBond(ctx, bond2to3)
|
2018-03-30 11:23:16 -07:00
|
|
|
_, found = keeper.getDelegatorBond(ctx, addrDels[1], addrVals[2])
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.False(t, found)
|
2018-03-30 11:23:16 -07:00
|
|
|
resBonds = keeper.getDelegatorBonds(ctx, addrDels[1], 5)
|
2018-03-27 17:17:11 -07:00
|
|
|
require.Equal(t, 2, len(resBonds))
|
|
|
|
assert.True(t, bondsEqual(bond2to1, resBonds[0]))
|
|
|
|
assert.True(t, bondsEqual(bond2to2, resBonds[1]))
|
|
|
|
|
|
|
|
// delete all the records from delegator 2
|
|
|
|
keeper.removeDelegatorBond(ctx, bond2to1)
|
|
|
|
keeper.removeDelegatorBond(ctx, bond2to2)
|
2018-03-30 11:23:16 -07:00
|
|
|
_, found = keeper.getDelegatorBond(ctx, addrDels[1], addrVals[0])
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.False(t, found)
|
2018-03-30 11:23:16 -07:00
|
|
|
_, found = keeper.getDelegatorBond(ctx, addrDels[1], addrVals[1])
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.False(t, found)
|
2018-03-30 11:23:16 -07:00
|
|
|
resBonds = keeper.getDelegatorBonds(ctx, addrDels[1], 5)
|
2018-03-27 17:17:11 -07:00
|
|
|
require.Equal(t, 0, len(resBonds))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO integrate in testing for equal validators, whichever one was a validator
|
|
|
|
// first remains the validator https://github.com/cosmos/cosmos-sdk/issues/582
|
|
|
|
func TestGetValidators(t *testing.T) {
|
2018-04-01 09:05:58 -07:00
|
|
|
ctx, _, keeper := createTestInput(t, false, 0)
|
2018-03-27 17:17:11 -07:00
|
|
|
|
|
|
|
// initialize some candidates into the state
|
|
|
|
amts := []int64{0, 100, 1, 400, 200}
|
|
|
|
n := len(amts)
|
2018-03-30 11:23:16 -07:00
|
|
|
var candidates [5]Candidate
|
|
|
|
for i, amt := range amts {
|
2018-03-27 17:17:11 -07:00
|
|
|
c := Candidate{
|
|
|
|
Status: Unbonded,
|
|
|
|
PubKey: pks[i],
|
|
|
|
Address: addrs[i],
|
2018-03-30 11:23:16 -07:00
|
|
|
Assets: sdk.NewRat(amt),
|
|
|
|
Liabilities: sdk.NewRat(amt),
|
2018-03-27 17:17:11 -07:00
|
|
|
}
|
|
|
|
keeper.setCandidate(ctx, c)
|
|
|
|
candidates[i] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
// first make sure everything as normal is ordered
|
|
|
|
validators := keeper.GetValidators(ctx)
|
|
|
|
require.Equal(t, len(validators), n)
|
2018-04-05 15:34:25 -07:00
|
|
|
assert.Equal(t, sdk.NewRat(400), validators[0].Power, "%v", validators)
|
|
|
|
assert.Equal(t, sdk.NewRat(200), validators[1].Power, "%v", validators)
|
|
|
|
assert.Equal(t, sdk.NewRat(100), validators[2].Power, "%v", validators)
|
|
|
|
assert.Equal(t, sdk.NewRat(1), validators[3].Power, "%v", validators)
|
|
|
|
assert.Equal(t, sdk.NewRat(0), validators[4].Power, "%v", validators)
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators)
|
|
|
|
assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators)
|
|
|
|
assert.Equal(t, candidates[1].Address, validators[2].Address, "%v", validators)
|
|
|
|
assert.Equal(t, candidates[2].Address, validators[3].Address, "%v", validators)
|
|
|
|
assert.Equal(t, candidates[0].Address, validators[4].Address, "%v", validators)
|
|
|
|
|
|
|
|
// test a basic increase in voting power
|
|
|
|
candidates[3].Assets = sdk.NewRat(500)
|
|
|
|
keeper.setCandidate(ctx, candidates[3])
|
|
|
|
validators = keeper.GetValidators(ctx)
|
|
|
|
require.Equal(t, len(validators), n)
|
2018-04-05 15:34:25 -07:00
|
|
|
assert.Equal(t, sdk.NewRat(500), validators[0].Power, "%v", validators)
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators)
|
|
|
|
|
|
|
|
// test a decrease in voting power
|
|
|
|
candidates[3].Assets = sdk.NewRat(300)
|
|
|
|
keeper.setCandidate(ctx, candidates[3])
|
|
|
|
validators = keeper.GetValidators(ctx)
|
|
|
|
require.Equal(t, len(validators), n)
|
2018-04-05 15:34:25 -07:00
|
|
|
assert.Equal(t, sdk.NewRat(300), validators[0].Power, "%v", validators)
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators)
|
|
|
|
|
|
|
|
// test a swap in voting power
|
|
|
|
candidates[0].Assets = sdk.NewRat(600)
|
|
|
|
keeper.setCandidate(ctx, candidates[0])
|
|
|
|
validators = keeper.GetValidators(ctx)
|
|
|
|
require.Equal(t, len(validators), n)
|
2018-04-05 15:34:25 -07:00
|
|
|
assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators)
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
|
2018-04-05 15:34:25 -07:00
|
|
|
assert.Equal(t, sdk.NewRat(300), validators[1].Power, "%v", validators)
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators)
|
|
|
|
|
|
|
|
// test the max validators term
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
n = 2
|
|
|
|
params.MaxValidators = uint16(n)
|
|
|
|
keeper.setParams(ctx, params)
|
|
|
|
validators = keeper.GetValidators(ctx)
|
|
|
|
require.Equal(t, len(validators), n)
|
2018-04-05 15:34:25 -07:00
|
|
|
assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators)
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
|
2018-04-05 15:34:25 -07:00
|
|
|
assert.Equal(t, sdk.NewRat(300), validators[1].Power, "%v", validators)
|
2018-03-27 17:17:11 -07:00
|
|
|
assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators)
|
|
|
|
}
|
|
|
|
|
2018-04-02 11:37:35 -07:00
|
|
|
// clear the tracked changes to the validator set
|
|
|
|
func TestClearAccUpdateValidators(t *testing.T) {
|
2018-04-01 09:05:58 -07:00
|
|
|
ctx, _, keeper := createTestInput(t, false, 0)
|
2018-04-02 11:37:35 -07:00
|
|
|
|
|
|
|
amts := []int64{100, 400, 200}
|
|
|
|
candidates := make([]Candidate, len(amts))
|
|
|
|
for i, amt := range amts {
|
|
|
|
c := Candidate{
|
|
|
|
Status: Unbonded,
|
|
|
|
PubKey: pks[i],
|
|
|
|
Address: addrs[i],
|
|
|
|
Assets: sdk.NewRat(amt),
|
|
|
|
Liabilities: sdk.NewRat(amt),
|
|
|
|
}
|
|
|
|
candidates[i] = c
|
|
|
|
keeper.setCandidate(ctx, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := keeper.getAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, len(amts), len(acc))
|
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
|
|
|
acc = keeper.getAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, 0, len(acc))
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// test the mechanism which keeps track of a validator set change
|
|
|
|
func TestGetAccUpdateValidators(t *testing.T) {
|
2018-04-01 09:05:58 -07:00
|
|
|
ctx, _, keeper := createTestInput(t, false, 0)
|
2018-03-30 11:23:16 -07:00
|
|
|
params := defaultParams()
|
|
|
|
params.MaxValidators = 4
|
|
|
|
keeper.setParams(ctx, params)
|
2018-03-28 11:12:21 -07:00
|
|
|
|
2018-04-02 11:37:35 -07:00
|
|
|
// TODO eliminate use of candidatesIn here
|
|
|
|
// tests could be clearer if they just
|
|
|
|
// created the candidate at time of use
|
|
|
|
// and were labelled by power in the comments
|
|
|
|
// outlining in each test
|
|
|
|
amts := []int64{10, 11, 12, 13, 1}
|
2018-03-30 11:23:16 -07:00
|
|
|
var candidatesIn [5]Candidate
|
|
|
|
for i, amt := range amts {
|
|
|
|
candidatesIn[i] = Candidate{
|
2018-04-02 11:37:35 -07:00
|
|
|
Address: addrs[i],
|
2018-03-30 11:23:16 -07:00
|
|
|
PubKey: pks[i],
|
|
|
|
Assets: sdk.NewRat(amt),
|
|
|
|
Liabilities: sdk.NewRat(amt),
|
2018-03-28 11:12:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:31:36 -07:00
|
|
|
// to compare pubkeys between abci pubkey and crypto.PubKey
|
|
|
|
wirePK := func(pk crypto.PubKey) []byte {
|
|
|
|
pkBytes, err := keeper.cdc.MarshalBinary(pk)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pkBytes
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// test from nothing to something
|
2018-04-02 11:37:35 -07:00
|
|
|
// candidate set: {} -> {c1, c3}
|
|
|
|
// validator set: {} -> {c1, c3}
|
|
|
|
// accUpdate set: {} -> {c1, c3}
|
|
|
|
assert.Equal(t, 0, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 0, len(keeper.GetValidators(ctx)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
|
|
|
|
2018-03-30 11:23:16 -07:00
|
|
|
keeper.setCandidate(ctx, candidatesIn[1])
|
|
|
|
keeper.setCandidate(ctx, candidatesIn[3])
|
2018-04-02 11:37:35 -07:00
|
|
|
|
|
|
|
vals := keeper.GetValidators(ctx) // to init recent validator set
|
|
|
|
require.Equal(t, 2, len(vals))
|
|
|
|
acc := keeper.getAccUpdateValidators(ctx)
|
2018-03-30 11:23:16 -07:00
|
|
|
require.Equal(t, 2, len(acc))
|
|
|
|
candidates := keeper.GetCandidates(ctx, 5)
|
|
|
|
require.Equal(t, 2, len(candidates))
|
2018-04-05 09:31:36 -07:00
|
|
|
assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0])
|
|
|
|
assert.Equal(t, candidates[1].validator().abciValidator(keeper.cdc), acc[1])
|
2018-04-02 11:37:35 -07:00
|
|
|
assert.Equal(t, candidates[0].validator(), vals[1])
|
|
|
|
assert.Equal(t, candidates[1].validator(), vals[0])
|
|
|
|
|
|
|
|
// test identical,
|
|
|
|
// candidate set: {c1, c3} -> {c1, c3}
|
|
|
|
// accUpdate set: {} -> {}
|
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
2018-03-28 11:12:21 -07:00
|
|
|
|
|
|
|
keeper.setCandidate(ctx, candidates[0])
|
|
|
|
keeper.setCandidate(ctx, candidates[1])
|
2018-04-02 11:37:35 -07:00
|
|
|
|
|
|
|
require.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
2018-03-29 10:37:04 -07:00
|
|
|
|
|
|
|
// test single value change
|
2018-04-02 11:37:35 -07:00
|
|
|
// candidate set: {c1, c3} -> {c1', c3}
|
|
|
|
// accUpdate set: {} -> {c1'}
|
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
|
|
|
|
2018-03-29 11:23:23 -07:00
|
|
|
candidates[0].Assets = sdk.NewRat(600)
|
2018-03-29 10:37:04 -07:00
|
|
|
keeper.setCandidate(ctx, candidates[0])
|
2018-04-02 11:37:35 -07:00
|
|
|
|
2018-03-30 11:23:16 -07:00
|
|
|
candidates = keeper.GetCandidates(ctx, 5)
|
|
|
|
require.Equal(t, 2, len(candidates))
|
2018-04-02 11:37:35 -07:00
|
|
|
assert.True(t, candidates[0].Assets.Equal(sdk.NewRat(600)))
|
|
|
|
acc = keeper.getAccUpdateValidators(ctx)
|
|
|
|
require.Equal(t, 1, len(acc))
|
2018-04-05 09:31:36 -07:00
|
|
|
assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0])
|
2018-03-29 10:37:04 -07:00
|
|
|
|
|
|
|
// test multiple value change
|
2018-04-02 11:37:35 -07:00
|
|
|
// candidate set: {c1, c3} -> {c1', c3'}
|
|
|
|
// accUpdate set: {c1, c3} -> {c1', c3'}
|
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
|
|
|
|
2018-03-29 11:23:23 -07:00
|
|
|
candidates[0].Assets = sdk.NewRat(200)
|
2018-03-30 11:23:16 -07:00
|
|
|
candidates[1].Assets = sdk.NewRat(100)
|
2018-03-29 10:37:04 -07:00
|
|
|
keeper.setCandidate(ctx, candidates[0])
|
|
|
|
keeper.setCandidate(ctx, candidates[1])
|
2018-04-02 11:37:35 -07:00
|
|
|
|
2018-03-29 10:37:04 -07:00
|
|
|
acc = keeper.getAccUpdateValidators(ctx)
|
2018-03-30 11:23:16 -07:00
|
|
|
require.Equal(t, 2, len(acc))
|
|
|
|
candidates = keeper.GetCandidates(ctx, 5)
|
|
|
|
require.Equal(t, 2, len(candidates))
|
2018-04-05 09:31:36 -07:00
|
|
|
require.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0])
|
|
|
|
require.Equal(t, candidates[1].validator().abciValidator(keeper.cdc), acc[1])
|
2018-03-30 11:23:16 -07:00
|
|
|
|
|
|
|
// test validtor added at the beginning
|
2018-04-02 11:37:35 -07:00
|
|
|
// candidate set: {c1, c3} -> {c0, c1, c3}
|
|
|
|
// accUpdate set: {} -> {c0}
|
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
|
|
|
|
2018-03-31 12:39:38 -07:00
|
|
|
keeper.setCandidate(ctx, candidatesIn[0])
|
2018-03-29 10:37:04 -07:00
|
|
|
acc = keeper.getAccUpdateValidators(ctx)
|
2018-04-02 11:37:35 -07:00
|
|
|
require.Equal(t, 1, len(acc))
|
2018-03-30 11:23:16 -07:00
|
|
|
candidates = keeper.GetCandidates(ctx, 5)
|
|
|
|
require.Equal(t, 3, len(candidates))
|
2018-04-05 09:31:36 -07:00
|
|
|
assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0])
|
2018-03-29 10:37:04 -07:00
|
|
|
|
2018-03-29 11:23:23 -07:00
|
|
|
// test validator added at the middle
|
2018-04-02 11:37:35 -07:00
|
|
|
// candidate set: {c0, c1, c3} -> {c0, c1, c2, c3]
|
|
|
|
// accUpdate set: {} -> {c2}
|
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, 3, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
|
|
|
|
2018-03-31 12:39:38 -07:00
|
|
|
keeper.setCandidate(ctx, candidatesIn[2])
|
2018-03-29 11:23:23 -07:00
|
|
|
acc = keeper.getAccUpdateValidators(ctx)
|
2018-04-02 11:37:35 -07:00
|
|
|
require.Equal(t, 1, len(acc))
|
2018-03-30 11:23:16 -07:00
|
|
|
candidates = keeper.GetCandidates(ctx, 5)
|
|
|
|
require.Equal(t, 4, len(candidates))
|
2018-04-05 09:31:36 -07:00
|
|
|
assert.Equal(t, candidates[2].validator().abciValidator(keeper.cdc), acc[0])
|
2018-03-30 11:23:16 -07:00
|
|
|
|
2018-03-31 12:39:38 -07:00
|
|
|
// test candidate added at the end but not inserted in the valset
|
2018-04-02 11:37:35 -07:00
|
|
|
// candidate set: {c0, c1, c2, c3} -> {c0, c1, c2, c3, c4}
|
|
|
|
// validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3}
|
|
|
|
// accUpdate set: {} -> {}
|
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, 4, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 4, len(keeper.GetValidators(ctx)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
|
|
|
|
2018-03-31 12:39:38 -07:00
|
|
|
keeper.setCandidate(ctx, candidatesIn[4])
|
2018-04-02 11:37:35 -07:00
|
|
|
|
|
|
|
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 4, len(keeper.GetValidators(ctx)))
|
|
|
|
require.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) // max validator number is 4
|
2018-03-30 11:23:16 -07:00
|
|
|
|
2018-03-31 12:39:38 -07:00
|
|
|
// test candidate change its power but still not in the valset
|
2018-04-02 11:37:35 -07:00
|
|
|
// candidate set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4}
|
|
|
|
// validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3}
|
|
|
|
// accUpdate set: {} -> {}
|
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 4, len(keeper.GetValidators(ctx)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
|
|
|
|
|
|
|
candidatesIn[4].Assets = sdk.NewRat(1)
|
2018-03-31 12:39:38 -07:00
|
|
|
keeper.setCandidate(ctx, candidatesIn[4])
|
2018-04-02 11:37:35 -07:00
|
|
|
|
|
|
|
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 4, len(keeper.GetValidators(ctx)))
|
|
|
|
require.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) // max validator number is 4
|
2018-03-30 11:23:16 -07:00
|
|
|
|
2018-04-02 13:18:54 -07:00
|
|
|
// test candidate change its power and become a validator (pushing out an existing)
|
2018-04-02 11:37:35 -07:00
|
|
|
// candidate set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4}
|
|
|
|
// validator set: {c0, c1, c2, c3} -> {c1, c2, c3, c4}
|
|
|
|
// accUpdate set: {} -> {c0, c4}
|
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
|
|
|
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 4, len(keeper.GetValidators(ctx)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
|
|
|
|
|
|
|
candidatesIn[4].Assets = sdk.NewRat(1000)
|
|
|
|
keeper.setCandidate(ctx, candidatesIn[4])
|
|
|
|
|
2018-03-30 11:23:16 -07:00
|
|
|
candidates = keeper.GetCandidates(ctx, 5)
|
|
|
|
require.Equal(t, 5, len(candidates))
|
2018-04-02 11:37:35 -07:00
|
|
|
vals = keeper.GetValidators(ctx)
|
|
|
|
require.Equal(t, 4, len(vals))
|
2018-04-02 13:18:54 -07:00
|
|
|
assert.Equal(t, candidatesIn[1].Address, vals[1].Address)
|
|
|
|
assert.Equal(t, candidatesIn[2].Address, vals[3].Address)
|
|
|
|
assert.Equal(t, candidatesIn[3].Address, vals[2].Address)
|
|
|
|
assert.Equal(t, candidatesIn[4].Address, vals[0].Address)
|
|
|
|
|
2018-04-02 11:37:35 -07:00
|
|
|
acc = keeper.getAccUpdateValidators(ctx)
|
|
|
|
require.Equal(t, 2, len(acc), "%v", acc)
|
|
|
|
|
2018-04-05 09:31:36 -07:00
|
|
|
assert.Equal(t, wirePK(candidatesIn[0].PubKey), acc[0].PubKey)
|
|
|
|
assert.Equal(t, int64(0), acc[0].Power)
|
|
|
|
assert.Equal(t, vals[0].abciValidator(keeper.cdc), acc[1])
|
2018-03-30 11:23:16 -07:00
|
|
|
|
|
|
|
// test from something to nothing
|
2018-04-02 13:18:54 -07:00
|
|
|
// candidate set: {c0, c1, c2, c3, c4} -> {}
|
|
|
|
// validator set: {c1, c2, c3, c4} -> {}
|
|
|
|
// accUpdate set: {} -> {c1, c2, c3, c4}
|
2018-04-02 11:37:35 -07:00
|
|
|
keeper.clearAccUpdateValidators(ctx)
|
2018-04-02 13:18:54 -07:00
|
|
|
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
|
|
|
|
assert.Equal(t, 4, len(keeper.GetValidators(ctx)))
|
|
|
|
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx)))
|
|
|
|
|
|
|
|
keeper.removeCandidate(ctx, candidatesIn[0].Address)
|
|
|
|
keeper.removeCandidate(ctx, candidatesIn[1].Address)
|
|
|
|
keeper.removeCandidate(ctx, candidatesIn[2].Address)
|
|
|
|
keeper.removeCandidate(ctx, candidatesIn[3].Address)
|
|
|
|
keeper.removeCandidate(ctx, candidatesIn[4].Address)
|
|
|
|
|
|
|
|
vals = keeper.GetValidators(ctx)
|
|
|
|
assert.Equal(t, 0, len(vals), "%v", vals)
|
2018-03-30 11:23:16 -07:00
|
|
|
candidates = keeper.GetCandidates(ctx, 5)
|
|
|
|
require.Equal(t, 0, len(candidates))
|
2018-04-02 13:18:54 -07:00
|
|
|
acc = keeper.getAccUpdateValidators(ctx)
|
|
|
|
require.Equal(t, 4, len(acc))
|
2018-04-05 09:31:36 -07:00
|
|
|
assert.Equal(t, wirePK(candidatesIn[1].PubKey), acc[0].PubKey)
|
|
|
|
assert.Equal(t, wirePK(candidatesIn[2].PubKey), acc[1].PubKey)
|
|
|
|
assert.Equal(t, wirePK(candidatesIn[3].PubKey), acc[2].PubKey)
|
|
|
|
assert.Equal(t, wirePK(candidatesIn[4].PubKey), acc[3].PubKey)
|
|
|
|
assert.Equal(t, int64(0), acc[0].Power)
|
|
|
|
assert.Equal(t, int64(0), acc[1].Power)
|
|
|
|
assert.Equal(t, int64(0), acc[2].Power)
|
|
|
|
assert.Equal(t, int64(0), acc[3].Power)
|
2018-03-27 17:17:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// test if is a validator from the last update
|
|
|
|
func TestIsRecentValidator(t *testing.T) {
|
2018-04-01 09:05:58 -07:00
|
|
|
ctx, _, keeper := createTestInput(t, false, 0)
|
2018-03-27 17:17:11 -07:00
|
|
|
|
2018-03-30 11:23:16 -07:00
|
|
|
amts := []int64{9, 8, 7, 10, 6}
|
|
|
|
var candidatesIn [5]Candidate
|
|
|
|
for i, amt := range amts {
|
|
|
|
candidatesIn[i] = Candidate{
|
|
|
|
Address: addrVals[i],
|
|
|
|
PubKey: pks[i],
|
|
|
|
Assets: sdk.NewRat(amt),
|
|
|
|
Liabilities: sdk.NewRat(amt),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// test that an empty validator set doesn't have any validators
|
2018-03-29 10:37:04 -07:00
|
|
|
validators := keeper.GetValidators(ctx)
|
|
|
|
assert.Equal(t, 0, len(validators))
|
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// get the validators for the first time
|
2018-03-30 11:23:16 -07:00
|
|
|
keeper.setCandidate(ctx, candidatesIn[0])
|
|
|
|
keeper.setCandidate(ctx, candidatesIn[1])
|
2018-03-29 10:37:04 -07:00
|
|
|
validators = keeper.GetValidators(ctx)
|
|
|
|
require.Equal(t, 2, len(validators))
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.Equal(t, candidatesIn[0].validator(), validators[0])
|
|
|
|
assert.Equal(t, candidatesIn[1].validator(), validators[1])
|
2018-03-29 10:37:04 -07:00
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// test a basic retrieve of something that should be a recent validator
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.True(t, keeper.IsRecentValidator(ctx, candidatesIn[0].Address))
|
|
|
|
assert.True(t, keeper.IsRecentValidator(ctx, candidatesIn[1].Address))
|
2018-03-29 10:37:04 -07:00
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// test a basic retrieve of something that should not be a recent validator
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.False(t, keeper.IsRecentValidator(ctx, candidatesIn[2].Address))
|
2018-03-29 10:37:04 -07:00
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// remove that validator, but don't retrieve the recent validator group
|
2018-03-30 11:23:16 -07:00
|
|
|
keeper.removeCandidate(ctx, candidatesIn[0].Address)
|
2018-03-29 10:37:04 -07:00
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
// test that removed validator is not considered a recent validator
|
2018-03-30 11:23:16 -07:00
|
|
|
assert.False(t, keeper.IsRecentValidator(ctx, candidatesIn[0].Address))
|
2018-03-22 09:00:45 -07:00
|
|
|
}
|
2018-01-11 21:30:39 -08:00
|
|
|
|
2018-03-22 09:00:45 -07:00
|
|
|
func TestParams(t *testing.T) {
|
2018-04-01 09:05:58 -07:00
|
|
|
ctx, _, keeper := createTestInput(t, false, 0)
|
2018-03-22 09:00:45 -07:00
|
|
|
expParams := defaultParams()
|
2018-01-11 21:30:39 -08:00
|
|
|
|
2018-03-20 06:56:07 -07:00
|
|
|
//check that the empty keeper loads the default
|
2018-03-22 10:10:25 -07:00
|
|
|
resParams := keeper.GetParams(ctx)
|
2018-03-22 09:00:45 -07:00
|
|
|
assert.Equal(t, expParams, resParams)
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
//modify a params, save, and retrieve
|
2018-03-22 09:00:45 -07:00
|
|
|
expParams.MaxValidators = 777
|
|
|
|
keeper.setParams(ctx, expParams)
|
2018-03-22 10:10:25 -07:00
|
|
|
resParams = keeper.GetParams(ctx)
|
2018-03-22 09:00:45 -07:00
|
|
|
assert.Equal(t, expParams, resParams)
|
2018-01-11 21:30:39 -08:00
|
|
|
}
|
2018-03-28 13:37:42 -07:00
|
|
|
|
|
|
|
func TestPool(t *testing.T) {
|
2018-04-01 09:05:58 -07:00
|
|
|
ctx, _, keeper := createTestInput(t, false, 0)
|
2018-03-28 13:37:42 -07:00
|
|
|
expPool := initialPool()
|
|
|
|
|
|
|
|
//check that the empty keeper loads the default
|
|
|
|
resPool := keeper.GetPool(ctx)
|
|
|
|
assert.Equal(t, expPool, resPool)
|
|
|
|
|
|
|
|
//modify a params, save, and retrieve
|
|
|
|
expPool.TotalSupply = 777
|
|
|
|
keeper.setPool(ctx, expPool)
|
|
|
|
resPool = keeper.GetPool(ctx)
|
|
|
|
assert.Equal(t, expPool, resPool)
|
|
|
|
}
|
2018-04-03 11:50:31 -07:00
|
|
|
|
|
|
|
func TestInitGenesis(t *testing.T) {
|
2018-04-03 12:15:08 -07:00
|
|
|
ctx, _, keeper := createTestInput(t, false, 0)
|
2018-04-03 12:47:26 -07:00
|
|
|
jsonStr := `{
|
|
|
|
"params": {
|
|
|
|
"inflation_rate_change": {"num": 13, "denom": 100},
|
|
|
|
"inflation_max": {"num": 20, "denom": 100},
|
|
|
|
"inflation_min": {"num": 7, "denom": 100},
|
|
|
|
"goal_bonded": {"num": 67, "denom": 100},
|
|
|
|
"max_validators": 100,
|
|
|
|
"bond_denom": "fermion"
|
|
|
|
},
|
|
|
|
"pool": {
|
|
|
|
"total_supply": 0,
|
|
|
|
"bonded_shares": {"num": 0, "denom": 1},
|
|
|
|
"unbonded_shares": {"num": 0, "denom": 1},
|
|
|
|
"bonded_pool": 0,
|
|
|
|
"unbonded_pool": 0,
|
|
|
|
"inflation_last_time": 0,
|
|
|
|
"inflation": {"num": 7, "denom": 100}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
encoded := json.RawMessage(jsonStr)
|
2018-04-03 11:50:31 -07:00
|
|
|
err := keeper.InitGenesis(ctx, encoded)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, keeper.GetPool(ctx), initialPool())
|
|
|
|
require.Equal(t, keeper.GetParams(ctx), defaultParams())
|
|
|
|
}
|