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(blockManager): refactor and use state as single source of truth for height #847

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e36a9a5
removed pending block as submit to SL retries forever
mtsitrin May 5, 2024
0d4c641
removed BlockBatchSize and fix UT
mtsitrin May 5, 2024
5803cda
moved accumulated count to produce to be mutex protected
mtsitrin May 6, 2024
aef3bc4
refactored error handling
mtsitrin May 6, 2024
1e766ab
removed healthEvents from layers. set by manager on submission skew
mtsitrin May 6, 2024
8c4df90
cleanup
mtsitrin May 6, 2024
f3592e5
fixed defaults
mtsitrin May 6, 2024
6973282
fix UT
mtsitrin May 6, 2024
81baf80
changed accumaletd counter to be atomic
mtsitrin May 6, 2024
24d8b85
fixed UT
mtsitrin May 6, 2024
6c1741a
spelling, typo, format
danwt May 7, 2024
b1c0131
spelling
danwt May 7, 2024
9364c7c
feat: block progress to support ibc should be managed by produceloop …
mtsitrin May 8, 2024
9f76e0b
refactored the signaling
mtsitrin May 8, 2024
c7ffbf1
added ctx support for blocking signals
mtsitrin May 8, 2024
a501fa2
cleanup. comments
mtsitrin May 8, 2024
72a8893
moved indexers to own package
mtsitrin May 9, 2024
9815da3
renamed state and some struct fields
mtsitrin May 9, 2024
5c7c454
moved store back to store package
mtsitrin May 9, 2024
f53ddc4
moving height related managment to be based on State
mtsitrin May 9, 2024
8402076
fixed store pruning
mtsitrin May 9, 2024
90031e7
updated block manager to use state. rpc needs fix
mtsitrin May 9, 2024
d2be2ce
cleanup
mtsitrin May 9, 2024
a095024
Merge branch 'main' into mtsitrin/634-refactor-use-state-as-single-so…
mtsitrin May 12, 2024
d9698f8
simplified commit
mtsitrin May 12, 2024
051a4e7
moved gossip methods to gossip.go
mtsitrin May 12, 2024
cecf106
reverted executer key
mtsitrin May 12, 2024
b896eaa
fixed publishEvents
mtsitrin May 12, 2024
bb66c2c
removed unused fields. saving state post commit
mtsitrin May 12, 2024
efcf193
removed unused params. cleaned the state flow
mtsitrin May 12, 2024
3992f2a
fixed LastBlockHeight to be atomic
mtsitrin May 13, 2024
5b3cda0
avoid copying state and pass by reference
mtsitrin May 13, 2024
953a9b8
fixed PR comments
mtsitrin May 15, 2024
576325e
simplified genesis check on produce block
mtsitrin May 15, 2024
403ec9e
Merge branch 'main' into mtsitrin/634-refactor-use-state-as-single-so…
mtsitrin May 15, 2024
31c8906
pr comments
mtsitrin May 15, 2024
5ab2f15
Merge branch 'main' into mtsitrin/634-refactor-use-state-as-single-so…
mtsitrin May 15, 2024
74bfba0
linter
mtsitrin May 15, 2024
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
Prev Previous commit
Next Next commit
simplified commit
  • Loading branch information
mtsitrin committed May 12, 2024
commit d9698f80faf4d4da3c9fed3b8be5dc5fb12fd2ac
61 changes: 11 additions & 50 deletions block/block.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package block

import (
"context"
"fmt"

errorsmod "cosmossdk.io/errors"

"github.com/dymensionxyz/dymint/p2p"
"github.com/dymensionxyz/dymint/types"
tmtypes "github.com/tendermint/tendermint/types"
)

