github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/rawdb/accessors_l1_message_test.go (about) 1 package rawdb 2 3 import ( 4 "math/big" 5 "testing" 6 7 "github.com/scroll-tech/go-ethereum/common" 8 "github.com/scroll-tech/go-ethereum/core/types" 9 ) 10 11 func TestReadWriteSyncedL1BlockNumber(t *testing.T) { 12 blockNumbers := []uint64{ 13 1, 14 1 << 2, 15 1 << 8, 16 1 << 16, 17 1 << 32, 18 } 19 20 db := NewMemoryDatabase() 21 for _, num := range blockNumbers { 22 WriteSyncedL1BlockNumber(db, num) 23 got := ReadSyncedL1BlockNumber(db) 24 25 if got == nil || *got != num { 26 t.Fatal("Block number mismatch", "expected", num, "got", got) 27 } 28 } 29 } 30 31 func newL1MessageTx(queueIndex uint64) types.L1MessageTx { 32 return types.L1MessageTx{ 33 QueueIndex: queueIndex, 34 Gas: 0, 35 To: &common.Address{}, 36 Value: big.NewInt(0), 37 Data: nil, 38 Sender: common.Address{}, 39 } 40 } 41 42 func TestReadWriteL1Message(t *testing.T) { 43 queueIndex := uint64(123) 44 msg := newL1MessageTx(queueIndex) 45 db := NewMemoryDatabase() 46 WriteL1Messages(db, []types.L1MessageTx{msg}) 47 got := ReadL1Message(db, queueIndex) 48 if got == nil || got.QueueIndex != queueIndex { 49 t.Fatal("L1 message mismatch", "expected", queueIndex, "got", got) 50 } 51 52 max := ReadHighestSyncedQueueIndex(db) 53 if max != 123 { 54 t.Fatal("max index mismatch", "expected", 123, "got", max) 55 } 56 } 57 58 func TestIterateL1Message(t *testing.T) { 59 msgs := []types.L1MessageTx{ 60 newL1MessageTx(100), 61 newL1MessageTx(101), 62 newL1MessageTx(103), 63 newL1MessageTx(200), 64 newL1MessageTx(1000), 65 } 66 67 db := NewMemoryDatabase() 68 WriteL1Messages(db, msgs) 69 70 max := ReadHighestSyncedQueueIndex(db) 71 if max != 1000 { 72 t.Fatal("max index mismatch", "expected", 1000, "got", max) 73 } 74 75 it := IterateL1MessagesFrom(db, 103) 76 defer it.Release() 77 78 for ii := 2; ii < len(msgs); ii++ { 79 finished := !it.Next() 80 if finished { 81 t.Fatal("Iterator terminated early", "ii", ii) 82 } 83 84 got := it.L1Message() 85 if got.QueueIndex != msgs[ii].QueueIndex { 86 t.Fatal("Invalid result", "expected", msgs[ii].QueueIndex, "got", got.QueueIndex) 87 } 88 } 89 90 finished := !it.Next() 91 if !finished { 92 t.Fatal("Iterator did not terminate") 93 } 94 } 95 96 func TestReadL1MessageTxRange(t *testing.T) { 97 msgs := []types.L1MessageTx{ 98 newL1MessageTx(100), 99 newL1MessageTx(101), 100 newL1MessageTx(102), 101 newL1MessageTx(103), 102 } 103 104 db := NewMemoryDatabase() 105 WriteL1Messages(db, msgs) 106 107 got := ReadL1MessagesFrom(db, 101, 3) 108 109 if len(got) != 3 { 110 t.Fatal("Invalid length", "expected", 3, "got", len(got)) 111 } 112 113 if got[0].QueueIndex != 101 || got[1].QueueIndex != 102 || got[2].QueueIndex != 103 { 114 t.Fatal("Invalid result", "got", got) 115 } 116 } 117 118 func TestReadWriteLastL1MessageInL2Block(t *testing.T) { 119 inputs := []uint64{ 120 1, 121 1 << 2, 122 1 << 8, 123 1 << 16, 124 1 << 32, 125 } 126 127 db := NewMemoryDatabase() 128 for _, num := range inputs { 129 l2BlockHash := common.Hash{byte(num)} 130 WriteFirstQueueIndexNotInL2Block(db, l2BlockHash, num) 131 got := ReadFirstQueueIndexNotInL2Block(db, l2BlockHash) 132 133 if got == nil || *got != num { 134 t.Fatal("Enqueue index mismatch", "expected", num, "got", got) 135 } 136 } 137 } 138 139 func TestIterationStopsAtMaxQueueIndex(t *testing.T) { 140 msgs := []types.L1MessageTx{ 141 newL1MessageTx(100), 142 newL1MessageTx(101), 143 newL1MessageTx(102), 144 newL1MessageTx(103), 145 } 146 147 db := NewMemoryDatabase() 148 WriteL1Messages(db, msgs) 149 150 // artificially change max index from 103 to 102 151 WriteHighestSyncedQueueIndex(db, 102) 152 153 // iteration should terminate at 102 and not read 103 154 got := ReadL1MessagesFrom(db, 100, 10) 155 156 if len(got) != 3 { 157 t.Fatal("Invalid length", "expected", 3, "got", len(got)) 158 } 159 }