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

fix(manager): use block params from consensus param #1042

Merged
merged 19 commits into from
Sep 6, 2024
Merged
17 changes: 9 additions & 8 deletions block/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/dymensionxyz/dymint/types"
)

// default minimum block max size allowed. not specific reason to set it to 10K, but we need to avoid no transactions can be included in a block.
const minBlockMaxBytes = 10000

// Executor creates and applies blocks and maintains state.
type Executor struct {
localAddress []byte
Expand Down Expand Up @@ -60,6 +63,7 @@ func (e *Executor) InitChain(genesis *tmtypes.GenesisDoc, valset []*tmtypes.Vali
Power: validator.VotingPower,
})
}

params := genesis.ConsensusParams

return e.proxyAppConsensusConn.InitChainSync(abci.RequestInitChain{
Expand All @@ -81,19 +85,16 @@ func (e *Executor) InitChain(genesis *tmtypes.GenesisDoc, valset []*tmtypes.Vali
Version: &tmproto.VersionParams{
AppVersion: params.Version.AppVersion,
},
},
Validators: valUpdates,
}, Validators: valUpdates,
AppStateBytes: genesis.AppState,
InitialHeight: genesis.InitialHeight,
})
}

// CreateBlock reaps transactions from mempool and builds a block.
func (e *Executor) CreateBlock(height uint64, lastCommit *types.Commit, lastHeaderHash, nextSeqHash [32]byte, state *types.State, maxBlockDataSizeBytes uint64) *types.Block {
if state.ConsensusParams.Blockmaxsize > 0 {
maxBlockDataSizeBytes = min(maxBlockDataSizeBytes, uint64(state.ConsensusParams.Blockmaxsize))
}
mempoolTxs := e.mempool.ReapMaxBytesMaxGas(int64(maxBlockDataSizeBytes), state.ConsensusParams.Blockmaxgas)
maxBlockDataSizeBytes = min(maxBlockDataSizeBytes, uint64(max(minBlockMaxBytes, state.ConsensusParams.Block.MaxBytes)))
mempoolTxs := e.mempool.ReapMaxBytesMaxGas(int64(maxBlockDataSizeBytes), state.ConsensusParams.Block.MaxGas)

block := &types.Block{
Header: types.Header{
Expand Down Expand Up @@ -160,8 +161,8 @@ func (e *Executor) commit(state *types.State, block *types.Block, deliverTxs []*
return nil, 0, err
}

maxBytes := state.ConsensusParams.Blockmaxsize
maxGas := state.ConsensusParams.Blockmaxgas
maxBytes := state.ConsensusParams.Block.MaxBytes
maxGas := state.ConsensusParams.Block.MaxGas
err = e.mempool.Update(int64(block.Header.Height), fromDymintTxs(block.Data.Txs), deliverTxs)
if err != nil {
return nil, 0, err
Expand Down
37 changes: 16 additions & 21 deletions block/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/dymensionxyz/dymint/block"
"github.com/dymensionxyz/dymint/types/pb/dymint"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand Down Expand Up @@ -61,11 +60,8 @@ func TestCreateBlock(t *testing.T) {
// Init state
state := &types.State{}
state.Sequencers.SetProposer(types.NewSequencerFromValidator(*tmtypes.NewValidator(tmPubKey, 1)))
state.ConsensusParams = dymint.RollappConsensusParams{
Blockmaxsize: int64(maxBytes),
Blockmaxgas: 100000,
}

state.ConsensusParams.Block.MaxBytes = int64(maxBytes)
state.ConsensusParams.Block.MaxGas = 100000
// empty block
block := executor.CreateBlock(1, &types.Commit{}, [32]byte{}, [32]byte(state.Sequencers.ProposerHash()[:]), state, maxBytes)
require.NotNil(block)
Expand Down Expand Up @@ -103,11 +99,13 @@ func TestApplyBlock(t *testing.T) {
app.On("DeliverTx", mock.Anything).Return(abci.ResponseDeliverTx{})
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{
RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "celestia",
Commit: "abcde",
Da: "celestia",
Version: "abcde",
},
ConsensusParamUpdates: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxBytes: 100,
MaxGas: 100,
MaxBytes: 100,
},
},
})
Expand Down Expand Up @@ -167,13 +165,10 @@ func TestApplyBlock(t *testing.T) {
state.Sequencers.SetProposer(types.NewSequencerFromValidator(*tmtypes.NewValidator(tmPubKey, 1)))
state.InitialHeight = 1
state.SetHeight(0)
maxBytes := uint64(1000)
state.ConsensusParams = dymint.RollappConsensusParams{
Blockmaxgas: 100000,
Blockmaxsize: int64(maxBytes),
Da: "mock",
Commit: "",
}
maxBytes := uint64(10000)
state.ConsensusParams.Block.MaxBytes = int64(maxBytes)
state.ConsensusParams.Block.MaxGas = 100000
state.RollappParams.Da = "mock"

// Create first block with one Tx from mempool
_ = mpool.CheckTx([]byte{1, 2, 3, 4}, func(r *abci.Response) {}, mempool.TxInfo{})
Expand Down Expand Up @@ -212,7 +207,7 @@ func TestApplyBlock(t *testing.T) {
require.NoError(mpool.CheckTx([]byte{0, 1, 2, 3, 4}, func(r *abci.Response) {}, mempool.TxInfo{}))
require.NoError(mpool.CheckTx([]byte{5, 6, 7, 8, 9}, func(r *abci.Response) {}, mempool.TxInfo{}))
require.NoError(mpool.CheckTx([]byte{1, 2, 3, 4, 5}, func(r *abci.Response) {}, mempool.TxInfo{}))
require.NoError(mpool.CheckTx(make([]byte, 90), func(r *abci.Response) {}, mempool.TxInfo{}))
require.NoError(mpool.CheckTx(make([]byte, 9990), func(r *abci.Response) {}, mempool.TxInfo{}))
block = executor.CreateBlock(2, commit, [32]byte{}, [32]byte(state.Sequencers.ProposerHash()), state, maxBytes)
require.NotNil(block)
assert.Equal(uint64(2), block.Header.Height)
Expand Down Expand Up @@ -258,10 +253,10 @@ func TestApplyBlock(t *testing.T) {
assert.Equal(uint64(2), state.Height())

// check rollapp params update
assert.Equal(state.ConsensusParams.Da, "celestia")
assert.Equal(state.ConsensusParams.Commit, "abcde")
assert.Equal(state.ConsensusParams.Blockmaxsize, int64(100))
assert.Equal(state.ConsensusParams.Blockmaxgas, int64(100))
assert.Equal(state.RollappParams.Da, "celestia")
assert.Equal(state.RollappParams.Version, "abcde")
assert.Equal(state.ConsensusParams.Block.MaxBytes, int64(100))
assert.Equal(state.ConsensusParams.Block.MaxGas, int64(100))

// wait for at least 4 Tx events, for up to 3 second.
// 3 seconds is a fail-scenario only
Expand Down
1 change: 0 additions & 1 deletion block/initchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ func (m *Manager) RunInitChain(ctx context.Context) error {
if _, err := m.Store.SaveState(m.State, nil); err != nil {
return err
}

return nil
}
16 changes: 6 additions & 10 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,28 +289,24 @@ func (m *Manager) UpdateTargetHeight(h uint64) {

// ValidateConfigWithRollappParams checks the configuration params are consistent with the params in the dymint state (e.g. DA and version)
func (m *Manager) ValidateConfigWithRollappParams() error {
if version.Commit != m.State.ConsensusParams.Commit {
return fmt.Errorf("binary version mismatch. rollapp param: %s binary used:%s", version.Commit, m.State.ConsensusParams.Commit)
if version.Commit != m.State.RollappParams.Version {
return fmt.Errorf("binary version mismatch. rollapp param: %s binary used:%s", m.State.RollappParams.Version, version.Commit)
}

if da.Client(m.State.ConsensusParams.Da) != m.DAClient.GetClientType() {
return fmt.Errorf("da client mismatch. rollapp param: %s da configured: %s", m.DAClient.GetClientType(), m.State.ConsensusParams.Da)
if da.Client(m.State.RollappParams.Da) != m.DAClient.GetClientType() {
return fmt.Errorf("da client mismatch. rollapp param: %s da configured: %s", m.State.RollappParams.Da, m.DAClient.GetClientType())
}

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

if m.State.ConsensusParams.Blockmaxsize > int64(m.DAClient.GetMaxBlobSizeBytes()) {
return fmt.Errorf("max block size above limit: %d: DA: %s", int64(m.DAClient.GetMaxBlobSizeBytes()), m.DAClient.GetClientType())
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.ConsensusParams.Da
daLayer := m.State.RollappParams.Da
dalc := registry.GetClient(daLayer)
if dalc == nil {
return fmt.Errorf("get data availability client named '%s'", daLayer)
Expand Down
108 changes: 66 additions & 42 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,18 @@ func TestInitialState(t *testing.T) {
func TestProduceOnlyAfterSynced(t *testing.T) {
// Init app
app := testutil.GetAppMock(testutil.EndBlock)
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Commit: version.Commit,
Block: &abci.BlockParams{
MaxBytes: 500000,
MaxGas: 40000000,
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{
RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Version: version.Commit,
},
}})
ConsensusParamUpdates: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxGas: 40000000,
MaxBytes: 500000,
},
},
})
// Create proxy app
clientCreator := proxy.NewLocalClientCreator(app)
proxyApp := proxy.NewAppConns(clientCreator)
Expand Down Expand Up @@ -207,14 +211,18 @@ func TestProduceNewBlock(t *testing.T) {
app := testutil.GetAppMock(testutil.Commit, testutil.EndBlock)
commitHash := [32]byte{1}
app.On("Commit", mock.Anything).Return(abci.ResponseCommit{Data: commitHash[:]})
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Commit: version.Commit,
Block: &abci.BlockParams{
MaxBytes: 500000,
MaxGas: 40000000,
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{
RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Version: version.Commit,
},
ConsensusParamUpdates: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxGas: 40000000,
MaxBytes: 500000,
},
},
}})
})
// Create proxy app
clientCreator := proxy.NewLocalClientCreator(app)
proxyApp := proxy.NewAppConns(clientCreator)
Expand All @@ -236,14 +244,18 @@ func TestProducePendingBlock(t *testing.T) {
app := testutil.GetAppMock(testutil.Commit, testutil.EndBlock)
commitHash := [32]byte{1}
app.On("Commit", mock.Anything).Return(abci.ResponseCommit{Data: commitHash[:]})
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Commit: version.Commit,
Block: &abci.BlockParams{
MaxBytes: 500000,
MaxGas: 40000000,
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{
RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Version: version.Commit,
},
}})
ConsensusParamUpdates: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxGas: 40000000,
MaxBytes: 500000,
},
},
})
// Create proxy app
clientCreator := proxy.NewLocalClientCreator(app)
proxyApp := proxy.NewAppConns(clientCreator)
Expand Down Expand Up @@ -346,14 +358,18 @@ func TestProduceBlockFailAfterCommit(t *testing.T) {
LastBlockHeight: tc.LastAppBlockHeight,
LastBlockAppHash: tc.LastAppCommitHash[:],
})
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Commit: version.Commit,
Block: &abci.BlockParams{
MaxBytes: 500000,
MaxGas: 40000000,
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{
RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Version: version.Commit,
},
ConsensusParamUpdates: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxGas: 40000000,
MaxBytes: 500000,
},
},
}})
})
mockStore.ShouldFailUpdateStateWithBatch = tc.shoudFailOnSaveState
_, _, _ = manager.ProduceApplyGossipBlock(context.Background(), true)
storeState, err := manager.Store.LoadState()
Expand All @@ -374,14 +390,18 @@ func TestCreateNextDABatchWithBytesLimit(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
app := testutil.GetAppMock(testutil.EndBlock)
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Commit: version.Commit,
Block: &abci.BlockParams{
MaxBytes: 500000,
MaxGas: 40000000,
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{
RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Version: version.Commit,
},
ConsensusParamUpdates: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxGas: 40000000,
MaxBytes: 500000,
},
},
}})
})
// Create proxy app
clientCreator := proxy.NewLocalClientCreator(app)
proxyApp := proxy.NewAppConns(clientCreator)
Expand Down Expand Up @@ -452,14 +472,18 @@ func TestDAFetch(t *testing.T) {
require := require.New(t)
// Setup app
app := testutil.GetAppMock(testutil.Info, testutil.Commit, testutil.EndBlock)
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Commit: version.Commit,
Block: &abci.BlockParams{
MaxBytes: 500000,
MaxGas: 40000000,
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{
RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Version: version.Commit,
},
ConsensusParamUpdates: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxGas: 40000000,
MaxBytes: 500000,
},
},
}})
})
// Create proxy app
clientCreator := proxy.NewLocalClientCreator(app)
proxyApp := proxy.NewAppConns(clientCreator)
Expand Down
36 changes: 22 additions & 14 deletions block/production_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ func TestCreateEmptyBlocksEnableDisable(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
app := testutil.GetAppMock(testutil.EndBlock)
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Commit: version.Commit,
Block: &abci.BlockParams{
MaxBytes: 500000,
MaxGas: 40000000,
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{
RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Version: version.Commit,
},
}})
ConsensusParamUpdates: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxGas: 40000000,
MaxBytes: 500000,
},
},
})
// Create proxy app
clientCreator := proxy.NewLocalClientCreator(app)
proxyApp := proxy.NewAppConns(clientCreator)
Expand Down Expand Up @@ -197,14 +201,18 @@ func TestStopBlockProduction(t *testing.T) {
require := require.New(t)

app := testutil.GetAppMock(testutil.EndBlock)
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Commit: version.Commit,
Block: &abci.BlockParams{
MaxBytes: 500000,
MaxGas: 40000000,
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{
RollappConsensusParamUpdates: &abci.RollappConsensusParams{
Da: "mock",
Version: version.Commit,
},
ConsensusParamUpdates: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxGas: 40000000,
MaxBytes: 500000,
},
},
}})
})
// Create proxy app
clientCreator := proxy.NewLocalClientCreator(app)
proxyApp := proxy.NewAppConns(clientCreator)
Expand Down
Loading
Loading