From 6b9d836f406981a4965bc395a8b99e7bd027f18b Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 25 Jan 2018 20:11:58 +0000 Subject: [PATCH] gaia store compile errors resolved --- .gitignore | 1 + x/stake/store_test.go | 71 ++++++++++++---------------------- x/stake/test_common.go | 87 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 47 deletions(-) create mode 100644 x/stake/test_common.go diff --git a/.gitignore b/.gitignore index 93905e1f2..ed8f93c68 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ examples/basecoin/app/data baseapp/data/* docs/_build <<<<<<< HEAD +<<<<<<< HEAD .DS_Store coverage.txt profile.out diff --git a/x/stake/store_test.go b/x/stake/store_test.go index e82154a6c..a1955862d 100644 --- a/x/stake/store_test.go +++ b/x/stake/store_test.go @@ -2,41 +2,32 @@ package stake import ( "bytes" - "encoding/hex" "testing" - sdkstore "github.com/cosmos/cosmos-sdk/store" - "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store" + sdk "github.com/cosmos/cosmos-sdk/types" + dbm "github.com/tendermint/tmlibs/db" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" crypto "github.com/tendermint/go-crypto" - dbm "github.com/tendermint/tmlibs/db" ) -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 +func initTestStore(t *testing.T) sdk.KVStore { + // Capabilities key to access the main KVStore. + db, err := dbm.NewGoLevelDB("stake", "data") + require.Nil(t, err) + stakeStoreKey := sdk.NewKVStoreKey("stake") + ms := store.NewCommitMultiStore(db) + ms.MountStoreWithDB(stakeStoreKey, sdk.StoreTypeIAVL, db) + ms.LoadLatestVersion() + return ms.GetKVStore(stakeStoreKey) } func TestState(t *testing.T) { assert, require := assert.New(t), require.New(t) - db, err := dbm.NewGoLevelDB("basecoin", "basecoin-data") - require.Nil(err) - 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) + store := initTestStore(t) cdc.RegisterInterface((*crypto.PubKey)(nil), nil) cdc.RegisterConcrete(crypto.PubKeyEd25519{}, "crypto/PubKeyEd25519", nil) @@ -45,8 +36,7 @@ func TestState(t *testing.T) { delegator := []byte("addressdelegator") validator := []byte("addressvalidator") - pk, err := newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57") - require.Nil(err) + pk := newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57") //---------------------------------------------------------------------- // Candidate checks @@ -73,7 +63,7 @@ func TestState(t *testing.T) { // check the empty store first resCand := loadCandidate(store, pk) assert.Nil(resCand) - resPks := loadCandidatesPubKeys(store) + resPks := loadCandidates(store) assert.Zero(len(resPks)) // set and retrieve a record @@ -88,9 +78,9 @@ func TestState(t *testing.T) { assert.True(candidatesEqual(candidate, resCand)) // also test that the pubkey has been added to pubkey list - resPks = loadCandidatesPubKeys(store) + resPks = loadCandidates(store) require.Equal(1, len(resPks)) - assert.Equal(pk, resPks[0]) + assert.Equal(pk, resPks[0].PubKey) //---------------------------------------------------------------------- // Bond checks @@ -136,29 +126,16 @@ func TestState(t *testing.T) { assert.Equal(params, resParams) } -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) + store := initTestStore(t) N := 5 - actors := newActors(N) - candidates := candidatesFromActors(actors, []int{400, 200, 0, 0, 0}) + addrs := newAddrs(N) + candidatesFromActors(store, addrs, []int{400, 200, 0, 0, 0}) - validators := candidates.Validators() + validators := getValidators(store, 5) require.Equal(2, len(validators)) - assert.Equal(candidates[0].PubKey, validators[0].PubKey) - assert.Equal(candidates[1].PubKey, validators[1].PubKey) + assert.Equal(pks[0], validators[0].PubKey) + assert.Equal(pks[1], validators[1].PubKey) } diff --git a/x/stake/test_common.go b/x/stake/test_common.go new file mode 100644 index 000000000..57ab56434 --- /dev/null +++ b/x/stake/test_common.go @@ -0,0 +1,87 @@ +package stake + +import ( + "encoding/hex" + "fmt" + + crypto "github.com/tendermint/go-crypto" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func newAddrs(n int) (addrs []crypto.Address) { + for i := 0; i < n; i++ { + addrs = append(addrs, []byte(fmt.Sprintf("addr%d", i))) + } + return +} + +func newPubKey(pk string) (res crypto.PubKey) { + pkBytes, err := hex.DecodeString(pk) + if err != nil { + panic(err) + } + //res, err = crypto.PubKeyFromBytes(pkBytes) + var pkEd crypto.PubKeyEd25519 + copy(pkEd[:], pkBytes[:]) + return pkEd +} + +// dummy pubkeys used for testing +var pks = []crypto.PubKey{ + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"), + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"), + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB53"), + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB54"), + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB55"), + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB56"), + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57"), + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB58"), + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB59"), + newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB60"), +} + +// NOTE: PubKey is supposed to be the binaryBytes of the crypto.PubKey +// instead this is just being set the address here for testing purposes +func candidatesFromActors(store sdk.KVStore, addrs []crypto.Address, amts []int) { + for i := 0; i < len(addrs); i++ { + c := &Candidate{ + Status: Unbonded, + PubKey: pks[i], + Owner: addrs[i], + Assets: int64(amts[i]), //rational.New(amts[i]), + Liabilities: int64(amts[i]), //rational.New(amts[i]), + VotingPower: int64(amts[i]), //rational.New(amts[i]), + } + saveCandidate(store, c) + } +} + +func candidatesFromActorsEmpty(addrs []crypto.Address) (candidates Candidates) { + for i := 0; i < len(addrs); i++ { + c := &Candidate{ + Status: Unbonded, + PubKey: pks[i], + Owner: addrs[i], + Assets: 0, //rational.Zero, + Liabilities: 0, //rational.Zero, + VotingPower: 0, //rational.Zero, + } + candidates = append(candidates, c) + } + return +} + +//// helper function test if Candidate is changed asabci.Validator +//func testChange(t *testing.T, val Validator, chg *abci.Validator) { +//assert := assert.New(t) +//assert.Equal(val.PubKey.Bytes(), chg.PubKey) +//assert.Equal(val.VotingPower.Evaluate(), chg.Power) +//} + +//// helper function test if Candidate is removed as abci.Validator +//func testRemove(t *testing.T, val Validator, chg *abci.Validator) { +//assert := assert.New(t) +//assert.Equal(val.PubKey.Bytes(), chg.PubKey) +//assert.Equal(int64(0), chg.Power) +//}