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

refactor(block): move da init to NewNode instead of NewManager #1332

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 11 additions & 35 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"github.com/dymensionxyz/gerr-cosmos/gerrc"
"golang.org/x/sync/errgroup"

"github.com/dymensionxyz/dymint/da/registry"
"github.com/dymensionxyz/dymint/indexers/txindex"
"github.com/dymensionxyz/dymint/node/events"
"github.com/dymensionxyz/dymint/store"
Expand Down Expand Up @@ -132,10 +131,10 @@
mempool mempool.Mempool,
proxyApp proxy.AppConns,
settlementClient settlement.ClientI,
daClient da.DataAvailabilityLayerClient,
eventBus *tmtypes.EventBus,
pubsub *pubsub.Server,
p2pClient *p2p.Client,
dalcKV *store.PrefixKV,
indexerService *txindex.IndexerService,
logger log.Logger,
) (*Manager, error) {
Expand Down Expand Up @@ -168,6 +167,8 @@
Executor: exec,
Sequencers: types.NewSequencerSet(),
SLClient: settlementClient,
DAClient: daClient,
Retriever: daClient,
IndexerService: indexerService,
logger: logger.With("module", "block_manager"),
blockCache: &Cache{
Expand All @@ -183,17 +184,6 @@
return nil, fmt.Errorf("get initial state: %w", err)
}

err = m.setDA(conf.DAConfig, dalcKV, logger)
if err != nil {
return nil, err
}

// validate configuration params and rollapp consensus params are in line
err = m.ValidateConfigWithRollappParams()
if err != nil {
return nil, err
}

m.SettlementValidator = NewSettlementValidator(m.logger, m)

return m, nil
Expand All @@ -217,8 +207,14 @@
}
}

// validate configuration params and rollapp consensus params are in line
err := m.ValidateConfigWithRollappParams()
if err != nil {
return err
}

Check warning on line 214 in block/manager.go

View check run for this annotation

Codecov / codecov/patch

block/manager.go#L213-L214

Added lines #L213 - L214 were not covered by tests

// update dymint state with next revision info
err := m.updateStateForNextRevision()
err = m.updateStateForNextRevision()
if err != nil {
return err
}
Expand Down Expand Up @@ -405,34 +401,14 @@
return fmt.Errorf("da client mismatch. rollapp param: %s da configured: %s", m.State.RollappParams.Da, m.DAClient.GetClientType())
}

// TODO: proposer only?
if m.Conf.BatchSubmitBytes > uint64(m.DAClient.GetMaxBlobSizeBytes()) {
return fmt.Errorf("batch size above limit: batch size: %d limit: %d: DA %s", m.Conf.BatchSubmitBytes, m.DAClient.GetMaxBlobSizeBytes(), m.DAClient.GetClientType())
}

return nil
}

// setDA initializes DA client in blockmanager according to DA type set in genesis or stored in state
func (m *Manager) setDA(daconfig string, dalcKV store.KV, logger log.Logger) error {
daLayer := m.State.RollappParams.Da
dalc := registry.GetClient(daLayer)
if dalc == nil {
return fmt.Errorf("get data availability client named '%s'", daLayer)
}

err := dalc.Init([]byte(daconfig), m.Pubsub, dalcKV, logger.With("module", string(dalc.GetClientType())))
if err != nil {
return fmt.Errorf("data availability layer client initialization: %w", err)
}
m.DAClient = dalc
retriever, ok := dalc.(da.BatchRetriever)
if !ok {
return fmt.Errorf("data availability layer client is not of type BatchRetriever")
}
m.Retriever = retriever
return nil
}

