Skip to content

Commit

Permalink
staking orig tests passing with new store
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Mar 28, 2018
1 parent 7d4528e commit eb597e1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ examples/basecoin/glide.lock
examples/basecoin/app/data
baseapp/data/*
docs/_build
<<<<<<< HEAD
.DS_Store
coverage.txt
profile.out
Expand Down
56 changes: 30 additions & 26 deletions x/stake/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package stake
import (
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
"github.com/tendermint/tmlibs/rational"

"github.com/cosmos/cosmos-sdk/types"
)

/////////////////////////////////////////////////////////// temp types

var cdc = wire.NewCodec()

//nolint
type Params struct {
HoldBonded crypto.Address `json:"hold_bonded"` // account where all bonded coins are held
Expand All @@ -34,10 +35,10 @@ func defaultParams() Params {
return Params{
HoldBonded: []byte("77777777777777777777777777777777"),
HoldUnbonded: []byte("88888888888888888888888888888888"),
InflationRateChange: rational.New(13, 100),
InflationMax: rational.New(20, 100),
InflationMin: rational.New(7, 100),
GoalBonded: rational.New(67, 100),
InflationRateChange: 13, //rational.New(13, 100),
InflationMax: 20, //rational.New(20, 100),
InflationMin: 7, //rational.New(7, 100),
GoalBonded: 67, //rational.New(67, 100),
MaxVals: 100,
AllowedBondDenom: "fermion",
GasDeclareCandidacy: 20,
Expand All @@ -63,12 +64,12 @@ type GlobalState struct {
func initialGlobalState() *GlobalState {
return &GlobalState{
TotalSupply: 0,
BondedShares: rational.Zero,
UnbondedShares: rational.Zero,
BondedShares: 0, //rational.Zero,
UnbondedShares: 0, //rational.Zero,
BondedPool: 0,
UnbondedPool: 0,
InflationLastTime: 0,
Inflation: rational.New(7, 100),
Inflation: 0, //rational.New(7, 100),
}
}

Expand Down Expand Up @@ -99,6 +100,9 @@ type Candidate struct {
Description Description `json:"description"` // Description terms for the candidate
}

//nolint
type Candidates []*Candidate

// Description - description fields for a candidate
type Description struct {
Moniker string `json:"moniker"`
Expand All @@ -113,9 +117,9 @@ func NewCandidate(pubKey crypto.PubKey, owner crypto.Address, description Descri
Status: Unbonded,
PubKey: pubKey,
Owner: owner,
Assets: rational.Zero,
Liabilities: rational.Zero,
VotingPower: rational.Zero,
Assets: 0, // rational.Zero,
Liabilities: 0, // rational.Zero,
VotingPower: 0, //rational.Zero,
Description: description,
}
}
Expand Down Expand Up @@ -153,7 +157,7 @@ func GetDelegatorBondKey(delegator crypto.Address, candidate crypto.PubKey) []by

// GetDelegatorBondKeyPrefix - get the prefix for a delegator for all candidates
func GetDelegatorBondKeyPrefix(delegator crypto.Address) []byte {
res, err := wire.MarshalBinary(&delegator)
res, err := cdc.MarshalBinary(&delegator)
if err != nil {
panic(err)
}
Expand All @@ -162,7 +166,7 @@ func GetDelegatorBondKeyPrefix(delegator crypto.Address) []byte {

// GetDelegatorBondsKey - get the key for list of all the delegator's bonds
func GetDelegatorBondsKey(delegator crypto.Address) []byte {
res, err := wire.MarshalBinary(&delegator)
res, err := cdc.MarshalBinary(&delegator)
if err != nil {
panic(err)
}
Expand All @@ -177,14 +181,14 @@ func loadCandidatesPubKeys(store types.KVStore) (pubKeys []crypto.PubKey) {
if bytes == nil {
return
}
err := wire.UnmarshalBinary(bytes, &pubKeys)
err := cdc.UnmarshalBinary(bytes, &pubKeys)
if err != nil {
panic(err)
}
return
}
func saveCandidatesPubKeys(store types.KVStore, pubKeys []crypto.PubKey) {
b, err := wire.MarshalBinary(pubKeys)
b, err := cdc.MarshalBinary(pubKeys)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -212,7 +216,7 @@ func loadCandidate(store types.KVStore, pubKey crypto.PubKey) *Candidate {
return nil
}
candidate := new(Candidate)
err := wire.UnmarshalBinary(b, candidate)
err := cdc.UnmarshalBinary(b, candidate)
if err != nil {
panic(err) // This error should never occure big problem if does
}
Expand All @@ -227,7 +231,7 @@ func saveCandidate(store types.KVStore, candidate *Candidate) {
saveCandidatesPubKeys(store, append(pks, candidate.PubKey))
}

b, err := wire.MarshalBinary(*candidate)
b, err := cdc.MarshalBinary(*candidate)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -259,7 +263,7 @@ func loadDelegatorCandidates(store types.KVStore,
return nil
}

err := wire.UnmarshalBinary(candidateBytes, &candidates)
err := cdc.UnmarshalBinary(candidateBytes, &candidates)
if err != nil {
panic(err)
}
Expand All @@ -277,7 +281,7 @@ func loadDelegatorBond(store types.KVStore,
}

bond := new(DelegatorBond)
err := wire.UnmarshalBinary(delegatorBytes, bond)
err := cdc.UnmarshalBinary(delegatorBytes, bond)
if err != nil {
panic(err)
}
Expand All @@ -290,15 +294,15 @@ func saveDelegatorBond(store types.KVStore, delegator crypto.Address, bond *Dele
if loadDelegatorBond(store, delegator, bond.PubKey) == nil {
pks := loadDelegatorCandidates(store, delegator)
pks = append(pks, (*bond).PubKey)
b, err := wire.MarshalBinary(pks)
b, err := cdc.MarshalBinary(pks)
if err != nil {
panic(err)
}
store.Set(GetDelegatorBondsKey(delegator), b)
}

// now actually save the bond
b, err := wire.MarshalBinary(*bond)
b, err := cdc.MarshalBinary(*bond)
if err != nil {
panic(err)
}
Expand All @@ -316,7 +320,7 @@ func removeDelegatorBond(store types.KVStore, delegator crypto.Address, candidat
pks = append(pks[:i], pks[i+1:]...)
}
}
b, err := wire.MarshalBinary(pks)
b, err := cdc.MarshalBinary(pks)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -369,15 +373,15 @@ func loadParams(store types.KVStore) (params Params) {
return defaultParams()
}

err := wire.UnmarshalBinary(b, &params)
err := cdc.UnmarshalBinary(b, &params)
if err != nil {
panic(err) // This error should never occure big problem if does
}

return
}
func saveParams(store types.KVStore, params Params) {
b, err := wire.MarshalBinary(params)
b, err := cdc.MarshalBinary(params)
if err != nil {
panic(err)
}
Expand All @@ -393,14 +397,14 @@ func loadGlobalState(store types.KVStore) (gs *GlobalState) {
return initialGlobalState()
}
gs = new(GlobalState)
err := wire.UnmarshalBinary(b, gs)
err := cdc.UnmarshalBinary(b, gs)
if err != nil {
panic(err) // This error should never occure big problem if does
}
return
}
func saveGlobalState(store types.KVStore, gs *GlobalState) {
b, err := wire.MarshalBinary(*gs)
b, err := cdc.MarshalBinary(*gs)
if err != nil {
panic(err)
}
Expand Down
13 changes: 6 additions & 7 deletions x/stake/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
dbm "github.com/tendermint/tmlibs/db"
)

Expand Down Expand Up @@ -38,8 +37,8 @@ func TestState(t *testing.T) {
multiStore.SetSubstoreLoader(stakeStoreKey, stakeLoader)
multiStore.LoadLatestVersion()
store := multiStore.GetKVStore(stakeStoreKey)
wire.RegisterInterface((*crypto.PubKey)(nil), nil)
wire.RegisterConcrete(crypto.PubKeyEd25519{}, "crypto/PubKeyEd25519", nil)
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
cdc.RegisterConcrete(crypto.PubKeyEd25519{}, "crypto/PubKeyEd25519", nil)

//delegator := crypto.Address{[]byte("addressdelegator")}
//validator := crypto.Address{[]byte("addressvalidator")}
Expand All @@ -65,9 +64,9 @@ func TestState(t *testing.T) {
return c1.Status == c2.Status &&
c1.PubKey.Equals(c2.PubKey) &&
bytes.Equal(c1.Owner, c2.Owner) &&
c1.Assets.Equal(c2.Assets) &&
c1.Liabilities.Equal(c2.Liabilities) &&
c1.VotingPower.Equal(c2.VotingPower) &&
c1.Assets == c2.Assets &&
c1.Liabilities == c2.Liabilities &&
c1.VotingPower == c2.VotingPower &&
c1.Description == c2.Description
}

Expand Down Expand Up @@ -103,7 +102,7 @@ func TestState(t *testing.T) {

bondsEqual := func(b1, b2 *DelegatorBond) bool {
return b1.PubKey.Equals(b2.PubKey) &&
b1.Shares.Equal(b2.Shares)
b1.Shares == b2.Shares
}

//check the empty store first
Expand Down

0 comments on commit eb597e1

Please sign in to comment.