mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-10 22:13:47 +00:00
refactor: improve L1MessageTx tests (#456)
This commit is contained in:
parent
eda99eec35
commit
edaf59bf06
1 changed files with 85 additions and 16 deletions
|
|
@ -732,17 +732,12 @@ func TestL1MsgCorrectOrder(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestL1MessageOverGasLimit(t *testing.T) {
|
func l1MessageTest(t *testing.T, msgs []types.L1MessageTx, callback func(i int, block *types.Block, db ethdb.Database) bool) {
|
||||||
assert := assert.New(t)
|
|
||||||
var (
|
var (
|
||||||
engine consensus.Engine
|
engine consensus.Engine
|
||||||
chainConfig *params.ChainConfig
|
chainConfig *params.ChainConfig
|
||||||
db = rawdb.NewMemoryDatabase()
|
db = rawdb.NewMemoryDatabase()
|
||||||
)
|
)
|
||||||
msgs := []types.L1MessageTx{
|
|
||||||
{QueueIndex: 0, Gas: 10000000, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}},
|
|
||||||
{QueueIndex: 1, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}}, // same sender
|
|
||||||
{QueueIndex: 2, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}}} // different sender
|
|
||||||
rawdb.WriteL1Messages(db, msgs)
|
rawdb.WriteL1Messages(db, msgs)
|
||||||
|
|
||||||
chainConfig = params.AllCliqueProtocolChanges
|
chainConfig = params.AllCliqueProtocolChanges
|
||||||
|
|
@ -775,21 +770,95 @@ func TestL1MessageOverGasLimit(t *testing.T) {
|
||||||
// Start mining!
|
// Start mining!
|
||||||
w.start()
|
w.start()
|
||||||
|
|
||||||
select {
|
for ii := 1; true; ii++ {
|
||||||
case ev := <-sub.Chan():
|
// timeout for all blocks
|
||||||
block := ev.Data.(core.NewMinedBlockEvent).Block
|
select {
|
||||||
if _, err := chain.InsertChain([]*types.Block{block}); err != nil {
|
case <-time.After(3 * time.Second):
|
||||||
t.Fatalf("failed to insert new mined block %d: %v", block.NumberU64(), err)
|
t.Fatalf("timeout")
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should contain 2, not 1
|
select {
|
||||||
// i.e. we should only skip 1 message
|
case ev := <-sub.Chan():
|
||||||
|
block := ev.Data.(core.NewMinedBlockEvent).Block
|
||||||
|
// TODO
|
||||||
|
if callback(ii, block, db) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// timeout for one block
|
||||||
|
case <-time.After(3 * time.Second):
|
||||||
|
t.Fatalf("timeout")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestL1SingleMessageOverGasLimit(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
msgs := []types.L1MessageTx{
|
||||||
|
{QueueIndex: 0, Gas: 10000000, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}}, // over gas limit
|
||||||
|
{QueueIndex: 1, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}}, // same sender
|
||||||
|
{QueueIndex: 2, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}}, // different sender
|
||||||
|
}
|
||||||
|
|
||||||
|
l1MessageTest(t, msgs, func(_i int, block *types.Block, db ethdb.Database) bool {
|
||||||
|
// skip #0, include #1 and #2
|
||||||
assert.Equal(2, len(block.Transactions()))
|
assert.Equal(2, len(block.Transactions()))
|
||||||
|
|
||||||
|
assert.True(block.Transactions()[0].IsL1MessageTx())
|
||||||
|
assert.Equal(uint64(1), block.Transactions()[0].AsL1MessageTx().QueueIndex)
|
||||||
|
assert.True(block.Transactions()[1].IsL1MessageTx())
|
||||||
|
assert.Equal(uint64(2), block.Transactions()[1].AsL1MessageTx().QueueIndex)
|
||||||
|
|
||||||
|
// db is updated correctly
|
||||||
queueIndex := rawdb.ReadFirstQueueIndexNotInL2Block(db, block.Hash())
|
queueIndex := rawdb.ReadFirstQueueIndexNotInL2Block(db, block.Hash())
|
||||||
assert.NotNil(queueIndex)
|
assert.NotNil(queueIndex)
|
||||||
assert.Equal(uint64(3), *queueIndex)
|
assert.Equal(uint64(3), *queueIndex)
|
||||||
case <-time.After(3 * time.Second):
|
|
||||||
t.Fatalf("timeout")
|
return true
|
||||||
}
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestL1CombinedMessagesOverGasLimit(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
// message #0 is over the gas limit
|
||||||
|
// we should skip #0 but not #1 and #2
|
||||||
|
msgs := []types.L1MessageTx{
|
||||||
|
{QueueIndex: 0, Gas: 4000000, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}},
|
||||||
|
{QueueIndex: 1, Gas: 4000000, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}}, // same sender
|
||||||
|
{QueueIndex: 2, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}}, // different sender
|
||||||
|
}
|
||||||
|
|
||||||
|
l1MessageTest(t, msgs, func(blockNum int, block *types.Block, db ethdb.Database) bool {
|
||||||
|
switch blockNum {
|
||||||
|
case 1:
|
||||||
|
// block #1 only includes 1 message
|
||||||
|
assert.Equal(1, len(block.Transactions()))
|
||||||
|
assert.True(block.Transactions()[0].IsL1MessageTx())
|
||||||
|
assert.Equal(uint64(0), block.Transactions()[0].AsL1MessageTx().QueueIndex)
|
||||||
|
|
||||||
|
// db is updated correctly
|
||||||
|
queueIndex := rawdb.ReadFirstQueueIndexNotInL2Block(db, block.Hash())
|
||||||
|
assert.NotNil(queueIndex)
|
||||||
|
assert.Equal(uint64(1), *queueIndex)
|
||||||
|
return false
|
||||||
|
case 2:
|
||||||
|
// block #2 includes the other 2 messages
|
||||||
|
assert.Equal(2, len(block.Transactions()))
|
||||||
|
assert.True(block.Transactions()[0].IsL1MessageTx())
|
||||||
|
assert.Equal(uint64(1), block.Transactions()[0].AsL1MessageTx().QueueIndex)
|
||||||
|
assert.True(block.Transactions()[1].IsL1MessageTx())
|
||||||
|
assert.Equal(uint64(2), block.Transactions()[1].AsL1MessageTx().QueueIndex)
|
||||||
|
|
||||||
|
// db is updated correctly
|
||||||
|
queueIndex := rawdb.ReadFirstQueueIndexNotInL2Block(db, block.Hash())
|
||||||
|
assert.NotNil(queueIndex)
|
||||||
|
assert.Equal(uint64(3), *queueIndex)
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue