2018-01-11 21:30:39 -08:00
|
|
|
package stake
|
|
|
|
|
|
|
|
import (
|
2018-01-18 00:39:16 -08:00
|
|
|
"bytes"
|
2018-01-11 21:30:39 -08:00
|
|
|
"testing"
|
|
|
|
|
2018-02-20 07:55:53 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types"
|
2018-02-02 08:38:40 -08: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-01-31 18:56:46 -08:00
|
|
|
// XXX XXX XXX
|
|
|
|
// XXX revive these tests but for the store update proceedure
|
|
|
|
// XXX XXX XXX
|
|
|
|
|
|
|
|
//func TestUpdateVotingPower(t *testing.T) {
|
|
|
|
//assert := assert.New(t)
|
|
|
|
//store := initTestStore(t)
|
|
|
|
//params := loadParams(store)
|
|
|
|
//gs := loadGlobalState(store)
|
|
|
|
|
|
|
|
//N := 5
|
|
|
|
//actors := newAddrs(N)
|
|
|
|
//candidates := candidatesFromActors(actors, []int64{400, 200, 100, 10, 1})
|
|
|
|
|
|
|
|
//// test a basic change in voting power
|
2018-02-23 05:13:55 -08:00
|
|
|
//candidates[0].Assets = types.NewRat(500)
|
2018-01-31 18:56:46 -08:00
|
|
|
//candidates.updateVotingPower(store, gs, params)
|
|
|
|
//assert.Equal(int64(500), candidates[0].VotingPower.Evaluate(), "%v", candidates[0])
|
|
|
|
|
|
|
|
//// test a swap in voting power
|
2018-02-23 05:13:55 -08:00
|
|
|
//candidates[1].Assets = types.NewRat(600)
|
2018-01-31 18:56:46 -08:00
|
|
|
//candidates.updateVotingPower(store, gs, params)
|
|
|
|
//assert.Equal(int64(600), candidates[0].VotingPower.Evaluate(), "%v", candidates[0])
|
|
|
|
//assert.Equal(int64(500), candidates[1].VotingPower.Evaluate(), "%v", candidates[1])
|
|
|
|
|
|
|
|
//// test the max validators term
|
|
|
|
//params.MaxVals = 4
|
|
|
|
//saveParams(store, params)
|
|
|
|
//candidates.updateVotingPower(store, gs, params)
|
|
|
|
//assert.Equal(int64(0), candidates[4].VotingPower.Evaluate(), "%v", candidates[4])
|
|
|
|
//}
|
|
|
|
|
|
|
|
//func TestValidatorsChanged(t *testing.T) {
|
|
|
|
//require := require.New(t)
|
|
|
|
|
2018-02-23 05:13:55 -08:00
|
|
|
//v1 := (&Candidate{PubKey: pks[0], VotingPower: types.NewRat(10)}).validator()
|
|
|
|
//v2 := (&Candidate{PubKey: pks[1], VotingPower: types.NewRat(10)}).validator()
|
|
|
|
//v3 := (&Candidate{PubKey: pks[2], VotingPower: types.NewRat(10)}).validator()
|
|
|
|
//v4 := (&Candidate{PubKey: pks[3], VotingPower: types.NewRat(10)}).validator()
|
|
|
|
//v5 := (&Candidate{PubKey: pks[4], VotingPower: types.NewRat(10)}).validator()
|
2018-01-31 18:56:46 -08:00
|
|
|
|
|
|
|
//// test from nothing to something
|
|
|
|
//vs1 := []Validator{}
|
|
|
|
//vs2 := []Validator{v1, v2}
|
|
|
|
//changed := vs1.validatorsUpdated(vs2)
|
|
|
|
//require.Equal(2, len(changed))
|
|
|
|
//testChange(t, vs2[0], changed[0])
|
|
|
|
//testChange(t, vs2[1], changed[1])
|
|
|
|
|
|
|
|
//// test from something to nothing
|
|
|
|
//vs1 = []Validator{v1, v2}
|
|
|
|
//vs2 = []Validator{}
|
|
|
|
//changed = vs1.validatorsUpdated(vs2)
|
|
|
|
//require.Equal(2, len(changed))
|
|
|
|
//testRemove(t, vs1[0], changed[0])
|
|
|
|
//testRemove(t, vs1[1], changed[1])
|
|
|
|
|
|
|
|
//// test identical
|
|
|
|
//vs1 = []Validator{v1, v2, v4}
|
|
|
|
//vs2 = []Validator{v1, v2, v4}
|
|
|
|
//changed = vs1.validatorsUpdated(vs2)
|
2018-02-23 05:13:55 -08:00
|
|
|
//require.ZeroRat(len(changed))
|
2018-01-31 18:56:46 -08:00
|
|
|
|
|
|
|
//// test single value change
|
2018-02-23 05:13:55 -08:00
|
|
|
//vs2[2].VotingPower = types.OneRat
|
2018-01-31 18:56:46 -08:00
|
|
|
//changed = vs1.validatorsUpdated(vs2)
|
|
|
|
//require.Equal(1, len(changed))
|
|
|
|
//testChange(t, vs2[2], changed[0])
|
|
|
|
|
|
|
|
//// test multiple value change
|
2018-02-23 05:13:55 -08:00
|
|
|
//vs2[0].VotingPower = types.NewRat(11)
|
|
|
|
//vs2[2].VotingPower = types.NewRat(5)
|
2018-01-31 18:56:46 -08:00
|
|
|
//changed = vs1.validatorsUpdated(vs2)
|
|
|
|
//require.Equal(2, len(changed))
|
|
|
|
//testChange(t, vs2[0], changed[0])
|
|
|
|
//testChange(t, vs2[2], changed[1])
|
|
|
|
|
|
|
|
//// test validator added at the beginning
|
|
|
|
//vs1 = []Validator{v2, v4}
|
|
|
|
//vs2 = []Validator{v2, v4, v1}
|
|
|
|
//changed = vs1.validatorsUpdated(vs2)
|
|
|
|
//require.Equal(1, len(changed))
|
|
|
|
//testChange(t, vs2[0], changed[0])
|
|
|
|
|
|
|
|
//// test validator added in the middle
|
|
|
|
//vs1 = []Validator{v1, v2, v4}
|
|
|
|
//vs2 = []Validator{v3, v1, v4, v2}
|
|
|
|
//changed = vs1.validatorsUpdated(vs2)
|
|
|
|
//require.Equal(1, len(changed))
|
|
|
|
//testChange(t, vs2[2], changed[0])
|
|
|
|
|
|
|
|
//// test validator added at the end
|
|
|
|
//vs2 = []Validator{v1, v2, v4, v5}
|
|
|
|
//changed = vs1.validatorsUpdated(vs2)
|
2018-02-02 08:38:40 -08:00
|
|
|
//require.Equal(1, len(changed)) //testChange(t, vs2[3], changed[0]) //// test multiple validators added //vs2 = []Validator{v1, v2, v3, v4, v5} //changed = vs1.validatorsUpdated(vs2) //require.Equal(2, len(changed)) //testChange(t, vs2[2], changed[0]) //testChange(t, vs2[4], changed[1]) //// test validator removed at the beginning //vs2 = []Validator{v2, v4} //changed = vs1.validatorsUpdated(vs2) //require.Equal(1, len(changed)) //testRemove(t, vs1[0], changed[0]) //// test validator removed in the middle //vs2 = []Validator{v1, v4} //changed = vs1.validatorsUpdated(vs2) //require.Equal(1, len(changed)) //testRemove(t, vs1[1], changed[0]) //// test validator removed at the end
|
2018-01-31 18:56:46 -08:00
|
|
|
//vs2 = []Validator{v1, v2}
|
|
|
|
//changed = vs1.validatorsUpdated(vs2)
|
|
|
|
//require.Equal(1, len(changed))
|
|
|
|
//testRemove(t, vs1[2], changed[0])
|
|
|
|
|
|
|
|
//// test multiple validators removed
|
|
|
|
//vs2 = []Validator{v1}
|
|
|
|
//changed = vs1.validatorsUpdated(vs2)
|
|
|
|
//require.Equal(2, len(changed))
|
|
|
|
//testRemove(t, vs1[1], changed[0])
|
|
|
|
//testRemove(t, vs1[2], changed[1])
|
|
|
|
|
2018-02-23 05:13:55 -08:00
|
|
|
//// test many types of changes //vs2 = []Validator{v1, v3, v4, v5} //vs2[2].VotingPower = types.NewRat(11) //changed = vs1.validatorsUpdated(vs2) //require.Equal(4, len(changed), "%v", changed) // change 1, remove 1, add 2 //testRemove(t, vs1[1], changed[0]) //testChange(t, vs2[1], changed[1]) //testChange(t, vs2[2], changed[2]) //testChange(t, vs2[3], changed[3]) //} //func TestUpdateValidatorSet(t *testing.T) { //assert, require := assert.New(t), require.New(t) //store := initTestStore(t) //params := loadParams(store) //gs := loadGlobalState(store) //N := 5
|
2018-01-31 18:56:46 -08:00
|
|
|
//actors := newAddrs(N)
|
|
|
|
//candidates := candidatesFromActors(actors, []int64{400, 200, 100, 10, 1})
|
|
|
|
//for _, c := range candidates {
|
|
|
|
//saveCandidate(store, c)
|
|
|
|
//}
|
|
|
|
|
|
|
|
//// they should all already be validators
|
|
|
|
//change, err := UpdateValidatorSet(store, gs, params)
|
|
|
|
//require.Nil(err)
|
|
|
|
//require.Equal(0, len(change), "%v", change) // change 1, remove 1, add 2
|
|
|
|
|
|
|
|
//// test the max value and test again
|
|
|
|
//params.MaxVals = 4
|
|
|
|
//saveParams(store, params)
|
|
|
|
//change, err = UpdateValidatorSet(store, gs, params)
|
|
|
|
//require.Nil(err)
|
|
|
|
//require.Equal(1, len(change), "%v", change)
|
|
|
|
//testRemove(t, candidates[4].validator(), change[0])
|
|
|
|
//candidates = loadCandidates(store)
|
|
|
|
//assert.Equal(int64(0), candidates[4].VotingPower.Evaluate())
|
|
|
|
|
|
|
|
//// mess with the power's of the candidates and test
|
2018-02-23 05:13:55 -08:00
|
|
|
//candidates[0].Assets = types.NewRat(10)
|
|
|
|
//candidates[1].Assets = types.NewRat(600)
|
|
|
|
//candidates[2].Assets = types.NewRat(1000)
|
|
|
|
//candidates[3].Assets = types.OneRat
|
|
|
|
//candidates[4].Assets = types.NewRat(10)
|
2018-01-31 18:56:46 -08:00
|
|
|
//for _, c := range candidates {
|
|
|
|
//saveCandidate(store, c)
|
|
|
|
//}
|
|
|
|
//change, err = UpdateValidatorSet(store, gs, params)
|
|
|
|
//require.Nil(err)
|
|
|
|
//require.Equal(5, len(change), "%v", change) // 3 changed, 1 added, 1 removed
|
|
|
|
//candidates = loadCandidates(store)
|
|
|
|
//testChange(t, candidates[0].validator(), change[0])
|
|
|
|
//testChange(t, candidates[1].validator(), change[1])
|
|
|
|
//testChange(t, candidates[2].validator(), change[2])
|
|
|
|
//testRemove(t, candidates[3].validator(), change[3])
|
|
|
|
//testChange(t, candidates[4].validator(), change[4])
|
|
|
|
//}
|
2018-01-18 00:39:16 -08:00
|
|
|
|
2018-01-11 21:30:39 -08:00
|
|
|
func TestState(t *testing.T) {
|
2018-01-25 12:11:58 -08:00
|
|
|
store := initTestStore(t)
|
2018-01-11 21:30:39 -08:00
|
|
|
|
2018-01-18 00:39:16 -08:00
|
|
|
//delegator := crypto.Address{[]byte("addressdelegator")}
|
|
|
|
//validator := crypto.Address{[]byte("addressvalidator")}
|
|
|
|
delegator := []byte("addressdelegator")
|
|
|
|
validator := []byte("addressvalidator")
|
2018-02-02 08:38:40 -08:00
|
|
|
//pk := newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57")
|
|
|
|
pk := crypto.GenPrivKeyEd25519().PubKey()
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Candidate checks
|
|
|
|
|
|
|
|
// XXX expand to include both liabilities and assets use/test all candidate fields
|
|
|
|
candidate := &Candidate{
|
|
|
|
Owner: validator,
|
|
|
|
PubKey: pk,
|
2018-02-23 05:13:55 -08:00
|
|
|
Assets: types.NewRat(9),
|
|
|
|
Liabilities: types.NewRat(9),
|
|
|
|
VotingPower: types.ZeroRat,
|
2018-01-11 21:30:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
candidatesEqual := func(c1, c2 *Candidate) bool {
|
|
|
|
return c1.Status == c2.Status &&
|
|
|
|
c1.PubKey.Equals(c2.PubKey) &&
|
2018-01-18 00:39:16 -08:00
|
|
|
bytes.Equal(c1.Owner, c2.Owner) &&
|
2018-02-02 08:38:40 -08:00
|
|
|
c1.Assets.Equal(c2.Assets) &&
|
|
|
|
c1.Liabilities.Equal(c2.Liabilities) &&
|
|
|
|
c1.VotingPower.Equal(c2.VotingPower) &&
|
2018-01-11 21:30:39 -08:00
|
|
|
c1.Description == c2.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the empty store first
|
|
|
|
resCand := loadCandidate(store, pk)
|
2018-02-23 05:13:55 -08:00
|
|
|
assert.Nil(t, resCand)
|
2018-01-25 12:11:58 -08:00
|
|
|
resPks := loadCandidates(store)
|
2018-02-23 05:13:55 -08:00
|
|
|
assert.Zero(t, len(resPks))
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
// set and retrieve a record
|
|
|
|
saveCandidate(store, candidate)
|
|
|
|
resCand = loadCandidate(store, pk)
|
2018-02-02 08:51:08 -08:00
|
|
|
//assert.Equal(candidate, resCand)
|
2018-02-23 05:13:55 -08:00
|
|
|
assert.True(t, candidatesEqual(candidate, resCand), "%#v \n %#v", resCand, candidate)
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
// modify a records, save, and retrieve
|
2018-02-23 05:13:55 -08:00
|
|
|
candidate.Liabilities = types.NewRat(99)
|
2018-01-11 21:30:39 -08:00
|
|
|
saveCandidate(store, candidate)
|
|
|
|
resCand = loadCandidate(store, pk)
|
2018-02-23 05:13:55 -08:00
|
|
|
assert.True(t, candidatesEqual(candidate, resCand))
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
// also test that the pubkey has been added to pubkey list
|
2018-01-25 12:11:58 -08:00
|
|
|
resPks = loadCandidates(store)
|
2018-02-23 05:13:55 -08:00
|
|
|
require.Equal(t, 1, len(resPks))
|
|
|
|
assert.Equal(t, pk, resPks[0].PubKey)
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Bond checks
|
|
|
|
|
|
|
|
bond := &DelegatorBond{
|
|
|
|
PubKey: pk,
|
2018-02-23 05:13:55 -08:00
|
|
|
Shares: types.NewRat(9),
|
2018-01-11 21:30:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bondsEqual := func(b1, b2 *DelegatorBond) bool {
|
|
|
|
return b1.PubKey.Equals(b2.PubKey) &&
|
2018-01-22 10:09:05 -08:00
|
|
|
b1.Shares == b2.Shares
|
2018-01-11 21:30:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//check the empty store first
|
|
|
|
resBond := loadDelegatorBond(store, delegator, pk)
|
2018-02-23 05:13:55 -08:00
|
|
|
assert.Nil(t, resBond)
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
//Set and retrieve a record
|
|
|
|
saveDelegatorBond(store, delegator, bond)
|
|
|
|
resBond = loadDelegatorBond(store, delegator, pk)
|
2018-02-23 05:13:55 -08:00
|
|
|
assert.True(t, bondsEqual(bond, resBond))
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
//modify a records, save, and retrieve
|
2018-02-23 05:13:55 -08:00
|
|
|
bond.Shares = types.NewRat(99)
|
2018-01-11 21:30:39 -08:00
|
|
|
saveDelegatorBond(store, delegator, bond)
|
|
|
|
resBond = loadDelegatorBond(store, delegator, pk)
|
2018-02-23 05:13:55 -08:00
|
|
|
assert.True(t, bondsEqual(bond, resBond))
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Param checks
|
|
|
|
|
|
|
|
params := defaultParams()
|
|
|
|
|
|
|
|
//check that the empty store loads the default
|
|
|
|
resParams := loadParams(store)
|
2018-02-23 05:13:55 -08:00
|
|
|
assert.Equal(t, params, resParams)
|
2018-01-11 21:30:39 -08:00
|
|
|
|
|
|
|
//modify a params, save, and retrieve
|
|
|
|
params.MaxVals = 777
|
|
|
|
saveParams(store, params)
|
|
|
|
resParams = loadParams(store)
|
2018-02-23 05:13:55 -08:00
|
|
|
assert.Equal(t, params, resParams)
|
2018-01-11 21:30:39 -08:00
|
|
|
}
|
2018-01-23 01:04:36 -08:00
|
|
|
|
2018-01-25 01:06:25 -08:00
|
|
|
func TestGetValidators(t *testing.T) {
|
2018-01-25 12:11:58 -08:00
|
|
|
store := initTestStore(t)
|
2018-01-25 01:06:25 -08:00
|
|
|
N := 5
|
2018-01-25 12:11:58 -08:00
|
|
|
addrs := newAddrs(N)
|
2018-01-31 18:56:46 -08:00
|
|
|
candidatesFromActors(store, addrs, []int64{400, 200, 0, 0, 0})
|
2018-01-25 01:06:25 -08:00
|
|
|
|
2018-01-25 12:11:58 -08:00
|
|
|
validators := getValidators(store, 5)
|
2018-02-23 05:13:55 -08:00
|
|
|
require.Equal(t, 2, len(validators))
|
|
|
|
assert.Equal(t, pks[0], validators[0].PubKey)
|
|
|
|
assert.Equal(t, pks[1], validators[1].PubKey)
|
2018-01-25 01:06:25 -08:00
|
|
|
}
|