Skip to content

Commit

Permalink
refactor(server/v2/cometbft): Consensus get chain-id from viper (co…
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk authored Aug 2, 2024
1 parent cffeedf commit a26970e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 4 deletions.
3 changes: 2 additions & 1 deletion server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func NewConsensus[T transaction.Tx](
store types.Store,
cfg Config,
txCodec transaction.Codec[T],
chainId string,
) *Consensus[T] {
return &Consensus[T]{
appName: appName,
Expand All @@ -93,7 +94,7 @@ func NewConsensus[T transaction.Tx](
processProposalHandler: nil,
verifyVoteExt: nil,
extendVote: nil,
chainID: "",
chainID: chainId,
indexedEvents: indexedEvents,
initialHeight: 0,
}
Expand Down
2 changes: 2 additions & 0 deletions server/v2/cometbft/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
Expand Down
17 changes: 17 additions & 0 deletions server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"os"
"path/filepath"

abciserver "github.com/cometbft/cometbft/abci/server"
Expand Down Expand Up @@ -68,6 +69,21 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger l
AppTomlConfig: appTomlConfig,
}

chainID := v.GetString(FlagChainID)
if chainID == "" {
// fallback to genesis chain-id
reader, err := os.Open(filepath.Join(v.GetString(serverv2.FlagHome), "config", "genesis.json"))
if err != nil {
panic(err)
}
defer reader.Close()

chainID, err = genutiltypes.ParseChainIDFromGenesis(reader)
if err != nil {
panic(fmt.Errorf("failed to parse chain-id from genesis file: %w", err))
}
}

indexEvents := make(map[string]struct{}, len(s.config.AppTomlConfig.IndexEvents))
for _, e := range s.config.AppTomlConfig.IndexEvents {
indexEvents[e] = struct{}{}
Expand All @@ -86,6 +102,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger l
store,
s.config,
s.initTxCodec,
chainID,
)
consensus.prepareProposalHandler = s.serverOptions.PrepareProposalHandler
consensus.processProposalHandler = s.serverOptions.ProcessProposalHandler
Expand Down
4 changes: 2 additions & 2 deletions server/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package serverv2

import (
"fmt"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
Expand Down Expand Up @@ -34,8 +33,9 @@ func ReadConfig(configPath string) (*viper.Viper, error) {
func UnmarshalSubConfig(v *viper.Viper, subName string, target any) error {
var sub any
for k, val := range v.AllSettings() {
if strings.HasPrefix(k, subName) {
if k == subName {
sub = val
break
}
}

Expand Down
7 changes: 7 additions & 0 deletions server/v2/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

serverv2 "cosmossdk.io/server/v2"
grpc "cosmossdk.io/server/v2/api/grpc"
store "cosmossdk.io/server/v2/store"
)

func TestReadConfig(t *testing.T) {
Expand All @@ -20,6 +21,7 @@ func TestReadConfig(t *testing.T) {
require.NoError(t, err)

require.Equal(t, v.GetString("grpc.address"), grpc.DefaultConfig().Address)
require.Equal(t, v.GetString("store.app-db-backend"), store.DefaultConfig().AppDBBackend)
}

func TestUnmarshalSubConfig(t *testing.T) {
Expand All @@ -36,4 +38,9 @@ func TestUnmarshalSubConfig(t *testing.T) {

require.True(t, grpc.DefaultConfig().Enable)
require.False(t, grpcConfig.Enable)

storeConfig := store.Config{}
err = serverv2.UnmarshalSubConfig(v, "store", &storeConfig)
require.NoError(t, err)
require.Equal(t, *store.DefaultConfig(), storeConfig)
}
2 changes: 1 addition & 1 deletion server/v2/store/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func New[T transaction.Tx]() *StoreComponent[T] {
func (s *StoreComponent[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) error {
cfg := DefaultConfig()
if v != nil {
if err := v.Sub(s.Name()).Unmarshal(&cfg); err != nil {
if err := serverv2.UnmarshalSubConfig(v, s.Name(), &cfg); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
}
Expand Down
30 changes: 30 additions & 0 deletions server/v2/testdata/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,36 @@ max-recv-msg-size = 10485760
# The default value is math.MaxInt32.
max-send-msg-size = 2147483647

[store]
# The type of database for application and snapshots databases.
app-db-backend = 'goleveldb'

[store.options]
# State storage database type. Currently we support: 0 for SQLite, 1 for Pebble
ss-type = 0
# State commitment database type. Currently we support:0 for iavl, 1 for iavl v2
sc-type = 0

# Pruning options for state storage
[store.options.ss-pruning-option]
# Number of recent heights to keep on disk.
keep-recent = 2
# Height interval at which pruned heights are removed from disk.
interval = 1

# Pruning options for state commitment
[store.options.sc-pruning-option]
# Number of recent heights to keep on disk.
keep-recent = 2
# Height interval at which pruned heights are removed from disk.
interval = 1

[store.options.iavl-config]
# CacheSize set the size of the iavl tree cache.
cache-size = 100000
# If true, the tree will work like no fast storage and always not upgrade fast storage.
skip-fast-storage-upgrade = true

[mock-server-1]
# Mock field
mock_field = 'default'
Expand Down

0 comments on commit a26970e

Please sign in to comment.