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(statesync): extract app version from snapshot (backport #3871) #3936

Merged
merged 11 commits into from
Oct 4, 2024
27 changes: 23 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ func setDefaultAppVersion(req abci.RequestInitChain) abci.RequestInitChain {
// mountKeysAndInit mounts the keys for the provided app version and then
// invokes baseapp.Init().
func (app *App) mountKeysAndInit(appVersion uint64) {
app.BaseApp.Logger().Info(fmt.Sprintf("mounting KV stores for app version %v", appVersion))
app.Logger().Info(fmt.Sprintf("mounting KV stores for app version %v", appVersion))
app.MountKVStores(app.versionedKeys(appVersion))

// Invoke load latest version for its side-effect of invoking baseapp.Init()
Expand Down Expand Up @@ -805,19 +805,38 @@ func (app *App) OfferSnapshot(req abci.RequestOfferSnapshot) abci.ResponseOfferS
return app.BaseApp.OfferSnapshot(req)
}

app.Logger().Info("offering snapshot", "height", req.Snapshot.Height, "app_version", req.AppVersion)
if req.AppVersion != 0 {
if !isSupportedAppVersion(req.AppVersion) {
app.Logger().Info("rejecting snapshot because unsupported app version", "app_version", req.AppVersion)
return abci.ResponseOfferSnapshot{
Result: abci.ResponseOfferSnapshot_REJECT,
}
}

app.Logger().Info("mounting keys for snapshot", "app_version", req.AppVersion)
app.mountKeysAndInit(req.AppVersion)
return app.BaseApp.OfferSnapshot(req)
}

// If the app version is not set in the snapshot, this falls back to inferring the app version based on the upgrade height.
if app.upgradeHeightV2 == 0 {
app.Logger().Debug("v2 upgrade height not set, assuming app version 2")
app.Logger().Info("v2 upgrade height not set, assuming app version 2")
app.mountKeysAndInit(v2)
return app.BaseApp.OfferSnapshot(req)
}

if req.Snapshot.Height >= uint64(app.upgradeHeightV2) {
app.Logger().Debug("snapshot height is greater than or equal to upgrade height, assuming app version 2")
app.Logger().Info("snapshot height is greater than or equal to upgrade height, assuming app version 2")
app.mountKeysAndInit(v2)
return app.BaseApp.OfferSnapshot(req)
}

app.Logger().Debug("snapshot height is less than upgrade height, assuming app version 1")
app.Logger().Info("snapshot height is less than upgrade height, assuming app version 1")
app.mountKeysAndInit(v1)
return app.BaseApp.OfferSnapshot(req)
}

func isSupportedAppVersion(appVersion uint64) bool {
return appVersion == v1 || appVersion == v2 || appVersion == v3
}
92 changes: 65 additions & 27 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app_test

import (
"encoding/json"
"path/filepath"
"testing"

"github.com/celestiaorg/celestia-app/v3/app"
Expand Down Expand Up @@ -112,42 +113,79 @@ func TestInitChain(t *testing.T) {
}

func TestOfferSnapshot(t *testing.T) {
logger := log.NewNopLogger()
db := tmdb.NewMemDB()
traceStore := &NoopWriter{}
invCheckPeriod := uint(1)
encodingConfig := encoding.MakeConfig(app.ModuleEncodingRegisters...)
upgradeHeight := int64(0)
appOptions := NoopAppOptions{}
snapshotOption := getSnapshotOption(t)
app := app.New(logger, db, traceStore, invCheckPeriod, encodingConfig, upgradeHeight, appOptions, snapshotOption)

t.Run("should return ACCEPT", func(t *testing.T) {
request := abci.RequestOfferSnapshot{
Snapshot: &abci.Snapshot{
Height: 0x1b07ec,
Format: 0x2,
Chunks: 0x1,
Hash: []uint8{0xaf, 0xa5, 0xe, 0x16, 0x45, 0x4, 0x2e, 0x45, 0xd3, 0x49, 0xdf, 0x83, 0x2a, 0x57, 0x9d, 0x64, 0xc8, 0xad, 0xa5, 0xb, 0x65, 0x1b, 0x46, 0xd6, 0xc3, 0x85, 0x6, 0x51, 0xd7, 0x45, 0x8e, 0xb8},
Metadata: []uint8{0xa, 0x20, 0xaf, 0xa5, 0xe, 0x16, 0x45, 0x4, 0x2e, 0x45, 0xd3, 0x49, 0xdf, 0x83, 0x2a, 0x57, 0x9d, 0x64, 0xc8, 0xad, 0xa5, 0xb, 0x65, 0x1b, 0x46, 0xd6, 0xc3, 0x85, 0x6, 0x51, 0xd7, 0x45, 0x8e, 0xb8},
},
AppHash: []byte("apphash"),
}
t.Run("should ACCEPT a snapshot with app version 0", func(t *testing.T) {
// Snapshots taken before the app version field was introduced to RequestOfferSnapshot should still be accepted.
app := createTestApp(t)
request := createRequest()
want := abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_ACCEPT}
got := app.OfferSnapshot(request)
assert.Equal(t, want, got)
})
t.Run("should ACCEPT a snapshot with app version 1", func(t *testing.T) {
app := createTestApp(t)
request := createRequest()
request.AppVersion = 1
want := abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_ACCEPT}
got := app.OfferSnapshot(request)
assert.Equal(t, want, got)
})
t.Run("should ACCEPT a snapshot with app version 2", func(t *testing.T) {
app := createTestApp(t)
request := createRequest()
request.AppVersion = 2
want := abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_ACCEPT}
got := app.OfferSnapshot(request)
assert.Equal(t, want, got)
})
t.Run("should ACCEPT a snapshot with app version 3", func(t *testing.T) {
app := createTestApp(t)
request := createRequest()
request.AppVersion = 3
want := abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_ACCEPT}
got := app.OfferSnapshot(request)
assert.Equal(t, want, got)
})
t.Run("should REJECT a snapshot with unsupported app version", func(t *testing.T) {
app := createTestApp(t)
request := createRequest()
request.AppVersion = 4 // unsupported app version
want := abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_REJECT}
got := app.OfferSnapshot(request)
assert.Equal(t, want, got)
})
}

