|
1 | 1 | package flatkv |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "errors" |
| 6 | + "slices" |
5 | 7 | "sync/atomic" |
6 | 8 | "testing" |
7 | 9 | "time" |
8 | 10 |
|
| 11 | + "github.com/sei-protocol/sei-chain/sei-db/common/keys" |
| 12 | + "github.com/sei-protocol/sei-chain/sei-db/proto" |
9 | 13 | "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/ktype" |
| 14 | + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/vtype" |
10 | 15 | "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/types" |
11 | 16 | "github.com/stretchr/testify/require" |
12 | 17 | ) |
@@ -446,3 +451,98 @@ func TestKVImporter_BackpressureBlocksProducerUntilWorkersDrain(t *testing.T) { |
446 | 451 | require.GreaterOrEqual(t, flushes, int64(2), |
447 | 452 | "expected multiple flushes for %d storage pairs (got %d)", totalPairs, flushes) |
448 | 453 | } |
| 454 | + |
| 455 | +// storageNode returns an import node carrying a serialized storage row at version 1. |
| 456 | +func storageNode(t *testing.T, addr ktype.Address, slot ktype.Slot, value []byte) *types.SnapshotNode { |
| 457 | + t.Helper() |
| 458 | + row := vtype.NewStorageData().SetBlockHeight(1).SetValue((*[32]byte)(value)).Serialize() |
| 459 | + return &types.SnapshotNode{Key: storagePhysKey(addr, slot), Value: row, Version: 1} |
| 460 | +} |
| 461 | + |
| 462 | +// addNodesInKeyOrder feeds nodes to imp in ascending physical-key order, the order Importer requires. |
| 463 | +func addNodesInKeyOrder(imp types.Importer, nodes []*types.SnapshotNode) { |
| 464 | + sorted := slices.Clone(nodes) |
| 465 | + slices.SortFunc(sorted, func(a *types.SnapshotNode, b *types.SnapshotNode) int { |
| 466 | + return bytes.Compare(a.Key, b.Key) |
| 467 | + }) |
| 468 | + for _, n := range sorted { |
| 469 | + imp.AddNode(n) |
| 470 | + } |
| 471 | +} |
| 472 | + |
| 473 | +// TestKVImporter_RepeatedPairRejected feeds an honest export followed by one forged pair 2^16 times. Accepted, |
| 474 | +// the forged row would be live while its uint16 LtHash limbs wrapped to zero, leaving the root hash equal to |
| 475 | +// the honest source's. |
| 476 | +func TestKVImporter_RepeatedPairRejected(t *testing.T) { |
| 477 | + src := setupTestStore(t) |
| 478 | + defer func() { require.NoError(t, src.Close()) }() |
| 479 | + |
| 480 | + require.NoError(t, src.ApplyChangeSets(src.Version()+1, []*proto.NamedChangeSet{ |
| 481 | + {Name: "evm", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ |
| 482 | + {Key: evmStorageKey(addrN(0x01), slotN(0x01)), Value: padLeft32(0x11)}, |
| 483 | + {Key: evmStorageKey(addrN(0x02), slotN(0x02)), Value: padLeft32(0x22)}, |
| 484 | + }}}, |
| 485 | + })) |
| 486 | + commitAndCheck(t, src) |
| 487 | + |
| 488 | + exp, err := src.Exporter(1) |
| 489 | + require.NoError(t, err) |
| 490 | + nodes := drainExporter(t, exp) |
| 491 | + require.NoError(t, exp.Close()) |
| 492 | + require.NotEmpty(t, nodes) |
| 493 | + |
| 494 | + dst, imp := newKVImporterForTest(t, 1) |
| 495 | + defer func() { require.NoError(t, dst.Close()) }() |
| 496 | + |
| 497 | + require.NoError(t, imp.AddModule(keys.FlatKVStoreKey)) |
| 498 | + for _, n := range nodes { |
| 499 | + imp.AddNode(n) |
| 500 | + } |
| 501 | + forged := storageNode(t, addrN(0xBA), slotN(0xD0), padLeft32(0x66)) |
| 502 | + for i := 0; i < 1<<16; i++ { |
| 503 | + imp.AddNode(forged) |
| 504 | + } |
| 505 | + |
| 506 | + require.ErrorContains(t, imp.Close(), "duplicate physical key") |
| 507 | + require.Equal(t, int64(0), dst.Version(), "a rejected import must not finalize") |
| 508 | +} |
| 509 | + |
| 510 | +// TestKVImporter_OutOfOrderKeyRejected verifies that Importer fails an import whose physical keys descend. |
| 511 | +func TestKVImporter_OutOfOrderKeyRejected(t *testing.T) { |
| 512 | + s, imp := newKVImporterForTest(t, 1) |
| 513 | + defer func() { require.NoError(t, s.Close()) }() |
| 514 | + |
| 515 | + imp.AddNode(storageNode(t, addrN(0x02), slotN(0x02), padLeft32(0x22))) |
| 516 | + imp.AddNode(storageNode(t, addrN(0x01), slotN(0x01), padLeft32(0x11))) |
| 517 | + |
| 518 | + require.ErrorContains(t, imp.Close(), "keys must be in ascending order") |
| 519 | + require.Equal(t, int64(0), s.Version(), "a rejected import must not finalize") |
| 520 | +} |
| 521 | + |
| 522 | +// TestTrustedImporter_AcceptsAnyOrder verifies that TrustedImporter imports physical keys given in |
| 523 | +// descending order. |
| 524 | +func TestTrustedImporter_AcceptsAnyOrder(t *testing.T) { |
| 525 | + s := setupTestStore(t) |
| 526 | + defer func() { require.NoError(t, s.Close()) }() |
| 527 | + |
| 528 | + imp, err := s.TrustedImporter(1) |
| 529 | + require.NoError(t, err) |
| 530 | + imp.AddNode(storageNode(t, addrN(0x02), slotN(0x02), padLeft32(0x22))) |
| 531 | + imp.AddNode(storageNode(t, addrN(0x01), slotN(0x01), padLeft32(0x11))) |
| 532 | + require.NoError(t, imp.Close()) |
| 533 | + |
| 534 | + require.Equal(t, int64(1), s.Version()) |
| 535 | + for _, want := range []struct { |
| 536 | + addr ktype.Address |
| 537 | + slot ktype.Slot |
| 538 | + value []byte |
| 539 | + }{ |
| 540 | + {addrN(0x01), slotN(0x01), padLeft32(0x11)}, |
| 541 | + {addrN(0x02), slotN(0x02), padLeft32(0x22)}, |
| 542 | + } { |
| 543 | + got, found := s.Get(keys.EVMStoreKey, evmStorageKey(want.addr, want.slot)) |
| 544 | + require.True(t, found) |
| 545 | + require.Equal(t, want.value, got) |
| 546 | + } |
| 547 | + require.NoError(t, VerifyLtHash(s)) |
| 548 | +} |
0 commit comments