Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add file-based transaction indexing #546

Merged
merged 38 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
38fb2f8
Add base tx indexer logic
zivkovicmilos Feb 27, 2023
ed13499
Add file indexer
zivkovicmilos Feb 27, 2023
08360c2
Simplify types
zivkovicmilos Feb 27, 2023
367b20d
Add unit test for file tx indexer
zivkovicmilos Feb 27, 2023
0ce73e5
Add unit test for indexer config
zivkovicmilos Feb 27, 2023
994de46
Add unit test for the indexer service
zivkovicmilos Feb 28, 2023
6d21b54
Add additional docs
zivkovicmilos Feb 28, 2023
ccdd6fd
Fix help output
zivkovicmilos Feb 28, 2023
5c82d82
Update node info for the indexer
zivkovicmilos Feb 28, 2023
e612c06
Add flush operation to indexer
zivkovicmilos Feb 28, 2023
b824a60
Add indexer config to test files
zivkovicmilos Feb 28, 2023
2b9d8f3
Add constant default value for indexer type
zivkovicmilos Feb 28, 2023
7387d4a
Change binding to work on Macs for some reason
zivkovicmilos Mar 2, 2023
8c2e4b4
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos Mar 2, 2023
c0da941
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos May 15, 2023
90e69c2
Remove leftover .gno files
zivkovicmilos May 15, 2023
9df0ce3
Run gofumpt
zivkovicmilos May 15, 2023
c09a831
Simplify indexer type listing
zivkovicmilos May 16, 2023
dee7806
Add error logging for group methods
zivkovicmilos May 16, 2023
8f13f4e
Add constants for indexer status
zivkovicmilos May 16, 2023
2c5aaa6
Change import alias
zivkovicmilos May 16, 2023
86c11c9
Standardize error logging in the indexer service
zivkovicmilos May 16, 2023
7f81211
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos Jul 4, 2023
443953d
Rename indexer -> event store
zivkovicmilos Jul 7, 2023
16f07c8
Rename event switch reference
zivkovicmilos Jul 7, 2023
b7c1dca
Revert error assignment
zivkovicmilos Jul 7, 2023
20e9a5c
Move event store status to the event store package
zivkovicmilos Jul 7, 2023
c865287
Rename event store interface method
zivkovicmilos Jul 7, 2023
10c2483
Rename event store files
zivkovicmilos Jul 7, 2023
f1567f4
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos Jul 13, 2023
2f9cf60
Resolve renamed cfg reference
zivkovicmilos Jul 13, 2023
6deaa60
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos Jul 27, 2023
b562b19
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos Aug 3, 2023
520c153
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos Aug 10, 2023
80c8c10
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos Aug 17, 2023
3204a38
Resolve typo in help output
zivkovicmilos Aug 17, 2023
b9b16d0
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos Aug 31, 2023
d43a83f
Merge branch 'master' into feature/simple-tx-indexing
zivkovicmilos Oct 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions gno.land/cmd/gnoland/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"os"
Expand All @@ -16,6 +17,9 @@ import (
"github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/bft/node"
"github.com/gnolang/gno/tm2/pkg/bft/privval"
indexercfg "github.com/gnolang/gno/tm2/pkg/bft/state/txindex/config"
"github.com/gnolang/gno/tm2/pkg/bft/state/txindex/file"
"github.com/gnolang/gno/tm2/pkg/bft/state/txindex/null"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto"
Expand All @@ -33,6 +37,9 @@ type gnolandCfg struct {
chainID string
genesisRemote string
rootDir string

txIndexerType string
txIndexerPath string
}

func main() {
Expand Down Expand Up @@ -105,6 +112,20 @@ func (c *gnolandCfg) RegisterFlags(fs *flag.FlagSet) {
"localhost:26657",
"replacement for '%%REMOTE%%' in genesis",
)

fs.StringVar(
&c.txIndexerType,
"tx-indexer-type",
null.IndexerType,
fmt.Sprintf("type of transaction indexer [%s, %s]", null.IndexerType, file.IndexerType),
)
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved

fs.StringVar(
&c.txIndexerPath,
"tx-indexer-path",
"",
fmt.Sprintf("path for the file tx-indexer (required if indexer if '%s')", file.IndexerType),
)
}

func exec(c *gnolandCfg) error {
Expand Down Expand Up @@ -134,6 +155,14 @@ func exec(c *gnolandCfg) error {
writeGenesisFile(genDoc, genesisFilePath)
}

// Initialize the indexer config
indexerCfg, err := getIndexerConfig(c)
if err != nil {
return fmt.Errorf("unable to parse indexer config, %w", err)
}

cfg.Indexer = indexerCfg

// create application and node.
gnoApp, err := gnoland.NewApp(rootDir, c.skipFailingGenesisTxs, logger)
if err != nil {
Expand Down Expand Up @@ -169,6 +198,30 @@ func exec(c *gnolandCfg) error {
select {} // run forever
}

// getIndexerConfig constructs an indexer config from provided user options
func getIndexerConfig(c *gnolandCfg) (*indexercfg.Config, error) {
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
var cfg *indexercfg.Config

switch c.txIndexerType {
case file.IndexerType:
if c.txIndexerPath == "" {
return nil, errors.New("unspecified file transaction indexer path")
}

// Fill out the configuration
cfg = &indexercfg.Config{
IndexerType: file.IndexerType,
Params: map[string]any{
file.Path: c.txIndexerPath,
},
}
default:
cfg = indexercfg.DefaultIndexerConfig()
}

return cfg, nil
}

// Makes a local test genesis doc with local privValidator.
func makeGenesisDoc(
pvPub crypto.PubKey,
Expand Down
5 changes: 3 additions & 2 deletions tm2/pkg/autofile/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ func (g *Group) Wait() {

// Close closes the head file. The group must be stopped by this moment.
func (g *Group) Close() {
g.FlushAndSync()
_ = g.FlushAndSync()

g.mtx.Lock()
defer g.mtx.Unlock()

_ = g.Head.Close()
g.mtx.Unlock()
}
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved

// HeadSizeLimit returns the current head size limit.
Expand Down
6 changes: 5 additions & 1 deletion tm2/pkg/bft/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cns "github.com/gnolang/gno/tm2/pkg/bft/consensus/config"
mem "github.com/gnolang/gno/tm2/pkg/bft/mempool/config"
rpc "github.com/gnolang/gno/tm2/pkg/bft/rpc/config"
"github.com/gnolang/gno/tm2/pkg/bft/state/txindex/config"
"github.com/gnolang/gno/tm2/pkg/errors"
osm "github.com/gnolang/gno/tm2/pkg/os"
p2p "github.com/gnolang/gno/tm2/pkg/p2p/config"
Expand All @@ -24,6 +25,7 @@ type Config struct {
P2P *p2p.P2PConfig `toml:"p2p"`
Mempool *mem.MempoolConfig `toml:"mempool"`
Consensus *cns.ConsensusConfig `toml:"consensus"`
Indexer *config.Config `toml:"indexer"`
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
}

// DefaultConfig returns a default configuration for a Tendermint node
Expand All @@ -34,6 +36,7 @@ func DefaultConfig() *Config {
P2P: p2p.DefaultP2PConfig(),
Mempool: mem.DefaultMempoolConfig(),
Consensus: cns.DefaultConsensusConfig(),
Indexer: config.DefaultIndexerConfig(),
}
}

Expand Down Expand Up @@ -73,6 +76,7 @@ func TestConfig() *Config {
P2P: p2p.TestP2PConfig(),
Mempool: mem.TestMempoolConfig(),
Consensus: cns.TestConsensusConfig(),
Indexer: config.DefaultIndexerConfig(),
}
}

Expand Down Expand Up @@ -121,7 +125,7 @@ func (cfg *Config) ValidateBasic() error {
return nil
}

//-----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// BaseConfig

const (
Expand Down
65 changes: 32 additions & 33 deletions tm2/pkg/bft/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/gnolang/cors"
"github.com/gnolang/gno/tm2/pkg/bft/state/txindex/file"

"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
Expand Down Expand Up @@ -41,7 +42,7 @@ import (
verset "github.com/gnolang/gno/tm2/pkg/versionset"
)

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

// DBContext specifies config information for loading a new DB.
type DBContext struct {
Expand Down Expand Up @@ -134,7 +135,7 @@ func CustomReactors(reactors map[string]p2p.Reactor) Option {
}
}

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

// Node is the highest level interface to a full Tendermint node.
// It includes all configuration information and running services.
Expand Down Expand Up @@ -193,35 +194,35 @@ func createAndStartProxyAppConns(clientCreator proxy.ClientCreator, logger log.L
return proxyApp, nil
}

func createAndStartIndexerService(config *cfg.Config, dbProvider DBProvider,
evsw events.EventSwitch, logger log.Logger,
func createAndStartIndexerService(
cfg *cfg.Config,
evSwitch events.EventSwitch,
logger log.Logger,
) (*txindex.IndexerService, txindex.TxIndexer, error) {
var txIndexer txindex.TxIndexer = &null.TxIndex{}
/*
switch config.TxIndex.Indexer {
case "kv":
store, err := dbProvider(&DBContext{"tx_index", config})
if err != nil {
return nil, nil, err
}
switch {
case config.TxIndex.IndexTags != "":
txIndexer = kv.NewTxIndex(store, kv.IndexTags(splitAndTrimEmpty(config.TxIndex.IndexTags, ",", " ")))
case config.TxIndex.IndexAllTags:
txIndexer = kv.NewTxIndex(store, kv.IndexAllTags())
default:
txIndexer = kv.NewTxIndex(store)
}
default:
txIndexer = &null.TxIndex{}
var (
err error
txIndexer txindex.TxIndexer
)

// Instantiate the indexer based on the configuration
switch cfg.Indexer.IndexerType {
case file.IndexerType:
// Transaction indexes should be logged to files
txIndexer, err = file.NewTxIndexer(cfg.Indexer)
if err != nil {
return nil, nil, fmt.Errorf("unable to create file tx indexer, %w", err)
}
*/
default:
// Transaction indexing should be omitted
txIndexer = null.NewNullIndexer()
}

indexerService := txindex.NewIndexerService(txIndexer, evsw)
indexerService := txindex.NewIndexerService(txIndexer, evSwitch)
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
indexerService.SetLogger(logger.With("module", "txindex"))
if err := indexerService.Start(); err != nil {
if err = indexerService.Start(); err != nil {
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil, err
}

return indexerService, txIndexer, nil
}

Expand Down Expand Up @@ -438,7 +439,7 @@ func NewNode(config *cfg.Config,
evsw := events.NewEventSwitch()

// Transaction indexing
indexerService, txIndexer, err := createAndStartIndexerService(config, dbProvider, evsw, logger)
indexerService, txIndexer, err := createAndStartIndexerService(config, evsw, logger)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -819,7 +820,7 @@ func (n *Node) Config() *cfg.Config {
return n.config
}

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

func (n *Node) Listeners() []string {
return []string{
Expand All @@ -843,11 +844,9 @@ func makeNodeInfo(
genDoc *types.GenesisDoc,
state sm.State,
) (p2p.NodeInfo, error) {
txIndexerStatus := "on"
if _, ok := txIndexer.(*null.TxIndex); ok {
txIndexerStatus = "off"
} else if txIndexer == nil {
txIndexerStatus = "none"
txIndexerStatus := "off"
if txIndexer.GetType() != null.IndexerType {
txIndexerStatus = "on"
}
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved

bcChannel := bc.BlockchainChannel
Expand Down Expand Up @@ -887,7 +886,7 @@ func makeNodeInfo(
return nodeInfo, err
}

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

var genesisDocKey = []byte("genesisDoc")

Expand Down
30 changes: 30 additions & 0 deletions tm2/pkg/bft/state/txindex/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved

import "github.com/gnolang/gno/tm2/pkg/bft/state/txindex/null"

// IndexerParams defines the arbitrary indexer config params
type IndexerParams map[string]any

// Config defines the specific transaction
// indexer configuration
type Config struct {
IndexerType string
Params IndexerParams
}

// GetParam fetches the specific config param, if any.
// Returns nil if the param is not present
func (c *Config) GetParam(name string) any {
if c.Params != nil {
return c.Params[name]
}

return nil
}

// DefaultIndexerConfig returns the default indexer config
func DefaultIndexerConfig() *Config {
return &Config{
IndexerType: null.IndexerType,
}
}
45 changes: 45 additions & 0 deletions tm2/pkg/bft/state/txindex/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package config

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfig_GetParam(t *testing.T) {
t.Parallel()

const paramName = "param"

testTable := []struct {
name string
cfg *Config

expectedParam any
}{
{
"param not set",
&Config{},
nil,
},
{
"valid param set",
&Config{
Params: map[string]any{
paramName: 10,
},
},
10,
},
}

for _, testCase := range testTable {
testCase := testCase

t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

assert.Equal(t, testCase.expectedParam, testCase.cfg.GetParam(paramName))
})
}
}
Loading