func getSnapshotOption(t *testing.T) func(*baseapp.BaseApp) {
snapshotDir := t.TempDir()
snapshotDB, err := tmdb.NewDB("metadata", tmdb.GoLevelDBBackend, t.TempDir())
func createTestApp(t *testing.T) *app.App {
db := tmdb.NewMemDB()
config := encoding.MakeConfig(app.ModuleEncodingRegisters...)
upgradeHeight := int64(3)
snapshotDir := filepath.Join(t.TempDir(), "data", "snapshots")
snapshotDB, err := tmdb.NewDB("metadata", tmdb.GoLevelDBBackend, snapshotDir)
require.NoError(t, err)
snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir)
require.NoError(t, err)
interval := uint64(10)
keepRecent := uint32(10)
return baseapp.SetSnapshot(snapshotStore, snapshottypes.NewSnapshotOptions(interval, keepRecent))
baseAppOption := baseapp.SetSnapshot(snapshotStore, snapshottypes.NewSnapshotOptions(10, 10))
testApp := app.New(log.NewNopLogger(), db, nil, 0, config, upgradeHeight, util.EmptyAppOptions{}, baseAppOption)
require.NoError(t, err)
response := testApp.Info(abci.RequestInfo{})
require.Equal(t, uint64(0), response.AppVersion)
return testApp
}

