Skip to content

Commit

Permalink
feat: add GenesisChecksum to init chain (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 authored Nov 4, 2024
1 parent 6062b3a commit 6ae1dc1
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 126 deletions.
12 changes: 7 additions & 5 deletions block/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

proto2 "github.com/gogo/protobuf/proto"
proto "github.com/gogo/protobuf/types"
"go.uber.org/multierr"

abci "github.com/tendermint/tendermint/abci/types"
tmcrypto "github.com/tendermint/tendermint/crypto/encoding"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/proxy"
tmtypes "github.com/tendermint/tendermint/types"
"go.uber.org/multierr"

"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/types"
Expand All @@ -22,7 +23,7 @@ import (
const minBlockMaxBytes = 10000

type ExecutorI interface {
InitChain(genesis *tmtypes.GenesisDoc, valset []*tmtypes.Validator) (*abci.ResponseInitChain, error)
InitChain(genesis *tmtypes.GenesisDoc, genesisChecksum string, valset []*tmtypes.Validator) (*abci.ResponseInitChain, error)
CreateBlock(height uint64, lastCommit *types.Commit, lastHeaderHash, nextSeqHash [32]byte, state *types.State, maxBlockDataSizeBytes uint64) *types.Block
Commit(state *types.State, block *types.Block, resp *tmstate.ABCIResponses) ([]byte, int64, error)
GetAppInfo() (*abci.ResponseInfo, error)
Expand Down Expand Up @@ -91,7 +92,7 @@ func (e *Executor) GetConsensusMsgs() []proto2.Message {
}

// InitChain calls InitChainSync using consensus connection to app.
func (e *Executor) InitChain(genesis *tmtypes.GenesisDoc, valset []*tmtypes.Validator) (*abci.ResponseInitChain, error) {
func (e *Executor) InitChain(genesis *tmtypes.GenesisDoc, genesisChecksum string, valset []*tmtypes.Validator) (*abci.ResponseInitChain, error) {
valUpdates := abci.ValidatorUpdates{}

// prepare the validator updates as expected by the ABCI app
Expand Down Expand Up @@ -129,8 +130,9 @@ func (e *Executor) InitChain(genesis *tmtypes.GenesisDoc, valset []*tmtypes.Vali
AppVersion: params.Version.AppVersion,
},
}, Validators: valUpdates,
AppStateBytes: genesis.AppState,
InitialHeight: genesis.InitialHeight,
AppStateBytes: genesis.AppState,
InitialHeight: genesis.InitialHeight,
GenesisChecksum: genesisChecksum,
})
}

Expand Down
2 changes: 1 addition & 1 deletion block/initchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (m *Manager) RunInitChain(ctx context.Context) error {
return errors.New("failed to get proposer")
}
tmProposer := proposer.TMValidator()
res, err := m.Executor.InitChain(m.Genesis, []*tmtypes.Validator{tmProposer})
res, err := m.Executor.InitChain(m.Genesis, m.GenesisChecksum, []*tmtypes.Validator{tmProposer})
if err != nil {
return err
}
Expand Down
32 changes: 18 additions & 14 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/dymensionxyz/dymint/version"

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

tmcrypto "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/pubsub"
Expand Down Expand Up @@ -46,9 +47,10 @@ type Manager struct {
logger types.Logger

// Configuration
Conf config.BlockManagerConfig
Genesis *tmtypes.GenesisDoc
LocalKey crypto.PrivKey
Conf config.BlockManagerConfig
Genesis *tmtypes.GenesisDoc
GenesisChecksum string
LocalKey crypto.PrivKey

// Store and execution
Store store.Store
Expand Down Expand Up @@ -118,6 +120,7 @@ func NewManager(
localKey crypto.PrivKey,
conf config.NodeConfig,
genesis *tmtypes.GenesisDoc,
genesisChecksum string,
store store.Store,
mempool mempool.Mempool,
proxyApp proxy.AppConns,
Expand Down Expand Up @@ -147,17 +150,18 @@ func NewManager(
}

m := &Manager{
Pubsub: pubsub,
P2PClient: p2pClient,
LocalKey: localKey,
Conf: conf.BlockManagerConfig,
Genesis: genesis,
Store: store,
Executor: exec,
Sequencers: types.NewSequencerSet(),
SLClient: settlementClient,
indexerService: indexerService,
logger: logger.With("module", "block_manager"),
Pubsub: pubsub,
P2PClient: p2pClient,
LocalKey: localKey,
Conf: conf.BlockManagerConfig,
Genesis: genesis,
GenesisChecksum: genesisChecksum,
Store: store,
Executor: exec,
Sequencers: types.NewSequencerSet(),
SLClient: settlementClient,
indexerService: indexerService,
logger: logger.With("module", "block_manager"),
blockCache: &Cache{
cache: make(map[uint64]types.CachedBlock),
},
Expand Down
6 changes: 3 additions & 3 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ 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,
agg, err := block.NewManager(key, conf, c.genesis, "", c.store, nil, proxyApp, settlementlc,
nil, pubsubServer, p2pClient, nil, nil, logger)
assert.NoError(err)
assert.NotNil(agg)
Expand Down Expand Up @@ -351,7 +351,7 @@ func TestApplyLocalBlock_WithFraudCheck(t *testing.T) {

mockExecutor := &blockmocks.MockExecutorI{}
manager.Executor = mockExecutor
mockExecutor.On("InitChain", mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{}, nil)
mockExecutor.On("InitChain", mock.Anything, mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{}, nil)
mockExecutor.On("GetAppInfo").Return(&abci.ResponseInfo{
LastBlockHeight: int64(batch.EndHeight()),
}, nil)
Expand Down Expand Up @@ -810,7 +810,7 @@ func TestManager_ApplyBatchFromSL_FraudHandling(t *testing.T) {
err = manager.SLClient.SubmitBatch(batch, manager.DAClient.GetClientType(), &daResultSubmitBatch)
require.NoError(err)

//// Mock Executor to return ErrFraud
// Mock Executor to return ErrFraud
mockExecutor := &blockmocks.MockExecutorI{}
manager.Executor = mockExecutor
mockExecutor.On("GetAppInfo").Return(&abci.ResponseInfo{
Expand Down
38 changes: 38 additions & 0 deletions cmd/dymint/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sort"

"github.com/spf13/cobra"
"github.com/spf13/viper"

tmcfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -87,13 +92,19 @@ func startInProcess(config *cfg.NodeConfig, tmConfig *tmcfg.Config, logger log.L
}
logger.Info("starting node with ABCI dymint in-process", "conf", config)

genesisChecksum, err := ComputeGenesisHash(tmConfig.GenesisFile())
if err != nil {
return fmt.Errorf("failed to compute genesis checksum: %w", err)
}

dymintNode, err := node.NewNode(
context.Background(),
*config,
p2pKey,
signingKey,
proxy.DefaultClientCreator(tmConfig.ProxyApp, tmConfig.ABCI, tmConfig.DBDir()),
genesis,
genesisChecksum,
logger,
mempool.PrometheusMetrics("dymint"),
)
Expand Down Expand Up @@ -162,3 +173,30 @@ func checkGenesisHash(config *tmcfg.Config) error {

return nil
}

func ComputeGenesisHash(genesisFilePath string) (string, error) {
fileContent, err := os.ReadFile(filepath.Clean(genesisFilePath))
if err != nil {
return "", err
}

var jsonObject map[string]interface{}
err = json.Unmarshal(fileContent, &jsonObject)
if err != nil {
return "", err
}

keys := make([]string, 0, len(jsonObject))
for k := range jsonObject {
keys = append(keys, k)
}
sort.Strings(keys)

sortedJSON, err := json.Marshal(jsonObject)
if err != nil {
return "", err
}

hash := sha256.Sum256(sortedJSON)
return hex.EncodeToString(hash[:]), nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ replace (
github.com/evmos/evmos/v12 => github.com/dymensionxyz/evmos/v12 v12.1.6-dymension-v0.3
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.2-alpha.regen.4
github.com/gorilla/rpc => github.com/dymensionxyz/rpc v1.3.1
github.com/tendermint/tendermint => github.com/dymensionxyz/cometbft v0.34.29-0.20241030154748-3f9dfa21d17b
github.com/tendermint/tendermint => github.com/dymensionxyz/cometbft v0.34.29-0.20241104165035-feade34f8f89
)

replace github.com/osmosis-labs/osmosis/v15 => github.com/dymensionxyz/osmosis/v15 v15.2.0-dymension-v1.1.2
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQxaLAeM=
github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/dymensionxyz/cometbft v0.34.29-0.20241030154748-3f9dfa21d17b h1:rxkH+9cBG2nnReMavb2FqQXwDLjKcz0/KB8/6SV5Xlo=
github.com/dymensionxyz/cometbft v0.34.29-0.20241030154748-3f9dfa21d17b/go.mod h1:L9shMfbkZ8B+7JlwANEr+NZbBcn+hBpwdbeYvA5rLCw=
github.com/dymensionxyz/cometbft v0.34.29-0.20241104165035-feade34f8f89 h1:rGkCcx4dWX9mxAUrq7zrdOc44XddMY/nM6kqYTWjerY=
github.com/dymensionxyz/cometbft v0.34.29-0.20241104165035-feade34f8f89/go.mod h1:L9shMfbkZ8B+7JlwANEr+NZbBcn+hBpwdbeYvA5rLCw=
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20240821081230-b4018b2bac13 h1:u5yeve5jZR6TdRjjR+vYT/8PWKbhwCZxUmAu+/Tnxyg=
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20240821081230-b4018b2bac13/go.mod h1:jabDQYXrccscSE0fXkh7eQFYPWJCRiuWKonFGObVq6s=
github.com/dymensionxyz/evmos/v12 v12.1.6-dymension-v0.3 h1:vmAdUGUc4rTIiO3Phezr7vGq+0uPDVKSA4WAe8+yl6w=
Expand Down
29 changes: 15 additions & 14 deletions mocks/github.com/dymensionxyz/dymint/block/mock_ExecutorI.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6ae1dc1

Please sign in to comment.