github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/rawdb/accessors_rollup_event_test.go (about) 1 package rawdb 2 3 import ( 4 "testing" 5 6 "github.com/scroll-tech/go-ethereum/common" 7 ) 8 9 func TestWriteRollupEventSyncedL1BlockNumber(t *testing.T) { 10 blockNumbers := []uint64{ 11 1, 12 1 << 2, 13 1 << 8, 14 1 << 16, 15 1 << 32, 16 } 17 18 db := NewMemoryDatabase() 19 20 // read non-existing value 21 if got := ReadRollupEventSyncedL1BlockNumber(db); got != nil { 22 t.Fatal("Expected 0 for non-existing value", "got", *got) 23 } 24 25 for _, num := range blockNumbers { 26 WriteRollupEventSyncedL1BlockNumber(db, num) 27 got := ReadRollupEventSyncedL1BlockNumber(db) 28 29 if *got != num { 30 t.Fatal("Block number mismatch", "expected", num, "got", got) 31 } 32 } 33 } 34 35 func TestFinalizedL2BlockNumber(t *testing.T) { 36 blockNumbers := []uint64{ 37 1, 38 1 << 2, 39 1 << 8, 40 1 << 16, 41 1 << 32, 42 } 43 44 db := NewMemoryDatabase() 45 46 // read non-existing value 47 if got := ReadFinalizedL2BlockNumber(db); got != nil { 48 t.Fatal("Expected 0 for non-existing value", "got", *got) 49 } 50 51 for _, num := range blockNumbers { 52 WriteFinalizedL2BlockNumber(db, num) 53 got := ReadFinalizedL2BlockNumber(db) 54 55 if *got != num { 56 t.Fatal("Block number mismatch", "expected", num, "got", got) 57 } 58 } 59 } 60 61 func TestFinalizedBatchMeta(t *testing.T) { 62 batches := []*FinalizedBatchMeta{ 63 { 64 BatchHash: common.BytesToHash([]byte("batch1")), 65 TotalL1MessagePopped: 123, 66 StateRoot: common.BytesToHash([]byte("stateRoot1")), 67 WithdrawRoot: common.BytesToHash([]byte("withdrawRoot1")), 68 }, 69 { 70 BatchHash: common.BytesToHash([]byte("batch2")), 71 TotalL1MessagePopped: 456, 72 StateRoot: common.BytesToHash([]byte("stateRoot2")), 73 WithdrawRoot: common.BytesToHash([]byte("withdrawRoot2")), 74 }, 75 { 76 BatchHash: common.BytesToHash([]byte("batch3")), 77 TotalL1MessagePopped: 789, 78 StateRoot: common.BytesToHash([]byte("stateRoot3")), 79 WithdrawRoot: common.BytesToHash([]byte("withdrawRoot3")), 80 }, 81 } 82 83 db := NewMemoryDatabase() 84 85 for i, batch := range batches { 86 batchIndex := uint64(i) 87 WriteFinalizedBatchMeta(db, batchIndex, batch) 88 } 89 90 for i, batch := range batches { 91 batchIndex := uint64(i) 92 readBatch := ReadFinalizedBatchMeta(db, batchIndex) 93 if readBatch == nil { 94 t.Fatal("Failed to read batch from database") 95 } 96 if readBatch.BatchHash != batch.BatchHash || readBatch.TotalL1MessagePopped != batch.TotalL1MessagePopped || 97 readBatch.StateRoot != batch.StateRoot || readBatch.WithdrawRoot != batch.WithdrawRoot { 98 t.Fatal("Mismatch in read batch", "expected", batch, "got", readBatch) 99 } 100 } 101 102 // over-write 103 newBatch := &FinalizedBatchMeta{ 104 BatchHash: common.BytesToHash([]byte("newBatch")), 105 TotalL1MessagePopped: 999, 106 StateRoot: common.BytesToHash([]byte("newStateRoot")), 107 WithdrawRoot: common.BytesToHash([]byte("newWithdrawRoot")), 108 } 109 WriteFinalizedBatchMeta(db, 0, newBatch) // over-writing the batch with index 0 110 readBatch := ReadFinalizedBatchMeta(db, 0) 111 if readBatch.BatchHash != newBatch.BatchHash || readBatch.TotalL1MessagePopped != newBatch.TotalL1MessagePopped || 112 readBatch.StateRoot != newBatch.StateRoot || readBatch.WithdrawRoot != newBatch.WithdrawRoot { 113 t.Fatal("Mismatch after over-writing batch", "expected", newBatch, "got", readBatch) 114 } 115 116 // read non-existing value 117 nonExistingIndex := uint64(len(batches) + 1) 118 readBatch = ReadFinalizedBatchMeta(db, nonExistingIndex) 119 if readBatch != nil { 120 t.Fatal("Expected nil for non-existing value", "got", readBatch) 121 } 122 } 123 124 func TestBatchChunkRanges(t *testing.T) { 125 chunks := [][]*ChunkBlockRange{ 126 { 127 {StartBlockNumber: 1, EndBlockNumber: 100}, 128 {StartBlockNumber: 101, EndBlockNumber: 200}, 129 }, 130 { 131 {StartBlockNumber: 201, EndBlockNumber: 300}, 132 {StartBlockNumber: 301, EndBlockNumber: 400}, 133 }, 134 { 135 {StartBlockNumber: 401, EndBlockNumber: 500}, 136 }, 137 } 138 139 db := NewMemoryDatabase() 140 141 for i, chunkRange := range chunks { 142 batchIndex := uint64(i) 143 WriteBatchChunkRanges(db, batchIndex, chunkRange) 144 } 145 146 for i, chunkRange := range chunks { 147 batchIndex := uint64(i) 148 readChunkRange := ReadBatchChunkRanges(db, batchIndex) 149 if len(readChunkRange) != len(chunkRange) { 150 t.Fatal("Mismatch in number of chunk ranges", "expected", len(chunkRange), "got", len(readChunkRange)) 151 } 152 153 for j, cr := range readChunkRange { 154 if cr.StartBlockNumber != chunkRange[j].StartBlockNumber || cr.EndBlockNumber != chunkRange[j].EndBlockNumber { 155 t.Fatal("Mismatch in chunk range", "batch index", batchIndex, "expected", chunkRange[j], "got", cr) 156 } 157 } 158 } 159 160 // over-write 161 newRange := []*ChunkBlockRange{{StartBlockNumber: 1001, EndBlockNumber: 1100}} 162 WriteBatchChunkRanges(db, 0, newRange) 163 readChunkRange := ReadBatchChunkRanges(db, 0) 164 if len(readChunkRange) != 1 || readChunkRange[0].StartBlockNumber != 1001 || readChunkRange[0].EndBlockNumber != 1100 { 165 t.Fatal("Over-write failed for chunk range", "expected", newRange, "got", readChunkRange) 166 } 167 168 // read non-existing value 169 if readChunkRange = ReadBatchChunkRanges(db, uint64(len(chunks)+1)); readChunkRange != nil { 170 t.Fatal("Expected nil for non-existing value", "got", readChunkRange) 171 } 172 173 // delete: revert batch 174 for i := range chunks { 175 batchIndex := uint64(i) 176 DeleteBatchChunkRanges(db, batchIndex) 177 178 readChunkRange := ReadBatchChunkRanges(db, batchIndex) 179 if readChunkRange != nil { 180 t.Fatal("Chunk range was not deleted", "batch index", batchIndex) 181 } 182 } 183 184 // delete non-existing value: ensure the delete operation handles non-existing values without errors. 185 DeleteBatchChunkRanges(db, uint64(len(chunks)+1)) 186 }