func createRequest() abci.RequestOfferSnapshot {
return abci.RequestOfferSnapshot{
// Snapshot was created by logging the contents of OfferSnapshot on a
// node that was syncing via state sync.
Snapshot: &abci.Snapshot{
Height: 0x1b07ec,
Format: 0x2,
Chunks: 0x1,
Hash: []uint8{0xaf, 0xa5, 0xe, 0x16, 0x45, 0x4, 0x2e, 0x45, 0xd3, 0x49, 0xdf, 0x83, 0x2a, 0x57, 0x9d, 0x64, 0xc8, 0xad, 0xa5, 0xb, 0x65, 0x1b, 0x46, 0xd6, 0xc3, 0x85, 0x6, 0x51, 0xd7, 0x45, 0x8e, 0xb8},
Metadata: []uint8{0xa, 0x20, 0xaf, 0xa5, 0xe, 0x16, 0x45, 0x4, 0x2e, 0x45, 0xd3, 0x49, 0xdf, 0x83, 0x2a, 0x57, 0x9d, 0x64, 0xc8, 0xad, 0xa5, 0xb, 0x65, 0x1b, 0x46, 0xd6, 0xc3, 0x85, 0x6, 0x51, 0xd7, 0x45, 0x8e, 0xb8},
},
AppHash: []byte("apphash"),
AppVersion: 0, // unit tests will override this
}
}

// NoopWriter is a no-op implementation of a writer.
Expand Down
54 changes: 27 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/cosmos/gogoproto v1.7.0
github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v6 v6.1.2
github.com/cosmos/ibc-go/v6 v6.2.2
github.com/ethereum/go-ethereum v1.14.7
github.com/ethereum/go-ethereum v1.14.11
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.4
Expand All @@ -26,14 +26,14 @@ require (
github.com/rakyll/statik v0.1.7
github.com/rs/zerolog v1.33.0
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
github.com/tendermint/tendermint v0.34.29
github.com/tendermint/tm-db v0.6.7
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142
google.golang.org/grpc v1.67.0
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117
google.golang.org/grpc v1.66.0
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.31.1
Expand All @@ -56,8 +56,9 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/bufbuild/protocompile v0.14.1 // indirect
github.com/celestiaorg/bittwister v0.0.0-20231213180407-65cdbaf5b8c7 // indirect
github.com/celestiaorg/merkletree v0.0.0-20210714075610-a84dc3ddbbe4 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
Expand All @@ -76,6 +77,7 @@ require (
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.6 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.2 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/creachadair/taskgroup v0.3.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
Expand All @@ -90,6 +92,7 @@ require (
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
Expand Down Expand Up @@ -119,7 +122,7 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/gorilla/handlers v1.5.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/grafana/otel-profiling-go v0.5.1 // indirect
github.com/grafana/pyroscope-go v1.1.2 // indirect
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect
Expand All @@ -131,12 +134,11 @@ require (
github.com/hashicorp/go-getter v1.7.4 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.0.0-20220222234857-c00d1f31bab3 // indirect
github.com/holiman/uint256 v1.3.0 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/iancoleman/orderedmap v0.2.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
Expand All @@ -148,16 +150,15 @@ require (
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/klauspost/reedsolomon v1.12.1 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/minio/highwayhash v1.0.3 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/minio-go/v7 v7.0.74 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand All @@ -172,17 +173,16 @@ require (
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.53.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/client_golang v1.20.3 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/regen-network/cosmos-proto v0.3.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rs/cors v1.8.3 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
Expand All @@ -192,7 +192,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.15.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/supranational/blst v0.3.13 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand All @@ -207,11 +207,11 @@ require (
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.18.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
Expand All @@ -224,7 +224,7 @@ require (
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.169.0 // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand All @@ -241,11 +241,11 @@ require (
)

replace (
github.com/cosmos/cosmos-sdk => github.com/celestiaorg/cosmos-sdk v1.24.1-sdk-v0.46.16
github.com/cosmos/cosmos-sdk => github.com/celestiaorg/cosmos-sdk v1.25.0-sdk-v0.46.16
// Pin to ledger-cosmos-go v0.12.4 to avoid a breaking change introduced in v0.13.0
// The following replace statement can be removed when we upgrade to cosmos-sdk >= v0.50.0
github.com/cosmos/ledger-cosmos-go => github.com/cosmos/ledger-cosmos-go v0.12.4
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tendermint/tendermint => github.com/celestiaorg/celestia-core v1.41.0-tm-v0.34.29
github.com/tendermint/tendermint => github.com/celestiaorg/celestia-core v1.42.0-tm-v0.34.35
)
Loading
Loading