stake refactor, tests compiling

This commit is contained in:
rigelrozanski 2018-05-10 21:38:57 -04:00
parent 518e2490d5
commit a0c73372be
9 changed files with 470 additions and 556 deletions

View File

@ -405,7 +405,7 @@ func TestStakeMsgs(t *testing.T) {
ctxDeliver := gapp.BaseApp.NewContext(false, abci.Header{})
res1 = gapp.accountMapper.GetAccount(ctxDeliver, addr1)
require.Equal(t, genCoins.Minus(sdk.Coins{bondCoin}), res1.GetCoins())
candidate, found := gapp.stakeKeeper.GetCandidate(ctxDeliver, addr1)
candidate, found := gapp.stakeKeeper.GetValidator(ctxDeliver, addr1)
require.True(t, found)
require.Equal(t, candidate.Address, addr1)
@ -417,7 +417,7 @@ func TestStakeMsgs(t *testing.T) {
)
SignDeliver(t, gapp, editCandidacyMsg, []int64{1}, true, priv1)
candidate, found = gapp.stakeKeeper.GetCandidate(ctxDeliver, addr1)
candidate, found = gapp.stakeKeeper.GetValidator(ctxDeliver, addr1)
require.True(t, found)
require.Equal(t, candidate.Description, description)

View File

@ -0,0 +1,31 @@
package stake
//// test if is a gotValidator from the last update
//func TestGetTotalPrecommitVotingPower(t *testing.T) {
//ctx, _, keeper := createTestInput(t, false, 0)
//amts := []int64{10000, 1000, 100, 10, 1}
//var candidatesIn [5]Candidate
//for i, amt := range amts {
//candidatesIn[i] = NewCandidate(addrVals[i], pks[i], Description{})
//candidatesIn[i].BondedShares = sdk.NewRat(amt)
//candidatesIn[i].DelegatorShares = sdk.NewRat(amt)
//keeper.setCandidate(ctx, candidatesIn[i])
//}
//// test that an empty gotValidator set doesn't have any gotValidators
//gotValidators := keeper.GetValidators(ctx)
//assert.Equal(t, 5, len(gotValidators))
//totPow := keeper.GetTotalPrecommitVotingPower(ctx)
//exp := sdk.NewRat(11111)
//assert.True(t, exp.Equal(totPow), "exp %v, got %v", exp, totPow)
//// set absent gotValidators to be the 1st and 3rd record sorted by pubKey address
//ctx = ctx.WithAbsentValidators([]int32{1, 3})
//totPow = keeper.GetTotalPrecommitVotingPower(ctx)
//// XXX verify that this order should infact exclude these two records
//exp = sdk.NewRat(11100)
//assert.True(t, exp.Equal(totPow), "exp %v, got %v", exp, totPow)
//}

View File

@ -43,7 +43,7 @@ func TestDuplicatesMsgDeclareCandidacy(t *testing.T) {
assert.True(t, got.IsOK(), "%v", got)
validator, found := keeper.GetValidator(ctx, validatorAddr)
require.True(t, found)
assert.Equal(t, Unbonded, validator.Status)
assert.Equal(t, sdk.Unbonded, validator.Status)
assert.Equal(t, validatorAddr, validator.Address)
assert.Equal(t, pk, validator.PubKey)
assert.Equal(t, sdk.NewRat(10), validator.BondedShares)
@ -296,7 +296,7 @@ func TestVoidCandidacy(t *testing.T) {
require.True(t, got.IsOK(), "expected no error on runMsgDeclareCandidacy")
validator, found := keeper.GetValidator(ctx, validatorAddr)
require.True(t, found)
require.Equal(t, Revoked, validator.Status)
require.Equal(t, sdk.Revoked, validator.Status)
// test that this address cannot yet be bonded too because is revoked
got = handleMsgDelegate(ctx, msgDelegate, keeper)

View File

@ -108,7 +108,7 @@ func (k Keeper) setValidator(ctx sdk.Context, validator Validator) {
// add to the validators and return to update list if is already a validator and power is increasing
if powerIncreasing && oldValidator.Status == sdk.Bonded {
bzABCI := k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc))
store.Set(GetValidatorsTendermintUpdatesKey(address), bzABCI)
store.Set(GetTendermintUpdatesKey(address), bzABCI)
// also update the recent validator store
store.Set(GetValidatorsBondedKey(validator.PubKey), bzVal)
@ -139,7 +139,7 @@ func (k Keeper) removeValidator(ctx sdk.Context, address sdk.Address) {
return
}
bz := k.cdc.MustMarshalBinary(validator.abciValidatorZero(k.cdc))
store.Set(GetValidatorsTendermintUpdatesKey(address), bz)
store.Set(GetTendermintUpdatesKey(address), bz)
store.Delete(GetValidatorsBondedKey(validator.PubKey))
}
@ -239,7 +239,7 @@ func (k Keeper) updateValidators(ctx sdk.Context, store sdk.KVStore, updatedVali
// MOST IMPORTANTLY, add to the accumulated changes if this is the modified validator
if bytes.Equal(updatedValidatorAddr, validator.Address) {
bz = k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc))
store.Set(GetValidatorsTendermintUpdatesKey(updatedValidatorAddr), bz)
store.Set(GetTendermintUpdatesKey(updatedValidatorAddr), bz)
}
iterator.Next()
@ -252,7 +252,7 @@ func (k Keeper) updateValidators(ctx sdk.Context, store sdk.KVStore, updatedVali
var validator Validator
k.cdc.MustUnmarshalBinary(value, &validator)
bz := k.cdc.MustMarshalBinary(validator.abciValidatorZero(k.cdc))
store.Set(GetValidatorsTendermintUpdatesKey(addr), bz)
store.Set(GetTendermintUpdatesKey(addr), bz)
}
}
@ -260,10 +260,10 @@ func (k Keeper) updateValidators(ctx sdk.Context, store sdk.KVStore, updatedVali
// Accumulated updates to the active/bonded validator set for tendermint
// get the most recently updated validators
func (k Keeper) getValidatorsTendermintUpdates(ctx sdk.Context) (updates []abci.Validator) {
func (k Keeper) getTendermintUpdates(ctx sdk.Context) (updates []abci.Validator) {
store := ctx.KVStore(k.storeKey)
iterator := store.SubspaceIterator(ValidatorsTendermintUpdatesKey) //smallest to largest
iterator := store.SubspaceIterator(TendermintUpdatesKey) //smallest to largest
for ; iterator.Valid(); iterator.Next() {
valBytes := iterator.Value()
var val abci.Validator
@ -275,11 +275,11 @@ func (k Keeper) getValidatorsTendermintUpdates(ctx sdk.Context) (updates []abci.
}
// remove all validator update entries after applied to Tendermint
func (k Keeper) clearValidatorsTendermintUpdates(ctx sdk.Context) {
func (k Keeper) clearTendermintUpdates(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
// delete subspace
iterator := store.SubspaceIterator(ValidatorsTendermintUpdatesKey)
iterator := store.SubspaceIterator(TendermintUpdatesKey)
for ; iterator.Valid(); iterator.Next() {
store.Delete(iterator.Key())
}

View File

@ -17,7 +17,7 @@ var (
PoolKey = []byte{0x01} // key for global parameters relating to staking
ValidatorsKey = []byte{0x02} // prefix for each key to a validator
ValidatorsByPowerKey = []byte{0x03} // prefix for each key to a validator
ValidatorsTendermintUpdatesKey = []byte{0x04} // prefix for each key to a validator which is being updated
TendermintUpdatesKey = []byte{0x04} // prefix for each key to a validator which is being updated
ValidatorsBondedKey = []byte{0x05} // prefix for each key to bonded/actively validating validators
DelegationKey = []byte{0x06} // prefix for each key to a delegator's bond
IntraTxCounterKey = []byte{0x07} // key for block-local tx index
@ -47,8 +47,8 @@ func GetValidatorsBondedByPowerKey(validator Validator) []byte {
}
// get the key for the accumulated update validators
func GetValidatorsTendermintUpdatesKey(addr sdk.Address) []byte {
return append(ValidatorsTendermintUpdatesKey, addr.Bytes()...)
func GetTendermintUpdatesKey(addr sdk.Address) []byte {
return append(TendermintUpdatesKey, addr.Bytes()...)
}
// get the key for the current validator group, ordered like tendermint

File diff suppressed because it is too large Load Diff

View File

@ -60,16 +60,16 @@ func TestBondedToUnbondedPool(t *testing.T) {
assert.Equal(t, poolA.bondedShareExRate(), sdk.OneRat())
assert.Equal(t, poolA.unbondedShareExRate(), sdk.OneRat())
candA := Validator{
Status: Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: sdk.OneRat(),
Status: sdk.Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: sdk.OneRat(),
DelegatorShares: sdk.OneRat(),
}
poolB, candB := poolA.bondedToUnbondedPool(candA)
// status unbonded
assert.Equal(t, candB.Status, Unbonded)
assert.Equal(t, candB.Status, sdk.Unbonded)
// same exchange rate, assets unchanged
assert.Equal(t, candB.BondedShares, candA.BondedShares)
// bonded pool decreased
@ -87,17 +87,17 @@ func TestUnbonbedtoBondedPool(t *testing.T) {
assert.Equal(t, poolA.bondedShareExRate(), sdk.OneRat())
assert.Equal(t, poolA.unbondedShareExRate(), sdk.OneRat())
candA := Validator{
Status: Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: sdk.OneRat(),
Status: sdk.Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: sdk.OneRat(),
DelegatorShares: sdk.OneRat(),
}
candA.Status = Unbonded
candA.Status = sdk.Unbonded
poolB, candB := poolA.unbondedToBondedPool(candA)
// status bonded
assert.Equal(t, candB.Status, Bonded)
assert.Equal(t, candB.Status, sdk.Bonded)
// same exchange rate, assets unchanged
assert.Equal(t, candB.BondedShares, candA.BondedShares)
// bonded pool increased
@ -177,10 +177,10 @@ func TestValidatorAddTokens(t *testing.T) {
poolA := keeper.GetPool(ctx)
candA := Validator{
Status: Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: sdk.NewRat(9),
Status: sdk.Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: sdk.NewRat(9),
DelegatorShares: sdk.NewRat(9),
}
poolA.BondedPool = candA.BondedShares.Evaluate()
@ -203,10 +203,10 @@ func TestValidatorRemoveShares(t *testing.T) {
poolA := keeper.GetPool(ctx)
candA := Validator{
Status: Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: sdk.NewRat(9),
Status: sdk.Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: sdk.NewRat(9),
DelegatorShares: sdk.NewRat(9),
}
poolA.BondedPool = candA.BondedShares.Evaluate()
@ -227,10 +227,10 @@ func TestValidatorRemoveShares(t *testing.T) {
assets := sdk.NewRat(5102)
liabilities := sdk.NewRat(115)
cand := Validator{
Status: Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: assets,
Status: sdk.Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: assets,
DelegatorShares: liabilities,
}
pool := Pool{
@ -258,19 +258,19 @@ func TestValidatorRemoveShares(t *testing.T) {
// generate a random validator
func randomValidator(r *rand.Rand) Validator {
var status ValidatorStatus
var status sdk.ValidatorStatus
if r.Float64() < float64(0.5) {
status = Bonded
status = sdk.Bonded
} else {
status = Unbonded
status = sdk.Unbonded
}
assets := sdk.NewRat(int64(r.Int31n(10000)))
liabilities := sdk.NewRat(int64(r.Int31n(10000)))
return Validator{
Status: status,
Address: addrs[0],
PubKey: pks[0],
BondedShares: assets,
Status: status,
Address: addrs[0],
PubKey: pks[0],
BondedShares: assets,
DelegatorShares: liabilities,
}
}
@ -290,10 +290,10 @@ func randomSetup(r *rand.Rand, numValidators int) (Pool, Validators) {
validators := make([]Validator, numValidators)
for i := 0; i < numValidators; i++ {
validator := randomValidator(r)
if validator.Status == Bonded {
if validator.Status == sdk.Bonded {
pool.BondedShares = pool.BondedShares.Add(validator.BondedShares)
pool.BondedPool += validator.BondedShares.Evaluate()
} else if validator.Status == Unbonded {
} else if validator.Status == sdk.Unbonded {
pool.UnbondedShares = pool.UnbondedShares.Add(validator.BondedShares)
pool.UnbondedPool += validator.BondedShares.Evaluate()
}
@ -310,13 +310,13 @@ type Operation func(r *rand.Rand, p Pool, c Validator) (Pool, Validator, int64,
// operation: bond or unbond a validator depending on current status
func OpBondOrUnbond(r *rand.Rand, p Pool, cand Validator) (Pool, Validator, int64, string) {
var msg string
if cand.Status == Bonded {
msg = fmt.Sprintf("Unbonded previously bonded validator %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)",
if cand.Status == sdk.Bonded {
msg = fmt.Sprintf("sdk.Unbonded previously bonded validator %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)",
cand.Address, cand.BondedShares, cand.DelegatorShares, cand.delegatorShareExRate())
p, cand = p.bondedToUnbondedPool(cand)
} else if cand.Status == Unbonded {
msg = fmt.Sprintf("Bonded previously unbonded validator %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)",
} else if cand.Status == sdk.Unbonded {
msg = fmt.Sprintf("sdk.Bonded previously unbonded validator %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)",
cand.Address, cand.BondedShares, cand.DelegatorShares, cand.delegatorShareExRate())
p, cand = p.unbondedToBondedPool(cand)
}
@ -436,10 +436,10 @@ func TestPossibleOverflow(t *testing.T) {
assets := sdk.NewRat(2159)
liabilities := sdk.NewRat(391432570689183511).Quo(sdk.NewRat(40113011844664))
cand := Validator{
Status: Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: assets,
Status: sdk.Bonded,
Address: addrs[0],
PubKey: pks[0],
BondedShares: assets,
DelegatorShares: liabilities,
}
pool := Pool{

View File

@ -30,8 +30,8 @@ func (k Keeper) Tick(ctx sdk.Context) (change []abci.Validator) {
k.setIntraTxCounter(ctx, 0)
// calculate validator set changes
change = k.getValidatorsTendermintUpdates(ctx)
k.clearValidatorsTendermintUpdates(ctx)
change = k.getTendermintUpdates(ctx)
k.clearTendermintUpdates(ctx)
// XXX get the total validator of the previous validator set
// XXX get the total validator of the current validator set

View File

@ -69,14 +69,14 @@ func TestProcessProvisions(t *testing.T) {
validators := make([]Validator, 10)
for i := 0; i < 10; i++ {
c := Validator{
Status: Unbonded,
PubKey: pks[i],
Address: addrs[i],
BondedShares: sdk.NewRat(0),
Status: sdk.Unbonded,
PubKey: pks[i],
Address: addrs[i],
BondedShares: sdk.NewRat(0),
DelegatorShares: sdk.NewRat(0),
}
if i < 5 {
c.Status = Bonded
c.Status = sdk.Bonded
}
mintedTokens := int64((i + 1) * 10000000)
pool.TotalSupply += mintedTokens