// applyBlock applies the block to the store and the abci app.
Expand All @@ -18,7 +15,6 @@ import (
// - block height is the expected block height on the store (height + 1).
// - block height is the expected block height on the app (last block height + 1).
func (m *Manager) applyBlock(block *types.Block, commit *types.Commit, blockMetaData blockMetaData) error {
// TODO (#330): allow genesis block with height > 0 to be applied.
// TODO: add switch case to have defined behavior for each case.
// validate block height
if block.Header.Height != m.State.NextHeight() {
Expand Down Expand Up @@ -58,33 +54,32 @@ func (m *Manager) applyBlock(block *types.Block, commit *types.Commit, blockMeta
return fmt.Errorf("update state from responses: %w", err)
}

batch := m.Store.NewBatch()

batch, err = m.Store.SaveBlockResponses(block.Header.Height, responses, batch)
dbBatch := m.Store.NewBatch()
dbBatch, err = m.Store.SaveBlockResponses(block.Header.Height, responses, dbBatch)
if err != nil {
batch.Discard()
dbBatch.Discard()
return fmt.Errorf("save block responses: %w", err)
}

m.State = newState
batch, err = m.Store.UpdateState(m.State, batch)
dbBatch, err = m.Store.UpdateState(m.State, dbBatch)
if err != nil {
batch.Discard()
dbBatch.Discard()
return fmt.Errorf("update state: %w", err)
}
batch, err = m.Store.SaveValidators(block.Header.Height, m.State.Validators, batch)
dbBatch, err = m.Store.SaveValidators(block.Header.Height, m.State.Validators, dbBatch)
if err != nil {
batch.Discard()
dbBatch.Discard()
return fmt.Errorf("save validators: %w", err)
}

err = batch.Commit()
err = dbBatch.Commit()
if err != nil {
return fmt.Errorf("commit batch to disk: %w", err)
}

// Commit block to app
retainHeight, err := m.Executor.Commit(&newState, block, responses)
appHash, retainHeight, err := m.Executor.Commit(&newState, block, responses)
if err != nil {
return fmt.Errorf("commit block: %w", err)
}
Expand All @@ -97,16 +92,12 @@ func (m *Manager) applyBlock(block *types.Block, commit *types.Commit, blockMeta
} else {
m.logger.Debug("pruned blocks", "pruned", pruned, "retain_height", retainHeight)
}
newState.BaseHeight = m.State.Base()
}

// Update the state with the new app hash, last validators and store height from the commit.
// Every one of those, if happens before commit, prevents us from re-executing the block in case failed during commit.
newState.LastValidators = m.State.Validators.Copy()
newState.LastStoreHeight = block.Header.Height
newState.BaseHeight = m.State.Base()
if ok := m.State.SetHeight(block.Header.Height); !ok {
return fmt.Errorf("store set height: %d", block.Header.Height)
}
newState.SetABCICommitResult(responses, appHash, block.Header.Height)
_, err = m.Store.UpdateState(newState, nil)
if err != nil {
return fmt.Errorf("final update state: %w", err)
Expand Down Expand Up @@ -160,36 +151,6 @@ func (m *Manager) isHeightAlreadyApplied(blockHeight uint64) (bool, error) {
return isBlockAlreadyApplied, nil
}

// UpdateStateFromApp is responsible for aligning the state of the store from the abci app
func (m *Manager) UpdateStateFromApp() error {
proxyAppInfo, err := m.Executor.GetAppInfo()
if err != nil {
return errorsmod.Wrap(err, "get app info")
}

appHeight := uint64(proxyAppInfo.LastBlockHeight)

// update the state with the hash, last store height and last validators.
m.State.AppHash = *(*[32]byte)(proxyAppInfo.LastBlockAppHash)
m.State.LastStoreHeight = appHeight
m.State.LastValidators = m.State.Validators.Copy()

resp, err := m.Store.LoadBlockResponses(appHeight)
if err != nil {
return errorsmod.Wrap(err, "load block responses")
}
copy(m.State.LastResultsHash[:], tmtypes.NewResults(resp.DeliverTxs).Hash())

if ok := m.State.SetHeight(appHeight); !ok {
return fmt.Errorf("state set height: %d", appHeight)
}
_, err = m.Store.UpdateState(m.State, nil)
if err != nil {
return errorsmod.Wrap(err, "update state")
}
return nil
}

func (m *Manager) validateBlock(block *types.Block, commit *types.Commit) error {
// Currently we're assuming proposer is never nil as it's a pre-condition for
// dymint to start
Expand Down
20 changes: 12 additions & 8 deletions block/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
tmtypes "github.com/tendermint/tendermint/types"
"go.uber.org/multierr"

libp2pcrypto "github.com/libp2p/go-libp2p-core/crypto"

"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/types"
)
Expand All @@ -33,7 +35,12 @@ type Executor struct {

// NewExecutor creates new instance of BlockExecutor.
// Proposer address and namespace ID will be used in all newly created blocks.
func NewExecutor(proposerAddress []byte, namespaceID string, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConns, eventBus *tmtypes.EventBus, logger types.Logger) (*Executor, error) {
func NewExecutor(proposerKey libp2pcrypto.PrivKey, namespaceID string, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConns, eventBus *tmtypes.EventBus, logger types.Logger) (*Executor, error) {
proposerAddress, err := getAddress(proposerKey)
if err != nil {
return nil, err
}

bytes, err := hex.DecodeString(namespaceID)
if err != nil {
return nil, err
Expand Down Expand Up @@ -134,21 +141,18 @@ func (e *Executor) CreateBlock(height uint64, lastCommit *types.Commit, lastHead
}

// Commit commits the block
func (e *Executor) Commit(state *types.State, block *types.Block, resp *tmstate.ABCIResponses) (int64, error) {
func (e *Executor) Commit(state *types.State, block *types.Block, resp *tmstate.ABCIResponses) ([]byte, int64, error) {
appHash, retainHeight, err := e.commit(state, block, resp.DeliverTxs)
if err != nil {
return 0, err
return nil, 0, err
}

copy(state.AppHash[:], appHash[:])
copy(state.LastResultsHash[:], tmtypes.NewResults(resp.DeliverTxs).Hash())

err = e.publishEvents(resp, block, *state)
if err != nil {
e.logger.Error("fire block events", "error", err)
return 0, err
return nil, 0, err
}
return retainHeight, nil
return appHash, retainHeight, nil
}

// GetAppInfo returns the latest AppInfo from the proxyApp.
Expand Down
13 changes: 1 addition & 12 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ import (
"github.com/dymensionxyz/dymint/types"
)

const (
// max amount of pending batches to be submitted. block production will be paused if this limit is reached.
// TODO: make this configurable
maxSupportedBatchSkew = 10
)

// Manager is responsible for aggregating transactions into blocks.
type Manager struct {
// Configuration
Expand Down Expand Up @@ -92,12 +86,7 @@ func NewManager(
p2pClient *p2p.Client,
logger types.Logger,
) (*Manager, error) {
proposerAddress, err := getAddress(proposerKey)
if err != nil {
return nil, err
}

exec, err := NewExecutor(proposerAddress, conf.NamespaceID, genesis.ChainID, mempool, proxyApp, eventBus, logger)
exec, err := NewExecutor(proposerKey, conf.NamespaceID, genesis.ChainID, mempool, proxyApp, eventBus, logger)
if err != nil {
return nil, fmt.Errorf("create block executor: %w", err)
}
Expand Down
34 changes: 34 additions & 0 deletions block/state.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package block

import (
"fmt"
"time"

errorsmod "cosmossdk.io/errors"

"github.com/cometbft/cometbft/crypto/merkle"
abci "github.com/tendermint/tendermint/abci/types"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
Expand All @@ -13,6 +16,37 @@ import (
)

// TODO: move all those methods from blockExecutor to manager

// UpdateStateFromApp is responsible for aligning the state of the store from the abci app
func (m *Manager) UpdateStateFromApp() error {
proxyAppInfo, err := m.Executor.GetAppInfo()
if err != nil {
return errorsmod.Wrap(err, "get app info")
}

appHeight := uint64(proxyAppInfo.LastBlockHeight)

// update the state with the hash, last store height and last validators.
m.State.AppHash = *(*[32]byte)(proxyAppInfo.LastBlockAppHash)
m.State.LastStoreHeight = appHeight
m.State.LastValidators = m.State.Validators.Copy()

resp, err := m.Store.LoadBlockResponses(appHeight)
if err != nil {
return errorsmod.Wrap(err, "load block responses")
}
copy(m.State.LastResultsHash[:], tmtypes.NewResults(resp.DeliverTxs).Hash())

if ok := m.State.SetHeight(appHeight); !ok {
return fmt.Errorf("state set height: %d", appHeight)
}
_, err = m.Store.UpdateState(m.State, nil)
if err != nil {
return errorsmod.Wrap(err, "update state")
}
return nil
}

func (e *Executor) updateState(state types.State, block *types.Block, abciResponses *tmstate.ABCIResponses, validatorUpdates []*tmtypes.Validator) (types.State, error) {
nValSet := state.NextValidators.Copy()
lastHeightValSetChanged := state.LastHeightValidatorsChanged
Expand Down
12 changes: 12 additions & 0 deletions types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sync/atomic"
"time"

tmtypes "github.com/tendermint/tendermint/types"

// TODO(tzdybal): copy to local project?
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand Down Expand Up @@ -133,3 +135,13 @@ func (s *State) SetBase(height uint64) {
func (s *State) Base() uint64 {
Copy link
Contributor

Choose a reason for hiding this comment

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

method not being used (though it can be used as we do get the BaseHeight in pruning.go)

return s.BaseHeight
}

// SetABCICommitResult
func (s *State) SetABCICommitResult(resp *tmstate.ABCIResponses, appHash []byte, height uint64) {
copy(s.AppHash[:], appHash[:])
copy(s.LastResultsHash[:], tmtypes.NewResults(resp.DeliverTxs).Hash())

s.LastValidators = s.Validators.Copy()
s.LastStoreHeight = height
s.SetHeight(height)
}