Skip to content

Commit

Permalink
feat: validate genesis bridge data against the hub (#1256)
Browse files Browse the repository at this point in the history
  • Loading branch information
keruch authored Dec 2, 2024
1 parent ab04841 commit 5a4205e
Show file tree
Hide file tree
Showing 40 changed files with 4,049 additions and 1,548 deletions.
26 changes: 24 additions & 2 deletions block/initchain.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package block

import (
"context"
"errors"
"fmt"

tmjson "github.com/tendermint/tendermint/libs/json"
tmtypes "github.com/tendermint/tendermint/types"

rollapptypes "github.com/dymensionxyz/dymint/types/pb/dymensionxyz/dymension/rollapp"
)

func (m *Manager) RunInitChain(ctx context.Context) error {
func (m *Manager) RunInitChain() error {
// Get the proposer at the initial height. If we're at genesis the height will be 0.
proposer, err := m.SLClient.GetProposerAtHeight(int64(m.State.Height()) + 1) //nolint:gosec // height is non-negative and falls in int64
if err != nil {
Expand All @@ -23,6 +25,12 @@ func (m *Manager) RunInitChain(ctx context.Context) error {
return err
}

// validate the resulting genesis bridge data against the hub
err = m.ValidateGenesisBridgeData(res.GenesisBridgeDataBytes)
if err != nil {
return fmt.Errorf("Cannot validate genesis bridge data: %w. Please call `$EXECUTABLE dymint unsafe-reset-all` before the next launch to reset this node to genesis state.", err)
}

// update the state with only the consensus pubkey
m.Executor.UpdateStateAfterInitChain(m.State, res)
m.Executor.UpdateMempoolAfterInitChain(m.State)
Expand All @@ -32,3 +40,17 @@ func (m *Manager) RunInitChain(ctx context.Context) error {

return nil
}

// ValidateGenesisBridgeData validates the genesis bridge data from
// InitChainResponse against the rollapp genesis stored in the hub.
func (m *Manager) ValidateGenesisBridgeData(dataBytes []byte) error {
if len(dataBytes) == 0 {
return fmt.Errorf("genesis bridge data is empty in InitChainResponse")
}
var genesisBridgeData rollapptypes.GenesisBridgeData
err := tmjson.Unmarshal(dataBytes, &genesisBridgeData)
if err != nil {
return fmt.Errorf("unmarshal genesis bridge data: %w", err)
}
return m.SLClient.ValidateGenesisBridgeData(genesisBridgeData)
}
2 changes: 1 addition & 1 deletion block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (m *Manager) Start(ctx context.Context) error {
if m.State.IsGenesis() {
m.logger.Info("Running InitChain")

err := m.RunInitChain(ctx)
err := m.RunInitChain()
if err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
tmjson "github.com/tendermint/tendermint/libs/json"

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

Expand All @@ -38,8 +39,9 @@ import (
slregistry "github.com/dymensionxyz/dymint/settlement/registry"
"github.com/dymensionxyz/dymint/store"

"github.com/dymensionxyz/dymint/utils/event"
"github.com/dymensionxyz/gerr-cosmos/gerrc"

"github.com/dymensionxyz/dymint/utils/event"
)

// TODO: test loading sequencer while rotation in progress
Expand Down Expand Up @@ -354,7 +356,8 @@ func TestApplyLocalBlock_WithFraudCheck(t *testing.T) {

mockExecutor := &blockmocks.MockExecutorI{}
manager.Executor = mockExecutor
mockExecutor.On("InitChain", mock.Anything, mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{}, nil)
gbdBz, _ := tmjson.Marshal(rollapp.GenesisBridgeData{})
mockExecutor.On("InitChain", mock.Anything, mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{GenesisBridgeDataBytes: gbdBz}, nil)
mockExecutor.On("GetAppInfo").Return(&abci.ResponseInfo{
LastBlockHeight: int64(batch.EndHeight()),
}, nil)
Expand Down Expand Up @@ -383,7 +386,7 @@ func TestApplyLocalBlock_WithFraudCheck(t *testing.T) {
go func() {
errChan <- manager.Start(ctx)
err := <-errChan
require.True(t, errors.Is(err, gerrc.ErrFault))
require.Truef(t, errors.Is(err, gerrc.ErrFault), "expected error to be %v, got: %v", gerrc.ErrFault, err)
}()
<-ctx.Done()
assert.Equal(t, batch.EndHeight(), manager.LastSettlementHeight.Load())
Expand Down
14 changes: 10 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/celestiaorg/go-cnc v0.4.2
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12
github.com/cosmos/cosmos-sdk v0.46.16
github.com/cosmos/ibc-go/v6 v6.2.1
github.com/dgraph-io/badger/v4 v4.3.0
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20241121093220-e0d7ad456fbd
github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241119103059-def6322e4345
Expand Down Expand Up @@ -54,6 +55,7 @@ require (
github.com/dgraph-io/badger/v3 v3.2103.3 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/ipfs/go-block-format v0.2.0 // indirect
github.com/regen-network/cosmos-proto v0.3.1 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
)

Expand Down Expand Up @@ -298,10 +300,14 @@ require (
replace (
github.com/CosmWasm/wasmd => github.com/decentrio/wasmd v0.33.0-sdk46.2
github.com/centrifuge/go-substrate-rpc-client/v4 => github.com/availproject/go-substrate-rpc-client/v4 v4.0.12-avail-1.4.0-rc1-5e286e3
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.20241104165035-feade34f8f89
)

replace github.com/osmosis-labs/osmosis/v15 => github.com/dymensionxyz/osmosis/v15 v15.2.0-dymension-v1.1.2
// Dymension Forks
replace (
github.com/cosmos/cosmos-sdk => github.com/dymensionxyz/cosmos-sdk v0.46.17-0.20241128210616-e9dfe47b8c73
github.com/evmos/evmos/v12 => github.com/dymensionxyz/evmos/v12 v12.1.6-dymension-v0.3
github.com/gorilla/rpc => github.com/dymensionxyz/rpc v1.3.1
github.com/osmosis-labs/osmosis/v15 => github.com/dymensionxyz/osmosis/v15 v15.2.0-dymension-v1.1.2
github.com/tendermint/tendermint => github.com/dymensionxyz/cometbft v0.34.29-0.20241128205759-2a9d5f015da5
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ github.com/cosmos/cosmos-db v1.0.0 h1:EVcQZ+qYag7W6uorBKFPvX6gRjw6Uq2hIh4hCWjuQ0
github.com/cosmos/cosmos-db v1.0.0/go.mod h1:iBvi1TtqaedwLdcrZVYRSSCb6eSy61NLj4UNmdIgs0U=
github.com/cosmos/cosmos-proto v1.0.0-beta.3 h1:VitvZ1lPORTVxkmF2fAp3IiA61xVwArQYKXTdEcpW6o=
github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I=
github.com/cosmos/cosmos-sdk v0.46.16 h1:RVGv1+RulLZeNyfCaPZrZtv0kY7ZZNAI6JGpub0Uh6o=
github.com/cosmos/cosmos-sdk v0.46.16/go.mod h1:05U50tAsOzQ8JOAePshJCbJQw5ib1YJR6IXcqyVI1Xg=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
Expand Down Expand Up @@ -324,8 +322,10 @@ 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.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/cometbft v0.34.29-0.20241128205759-2a9d5f015da5 h1:DIKsa7EWJRt+pIzzFn3MGYajwqaTmQQ/k255xONciRc=
github.com/dymensionxyz/cometbft v0.34.29-0.20241128205759-2a9d5f015da5/go.mod h1:L9shMfbkZ8B+7JlwANEr+NZbBcn+hBpwdbeYvA5rLCw=
github.com/dymensionxyz/cosmos-sdk v0.46.17-0.20241128210616-e9dfe47b8c73 h1:A0Oqua/AfzhBWvv4jiep48TAaAsh6wSwgCKBAqU/LQ0=
github.com/dymensionxyz/cosmos-sdk v0.46.17-0.20241128210616-e9dfe47b8c73/go.mod h1:VPUuzF+l+ekSGPV7VVB8m0OMQfwp3QdKWNZjvkU3A1U=
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20241121093220-e0d7ad456fbd h1:V89QyOFM84o9w0iFdctMU6So8SS/Xt32JWAXGqJduT0=
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20241121093220-e0d7ad456fbd/go.mod h1:3weqpVj/TqTFpC0LjEB3H+HZSpm7BrQ1QkEg1Ahy6KY=
github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241119103059-def6322e4345 h1:FcHidPgGEHh9ELwodNJkGcHqsG+mdPiGdughzG4W+X8=
Expand Down
48 changes: 48 additions & 0 deletions mocks/github.com/dymensionxyz/dymint/settlement/mock_ClientI.go

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

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

5 changes: 4 additions & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
tmjson "github.com/tendermint/tendermint/libs/json"

"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/node"
"github.com/dymensionxyz/dymint/settlement"
"github.com/dymensionxyz/dymint/testutil"
"github.com/dymensionxyz/dymint/types/pb/dymensionxyz/dymension/rollapp"
"github.com/dymensionxyz/dymint/version"

"github.com/libp2p/go-libp2p/core/crypto"
Expand Down Expand Up @@ -57,7 +59,8 @@ func TestMempoolDirectly(t *testing.T) {
require := require.New(t)

app := &tmmocks.MockApplication{}
app.On("InitChain", mock.Anything).Return(abci.ResponseInitChain{})
gbdBz, _ := tmjson.Marshal(rollapp.GenesisBridgeData{})
app.On("InitChain", mock.Anything).Return(abci.ResponseInitChain{GenesisBridgeDataBytes: gbdBz}, nil)
app.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{})
app.On("Info", mock.Anything).Return(abci.ResponseInfo{})
key, _, _ := crypto.GenerateEd25519Key(rand.Reader)
Expand Down
15 changes: 15 additions & 0 deletions proto/get_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ COSMOS_PROTO_FILES=(
base/v1beta1/coin.proto
base/query/v1beta1/pagination.proto
msg/v1/msg.proto
bank/v1beta1/bank.proto
)

echo Fetching protobuf dependencies from Cosmos $COSMOS_VERSION
Expand All @@ -41,3 +42,17 @@ for FILE in "${COSMOS_PROTO_FILES[@]}"; do
mkdir -p "cosmos/$(dirname $FILE)"
curl -sSL "$COSMOS_PROTO_URL/$FILE" > "cosmos/$FILE"
done

IBC_VERSION=v6.2.1
IBC_PROTO_URL=https://raw.githubusercontent.com/cosmos/ibc-go/refs/tags/$IBC_VERSION/proto

IBC_PROTO_FILES=(
ibc/applications/transfer/v2/packet.proto
)

echo Fetching protobuf dependencies from IBC $IBC_VERSION
for FILE in "${IBC_PROTO_FILES[@]}"; do
echo Fetching "$FILE"
mkdir -p "ibc/$(dirname $FILE)"
curl -sSL "$IBC_PROTO_URL/$FILE" > "ibc/$FILE"
done
Loading

0 comments on commit 5a4205e

Please sign in to comment.