mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-24 22:32:15 +00:00
Fix state tests
This commit is contained in:
parent
35a1d747b0
commit
89cdde7f1e
@ -276,8 +276,8 @@ var testGenesis = `{
|
||||
"validators": [
|
||||
{
|
||||
"pub_key": {
|
||||
"type": "ed25519",
|
||||
"data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
|
||||
"type": "AC26791624DE60",
|
||||
"value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="
|
||||
},
|
||||
"power": 10,
|
||||
"name": ""
|
||||
@ -289,12 +289,12 @@ var testGenesis = `{
|
||||
var testPrivValidator = `{
|
||||
"address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
|
||||
"pub_key": {
|
||||
"type": "ed25519",
|
||||
"data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
|
||||
"type": "AC26791624DE60",
|
||||
"value": "AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="
|
||||
},
|
||||
"priv_key": {
|
||||
"type": "ed25519",
|
||||
"data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
|
||||
"type": "954568A3288910",
|
||||
"value": "EVkqJO/jIXp3rkASXfh9YnyToYXRXhBr6g9cQVxPFnQBP/5povV4HTjvsy530kybxKHwEi85iU8YL0qQhSYVoQ=="
|
||||
},
|
||||
"last_height": 0,
|
||||
"last_round": 0,
|
||||
|
@ -245,7 +245,7 @@ func execBlockOnProxyApp(logger log.Logger, proxyAppConn proxy.AppConnConsensus,
|
||||
// ./lite/doc.go for details on how a light client tracks validators.
|
||||
func updateValidators(currentSet *types.ValidatorSet, updates []abci.Validator) error {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -66,8 +66,8 @@ func TestBeginBlockAbsentValidators(t *testing.T) {
|
||||
lastCommitPrecommits []*types.Vote
|
||||
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}},
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,6 @@ import (
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
wire "github.com/tendermint/go-wire"
|
||||
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
@ -81,16 +79,13 @@ func (s State) Copy() State {
|
||||
|
||||
// Equals returns true if the States are identical.
|
||||
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 {
|
||||
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.
|
||||
|
@ -7,11 +7,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
|
||||
"github.com/tendermint/go-crypto"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
|
||||
@ -42,7 +39,7 @@ func TestStateCopy(t *testing.T) {
|
||||
stateCopy := state.Copy()
|
||||
|
||||
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.LastBlockHeight++
|
||||
@ -62,7 +59,7 @@ func TestStateSaveLoad(t *testing.T) {
|
||||
|
||||
loadedState := LoadState(stateDB)
|
||||
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))
|
||||
}
|
||||
|
||||
@ -78,8 +75,8 @@ func TestABCIResponsesSaveLoad1(t *testing.T) {
|
||||
// build mock responses
|
||||
block := makeBlock(state, 2)
|
||||
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{
|
||||
{
|
||||
PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(),
|
||||
@ -88,11 +85,11 @@ func TestABCIResponsesSaveLoad1(t *testing.T) {
|
||||
}}
|
||||
|
||||
saveABCIResponses(stateDB, block.Height, abciResponses)
|
||||
loadedAbciResponses, err := LoadABCIResponses(stateDB, block.Height)
|
||||
loadedABCIResponses, err := LoadABCIResponses(stateDB, block.Height)
|
||||
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.
|
||||
@ -109,8 +106,8 @@ func TestABCIResponsesSaveLoad2(t *testing.T) {
|
||||
expected types.ABCIResults
|
||||
}{
|
||||
0: {
|
||||
[]*abci.ResponseDeliverTx{},
|
||||
types.ABCIResults{},
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
1: {
|
||||
[]*abci.ResponseDeliverTx{
|
||||
@ -129,12 +126,12 @@ func TestABCIResponsesSaveLoad2(t *testing.T) {
|
||||
}},
|
||||
},
|
||||
types.ABCIResults{
|
||||
{383, []byte{}},
|
||||
{383, nil},
|
||||
{0, []byte("Gotcha!")},
|
||||
}},
|
||||
3: {
|
||||
nil,
|
||||
types.ABCIResults{},
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
@ -430,7 +427,7 @@ func makeHeaderPartsResponsesValPubKeyChange(state State, height int64,
|
||||
|
||||
block := makeBlock(state, height)
|
||||
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
|
||||
@ -452,7 +449,7 @@ func makeHeaderPartsResponsesValPowerChange(state State, height int64,
|
||||
|
||||
block := makeBlock(state, height)
|
||||
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
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
wire "github.com/tendermint/go-wire"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
@ -69,7 +68,7 @@ func loadState(db dbm.DB, key []byte) (state State) {
|
||||
return state
|
||||
}
|
||||
|
||||
err := wire.UnmarshalBinary(buf, &state)
|
||||
err := cdc.UnmarshalBinaryBare(buf, &state)
|
||||
if err != nil {
|
||||
// DATA HAS BEEN CORRUPTED OR THE 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
|
||||
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{
|
||||
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()
|
||||
}
|
||||
|
||||
@ -133,7 +133,7 @@ func LoadABCIResponses(db dbm.DB, height int64) (*ABCIResponses, error) {
|
||||
}
|
||||
|
||||
abciResponses := new(ABCIResponses)
|
||||
err := wire.UnmarshalBinary(buf, abciResponses)
|
||||
err := cdc.UnmarshalBinaryBare(buf, abciResponses)
|
||||
if err != nil {
|
||||
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
|
||||
cmn.Exit(cmn.Fmt(`LoadABCIResponses: Data has been corrupted or its spec has
|
||||
@ -159,13 +159,9 @@ type ValidatorsInfo struct {
|
||||
LastHeightChanged int64
|
||||
}
|
||||
|
||||
// Bytes serializes the ValidatorsInfo using go-wire
|
||||
// Bytes serializes the ValidatorsInfo using go-amino.
|
||||
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.
|
||||
@ -194,7 +190,7 @@ func loadValidatorsInfo(db dbm.DB, height int64) *ValidatorsInfo {
|
||||
}
|
||||
|
||||
v := new(ValidatorsInfo)
|
||||
err := wire.UnmarshalBinary(buf, v)
|
||||
err := cdc.UnmarshalBinaryBare(buf, v)
|
||||
if err != nil {
|
||||
// DATA HAS BEEN CORRUPTED OR THE 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
|
||||
}
|
||||
|
||||
// Bytes serializes the ConsensusParamsInfo using go-wire
|
||||
// Bytes serializes the ConsensusParamsInfo using go-amino.
|
||||
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.
|
||||
@ -263,7 +255,7 @@ func loadConsensusParamsInfo(db dbm.DB, height int64) *ConsensusParamsInfo {
|
||||
}
|
||||
|
||||
paramsInfo := new(ConsensusParamsInfo)
|
||||
err := wire.UnmarshalBinary(buf, paramsInfo)
|
||||
err := cdc.UnmarshalBinaryBare(buf, paramsInfo)
|
||||
if err != nil {
|
||||
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
|
||||
cmn.Exit(cmn.Fmt(`LoadConsensusParams: Data has been corrupted or its spec has changed:
|
||||
|
@ -9,8 +9,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
wire "github.com/tendermint/go-wire"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/pubsub/query"
|
||||
@ -68,7 +66,7 @@ func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
|
||||
}
|
||||
|
||||
txResult := new(types.TxResult)
|
||||
err := wire.UnmarshalBinary(rawBytes, &txResult)
|
||||
err := cdc.UnmarshalBinaryBare(rawBytes, &txResult)
|
||||
if err != nil {
|
||||
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
|
||||
rawBytes, err := wire.MarshalBinary(result)
|
||||
rawBytes, err := cdc.MarshalBinaryBare(result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -116,7 +114,7 @@ func (txi *TxIndex) Index(result *types.TxResult) error {
|
||||
}
|
||||
|
||||
// index tx by hash
|
||||
rawBytes, err := wire.MarshalBinary(result)
|
||||
rawBytes, err := cdc.MarshalBinaryBare(result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -9,18 +9,19 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/state/txindex"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
db "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/pubsub/query"
|
||||
|
||||
"github.com/tendermint/tendermint/state/txindex"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
func TestTxIndex(t *testing.T) {
|
||||
indexer := NewTxIndex(db.NewMemDB())
|
||||
|
||||
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()
|
||||
|
||||
batch := txindex.NewBatch(1)
|
||||
@ -35,7 +36,7 @@ func TestTxIndex(t *testing.T) {
|
||||
assert.Equal(t, txResult, loadedTxResult)
|
||||
|
||||
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()
|
||||
|
||||
err = indexer.Index(txResult2)
|
||||
@ -151,7 +152,7 @@ func txResultWithTags(tags []cmn.KVPair) *types.TxResult {
|
||||
|
||||
func benchmarkTxIndex(txsCount int, b *testing.B) {
|
||||
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")
|
||||
if err != nil {
|
||||
|
10
state/txindex/kv/wire.go
Normal file
10
state/txindex/kv/wire.go
Normal file
@ -0,0 +1,10 @@
|
||||
package kv
|
||||
|
||||
import (
|
||||
"github.com/tendermint/go-amino"
|
||||
)
|
||||
|
||||
var cdc = amino.NewCodec()
|
||||
|
||||
func init() {
|
||||
}
|
12
state/wire.go
Normal file
12
state/wire.go
Normal 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)
|
||||
}
|
@ -518,13 +518,15 @@ type hasher struct {
|
||||
|
||||
func (h hasher) Hash() []byte {
|
||||
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)
|
||||
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
maxBlockSizeBytes = 104857600 // 100MB
|
||||
MaxBlockSizeBytes = 104857600 // 100MB
|
||||
)
|
||||
|
||||
// ConsensusParams contains consensus critical parameters
|
||||
@ -95,9 +95,9 @@ func (params *ConsensusParams) Validate() error {
|
||||
}
|
||||
|
||||
// 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",
|
||||
params.BlockSize.MaxBytes, maxBlockSizeBytes)
|
||||
params.BlockSize.MaxBytes, MaxBlockSizeBytes)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user