2018-01-11 21:30:39 -08:00
|
|
|
package stake
|
|
|
|
|
|
|
|
import (
|
2018-01-18 00:39:16 -08:00
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
2018-01-11 21:30:39 -08:00
|
|
|
"testing"
|
|
|
|
|
2018-01-18 00:39:16 -08:00
|
|
|
sdkstore "github.com/cosmos/cosmos-sdk/store"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types"
|
2018-01-11 21:30:39 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-01-18 00:39:16 -08:00
|
|
|
crypto "github.com/tendermint/go-crypto"
|
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
2018-01-11 21:30:39 -08:00
|
|
|
)
|
|
|
|
|
2018-01-18 00:39:16 -08:00
|
|
|
func newPubKey(pk string) (res crypto.PubKey, err error) {
|
|
|
|
pkBytes, err := hex.DecodeString(pk)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//res, err = crypto.PubKeyFromBytes(pkBytes)
|
|
|
|
var pkEd crypto.PubKeyEd25519
|
|
|
|
copy(pkEd[:], pkBytes[:])
|
|
|
|
return pkEd, nil
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:30:39 -08:00
|
|
|
func TestState(t *testing.T) {
|
|
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
|
|
|
|
db, err := dbm.NewGoLevelDB("basecoin", "basecoin-data")
|
|
|
|
require.Nil(err)
|
2018-01-18 00:39:16 -08:00
|
|
|
cacheSize := 10000
|
|
|
|
numHistory := int64(100)
|
|
|
|
stakeLoader := sdkstore.NewIAVLStoreLoader(db, cacheSize, numHistory)
|
|
|
|
var stakeStoreKey = types.NewKVStoreKey("stake")
|
|
|
|
multiStore := sdkstore.NewCommitMultiStore(db)
|
|
|
|
multiStore.SetSubstoreLoader(stakeStoreKey, stakeLoader)
|
|
|
|
multiStore.LoadLatestVersion()
|
|
|
|
store := multiStore.GetKVStore(stakeStoreKey)
|
2018-01-22 10:09:05 -08:00
|
|
|
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
|
|
|
|
cdc.RegisterConcrete(crypto.PubKeyEd25519{}, "crypto/PubKeyEd25519", nil)
|
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-01-11 21:30:39 -08:00
|
|
|
|
2018-01-18 00:39:16 -08:00
|
|
|
pk, err := newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57")
|
|
|
|
require.Nil(err)
|
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-01-18 11:00:03 -08:00
|
|
|
Assets: 9, //rational.New(9),
|
|
|
|
Liabilities: 9, // rational.New(9),
|
|
|
|
VotingPower: 0, //rational.Zero,
|
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-01-22 10:09:05 -08:00
|
|
|
c1.Assets == c2.Assets &&
|
|
|
|
c1.Liabilities == c2.Liabilities &&
|
|
|
|
c1.VotingPower == c2.VotingPower &&
|
2018-01-11 21:30:39 -08:00
|
|
|
c1.Description == c2.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the empty store first
|
|
|
|
resCand := loadCandidate(store, pk)
|
|
|
|
assert.Nil(resCand)
|
|
|
|
resPks := loadCandidatesPubKeys(store)
|
|
|
|
assert.Zero(len(resPks))
|
|
|
|
|
|
|
|
// set and retrieve a record
|
|
|
|
saveCandidate(store, candidate)
|
|
|
|
resCand = loadCandidate(store, pk)
|
|
|
|
assert.True(candidatesEqual(candidate, resCand))
|
|
|
|
|
|
|
|
// modify a records, save, and retrieve
|
2018-01-18 11:00:03 -08:00
|
|
|
candidate.Liabilities = 99 //rational.New(99)
|
2018-01-11 21:30:39 -08:00
|
|
|
saveCandidate(store, candidate)
|
|
|
|
resCand = loadCandidate(store, pk)
|
|
|
|
assert.True(candidatesEqual(candidate, resCand))
|
|
|
|
|
|
|
|
// also test that the pubkey has been added to pubkey list
|
|
|
|
resPks = loadCandidatesPubKeys(store)
|
|
|
|
require.Equal(1, len(resPks))
|
|
|
|
assert.Equal(pk, resPks[0])
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Bond checks
|
|
|
|
|
|
|
|
bond := &DelegatorBond{
|
|
|
|
PubKey: pk,
|
2018-01-18 11:00:03 -08:00
|
|
|
Shares: 9, // rational.New(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)
|
|
|
|
assert.Nil(resBond)
|
|
|
|
|
|
|
|
//Set and retrieve a record
|
|
|
|
saveDelegatorBond(store, delegator, bond)
|
|
|
|
resBond = loadDelegatorBond(store, delegator, pk)
|
|
|
|
assert.True(bondsEqual(bond, resBond))
|
|
|
|
|
|
|
|
//modify a records, save, and retrieve
|
2018-01-18 11:00:03 -08:00
|
|
|
bond.Shares = 99 //rational.New(99)
|
2018-01-11 21:30:39 -08:00
|
|
|
saveDelegatorBond(store, delegator, bond)
|
|
|
|
resBond = loadDelegatorBond(store, delegator, pk)
|
|
|
|
assert.True(bondsEqual(bond, resBond))
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Param checks
|
|
|
|
|
|
|
|
params := defaultParams()
|
|
|
|
|
|
|
|
//check that the empty store loads the default
|
|
|
|
resParams := loadParams(store)
|
|
|
|
assert.Equal(params, resParams)
|
|
|
|
|
|
|
|
//modify a params, save, and retrieve
|
|
|
|
params.MaxVals = 777
|
|
|
|
saveParams(store, params)
|
|
|
|
resParams = loadParams(store)
|
|
|
|
assert.Equal(params, resParams)
|
|
|
|
}
|
2018-01-23 01:04:36 -08:00
|
|
|
|
2018-01-25 01:06:25 -08:00
|
|
|
func candidatesFromActors(actors []sdk.Actor, amts []int) (candidates Candidates) {
|
|
|
|
for i := 0; i < len(actors); i++ {
|
|
|
|
c := &Candidate{
|
|
|
|
PubKey: pks[i],
|
|
|
|
Owner: actors[i],
|
|
|
|
Shares: int64(amts[i]),
|
|
|
|
VotingPower: int64(amts[i]),
|
|
|
|
}
|
|
|
|
candidates = append(candidates, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetValidators(t *testing.T) {
|
|
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
|
|
|
|
N := 5
|
|
|
|
actors := newActors(N)
|
|
|
|
candidates := candidatesFromActors(actors, []int{400, 200, 0, 0, 0})
|
|
|
|
|
|
|
|
validators := candidates.Validators()
|
|
|
|
require.Equal(2, len(validators))
|
|
|
|
assert.Equal(candidates[0].PubKey, validators[0].PubKey)
|
|
|
|
assert.Equal(candidates[1].PubKey, validators[1].PubKey)
|
|
|
|
}
|