Browse Source

Fix state tests

pull/1347/head
Jae Kwon 7 years ago
parent
commit
89cdde7f1e
12 changed files with 90 additions and 83 deletions
  1. +6
    -6
      config/toml.go
  2. +1
    -1
      state/execution.go
  3. +2
    -2
      state/execution_test.go
  4. +4
    -9
      state/state.go
  5. +15
    -18
      state/state_test.go
  6. +19
    -27
      state/store.go
  7. +3
    -5
      state/txindex/kv/kv.go
  8. +6
    -5
      state/txindex/kv/kv_test.go
  9. +10
    -0
      state/txindex/kv/wire.go
  10. +12
    -0
      state/wire.go
  11. +9
    -7
      types/block.go
  12. +3
    -3
      types/params.go

+ 6
- 6
config/toml.go View File

@ -276,8 +276,8 @@ var testGenesis = `{
"validators": [ "validators": [
{ {
"pub_key": { "pub_key": {
"type": "ed25519",
"data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
"type": "AC26791624DE60",
"value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="
}, },
"power": 10, "power": 10,
"name": "" "name": ""
@ -289,12 +289,12 @@ var testGenesis = `{
var testPrivValidator = `{ var testPrivValidator = `{
"address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456", "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
"pub_key": { "pub_key": {
"type": "ed25519",
"data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
"type": "AC26791624DE60",
"value": "AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="
}, },
"priv_key": { "priv_key": {
"type": "ed25519",
"data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
"type": "954568A3288910",
"value": "EVkqJO/jIXp3rkASXfh9YnyToYXRXhBr6g9cQVxPFnQBP/5povV4HTjvsy530kybxKHwEi85iU8YL0qQhSYVoQ=="
}, },
"last_height": 0, "last_height": 0,
"last_round": 0, "last_round": 0,


+ 1
- 1
state/execution.go View File

@ -245,7 +245,7 @@ func execBlockOnProxyApp(logger log.Logger, proxyAppConn proxy.AppConnConsensus,
// ./lite/doc.go for details on how a light client tracks validators. // ./lite/doc.go for details on how a light client tracks validators.
func updateValidators(currentSet *types.ValidatorSet, updates []abci.Validator) error { func updateValidators(currentSet *types.ValidatorSet, updates []abci.Validator) error {
for _, v := range updates { for _, v := range updates {
pubkey, err := crypto.PubKeyFromBytes(v.PubKey) // NOTE: expects go-wire encoded pubkey
pubkey, err := crypto.PubKeyFromBytes(v.PubKey) // NOTE: expects go-amino encoded pubkey
if err != nil { if err != nil {
return err return err
} }


+ 2
- 2
state/execution_test.go View File

@ -66,8 +66,8 @@ func TestBeginBlockAbsentValidators(t *testing.T) {
lastCommitPrecommits []*types.Vote lastCommitPrecommits []*types.Vote
expectedAbsentValidators []int32 expectedAbsentValidators []int32
}{ }{
{"none absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now}, {ValidatorIndex: 1, Timestamp: now}}, []int32{}},
{"one absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now}, nil}, []int32{1}},
{"none absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now, Type: types.VoteTypePrecommit}, {ValidatorIndex: 1, Timestamp: now}}, []int32{}},
{"one absent", []*types.Vote{{ValidatorIndex: 0, Timestamp: now, Type: types.VoteTypePrecommit}, nil}, []int32{1}},
{"multiple absent", []*types.Vote{nil, nil}, []int32{0, 1}}, {"multiple absent", []*types.Vote{nil, nil}, []int32{0, 1}},
} }


+ 4
- 9
state/state.go View File

@ -6,8 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"time" "time"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
) )
@ -81,16 +79,13 @@ func (s State) Copy() State {
// Equals returns true if the States are identical. // Equals returns true if the States are identical.
func (s State) Equals(s2 State) bool { func (s State) Equals(s2 State) bool {
return bytes.Equal(s.Bytes(), s2.Bytes())
sbz, s2bz := s.Bytes(), s2.Bytes()
return bytes.Equal(sbz, s2bz)
} }
// Bytes serializes the State using go-wire.
// Bytes serializes the State using go-amino.
func (s State) Bytes() []byte { func (s State) Bytes() []byte {
bz, err := wire.MarshalBinary(s)
if err != nil {
panic(err)
}
return bz
return cdc.MustMarshalBinaryBare(s)
} }
// IsEmpty returns true if the State is equal to the empty State. // IsEmpty returns true if the State is equal to the empty State.


+ 15
- 18
state/state_test.go View File

@ -7,11 +7,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db" dbm "github.com/tendermint/tmlibs/db"
@ -42,7 +39,7 @@ func TestStateCopy(t *testing.T) {
stateCopy := state.Copy() stateCopy := state.Copy()
assert.True(state.Equals(stateCopy), assert.True(state.Equals(stateCopy),
cmn.Fmt(`expected state and its copy to be identical. got %v\n expected %v\n`,
cmn.Fmt("expected state and its copy to be identical.\ngot: %v\nexpected: %v\n",
stateCopy, state)) stateCopy, state))
stateCopy.LastBlockHeight++ stateCopy.LastBlockHeight++
@ -62,7 +59,7 @@ func TestStateSaveLoad(t *testing.T) {
loadedState := LoadState(stateDB) loadedState := LoadState(stateDB)
assert.True(state.Equals(loadedState), assert.True(state.Equals(loadedState),
cmn.Fmt(`expected state and its copy to be identical. got %v\n expected %v\n`,
cmn.Fmt("expected state and its copy to be identical.\ngot: %v\nexpected: %v\n",
loadedState, state)) loadedState, state))
} }
@ -78,8 +75,8 @@ func TestABCIResponsesSaveLoad1(t *testing.T) {
// build mock responses // build mock responses
block := makeBlock(state, 2) block := makeBlock(state, 2)
abciResponses := NewABCIResponses(block) abciResponses := NewABCIResponses(block)
abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo"), Tags: []cmn.KVPair{}}
abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok", Tags: []cmn.KVPair{}}
abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo"), Tags: nil}
abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok", Tags: nil}
abciResponses.EndBlock = &abci.ResponseEndBlock{ValidatorUpdates: []abci.Validator{ abciResponses.EndBlock = &abci.ResponseEndBlock{ValidatorUpdates: []abci.Validator{
{ {
PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(), PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(),
@ -88,11 +85,11 @@ func TestABCIResponsesSaveLoad1(t *testing.T) {
}} }}
saveABCIResponses(stateDB, block.Height, abciResponses) saveABCIResponses(stateDB, block.Height, abciResponses)
loadedAbciResponses, err := LoadABCIResponses(stateDB, block.Height)
loadedABCIResponses, err := LoadABCIResponses(stateDB, block.Height)
assert.Nil(err) assert.Nil(err)
assert.Equal(abciResponses, loadedAbciResponses,
cmn.Fmt(`ABCIResponses don't match: Got %v, Expected %v`, loadedAbciResponses,
abciResponses))
assert.Equal(abciResponses, loadedABCIResponses,
cmn.Fmt("ABCIResponses don't match:\ngot: %v\nexpected: %v\n",
loadedABCIResponses, abciResponses))
} }
// TestResultsSaveLoad tests saving and loading abci results. // TestResultsSaveLoad tests saving and loading abci results.
@ -109,8 +106,8 @@ func TestABCIResponsesSaveLoad2(t *testing.T) {
expected types.ABCIResults expected types.ABCIResults
}{ }{
0: { 0: {
[]*abci.ResponseDeliverTx{},
types.ABCIResults{},
nil,
nil,
}, },
1: { 1: {
[]*abci.ResponseDeliverTx{ []*abci.ResponseDeliverTx{
@ -129,12 +126,12 @@ func TestABCIResponsesSaveLoad2(t *testing.T) {
}}, }},
}, },
types.ABCIResults{ types.ABCIResults{
{383, []byte{}},
{383, nil},
{0, []byte("Gotcha!")}, {0, []byte("Gotcha!")},
}}, }},
3: { 3: {
nil, nil,
types.ABCIResults{},
nil,
}, },
} }
@ -430,7 +427,7 @@ func makeHeaderPartsResponsesValPubKeyChange(state State, height int64,
block := makeBlock(state, height) block := makeBlock(state, height)
abciResponses := &ABCIResponses{ abciResponses := &ABCIResponses{
EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.Validator{}},
EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
} }
// if the pubkey is new, remove the old and add the new // if the pubkey is new, remove the old and add the new
@ -452,7 +449,7 @@ func makeHeaderPartsResponsesValPowerChange(state State, height int64,
block := makeBlock(state, height) block := makeBlock(state, height)
abciResponses := &ABCIResponses{ abciResponses := &ABCIResponses{
EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.Validator{}},
EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
} }
// if the pubkey is new, remove the old and add the new // if the pubkey is new, remove the old and add the new


+ 19
- 27
state/store.go View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db" dbm "github.com/tendermint/tmlibs/db"
@ -69,7 +68,7 @@ func loadState(db dbm.DB, key []byte) (state State) {
return state return state
} }
err := wire.UnmarshalBinary(buf, &state)
err := cdc.UnmarshalBinaryBare(buf, &state)
if err != nil { if err != nil {
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
cmn.Exit(cmn.Fmt(`LoadState: Data has been corrupted or its spec has changed: cmn.Exit(cmn.Fmt(`LoadState: Data has been corrupted or its spec has changed:
@ -104,22 +103,23 @@ type ABCIResponses struct {
// NewABCIResponses returns a new ABCIResponses // NewABCIResponses returns a new ABCIResponses
func NewABCIResponses(block *types.Block) *ABCIResponses { func NewABCIResponses(block *types.Block) *ABCIResponses {
resDeliverTxs := make([]*abci.ResponseDeliverTx, block.NumTxs)
if block.NumTxs == 0 {
// This makes Amino encoding/decoding consistent.
resDeliverTxs = nil
}
return &ABCIResponses{ return &ABCIResponses{
DeliverTx: make([]*abci.ResponseDeliverTx, block.NumTxs),
DeliverTx: resDeliverTxs,
} }
} }
// Bytes serializes the ABCIResponse using go-wire
func (a *ABCIResponses) Bytes() []byte {
bz, err := wire.MarshalBinary(*a)
if err != nil {
panic(err)
}
return bz
// Bytes serializes the ABCIResponse using go-amino.
func (arz *ABCIResponses) Bytes() []byte {
return cdc.MustMarshalBinaryBare(arz)
} }
func (a *ABCIResponses) ResultsHash() []byte {
results := types.NewResults(a.DeliverTx)
func (arz *ABCIResponses) ResultsHash() []byte {
results := types.NewResults(arz.DeliverTx)
return results.Hash() return results.Hash()
} }
@ -133,7 +133,7 @@ func LoadABCIResponses(db dbm.DB, height int64) (*ABCIResponses, error) {
} }
abciResponses := new(ABCIResponses) abciResponses := new(ABCIResponses)
err := wire.UnmarshalBinary(buf, abciResponses)
err := cdc.UnmarshalBinaryBare(buf, abciResponses)
if err != nil { if err != nil {
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
cmn.Exit(cmn.Fmt(`LoadABCIResponses: Data has been corrupted or its spec has cmn.Exit(cmn.Fmt(`LoadABCIResponses: Data has been corrupted or its spec has
@ -159,13 +159,9 @@ type ValidatorsInfo struct {
LastHeightChanged int64 LastHeightChanged int64
} }
// Bytes serializes the ValidatorsInfo using go-wire
// Bytes serializes the ValidatorsInfo using go-amino.
func (valInfo *ValidatorsInfo) Bytes() []byte { func (valInfo *ValidatorsInfo) Bytes() []byte {
bz, err := wire.MarshalBinary(*valInfo)
if err != nil {
panic(err)
}
return bz
return cdc.MustMarshalBinaryBare(valInfo)
} }
// LoadValidators loads the ValidatorSet for a given height. // LoadValidators loads the ValidatorSet for a given height.
@ -194,7 +190,7 @@ func loadValidatorsInfo(db dbm.DB, height int64) *ValidatorsInfo {
} }
v := new(ValidatorsInfo) v := new(ValidatorsInfo)
err := wire.UnmarshalBinary(buf, v)
err := cdc.UnmarshalBinaryBare(buf, v)
if err != nil { if err != nil {
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
cmn.Exit(cmn.Fmt(`LoadValidators: Data has been corrupted or its spec has changed: cmn.Exit(cmn.Fmt(`LoadValidators: Data has been corrupted or its spec has changed:
@ -227,13 +223,9 @@ type ConsensusParamsInfo struct {
LastHeightChanged int64 LastHeightChanged int64
} }
// Bytes serializes the ConsensusParamsInfo using go-wire
// Bytes serializes the ConsensusParamsInfo using go-amino.
func (params ConsensusParamsInfo) Bytes() []byte { func (params ConsensusParamsInfo) Bytes() []byte {
bz, err := wire.MarshalBinary(params)
if err != nil {
panic(err)
}
return bz
return cdc.MustMarshalBinaryBare(params)
} }
// LoadConsensusParams loads the ConsensusParams for a given height. // LoadConsensusParams loads the ConsensusParams for a given height.
@ -263,7 +255,7 @@ func loadConsensusParamsInfo(db dbm.DB, height int64) *ConsensusParamsInfo {
} }
paramsInfo := new(ConsensusParamsInfo) paramsInfo := new(ConsensusParamsInfo)
err := wire.UnmarshalBinary(buf, paramsInfo)
err := cdc.UnmarshalBinaryBare(buf, paramsInfo)
if err != nil { if err != nil {
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
cmn.Exit(cmn.Fmt(`LoadConsensusParams: Data has been corrupted or its spec has changed: cmn.Exit(cmn.Fmt(`LoadConsensusParams: Data has been corrupted or its spec has changed:


+ 3
- 5
state/txindex/kv/kv.go View File

@ -9,8 +9,6 @@ import (
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
wire "github.com/tendermint/go-wire"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db" dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/pubsub/query" "github.com/tendermint/tmlibs/pubsub/query"
@ -68,7 +66,7 @@ func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
} }
txResult := new(types.TxResult) txResult := new(types.TxResult)
err := wire.UnmarshalBinary(rawBytes, &txResult)
err := cdc.UnmarshalBinaryBare(rawBytes, &txResult)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error reading TxResult: %v", err) return nil, fmt.Errorf("Error reading TxResult: %v", err)
} }
@ -91,7 +89,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
} }
// index tx by hash // index tx by hash
rawBytes, err := wire.MarshalBinary(result)
rawBytes, err := cdc.MarshalBinaryBare(result)
if err != nil { if err != nil {
return err return err
} }
@ -116,7 +114,7 @@ func (txi *TxIndex) Index(result *types.TxResult) error {
} }
// index tx by hash // index tx by hash
rawBytes, err := wire.MarshalBinary(result)
rawBytes, err := cdc.MarshalBinaryBare(result)
if err != nil { if err != nil {
return err return err
} }


+ 6
- 5
state/txindex/kv/kv_test.go View File

@ -9,18 +9,19 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
db "github.com/tendermint/tmlibs/db" db "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/pubsub/query" "github.com/tendermint/tmlibs/pubsub/query"
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/types"
) )
func TestTxIndex(t *testing.T) { func TestTxIndex(t *testing.T) {
indexer := NewTxIndex(db.NewMemDB()) indexer := NewTxIndex(db.NewMemDB())
tx := types.Tx("HELLO WORLD") tx := types.Tx("HELLO WORLD")
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: []cmn.KVPair{}}}
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: nil}}
hash := tx.Hash() hash := tx.Hash()
batch := txindex.NewBatch(1) batch := txindex.NewBatch(1)
@ -35,7 +36,7 @@ func TestTxIndex(t *testing.T) {
assert.Equal(t, txResult, loadedTxResult) assert.Equal(t, txResult, loadedTxResult)
tx2 := types.Tx("BYE BYE WORLD") tx2 := types.Tx("BYE BYE WORLD")
txResult2 := &types.TxResult{1, 0, tx2, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: []cmn.KVPair{}}}
txResult2 := &types.TxResult{1, 0, tx2, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: nil}}
hash2 := tx2.Hash() hash2 := tx2.Hash()
err = indexer.Index(txResult2) err = indexer.Index(txResult2)
@ -151,7 +152,7 @@ func txResultWithTags(tags []cmn.KVPair) *types.TxResult {
func benchmarkTxIndex(txsCount int, b *testing.B) { func benchmarkTxIndex(txsCount int, b *testing.B) {
tx := types.Tx("HELLO WORLD") tx := types.Tx("HELLO WORLD")
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: []cmn.KVPair{}}}
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: nil}}
dir, err := ioutil.TempDir("", "tx_index_db") dir, err := ioutil.TempDir("", "tx_index_db")
if err != nil { if err != nil {


+ 10
- 0
state/txindex/kv/wire.go View File

@ -0,0 +1,10 @@
package kv
import (
"github.com/tendermint/go-amino"
)
var cdc = amino.NewCodec()
func init() {
}

+ 12
- 0
state/wire.go View File

@ -0,0 +1,12 @@
package state
import (
"github.com/tendermint/go-amino"
"github.com/tendermint/go-crypto"
)
var cdc = amino.NewCodec()
func init() {
crypto.RegisterAmino(cdc)
}

+ 9
- 7
types/block.go View File

@ -518,13 +518,15 @@ type hasher struct {
func (h hasher) Hash() []byte { func (h hasher) Hash() []byte {
hasher := ripemd160.New() hasher := ripemd160.New()
bz, err := cdc.MarshalBinaryBare(h.item)
if err != nil {
panic(err)
}
_, err = hasher.Write(bz)
if err != nil {
panic(err)
if h.item != nil && !cmn.IsTypedNil(h.item) {
bz, err := cdc.MarshalBinaryBare(h.item)
if err != nil {
panic(err)
}
_, err = hasher.Write(bz)
if err != nil {
panic(err)
}
} }
return hasher.Sum(nil) return hasher.Sum(nil)


+ 3
- 3
types/params.go View File

@ -7,7 +7,7 @@ import (
) )
const ( const (
maxBlockSizeBytes = 104857600 // 100MB
MaxBlockSizeBytes = 104857600 // 100MB
) )
// ConsensusParams contains consensus critical parameters // ConsensusParams contains consensus critical parameters
@ -95,9 +95,9 @@ func (params *ConsensusParams) Validate() error {
} }
// ensure blocks aren't too big // ensure blocks aren't too big
if params.BlockSize.MaxBytes > maxBlockSizeBytes {
if params.BlockSize.MaxBytes > MaxBlockSizeBytes {
return cmn.NewError("BlockSize.MaxBytes is too big. %d > %d", return cmn.NewError("BlockSize.MaxBytes is too big. %d > %d",
params.BlockSize.MaxBytes, maxBlockSizeBytes)
params.BlockSize.MaxBytes, MaxBlockSizeBytes)
} }
return nil return nil
} }


Loading…
Cancel
Save