// setFraudHandler sets the fraud handler for the block manager.
func (m *Manager) setFraudHandler(handler *FreezeHandler) {
m.FraudHandler = handler
Expand Down
12 changes: 7 additions & 5 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func TestInitialState(t *testing.T) {
settlementlc := slregistry.GetClient(slregistry.Local)
_ = settlementlc.Init(settlement.Config{}, genesis.ChainID, pubsubServer, logger)

daclient := testutil.GetMockDALC(logger)

// Init empty store and full store
emptyStore := store.New(store.NewDefaultInMemoryKVStore())
fullStore := store.New(store.NewDefaultInMemoryKVStore())
Expand Down Expand Up @@ -114,8 +116,8 @@ func TestInitialState(t *testing.T) {

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
agg, err := block.NewManager(key, conf, c.genesis, "", c.store, nil, proxyApp, settlementlc,
nil, pubsubServer, p2pClient, nil, nil, logger)
agg, err := block.NewManager(key, conf, c.genesis, "", c.store, nil, proxyApp, settlementlc, daclient,
nil, pubsubServer, p2pClient, nil, logger)
assert.NoError(err)
assert.NotNil(agg)
assert.Equal(c.expectedChainID, agg.State.ChainID)
Expand Down Expand Up @@ -160,7 +162,7 @@ func TestProduceOnlyAfterSynced(t *testing.T) {

t.Log("Taking the manager out of sync by submitting a batch")
manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
manager.Retriever = manager.DAClient.(da.BatchRetriever)
manager.Retriever = manager.DAClient

numBatchesToAdd := 2
nextBatchStartHeight := manager.NextHeightToSubmit()
Expand Down Expand Up @@ -205,7 +207,7 @@ func TestRetrieveDaBatchesFailed(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, manager)
manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
manager.Retriever = manager.DAClient.(da.BatchRetriever)
manager.Retriever = manager.DAClient

batch := &settlement.Batch{
MetaData: &settlement.BatchMetaData{
Expand Down Expand Up @@ -511,7 +513,7 @@ func TestDAFetch(t *testing.T) {
commitHash := [32]byte{}

manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
manager.Retriever = manager.DAClient.(da.BatchRetriever)
manager.Retriever = manager.DAClient

app.On("Commit", mock.Anything).Return(abci.ResponseCommit{Data: commitHash[:]})

Expand Down
5 changes: 2 additions & 3 deletions block/production_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/tendermint/tendermint/proxy"

block2 "github.com/dymensionxyz/dymint/block"
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/mempool"
mempoolv1 "github.com/dymensionxyz/dymint/mempool/v1"
slmocks "github.com/dymensionxyz/dymint/mocks/github.com/dymensionxyz/dymint/settlement"
Expand Down Expand Up @@ -335,7 +334,7 @@ func TestUpdateInitialSequencerSet(t *testing.T) {
require.NoError(err)

manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
manager.Retriever = manager.DAClient.(da.BatchRetriever)
manager.Retriever = manager.DAClient

// Check initial assertions
require.Zero(manager.State.Height())
Expand Down Expand Up @@ -466,7 +465,7 @@ func TestUpdateExistingSequencerSet(t *testing.T) {
require.NoError(err)

manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
manager.Retriever = manager.DAClient.(da.BatchRetriever)
manager.Retriever = manager.DAClient

// Set the initial sequencer set
manager.Sequencers.Set([]types.Sequencer{proposer, sequencer})
Expand Down
3 changes: 1 addition & 2 deletions block/pruning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/dymensionxyz/dymint/block"
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/testutil"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -41,7 +40,7 @@ func TestPruningRetainHeight(t *testing.T) {
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)
manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
manager.Retriever = manager.DAClient.(da.BatchRetriever)
manager.Retriever = manager.DAClient

// Check initial assertions
require.Zero(manager.State.Height())
Expand Down
4 changes: 2 additions & 2 deletions block/slvalidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func TestStateUpdateValidator_ValidateStateUpdate(t *testing.T) {

// Create DA
manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
manager.Retriever = manager.DAClient.(da.BatchRetriever)
manager.Retriever = manager.DAClient

// Generate batch
var batch *types.Batch
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestStateUpdateValidator_ValidateDAFraud(t *testing.T) {
manager.DAClient = mockDA.DaClient
err = manager.DAClient.Start()
require.NoError(t, err)
manager.Retriever = manager.DAClient.(da.BatchRetriever)
manager.Retriever = manager.DAClient

// Generate blob from batch data
require.NoError(t, err)
Expand Down
17 changes: 12 additions & 5 deletions da/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ type ResultRetrieveBatch struct {
// DataAvailabilityLayerClient defines generic interface for DA layer block submission.
// It also contains life-cycle methods.
type DataAvailabilityLayerClient interface {
BatchRetriever
BatchSubmitter

// Init is called once to allow DA client to read configuration and initialize resources.
Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KV, logger types.Logger, options ...Option) error

Expand All @@ -227,11 +230,6 @@ type DataAvailabilityLayerClient interface {
// Stop is called once, when DataAvailabilityLayerClient is no longer needed.
Stop() error

// SubmitBatch submits the passed in block to the DA layer.
// This should create a transaction which (potentially)
// triggers a state transition in the DA layer.
SubmitBatch(batch *types.Batch) ResultSubmitBatch

GetClientType() Client

// CheckBatchAvailability checks the availability of the blob submitted getting proofs and validating them
Expand All @@ -250,6 +248,15 @@ type DataAvailabilityLayerClient interface {
DAPath() string
}

// BatchSubmitter is additional interface that can be implemented by Data Availability Layer Client that is able to submit
// block data to DA layer.
type BatchSubmitter interface {
// SubmitBatch submits the passed in batch to the DA layer.
// This should create a transaction which (potentially)
// triggers a state transition in the DA layer.
SubmitBatch(batch *types.Batch) ResultSubmitBatch
}

// BatchRetriever is additional interface that can be implemented by Data Availability Layer Client that is able to retrieve
// block data from DA layer. This gives the ability to use it for block synchronization.
type BatchRetriever interface {
Expand Down
5 changes: 5 additions & 0 deletions da/stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

type Layer struct{}

// RetrieveBatches implements da.DataAvailabilityLayerClient.
func (l *Layer) RetrieveBatches(daMetaData *da.DASubmitMetaData) da.ResultRetrieveBatch {
panic("unimplemented")

Check warning on line 16 in da/stub/stub.go

View check run for this annotation

Codecov / codecov/patch

da/stub/stub.go#L15-L16

Added lines #L15 - L16 were not covered by tests
}

func (l Layer) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KV, logger types.Logger, options ...da.Option) error {
panic("implement me")
}
Expand Down
26 changes: 21 additions & 5 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
leveldb "github.com/ipfs/go-ds-leveldb"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/dymensionxyz/dymint/da/registry"
"github.com/libp2p/go-libp2p/core/crypto"

"github.com/tendermint/tendermint/libs/log"
Expand All @@ -32,6 +33,7 @@
"github.com/dymensionxyz/dymint/settlement"
slregistry "github.com/dymensionxyz/dymint/settlement/registry"
"github.com/dymensionxyz/dymint/store"
"github.com/dymensionxyz/dymint/types"
)

// prefixes used in KV store to separate main node data from DALC data
Expand Down Expand Up @@ -121,17 +123,31 @@
s := store.New(store.NewPrefixKV(baseKV, mainPrefix))
indexerKV := store.NewPrefixKV(baseKV, indexerPrefix)

// TODO: dalcKV is needed for mock only. Initialize only if mock used
dalcKV := store.NewPrefixKV(baseKV, dalcPrefix)
// Init the settlement layer client
/* ------------------------------ init DA layer ----------------------------- */
params, err := types.GetRollappParamsFromGenesis(genesis.AppState)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if its a migrated rollapp, that does not have rollapp params defined in genesis? in that case i think it will fail to get rollapp params from genesis appstate

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or also in case the da rollapp param is changed via gov proposal, the one defined in genesis wont be correct.

if err != nil {
return nil, fmt.Errorf("in genesis doc: %w", err)
}

Check warning on line 130 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L129-L130

Added lines #L129 - L130 were not covered by tests

dalc := registry.GetClient(params.Params.Da)
if dalc == nil {
return nil, fmt.Errorf("get data availability client named '%s'", params.Params.Da)
}

Check warning on line 135 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L134-L135

Added lines #L134 - L135 were not covered by tests

err = dalc.Init([]byte(conf.DAConfig), pubsubServer, store.NewPrefixKV(baseKV, dalcPrefix), logger.With("module", string(dalc.GetClientType())))
if err != nil {
return nil, fmt.Errorf("data availability layer client initialization: %w", err)
}

Check warning on line 140 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L139-L140

Added lines #L139 - L140 were not covered by tests

/* -------------------- Init the settlement layer client -------------------- */
settlementlc := slregistry.GetClient(slregistry.Client(conf.SettlementLayer))
if settlementlc == nil {
return nil, fmt.Errorf("get settlement client: named: %s", conf.SettlementLayer)
}
if conf.SettlementLayer == "mock" {
conf.SettlementConfig.KeyringHomeDir = conf.RootDir
}
err := settlementlc.Init(conf.SettlementConfig, genesis.ChainID, pubsubServer, logger.With("module", "settlement_client"))
err = settlementlc.Init(conf.SettlementConfig, genesis.ChainID, pubsubServer, logger.With("module", "settlement_client"))
if err != nil {
return nil, fmt.Errorf("settlement layer client initialization: %w", err)
}
Expand Down Expand Up @@ -160,10 +176,10 @@
mp,
proxyApp,
settlementlc,
dalc,
eventBus,
pubsubServer,
nil, // p2p client is set later
dalcKV,
indexerService,
logger,
)
Expand Down
6 changes: 4 additions & 2 deletions testutil/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func GetManagerWithProposerKey(conf config.BlockManagerConfig, proposerKey crypt
return nil, err
}

dacl := GetMockDALC(logger)

var proxyApp proxy.AppConns
if proxyAppConns == nil {
proxyApp = GetABCIProxyAppMock(logger.With("module", "proxy"))
Expand Down Expand Up @@ -118,8 +120,8 @@ func GetManagerWithProposerKey(conf config.BlockManagerConfig, proposerKey crypt
return nil, err
}

manager, err := block.NewManager(proposerKey, config, genesis, "", managerStore, mp, proxyApp, settlementlc, nil,
pubsubServer, p2pClient, nil, indexer, logger)
manager, err := block.NewManager(proposerKey, config, genesis, "", managerStore, mp, proxyApp, settlementlc, dacl, nil,
pubsubServer, p2pClient, indexer, logger)
if err != nil {
return nil, err
}
Expand Down
24 changes: 24 additions & 0 deletions types/rollapp_params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import (
"encoding/json"
"fmt"

"github.com/dymensionxyz/dymint/types/pb/dymint"
abci "github.com/tendermint/tendermint/abci/types"
)
Expand All @@ -15,3 +18,24 @@
ret.DrsVersion = r.DrsVersion
return ret
}

// GetRollappParamsFromGenesis returns the rollapp consensus params from genesis
func GetRollappParamsFromGenesis(appState json.RawMessage) (RollappParams, error) {
var objmap map[string]json.RawMessage
err := json.Unmarshal(appState, &objmap)
if err != nil {
return RollappParams{}, err
}

Check warning on line 28 in types/rollapp_params.go

View check run for this annotation

Codecov / codecov/patch

types/rollapp_params.go#L27-L28

Added lines #L27 - L28 were not covered by tests
params, ok := objmap[rollappparams_modulename]
if !ok {
return RollappParams{}, fmt.Errorf("module not defined in genesis: %s", rollappparams_modulename)
}

Check warning on line 32 in types/rollapp_params.go

View check run for this annotation

Codecov / codecov/patch

types/rollapp_params.go#L31-L32

Added lines #L31 - L32 were not covered by tests

var rollappParams RollappParams
err = json.Unmarshal(params, &rollappParams)
if err != nil {
return RollappParams{}, err
}

Check warning on line 38 in types/rollapp_params.go

View check run for this annotation

Codecov / codecov/patch

types/rollapp_params.go#L37-L38

Added lines #L37 - L38 were not covered by tests

return rollappParams, nil
}
15 changes: 2 additions & 13 deletions types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,9 @@

// SetRollappParamsFromGenesis sets the rollapp consensus params from genesis
func (s *State) SetRollappParamsFromGenesis(appState json.RawMessage) error {
var objmap map[string]json.RawMessage
err := json.Unmarshal(appState, &objmap)
rollappParams, err := GetRollappParamsFromGenesis(appState)
if err != nil {
return err
}
params, ok := objmap[rollappparams_modulename]
if !ok {
return fmt.Errorf("module not defined in genesis: %s", rollappparams_modulename)
}

var rollappParams RollappParams
err = json.Unmarshal(params, &rollappParams)
if err != nil {
return err
return fmt.Errorf("get rollapp params from genesis: %w", err)

Check warning on line 113 in types/state.go

View check run for this annotation

Codecov / codecov/patch

types/state.go#L113

Added line #L113 was not covered by tests
}
s.RollappParams = *rollappParams.Params
return nil
Expand Down
Loading