mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 06:31:57 +00:00
state: proto migration (#4951)
This commit is contained in:
+7
-2
@@ -1,6 +1,7 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -279,9 +280,13 @@ func execBlockOnProxyApp(
|
||||
|
||||
// Begin block
|
||||
var err error
|
||||
pbh := block.Header.ToProto()
|
||||
if pbh == nil {
|
||||
return nil, errors.New("nil header")
|
||||
}
|
||||
abciResponses.BeginBlock, err = proxyAppConn.BeginBlockSync(abci.RequestBeginBlock{
|
||||
Hash: block.Hash(),
|
||||
Header: types.TM2PB.Header(&block.Header),
|
||||
Header: *pbh,
|
||||
LastCommitInfo: commitInfo,
|
||||
ByzantineValidators: byzVals,
|
||||
})
|
||||
@@ -466,7 +471,7 @@ func fireEvents(
|
||||
})
|
||||
|
||||
for i, tx := range block.Data.Txs {
|
||||
eventBus.PublishEventTx(types.EventDataTx{TxResult: types.TxResult{
|
||||
eventBus.PublishEventTx(types.EventDataTx{TxResult: abci.TxResult{
|
||||
Height: block.Height,
|
||||
Index: uint32(i),
|
||||
Tx: tx,
|
||||
|
||||
+3
-2
@@ -6,6 +6,7 @@ import (
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
tmversion "github.com/tendermint/tendermint/proto/version"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
tmtime "github.com/tendermint/tendermint/types/time"
|
||||
"github.com/tendermint/tendermint/version"
|
||||
@@ -23,7 +24,7 @@ var (
|
||||
// and the software version to support upgrades to the format of
|
||||
// the State as stored on disk.
|
||||
type Version struct {
|
||||
Consensus version.Consensus
|
||||
Consensus tmversion.Consensus
|
||||
Software string
|
||||
}
|
||||
|
||||
@@ -32,7 +33,7 @@ type Version struct {
|
||||
// The Consensus.App version will be set during the Handshake, once
|
||||
// we hear from the app what protocol version it is running.
|
||||
var InitStateVersion = Version{
|
||||
Consensus: version.Consensus{
|
||||
Consensus: tmversion.Consensus{
|
||||
Block: version.BlockProtocol,
|
||||
App: 0,
|
||||
},
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/libs/pubsub/query"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
// TxIndexer interface defines methods to index and search transactions.
|
||||
@@ -15,14 +15,14 @@ type TxIndexer interface {
|
||||
AddBatch(b *Batch) error
|
||||
|
||||
// Index analyzes, indexes and stores a single transaction.
|
||||
Index(result *types.TxResult) error
|
||||
Index(result *abci.TxResult) error
|
||||
|
||||
// Get returns the transaction specified by hash or nil if the transaction is not indexed
|
||||
// or stored.
|
||||
Get(hash []byte) (*types.TxResult, error)
|
||||
Get(hash []byte) (*abci.TxResult, error)
|
||||
|
||||
// Search allows you to query for transactions.
|
||||
Search(ctx context.Context, q *query.Query) ([]*types.TxResult, error)
|
||||
Search(ctx context.Context, q *query.Query) ([]*abci.TxResult, error)
|
||||
}
|
||||
|
||||
//----------------------------------------------------
|
||||
@@ -31,18 +31,18 @@ type TxIndexer interface {
|
||||
// Batch groups together multiple Index operations to be performed at the same time.
|
||||
// NOTE: Batch is NOT thread-safe and must not be modified after starting its execution.
|
||||
type Batch struct {
|
||||
Ops []*types.TxResult
|
||||
Ops []*abci.TxResult
|
||||
}
|
||||
|
||||
// NewBatch creates a new Batch.
|
||||
func NewBatch(n int64) *Batch {
|
||||
return &Batch{
|
||||
Ops: make([]*types.TxResult, n),
|
||||
Ops: make([]*abci.TxResult, n),
|
||||
}
|
||||
}
|
||||
|
||||
// Add or update an entry for the given result.Index.
|
||||
func (b *Batch) Add(result *types.TxResult) error {
|
||||
func (b *Batch) Add(result *abci.TxResult) error {
|
||||
b.Ops[result.Index] = result
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -39,14 +39,14 @@ func TestIndexerServiceIndexesBlocks(t *testing.T) {
|
||||
Header: types.Header{Height: 1},
|
||||
NumTxs: int64(2),
|
||||
})
|
||||
txResult1 := &types.TxResult{
|
||||
txResult1 := &abci.TxResult{
|
||||
Height: 1,
|
||||
Index: uint32(0),
|
||||
Tx: types.Tx("foo"),
|
||||
Result: abci.ResponseDeliverTx{Code: 0},
|
||||
}
|
||||
eventBus.PublishEventTx(types.EventDataTx{TxResult: *txResult1})
|
||||
txResult2 := &types.TxResult{
|
||||
txResult2 := &abci.TxResult{
|
||||
Height: 1,
|
||||
Index: uint32(1),
|
||||
Tx: types.Tx("bar"),
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package kv
|
||||
|
||||
import (
|
||||
amino "github.com/tendermint/go-amino"
|
||||
)
|
||||
|
||||
var cdc = amino.NewCodec()
|
||||
|
||||
func init() {
|
||||
}
|
||||
+19
-17
@@ -9,8 +9,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
dbm "github.com/tendermint/tm-db"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/libs/pubsub/query"
|
||||
tmstring "github.com/tendermint/tendermint/libs/strings"
|
||||
"github.com/tendermint/tendermint/state/txindex"
|
||||
@@ -55,7 +57,7 @@ func IndexAllEvents() func(*TxIndex) {
|
||||
|
||||
// Get gets transaction from the TxIndex storage and returns it or nil if the
|
||||
// transaction is not found.
|
||||
func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
|
||||
func (txi *TxIndex) Get(hash []byte) (*abci.TxResult, error) {
|
||||
if len(hash) == 0 {
|
||||
return nil, txindex.ErrorEmptyHash
|
||||
}
|
||||
@@ -68,8 +70,8 @@ func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
txResult := new(types.TxResult)
|
||||
err = cdc.UnmarshalBinaryBare(rawBytes, &txResult)
|
||||
txResult := new(abci.TxResult)
|
||||
err = proto.Unmarshal(rawBytes, txResult)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading TxResult: %v", err)
|
||||
}
|
||||
@@ -86,7 +88,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
|
||||
defer storeBatch.Close()
|
||||
|
||||
for _, result := range b.Ops {
|
||||
hash := result.Tx.Hash()
|
||||
hash := types.Tx(result.Tx).Hash()
|
||||
|
||||
// index tx by events
|
||||
txi.indexEvents(result, hash, storeBatch)
|
||||
@@ -97,7 +99,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
|
||||
}
|
||||
|
||||
// index tx by hash
|
||||
rawBytes, err := cdc.MarshalBinaryBare(result)
|
||||
rawBytes, err := proto.Marshal(result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -112,11 +114,11 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
|
||||
// that indexed from the tx's events is a composite of the event type and the
|
||||
// respective attribute's key delimited by a "." (eg. "account.number").
|
||||
// Any event with an empty type is not indexed.
|
||||
func (txi *TxIndex) Index(result *types.TxResult) error {
|
||||
func (txi *TxIndex) Index(result *abci.TxResult) error {
|
||||
b := txi.store.NewBatch()
|
||||
defer b.Close()
|
||||
|
||||
hash := result.Tx.Hash()
|
||||
hash := types.Tx(result.Tx).Hash()
|
||||
|
||||
// index tx by events
|
||||
txi.indexEvents(result, hash, b)
|
||||
@@ -127,7 +129,7 @@ func (txi *TxIndex) Index(result *types.TxResult) error {
|
||||
}
|
||||
|
||||
// index tx by hash
|
||||
rawBytes, err := cdc.MarshalBinaryBare(result)
|
||||
rawBytes, err := proto.Marshal(result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -138,7 +140,7 @@ func (txi *TxIndex) Index(result *types.TxResult) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (txi *TxIndex) indexEvents(result *types.TxResult, hash []byte, store dbm.SetDeleter) {
|
||||
func (txi *TxIndex) indexEvents(result *abci.TxResult, hash []byte, store dbm.SetDeleter) {
|
||||
for _, event := range result.Result.Events {
|
||||
// only index events with a non-empty type
|
||||
if len(event.Type) == 0 {
|
||||
@@ -169,11 +171,11 @@ func (txi *TxIndex) indexEvents(result *types.TxResult, hash []byte, store dbm.S
|
||||
//
|
||||
// Search will exit early and return any result fetched so far,
|
||||
// when a message is received on the context chan.
|
||||
func (txi *TxIndex) Search(ctx context.Context, q *query.Query) ([]*types.TxResult, error) {
|
||||
func (txi *TxIndex) Search(ctx context.Context, q *query.Query) ([]*abci.TxResult, error) {
|
||||
// Potentially exit early.
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
results := make([]*types.TxResult, 0)
|
||||
results := make([]*abci.TxResult, 0)
|
||||
return results, nil
|
||||
default:
|
||||
}
|
||||
@@ -195,11 +197,11 @@ func (txi *TxIndex) Search(ctx context.Context, q *query.Query) ([]*types.TxResu
|
||||
res, err := txi.Get(hash)
|
||||
switch {
|
||||
case err != nil:
|
||||
return []*types.TxResult{}, fmt.Errorf("error while retrieving the result: %w", err)
|
||||
return []*abci.TxResult{}, fmt.Errorf("error while retrieving the result: %w", err)
|
||||
case res == nil:
|
||||
return []*types.TxResult{}, nil
|
||||
return []*abci.TxResult{}, nil
|
||||
default:
|
||||
return []*types.TxResult{res}, nil
|
||||
return []*abci.TxResult{res}, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,7 +254,7 @@ func (txi *TxIndex) Search(ctx context.Context, q *query.Query) ([]*types.TxResu
|
||||
}
|
||||
}
|
||||
|
||||
results := make([]*types.TxResult, 0, len(filteredHashes))
|
||||
results := make([]*abci.TxResult, 0, len(filteredHashes))
|
||||
for _, h := range filteredHashes {
|
||||
res, err := txi.Get(h)
|
||||
if err != nil {
|
||||
@@ -593,7 +595,7 @@ func extractValueFromKey(key []byte) string {
|
||||
return parts[1]
|
||||
}
|
||||
|
||||
func keyForEvent(key string, value []byte, result *types.TxResult) []byte {
|
||||
func keyForEvent(key string, value []byte, result *abci.TxResult) []byte {
|
||||
return []byte(fmt.Sprintf("%s/%s/%d/%d",
|
||||
key,
|
||||
value,
|
||||
@@ -602,7 +604,7 @@ func keyForEvent(key string, value []byte, result *types.TxResult) []byte {
|
||||
))
|
||||
}
|
||||
|
||||
func keyForHeight(result *types.TxResult) []byte {
|
||||
func keyForHeight(result *abci.TxResult) []byte {
|
||||
return []byte(fmt.Sprintf("%s/%d/%d/%d",
|
||||
types.TxHeightKey,
|
||||
result.Height,
|
||||
|
||||
@@ -44,7 +44,7 @@ func BenchmarkTxSearch(b *testing.B) {
|
||||
b.Errorf("failed produce random bytes: %s", err)
|
||||
}
|
||||
|
||||
txResult := &types.TxResult{
|
||||
txResult := &abci.TxResult{
|
||||
Height: int64(i),
|
||||
Index: 0,
|
||||
Tx: types.Tx(string(txBz)),
|
||||
|
||||
+34
-25
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@@ -23,7 +24,7 @@ func TestTxIndex(t *testing.T) {
|
||||
indexer := NewTxIndex(db.NewMemDB())
|
||||
|
||||
tx := types.Tx("HELLO WORLD")
|
||||
txResult := &types.TxResult{
|
||||
txResult := &abci.TxResult{
|
||||
Height: 1,
|
||||
Index: 0,
|
||||
Tx: tx,
|
||||
@@ -43,10 +44,10 @@ func TestTxIndex(t *testing.T) {
|
||||
|
||||
loadedTxResult, err := indexer.Get(hash)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, txResult, loadedTxResult)
|
||||
assert.True(t, proto.Equal(txResult, loadedTxResult))
|
||||
|
||||
tx2 := types.Tx("BYE BYE WORLD")
|
||||
txResult2 := &types.TxResult{
|
||||
txResult2 := &abci.TxResult{
|
||||
Height: 1,
|
||||
Index: 0,
|
||||
Tx: tx2,
|
||||
@@ -62,7 +63,7 @@ func TestTxIndex(t *testing.T) {
|
||||
|
||||
loadedTxResult2, err := indexer.Get(hash2)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, txResult2, loadedTxResult2)
|
||||
assert.True(t, proto.Equal(txResult2, loadedTxResult2))
|
||||
}
|
||||
|
||||
func TestTxSearch(t *testing.T) {
|
||||
@@ -74,7 +75,7 @@ func TestTxSearch(t *testing.T) {
|
||||
{Type: "account", Attributes: []abci.EventAttribute{{Key: []byte("owner"), Value: []byte("Ivan")}}},
|
||||
{Type: "", Attributes: []abci.EventAttribute{{Key: []byte("not_allowed"), Value: []byte("Vlad")}}},
|
||||
})
|
||||
hash := txResult.Tx.Hash()
|
||||
hash := types.Tx(txResult.Tx).Hash()
|
||||
|
||||
err := indexer.Index(txResult)
|
||||
require.NoError(t, err)
|
||||
@@ -128,7 +129,9 @@ func TestTxSearch(t *testing.T) {
|
||||
|
||||
assert.Len(t, results, tc.resultsLength)
|
||||
if tc.resultsLength > 0 {
|
||||
assert.Equal(t, []*types.TxResult{txResult}, results)
|
||||
for _, txr := range results {
|
||||
assert.True(t, proto.Equal(txResult, txr))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -161,7 +164,7 @@ func TestTxSearchDeprecatedIndexing(t *testing.T) {
|
||||
txResult1 := txResultWithEvents([]abci.Event{
|
||||
{Type: "account", Attributes: []abci.EventAttribute{{Key: []byte("number"), Value: []byte("1")}}},
|
||||
})
|
||||
hash1 := txResult1.Tx.Hash()
|
||||
hash1 := types.Tx(txResult1.Tx).Hash()
|
||||
|
||||
err := indexer.Index(txResult1)
|
||||
require.NoError(t, err)
|
||||
@@ -170,10 +173,10 @@ func TestTxSearchDeprecatedIndexing(t *testing.T) {
|
||||
txResult2 := txResultWithEvents(nil)
|
||||
txResult2.Tx = types.Tx("HELLO WORLD 2")
|
||||
|
||||
hash2 := txResult2.Tx.Hash()
|
||||
hash2 := types.Tx(txResult2.Tx).Hash()
|
||||
b := indexer.store.NewBatch()
|
||||
|
||||
rawBytes, err := cdc.MarshalBinaryBare(txResult2)
|
||||
rawBytes, err := proto.Marshal(txResult2)
|
||||
require.NoError(t, err)
|
||||
|
||||
depKey := []byte(fmt.Sprintf("%s/%s/%d/%d",
|
||||
@@ -190,27 +193,27 @@ func TestTxSearchDeprecatedIndexing(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
q string
|
||||
results []*types.TxResult
|
||||
results []*abci.TxResult
|
||||
}{
|
||||
// search by hash
|
||||
{fmt.Sprintf("tx.hash = '%X'", hash1), []*types.TxResult{txResult1}},
|
||||
{fmt.Sprintf("tx.hash = '%X'", hash1), []*abci.TxResult{txResult1}},
|
||||
// search by hash
|
||||
{fmt.Sprintf("tx.hash = '%X'", hash2), []*types.TxResult{txResult2}},
|
||||
{fmt.Sprintf("tx.hash = '%X'", hash2), []*abci.TxResult{txResult2}},
|
||||
// search by exact match (one key)
|
||||
{"account.number = 1", []*types.TxResult{txResult1}},
|
||||
{"account.number >= 1 AND account.number <= 5", []*types.TxResult{txResult1}},
|
||||
{"account.number = 1", []*abci.TxResult{txResult1}},
|
||||
{"account.number >= 1 AND account.number <= 5", []*abci.TxResult{txResult1}},
|
||||
// search by range (lower bound)
|
||||
{"account.number >= 1", []*types.TxResult{txResult1}},
|
||||
{"account.number >= 1", []*abci.TxResult{txResult1}},
|
||||
// search by range (upper bound)
|
||||
{"account.number <= 5", []*types.TxResult{txResult1}},
|
||||
{"account.number <= 5", []*abci.TxResult{txResult1}},
|
||||
// search using not allowed key
|
||||
{"not_allowed = 'boom'", []*types.TxResult{}},
|
||||
{"not_allowed = 'boom'", []*abci.TxResult{}},
|
||||
// search for not existing tx result
|
||||
{"account.number >= 2 AND account.number <= 5", []*types.TxResult{}},
|
||||
{"account.number >= 2 AND account.number <= 5", []*abci.TxResult{}},
|
||||
// search using not existing key
|
||||
{"account.date >= TIME 2013-05-03T14:45:00Z", []*types.TxResult{}},
|
||||
{"account.date >= TIME 2013-05-03T14:45:00Z", []*abci.TxResult{}},
|
||||
// search by deprecated key
|
||||
{"sender = 'addr1'", []*types.TxResult{txResult2}},
|
||||
{"sender = 'addr1'", []*abci.TxResult{txResult2}},
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
@@ -220,7 +223,11 @@ func TestTxSearchDeprecatedIndexing(t *testing.T) {
|
||||
t.Run(tc.q, func(t *testing.T) {
|
||||
results, err := indexer.Search(ctx, query.MustParse(tc.q))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, results, tc.results)
|
||||
for _, txr := range results {
|
||||
for _, tr := range tc.results {
|
||||
assert.True(t, proto.Equal(tr, txr))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -243,7 +250,9 @@ func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Len(t, results, 1)
|
||||
assert.Equal(t, []*types.TxResult{txResult}, results)
|
||||
for _, txr := range results {
|
||||
assert.True(t, proto.Equal(txResult, txr))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTxSearchMultipleTxs(t *testing.T) {
|
||||
@@ -301,9 +310,9 @@ func TestTxSearchMultipleTxs(t *testing.T) {
|
||||
require.Len(t, results, 3)
|
||||
}
|
||||
|
||||
func txResultWithEvents(events []abci.Event) *types.TxResult {
|
||||
func txResultWithEvents(events []abci.Event) *abci.TxResult {
|
||||
tx := types.Tx("HELLO WORLD")
|
||||
return &types.TxResult{
|
||||
return &abci.TxResult{
|
||||
Height: 1,
|
||||
Index: 0,
|
||||
Tx: tx,
|
||||
@@ -330,7 +339,7 @@ func benchmarkTxIndex(txsCount int64, b *testing.B) {
|
||||
txIndex := uint32(0)
|
||||
for i := int64(0); i < txsCount; i++ {
|
||||
tx := tmrand.Bytes(250)
|
||||
txResult := &types.TxResult{
|
||||
txResult := &abci.TxResult{
|
||||
Height: 1,
|
||||
Index: txIndex,
|
||||
Tx: tx,
|
||||
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/libs/pubsub/query"
|
||||
"github.com/tendermint/tendermint/state/txindex"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
var _ txindex.TxIndexer = (*TxIndex)(nil)
|
||||
@@ -15,7 +15,7 @@ var _ txindex.TxIndexer = (*TxIndex)(nil)
|
||||
type TxIndex struct{}
|
||||
|
||||
// Get on a TxIndex is disabled and panics when invoked.
|
||||
func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
|
||||
func (txi *TxIndex) Get(hash []byte) (*abci.TxResult, error) {
|
||||
return nil, errors.New(`indexing is disabled (set 'tx_index = "kv"' in config)`)
|
||||
}
|
||||
|
||||
@@ -25,10 +25,10 @@ func (txi *TxIndex) AddBatch(batch *txindex.Batch) error {
|
||||
}
|
||||
|
||||
// Index is a noop and always returns nil.
|
||||
func (txi *TxIndex) Index(result *types.TxResult) error {
|
||||
func (txi *TxIndex) Index(result *abci.TxResult) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (txi *TxIndex) Search(ctx context.Context, q *query.Query) ([]*types.TxResult, error) {
|
||||
return []*types.TxResult{}, nil
|
||||
func (txi *TxIndex) Search(ctx context.Context, q *query.Query) ([]*abci.TxResult, error) {
|
||||
return []*abci.TxResult{}, nil
|
||||
}
|
||||
|
||||
+2
-1
@@ -21,7 +21,8 @@ func validateBlock(evidencePool EvidencePool, stateDB dbm.DB, state State, block
|
||||
}
|
||||
|
||||
// Validate basic info.
|
||||
if block.Version != state.Version.Consensus {
|
||||
if block.Version.App != state.Version.Consensus.App ||
|
||||
block.Version.Block != state.Version.Consensus.Block {
|
||||
return fmt.Errorf("wrong Block.Header.Version. Expected %v, got %v",
|
||||
state.Version.Consensus,
|
||||
block.Version,
|
||||
|
||||
Reference in New Issue
Block a user