Skip to content

Commit

Permalink
chore: remove consensus authority (#21734)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <julien@rbrt.fr>
(cherry picked from commit a9f057b)

# Conflicts:
#	server/v2/types.go
#	store/v2/root/factory.go
  • Loading branch information
tac0turtle authored and mergify[bot] committed Sep 16, 2024
1 parent e8841b6 commit a204fe7
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 27 deletions.
20 changes: 8 additions & 12 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ import (
var _ abci.Application = (*Consensus[transaction.Tx])(nil)

type Consensus[T transaction.Tx] struct {
logger log.Logger
appName, version string
consensusAuthority string // Set by the application to grant authority to the consensus engine to send messages to the consensus module
app *appmanager.AppManager[T]
txCodec transaction.Codec[T]
store types.Store
streaming streaming.Manager
snapshotManager *snapshots.Manager
mempool mempool.Mempool[T]
logger log.Logger
appName, version string
app *appmanager.AppManager[T]
txCodec transaction.Codec[T]
store types.Store
streaming streaming.Manager
snapshotManager *snapshots.Manager
mempool mempool.Mempool[T]

cfg Config
indexedEvents map[string]struct{}
Expand All @@ -67,7 +66,6 @@ type Consensus[T transaction.Tx] struct {
func NewConsensus[T transaction.Tx](
logger log.Logger,
appName string,
consensusAuthority string, // TODO remove
app *appmanager.AppManager[T],
mp mempool.Mempool[T],
indexedEvents map[string]struct{},
Expand All @@ -80,7 +78,6 @@ func NewConsensus[T transaction.Tx](
return &Consensus[T]{
appName: appName,
version: getCometBFTServerVersion(),
consensusAuthority: consensusAuthority,
grpcMethodsMap: gRPCMethodsMap,
app: app,
cfg: cfg,
Expand Down Expand Up @@ -246,7 +243,6 @@ func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRe

if req.ConsensusParams != nil {
ctx = context.WithValue(ctx, corecontext.CometParamsInitInfoKey, &consensustypes.MsgUpdateParams{
Authority: c.consensusAuthority,
Block: req.ConsensusParams.Block,
Evidence: req.ConsensusParams.Evidence,
Validator: req.ConsensusParams.Validator,
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
am, err := b.Build()
require.NoError(t, err)

return NewConsensus[mock.Tx](log.NewNopLogger(), "testing-app", "authority", am, mempool, map[string]struct{}{}, nil, mockStore, Config{AppTomlConfig: DefaultAppTomlConfig()}, mock.TxCodec{}, "test")
return NewConsensus[mock.Tx](log.NewNopLogger(), "testing-app", am, mempool, map[string]struct{}{}, nil, mockStore, Config{AppTomlConfig: DefaultAppTomlConfig()}, mock.TxCodec{}, "test")
}

// Check target version same with store's latest version
Expand Down
1 change: 0 additions & 1 deletion server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
consensus := NewConsensus(
s.logger,
appI.Name(),
appI.GetConsensusAuthority(),
appI.GetAppManager(),
s.serverOptions.Mempool(cfg),
indexEvents,
Expand Down
21 changes: 21 additions & 0 deletions server/v2/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package serverv2

import (
gogoproto "github.com/cosmos/gogoproto/proto"
"github.com/spf13/viper"

"cosmossdk.io/core/server"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
"cosmossdk.io/server/v2/appmanager"

Check failure on line 10 in server/v2/types.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/server/v2/appmanager; to add it:

Check failure on line 10 in server/v2/types.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/server/v2/appmanager; to add it:

Check failure on line 10 in server/v2/types.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/server/v2/appmanager; to add it:

Check failure on line 10 in server/v2/types.go

View workflow job for this annotation

GitHub Actions / dependency-review

could not import cosmossdk.io/server/v2/appmanager (invalid package name: "")
)

type AppCreator[T transaction.Tx] func(log.Logger, *viper.Viper) AppI[T]

type AppI[T transaction.Tx] interface {
Name() string
InterfaceRegistry() server.InterfaceRegistry
GetAppManager() *appmanager.AppManager[T]
GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message
GetStore() any
}
6 changes: 2 additions & 4 deletions simapp/v2/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,8 @@ var (
Config: appconfig.WrapAny(&govmodulev1.Module{}),
},
{
Name: consensustypes.ModuleName,
Config: appconfig.WrapAny(&consensusmodulev1.Module{
Authority: "consensus", // TODO remove.
}),
Name: consensustypes.ModuleName,
Config: appconfig.WrapAny(&consensusmodulev1.Module{}),
},
{
Name: accounts.ModuleName,
Expand Down
10 changes: 1 addition & 9 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
"cosmossdk.io/store/v2/root"
consensuskeeper "cosmossdk.io/x/consensus/keeper"
upgradekeeper "cosmossdk.io/x/upgrade/keeper"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -38,8 +37,7 @@ type SimApp[T transaction.Tx] struct {

// required keepers during wiring
// others keepers are all in the app
UpgradeKeeper *upgradekeeper.Keeper
ConsensusParamsKeeper consensuskeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
}

func init() {
Expand Down Expand Up @@ -135,7 +133,6 @@ func NewSimApp[T transaction.Tx](
&app.txConfig,
&app.interfaceRegistry,
&app.UpgradeKeeper,
&app.ConsensusParamsKeeper,
); err != nil {
panic(err)
}
Expand Down Expand Up @@ -186,11 +183,6 @@ func (app *SimApp[T]) TxConfig() client.TxConfig {
return app.txConfig
}

// GetConsensusAuthority gets the consensus authority.
func (app *SimApp[T]) GetConsensusAuthority() string {
return app.ConsensusParamsKeeper.GetAuthority()
}

// GetStore gets the app store.
func (app *SimApp[T]) GetStore() any {
return app.App.GetStore()
Expand Down
181 changes: 181 additions & 0 deletions store/v2/root/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package root

import (
"errors"
"fmt"
"os"

"cosmossdk.io/core/log"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/v2"

Check failure on line 10 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/store/v2; to add it:

Check failure on line 10 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2; to add it:

Check failure on line 10 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2; to add it:
"cosmossdk.io/store/v2/commitment"

Check failure on line 11 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/store/v2/commitment; to add it:

Check failure on line 11 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/commitment; to add it:

Check failure on line 11 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/commitment; to add it:
"cosmossdk.io/store/v2/commitment/iavl"

Check failure on line 12 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/store/v2/commitment/iavl; to add it:

Check failure on line 12 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/commitment/iavl; to add it:

Check failure on line 12 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/commitment/iavl; to add it:
"cosmossdk.io/store/v2/commitment/mem"

Check failure on line 13 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/store/v2/commitment/mem; to add it:

Check failure on line 13 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/commitment/mem; to add it:

Check failure on line 13 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/commitment/mem; to add it:
"cosmossdk.io/store/v2/db"

Check failure on line 14 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/store/v2/db; to add it:

Check failure on line 14 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/db; to add it:

Check failure on line 14 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/db; to add it:
"cosmossdk.io/store/v2/internal"

Check failure on line 15 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/store/v2/internal; to add it:

Check failure on line 15 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/internal; to add it:

Check failure on line 15 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/internal; to add it:
"cosmossdk.io/store/v2/pruning"

Check failure on line 16 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/store/v2/pruning; to add it:

Check failure on line 16 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/pruning; to add it:

Check failure on line 16 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/pruning; to add it:
"cosmossdk.io/store/v2/storage"

Check failure on line 17 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/store/v2/storage; to add it:

Check failure on line 17 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/storage; to add it:

Check failure on line 17 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/storage; to add it:
"cosmossdk.io/store/v2/storage/pebbledb"

Check failure on line 18 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/store/v2/storage/pebbledb; to add it:

Check failure on line 18 in store/v2/root/factory.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/store/v2/storage/pebbledb; to add it:
"cosmossdk.io/store/v2/storage/rocksdb"
"cosmossdk.io/store/v2/storage/sqlite"
)

type (
SSType string
SCType string
)

const (
SSTypeSQLite SSType = "sqlite"
SSTypePebble SSType = "pebble"
SSTypeRocks SSType = "rocksdb"
SCTypeIavl SCType = "iavl"
SCTypeIavlV2 SCType = "iavl-v2"
)

// app.toml config options
type Options struct {
SSType SSType `mapstructure:"ss-type" toml:"ss-type" comment:"SState storage database type. Currently we support: \"sqlite\", \"pebble\" and \"rocksdb\""`
SCType SCType `mapstructure:"sc-type" toml:"sc-type" comment:"State commitment database type. Currently we support: \"iavl\" and \"iavl-v2\""`
SSPruningOption *store.PruningOption `mapstructure:"ss-pruning-option" toml:"ss-pruning-option" comment:"Pruning options for state storage"`
SCPruningOption *store.PruningOption `mapstructure:"sc-pruning-option" toml:"sc-pruning-option" comment:"Pruning options for state commitment"`
IavlConfig *iavl.Config `mapstructure:"iavl-config" toml:"iavl-config"`
}

// FactoryOptions are the options for creating a root store.
type FactoryOptions struct {
Logger log.Logger
RootDir string
Options Options
StoreKeys []string
SCRawDB corestore.KVStoreWithBatch
}

// DefaultStoreOptions returns the default options for creating a root store.
func DefaultStoreOptions() Options {
return Options{
SSType: SSTypeSQLite,
SCType: SCTypeIavl,
SCPruningOption: &store.PruningOption{
KeepRecent: 2,
Interval: 100,
},
SSPruningOption: &store.PruningOption{
KeepRecent: 2,
Interval: 100,
},
IavlConfig: &iavl.Config{
CacheSize: 100_000,
SkipFastStorageUpgrade: true,
},
}
}

// CreateRootStore is a convenience function to create a root store based on the
// provided FactoryOptions. Strictly speaking app developers can create the root
// store directly by calling root.New, so this function is not
// necessary, but demonstrates the required steps and configuration to create a root store.
func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
var (
ssDb storage.Database
ss *storage.StorageStore
sc *commitment.CommitStore
err error
ensureDir = func(dir string) error {
if err := os.MkdirAll(dir, 0o0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}
return nil
}
)

storeOpts := opts.Options
switch storeOpts.SSType {
case SSTypeSQLite:
dir := fmt.Sprintf("%s/data/ss/sqlite", opts.RootDir)
if err = ensureDir(dir); err != nil {
return nil, err
}
ssDb, err = sqlite.New(dir)
case SSTypePebble:
dir := fmt.Sprintf("%s/data/ss/pebble", opts.RootDir)
if err = ensureDir(dir); err != nil {
return nil, err
}
ssDb, err = pebbledb.New(dir)
case SSTypeRocks:
dir := fmt.Sprintf("%s/data/ss/rocksdb", opts.RootDir)
if err = ensureDir(dir); err != nil {
return nil, err
}
ssDb, err = rocksdb.New(dir)
default:
return nil, fmt.Errorf("unknown storage type: %s", opts.Options.SSType)
}
if err != nil {
return nil, err
}
ss = storage.NewStorageStore(ssDb, opts.Logger)

metadata := commitment.NewMetadataStore(opts.SCRawDB)
latestVersion, err := metadata.GetLatestVersion()
if err != nil {
return nil, err
}
if len(opts.StoreKeys) == 0 {
lastCommitInfo, err := metadata.GetCommitInfo(latestVersion)
if err != nil {
return nil, err
}
if lastCommitInfo == nil {
return nil, fmt.Errorf("tried to construct a root store with no store keys specified but no commit info found for version %d", latestVersion)
}
for _, si := range lastCommitInfo.StoreInfos {
opts.StoreKeys = append(opts.StoreKeys, string(si.Name))
}
}
removedStoreKeys, err := metadata.GetRemovedStoreKeys(latestVersion)
if err != nil {
return nil, err
}

newTreeFn := func(key string) (commitment.Tree, error) {
if internal.IsMemoryStoreKey(key) {
return mem.New(), nil
} else {
switch storeOpts.SCType {
case SCTypeIavl:
return iavl.NewIavlTree(db.NewPrefixDB(opts.SCRawDB, []byte(key)), opts.Logger, storeOpts.IavlConfig), nil
case SCTypeIavlV2:
return nil, errors.New("iavl v2 not supported")
default:
return nil, errors.New("unsupported commitment store type")
}
}
}

trees := make(map[string]commitment.Tree, len(opts.StoreKeys))
for _, key := range opts.StoreKeys {
tree, err := newTreeFn(key)
if err != nil {
return nil, err
}
trees[key] = tree
}
oldTrees := make(map[string]commitment.Tree, len(opts.StoreKeys))
for _, key := range removedStoreKeys {
tree, err := newTreeFn(string(key))
if err != nil {
return nil, err
}
oldTrees[string(key)] = tree
}

sc, err = commitment.NewCommitStore(trees, oldTrees, opts.SCRawDB, opts.Logger)
if err != nil {
return nil, err
}

pm := pruning.NewManager(sc, ss, storeOpts.SCPruningOption, storeOpts.SSPruningOption)
return New(opts.Logger, ss, sc, pm, nil, nil)
}

0 comments on commit a204fe7

Please sign in to comment.