|
| 1 | +package composite |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "go.opentelemetry.io/otel" |
| 10 | + sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 11 | + "go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 12 | + |
| 13 | + "github.com/sei-protocol/sei-chain/sei-db/common/keys" |
| 14 | + "github.com/sei-protocol/sei-chain/sei-db/config" |
| 15 | + "github.com/sei-protocol/sei-chain/sei-db/proto" |
| 16 | + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/migration" |
| 17 | + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/types" |
| 18 | +) |
| 19 | + |
| 20 | +const migrationVersionInstrument = "seidb_migration_version" |
| 21 | + |
| 22 | +// installManualMeterProvider routes the global OTel meter to a ManualReader |
| 23 | +// for the duration of the test. |
| 24 | +func installManualMeterProvider(t *testing.T) *sdkmetric.ManualReader { |
| 25 | + t.Helper() |
| 26 | + reader := sdkmetric.NewManualReader() |
| 27 | + provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 28 | + prev := otel.GetMeterProvider() |
| 29 | + otel.SetMeterProvider(provider) |
| 30 | + t.Cleanup(func() { |
| 31 | + otel.SetMeterProvider(prev) |
| 32 | + require.NoError(t, provider.Shutdown(context.Background())) |
| 33 | + }) |
| 34 | + return reader |
| 35 | +} |
| 36 | + |
| 37 | +// collectMigrationVersions collects once from reader and returns every |
| 38 | +// seidb_migration_version data point value. |
| 39 | +func collectMigrationVersions(t *testing.T, reader *sdkmetric.ManualReader) []int64 { |
| 40 | + t.Helper() |
| 41 | + var collected metricdata.ResourceMetrics |
| 42 | + require.NoError(t, reader.Collect(context.Background(), &collected)) |
| 43 | + |
| 44 | + var values []int64 |
| 45 | + for _, scope := range collected.ScopeMetrics { |
| 46 | + for _, m := range scope.Metrics { |
| 47 | + if m.Name != migrationVersionInstrument { |
| 48 | + continue |
| 49 | + } |
| 50 | + gauge, ok := m.Data.(metricdata.Gauge[int64]) |
| 51 | + require.True(t, ok, "unexpected data type %T", m.Data) |
| 52 | + for _, point := range gauge.DataPoints { |
| 53 | + values = append(values, point.Value) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return values |
| 58 | +} |
| 59 | + |
| 60 | +// TestLoadVersionReadOnlyDoesNotReportMigrationVersion pins that a read-only |
| 61 | +// handle opened at a mid-migration height leaves the process-wide migration |
| 62 | +// version gauge at the live manager's value. State-sync snapshot exports open |
| 63 | +// such handles after the migration completes; they must not report the |
| 64 | +// pre-completion version they observe. |
| 65 | +func TestLoadVersionReadOnlyDoesNotReportMigrationVersion(t *testing.T) { |
| 66 | + dir := t.TempDir() |
| 67 | + |
| 68 | + // Phase 1: seed evm/ keys in MemiavlOnly so the migration spans several blocks. |
| 69 | + v0Cfg := config.DefaultStateCommitConfig() |
| 70 | + v0Cfg.WriteMode = types.MemiavlOnly |
| 71 | + cs1, err := NewCompositeCommitStore(t.Context(), dir, v0Cfg) |
| 72 | + require.NoError(t, err) |
| 73 | + require.NoError(t, cs1.Initialize([]string{keys.BankStoreKey, keys.EVMStoreKey})) |
| 74 | + require.NoError(t, cs1.LoadLatest()) |
| 75 | + pairs := make([]*proto.KVPair, 0, 8) |
| 76 | + for i := 0; i < 8; i++ { |
| 77 | + pairs = append(pairs, &proto.KVPair{Key: []byte(fmt.Sprintf("evm_key_%d", i)), Value: []byte("v")}) |
| 78 | + } |
| 79 | + require.NoError(t, cs1.ApplyChangeSets([]*proto.NamedChangeSet{ |
| 80 | + {Name: keys.EVMStoreKey, Changeset: proto.ChangeSet{Pairs: pairs}}, |
| 81 | + })) |
| 82 | + _, err = cs1.Commit(cs1.Version() + 1) |
| 83 | + require.NoError(t, err) |
| 84 | + require.NoError(t, cs1.Close()) |
| 85 | + |
| 86 | + reader := installManualMeterProvider(t) |
| 87 | + |
| 88 | + // Phase 2: migrate one key per block until the completion block lands. |
| 89 | + migrateCfg := config.DefaultStateCommitConfig() |
| 90 | + migrateCfg.WriteMode = types.MigrateEVM |
| 91 | + cs2, err := NewCompositeCommitStore(t.Context(), dir, migrateCfg) |
| 92 | + require.NoError(t, err) |
| 93 | + require.NoError(t, cs2.SetMigrationBatchSize(1)) |
| 94 | + require.NoError(t, cs2.Initialize([]string{keys.BankStoreKey, keys.EVMStoreKey})) |
| 95 | + require.NoError(t, cs2.LoadLatest()) |
| 96 | + defer cs2.Close() |
| 97 | + |
| 98 | + var midMigrationVersion int64 |
| 99 | + for block := 0; block < 32; block++ { |
| 100 | + require.NoError(t, cs2.ApplyChangeSets([]*proto.NamedChangeSet{ |
| 101 | + {Name: keys.BankStoreKey, Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ |
| 102 | + {Key: []byte(fmt.Sprintf("bank_%d", block)), Value: []byte("v")}, |
| 103 | + }}}, |
| 104 | + })) |
| 105 | + _, err = cs2.Commit(cs2.Version() + 1) |
| 106 | + require.NoError(t, err) |
| 107 | + if _, done := cs2.flatKV.Get(migration.MigrationStore, []byte(migration.MigrationVersionKey)); done { |
| 108 | + break |
| 109 | + } |
| 110 | + midMigrationVersion = cs2.Version() |
| 111 | + } |
| 112 | + require.NotZero(t, midMigrationVersion, "migration must span more than one block") |
| 113 | + require.Equal(t, []int64{int64(migration.Version1_MigrateEVM)}, collectMigrationVersions(t, reader), |
| 114 | + "precondition: live manager reports the completed migration version") |
| 115 | + |
| 116 | + ro, err := cs2.LoadVersionReadOnly(midMigrationVersion) |
| 117 | + require.NoError(t, err) |
| 118 | + defer func() { _ = ro.Close() }() |
| 119 | + |
| 120 | + require.Equal(t, []int64{int64(migration.Version1_MigrateEVM)}, collectMigrationVersions(t, reader), |
| 121 | + "read-only handle at a mid-migration height must not overwrite the live migration version") |
| 122 | +} |
0 commit comments