From 50a826a818e3defe6c9b2ce478a1ca591fe807f9 Mon Sep 17 00:00:00 2001 From: krish-nr Date: Wed, 19 Apr 2023 18:40:59 +0800 Subject: [PATCH] feat: add events to be handled & juno version updated --- go.mod | 4 +- go.sum | 8 +-- service/blocksyncer/block_syncer_indexer.go | 59 ++++++++++++++++----- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index 90fcd06ff..d1517295a 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,10 @@ go 1.18 replace ( cosmossdk.io/math => github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c - github.com/bnb-chain/greenfield => github.com/bnb-chain/greenfield v0.0.12-0.20230417120203-7b2a32d58918 + github.com/bnb-chain/greenfield => github.com/bnb-chain/greenfield v0.0.12-0.20230418102232-efc0f221748f github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0 github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.0.13 - github.com/forbole/juno/v4 => github.com/bnb-chain/juno/v4 v4.0.0-20230418063138-cfe377950b33 + github.com/forbole/juno/v4 => github.com/bnb-chain/juno/v4 v4.0.0-20230419100458-9d9422d92390 github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 github.com/tendermint/tendermint => github.com/bnb-chain/gnfd-tendermint v0.0.3 ) diff --git a/go.sum b/go.sum index 4d6293e75..2f98dcf57 100644 --- a/go.sum +++ b/go.sum @@ -208,16 +208,16 @@ github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb/go.mod h1:PkYb9DJNAw github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c= github.com/bnb-chain/gnfd-tendermint v0.0.3 h1:RjEBQZJW7krM4DFxZbIasNNO7K2uohcmL9ULT84qllk= github.com/bnb-chain/gnfd-tendermint v0.0.3/go.mod h1:/v9z9F6cq0+f7EGG92lYSLBcPYQDILoK91X8YM28hWo= -github.com/bnb-chain/greenfield v0.0.12-0.20230417120203-7b2a32d58918 h1:moehHscmSGZXSeqUNoJZkU9rVv3FWGn9Q/EOFdwZQfk= -github.com/bnb-chain/greenfield v0.0.12-0.20230417120203-7b2a32d58918/go.mod h1:7344f+97RhnkZPOIE/uHdF5u6Awwyhfb/TgIA6nQhXA= +github.com/bnb-chain/greenfield v0.0.12-0.20230418102232-efc0f221748f h1:H3b8rXUuYNZMyCxWqsP+g3NeoR19FXLYixWIXnehJUM= +github.com/bnb-chain/greenfield v0.0.12-0.20230418102232-efc0f221748f/go.mod h1:iHqOyaOVmHg48syHW7AGVN5nzIGt2GZoNL+KmT4UoLY= github.com/bnb-chain/greenfield-common/go v0.0.0-20230407104542-ed19e3666522 h1:n3R7Ybzu9XlCHl63QT14XwqaSuGff9U+g3ZJic2frOM= github.com/bnb-chain/greenfield-common/go v0.0.0-20230407104542-ed19e3666522/go.mod h1:gGaPRDSjK1QH/lMjZ5wfhbP4SfrUUKX6Q72x/8uehxo= github.com/bnb-chain/greenfield-cosmos-sdk v0.0.13 h1:xm9qqrOjrGfX4imXxxruHIpuUUUAFbdtM/TXPXRc5zo= github.com/bnb-chain/greenfield-cosmos-sdk v0.0.13/go.mod h1:rvAY7ga/AakZWyYkA1zAsNtvKpdoyRFZTqF4MhFYzZ8= github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c h1:BLmdYaj7Dx0YOhfk77+KPPJSMCwpQl6f4Y30+801bf0= github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c/go.mod h1:u/MXvf8wbUbCsAEyQSSYXXMsczAsFX48e2D6JI86T4o= -github.com/bnb-chain/juno/v4 v4.0.0-20230418063138-cfe377950b33 h1:uD3jpCCavGcbEMpQbllld0sxR3OG2QqZ53ZOA8LwtFQ= -github.com/bnb-chain/juno/v4 v4.0.0-20230418063138-cfe377950b33/go.mod h1:3z4aiyd+ajj1PFfkT137SyVmtDuWo/6aObYvr38Fj1U= +github.com/bnb-chain/juno/v4 v4.0.0-20230419100458-9d9422d92390 h1:xaIUxxPHGJk6x/71YF6la3qtTyAQ6sINEXj/uHRt5nU= +github.com/bnb-chain/juno/v4 v4.0.0-20230419100458-9d9422d92390/go.mod h1:f8+o/XzjbBGWcKqxDBSTi3BwLItanrMkI4fNJ7P7miU= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= github.com/bradfitz/gomemcache v0.0.0-20170208213004-1952afaa557d/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60= diff --git a/service/blocksyncer/block_syncer_indexer.go b/service/blocksyncer/block_syncer_indexer.go index 38f54e015..1fc3fede8 100644 --- a/service/blocksyncer/block_syncer_indexer.go +++ b/service/blocksyncer/block_syncer_indexer.go @@ -3,6 +3,7 @@ package blocksyncer import ( "context" "encoding/json" + "fmt" "github.com/bnb-chain/greenfield-storage-provider/pkg/log" "github.com/bnb-chain/greenfield-storage-provider/pkg/metrics" @@ -17,6 +18,7 @@ import ( "github.com/forbole/juno/v4/node" "github.com/forbole/juno/v4/parser" "github.com/forbole/juno/v4/types" + abci "github.com/tendermint/tendermint/abci/types" coretypes "github.com/tendermint/tendermint/rpc/core/types" tmtypes "github.com/tendermint/tendermint/types" ) @@ -47,7 +49,6 @@ func (i *Impl) ExportBlock(block *coretypes.ResultBlock, events *coretypes.Resul func (i *Impl) HandleEvent(ctx context.Context, block *coretypes.ResultBlock, txHash common.Hash, event sdk.Event) { for _, module := range i.Modules { if eventModule, ok := module.(modules.EventModule); ok { - log.Infof("module name :%s event type: %s, height: %d", module.Name(), event.Type, block.Block.Height) err := eventModule.HandleEvent(ctx, block, txHash, event) if err != nil { log.Errorw("failed to handle event", "module", module.Name(), "event", event, "error", err) @@ -61,24 +62,45 @@ func (i *Impl) HandleEvent(ctx context.Context, block *coretypes.ResultBlock, tx func (i *Impl) Process(height uint64) error { log.Debugw("processing block", "height", height) + // get block info block, err := i.Node.Block(int64(height)) if err != nil { log.Errorf("failed to get block from node: %s", err) return err } + // get txs + txs, err := i.Node.Txs(block) + if err != nil { + return fmt.Errorf("failed to get transactions for block: %s", err) + } + + // get block results events, err := i.Node.BlockResults(int64(height)) if err != nil { log.Errorf("failed to get block results from node: %s", err) return err } + beginBlockEvents := events.BeginBlockEvents + endBlockEvents := events.EndBlockEvents + + // 1. handle events in startBlock + + if len(beginBlockEvents) > 0 { + i.ExportEventsWithoutTx(context.Background(), block, beginBlockEvents) + } - err = i.ExportEvents(context.Background(), block, events) + // 2. handle events in txs + err = i.ExportEventsInTxs(context.Background(), block, txs) if err != nil { - log.Errorf("failed to ExportEvents: %s", err) return err } + // 3. handle events in endBlock + if len(endBlockEvents) > 0 { + i.ExportEventsWithoutTx(context.Background(), block, endBlockEvents) + } + err = i.ExportEpoch(block) if err != nil { log.Errorf("failed to ExportEpoch: %s", err) @@ -144,6 +166,27 @@ func (i *Impl) ExportEvents(ctx context.Context, block *coretypes.ResultBlock, e return nil } +// ExportEventsInTxs accepts a slice of events in tx in order to save in database. +func (i *Impl) ExportEventsInTxs(ctx context.Context, block *coretypes.ResultBlock, txs []*types.Tx) error { + for _, tx := range txs { + txHash := common.HexToHash(tx.TxHash) + for _, event := range tx.Events { + i.HandleEvent(ctx, block, txHash, sdk.Event(event)) + } + } + return nil +} + +// ExportEventsWithoutTx accepts a slice of events not in tx in order to save in database. +// events here don't have txHash +func (i *Impl) ExportEventsWithoutTx(ctx context.Context, block *coretypes.ResultBlock, events []abci.Event) error { + // call the event handlers + for _, event := range events { + i.HandleEvent(ctx, block, common.Hash{}, sdk.Event(event)) + } + return nil +} + // HandleGenesis accepts a GenesisDoc and calls all the registered genesis handlers in the order in which they have been registered. func (i *Impl) HandleGenesis(genesisDoc *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error { return nil @@ -182,16 +225,6 @@ func (i *Impl) Processed(ctx context.Context, height uint64) (bool, error) { return ep.BlockHeight > int64(height), nil } -func (i *Impl) ExportEventsByTxs(ctx context.Context, block *coretypes.ResultBlock, txs []*types.Tx) error { - for _, tx := range txs { - txHash := common.HexToHash(tx.TxHash) - for _, event := range tx.Events { - i.HandleEvent(ctx, block, txHash, sdk.Event(event)) - } - } - return nil -} - // GetBlockRecordNum returns total number of blocks stored in database. func (i *Impl) GetBlockRecordNum(_ context.Context) int64 { return 1