mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-20 06:54:41 +00:00
implement finalize block
This commit is contained in:
+2
-13
@@ -135,8 +135,7 @@ func (b *EventBus) PublishEventNewBlock(data EventDataNewBlock) error {
|
||||
// no explicit deadline for publishing events
|
||||
ctx := context.Background()
|
||||
|
||||
resultEvents := append(data.ResultBeginBlock.Events, data.ResultEndBlock.Events...)
|
||||
events := b.validateAndStringifyEvents(resultEvents, b.Logger.With("block", data.Block.StringShort()))
|
||||
events := b.validateAndStringifyEvents(data.ResultFinalizeBlock.Events, b.Logger.With("block", data.Block.StringShort()))
|
||||
|
||||
// add predefined new block event
|
||||
events[EventTypeKey] = append(events[EventTypeKey], EventNewBlock)
|
||||
@@ -145,17 +144,7 @@ func (b *EventBus) PublishEventNewBlock(data EventDataNewBlock) error {
|
||||
}
|
||||
|
||||
func (b *EventBus) PublishEventNewBlockHeader(data EventDataNewBlockHeader) error {
|
||||
// no explicit deadline for publishing events
|
||||
ctx := context.Background()
|
||||
|
||||
resultTags := append(data.ResultBeginBlock.Events, data.ResultEndBlock.Events...)
|
||||
// TODO: Create StringShort method for Header and use it in logger.
|
||||
events := b.validateAndStringifyEvents(resultTags, b.Logger.With("header", data.Header))
|
||||
|
||||
// add predefined new block header event
|
||||
events[EventTypeKey] = append(events[EventTypeKey], EventNewBlockHeader)
|
||||
|
||||
return b.pubsub.PublishWithEvents(ctx, data, events)
|
||||
return b.Publish(EventNewBlockHeader, data)
|
||||
}
|
||||
|
||||
func (b *EventBus) PublishEventNewEvidence(evidence EventDataNewEvidence) error {
|
||||
|
||||
+18
-29
@@ -27,7 +27,7 @@ func TestEventBusPublishEventTx(t *testing.T) {
|
||||
})
|
||||
|
||||
tx := Tx("foo")
|
||||
result := abci.ResponseDeliverTx{
|
||||
result := abci.ExecTxResult{
|
||||
Data: []byte("bar"),
|
||||
Events: []abci.Event{
|
||||
{Type: "testType", Attributes: []abci.EventAttribute{{Key: "baz", Value: "1"}}},
|
||||
@@ -76,16 +76,11 @@ func TestEventBusPublishEventNewBlock(t *testing.T) {
|
||||
})
|
||||
|
||||
block := MakeBlock(0, []Tx{}, nil, []Evidence{})
|
||||
resultBeginBlock := abci.ResponseBeginBlock{
|
||||
resultFinalizeBlock := abci.ResponseFinalizeBlock{
|
||||
Events: []abci.Event{
|
||||
{Type: "testType", Attributes: []abci.EventAttribute{{Key: "baz", Value: "1"}}},
|
||||
},
|
||||
}
|
||||
resultEndBlock := abci.ResponseEndBlock{
|
||||
Events: []abci.Event{
|
||||
{Type: "testType", Attributes: []abci.EventAttribute{{Key: "foz", Value: "2"}}},
|
||||
},
|
||||
}
|
||||
|
||||
// PublishEventNewBlock adds the tm.event compositeKey, so the query below should work
|
||||
query := "tm.event='NewBlock' AND testType.baz=1 AND testType.foz=2"
|
||||
@@ -97,15 +92,21 @@ func TestEventBusPublishEventNewBlock(t *testing.T) {
|
||||
msg := <-blocksSub.Out()
|
||||
edt := msg.Data().(EventDataNewBlock)
|
||||
assert.Equal(t, block, edt.Block)
|
||||
assert.Equal(t, resultBeginBlock, edt.ResultBeginBlock)
|
||||
assert.Equal(t, resultEndBlock, edt.ResultEndBlock)
|
||||
assert.Equal(t, resultFinalizeBlock, edt.ResultFinalizeBlock)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
var ps *PartSet
|
||||
ps, err = block.MakePartSet(MaxBlockSizeBytes)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = eventBus.PublishEventNewBlock(EventDataNewBlock{
|
||||
Block: block,
|
||||
ResultBeginBlock: resultBeginBlock,
|
||||
ResultEndBlock: resultEndBlock,
|
||||
Block: block,
|
||||
BlockID: BlockID{
|
||||
Hash: block.Hash(),
|
||||
PartSetHeader: ps.Header(),
|
||||
},
|
||||
ResultFinalizeBlock: resultFinalizeBlock,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -127,7 +128,7 @@ func TestEventBusPublishEventTxDuplicateKeys(t *testing.T) {
|
||||
})
|
||||
|
||||
tx := Tx("foo")
|
||||
result := abci.ResponseDeliverTx{
|
||||
result := abci.ExecTxResult{
|
||||
Data: []byte("bar"),
|
||||
Events: []abci.Event{
|
||||
{
|
||||
@@ -234,18 +235,8 @@ func TestEventBusPublishEventNewBlockHeader(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
const numTxs = 100
|
||||
block := MakeBlock(0, []Tx{}, nil, []Evidence{})
|
||||
resultBeginBlock := abci.ResponseBeginBlock{
|
||||
Events: []abci.Event{
|
||||
{Type: "testType", Attributes: []abci.EventAttribute{{Key: "baz", Value: "1"}}},
|
||||
},
|
||||
}
|
||||
resultEndBlock := abci.ResponseEndBlock{
|
||||
Events: []abci.Event{
|
||||
{Type: "testType", Attributes: []abci.EventAttribute{{Key: "foz", Value: "2"}}},
|
||||
},
|
||||
}
|
||||
|
||||
// PublishEventNewBlockHeader adds the tm.event compositeKey, so the query below should work
|
||||
query := "tm.event='NewBlockHeader' AND testType.baz=1 AND testType.foz=2"
|
||||
headersSub, err := eventBus.Subscribe(context.Background(), "test", tmquery.MustParse(query))
|
||||
@@ -256,15 +247,13 @@ func TestEventBusPublishEventNewBlockHeader(t *testing.T) {
|
||||
msg := <-headersSub.Out()
|
||||
edt := msg.Data().(EventDataNewBlockHeader)
|
||||
assert.Equal(t, block.Header, edt.Header)
|
||||
assert.Equal(t, resultBeginBlock, edt.ResultBeginBlock)
|
||||
assert.Equal(t, resultEndBlock, edt.ResultEndBlock)
|
||||
assert.Equal(t, numTxs, edt.NumTxs)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
err = eventBus.PublishEventNewBlockHeader(EventDataNewBlockHeader{
|
||||
Header: block.Header,
|
||||
ResultBeginBlock: resultBeginBlock,
|
||||
ResultEndBlock: resultEndBlock,
|
||||
Header: block.Header,
|
||||
NumTxs: numTxs,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
||||
+7
-9
@@ -62,18 +62,15 @@ func init() {
|
||||
// but some (an input to a call tx or a receive) are more exotic
|
||||
|
||||
type EventDataNewBlock struct {
|
||||
Block *Block `json:"block"`
|
||||
Block *Block `json:"block"`
|
||||
BlockID BlockID `json:"block_id"`
|
||||
|
||||
ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"`
|
||||
ResultEndBlock abci.ResponseEndBlock `json:"result_end_block"`
|
||||
ResultFinalizeBlock abci.ResponseFinalizeBlock `json:"result_finalize_block"`
|
||||
}
|
||||
|
||||
type EventDataNewBlockHeader struct {
|
||||
Header Header `json:"header"`
|
||||
|
||||
NumTxs int64 `json:"num_txs"` // Number of txs in a block
|
||||
ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"`
|
||||
ResultEndBlock abci.ResponseEndBlock `json:"result_end_block"`
|
||||
NumTxs int64 `json:"num_txs,string"` // Number of txs in a block
|
||||
}
|
||||
|
||||
type EventDataNewEvidence struct {
|
||||
@@ -130,15 +127,16 @@ type EventDataValidatorSetUpdates struct {
|
||||
const (
|
||||
// EventTypeKey is a reserved composite key for event name.
|
||||
EventTypeKey = "tm.event"
|
||||
|
||||
// TxHashKey is a reserved key, used to specify transaction's hash.
|
||||
// see EventBus#PublishEventTx
|
||||
TxHashKey = "tx.hash"
|
||||
|
||||
// TxHeightKey is a reserved key, used to specify transaction block's height.
|
||||
// see EventBus#PublishEventTx
|
||||
TxHeightKey = "tx.height"
|
||||
|
||||
// BlockHeightKey is a reserved key used for indexing BeginBlock and Endblock
|
||||
// events.
|
||||
// BlockHeightKey is a reserved key used for indexing FinalizeBlock events.
|
||||
BlockHeightKey = "block.height"
|
||||
)
|
||||
|
||||
|
||||
+8
-8
@@ -6,14 +6,14 @@ import (
|
||||
)
|
||||
|
||||
// ABCIResults wraps the deliver tx results to return a proof.
|
||||
type ABCIResults []*abci.ResponseDeliverTx
|
||||
type ABCIResults []*abci.ExecTxResult
|
||||
|
||||
// NewResults strips non-deterministic fields from ResponseDeliverTx responses
|
||||
// NewResults strips non-deterministic fields from ExecTxResult responses
|
||||
// and returns ABCIResults.
|
||||
func NewResults(responses []*abci.ResponseDeliverTx) ABCIResults {
|
||||
func NewResults(responses []*abci.ExecTxResult) ABCIResults {
|
||||
res := make(ABCIResults, len(responses))
|
||||
for i, d := range responses {
|
||||
res[i] = deterministicResponseDeliverTx(d)
|
||||
res[i] = deterministicExecTxResult(d)
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -42,10 +42,10 @@ func (a ABCIResults) toByteSlices() [][]byte {
|
||||
return bzs
|
||||
}
|
||||
|
||||
// deterministicResponseDeliverTx strips non-deterministic fields from
|
||||
// ResponseDeliverTx and returns another ResponseDeliverTx.
|
||||
func deterministicResponseDeliverTx(response *abci.ResponseDeliverTx) *abci.ResponseDeliverTx {
|
||||
return &abci.ResponseDeliverTx{
|
||||
// deterministicExecTxResult strips non-deterministic fields from
|
||||
// ExecTxResult and returns another ExecTxResult.
|
||||
func deterministicExecTxResult(response *abci.ExecTxResult) *abci.ExecTxResult {
|
||||
return &abci.ExecTxResult{
|
||||
Code: response.Code,
|
||||
Data: response.Data,
|
||||
GasWanted: response.GasWanted,
|
||||
|
||||
@@ -10,12 +10,12 @@ import (
|
||||
)
|
||||
|
||||
func TestABCIResults(t *testing.T) {
|
||||
a := &abci.ResponseDeliverTx{Code: 0, Data: nil}
|
||||
b := &abci.ResponseDeliverTx{Code: 0, Data: []byte{}}
|
||||
c := &abci.ResponseDeliverTx{Code: 0, Data: []byte("one")}
|
||||
d := &abci.ResponseDeliverTx{Code: 14, Data: nil}
|
||||
e := &abci.ResponseDeliverTx{Code: 14, Data: []byte("foo")}
|
||||
f := &abci.ResponseDeliverTx{Code: 14, Data: []byte("bar")}
|
||||
a := &abci.ExecTxResult{Code: 0, Data: nil}
|
||||
b := &abci.ExecTxResult{Code: 0, Data: []byte{}}
|
||||
c := &abci.ExecTxResult{Code: 0, Data: []byte("one")}
|
||||
d := &abci.ExecTxResult{Code: 14, Data: nil}
|
||||
e := &abci.ExecTxResult{Code: 14, Data: []byte("foo")}
|
||||
f := &abci.ExecTxResult{Code: 14, Data: []byte("bar")}
|
||||
|
||||
// Nil and []byte{} should produce the same bytes
|
||||
bzA, err := a.Marshal()
|
||||
|
||||
Reference in New Issue
Block a user