Skip to content

Commit

Permalink
feat: add events to be handled & juno version updated
Browse files Browse the repository at this point in the history
  • Loading branch information
krish-nr committed Apr 19, 2023
1 parent 46f99c9 commit 50a826a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
59 changes: 46 additions & 13 deletions service/blocksyncer/block_syncer_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 50a826a

Please sign in to comment.