Skip to content

feat: include L1 message with insufficient balance #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,10 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
st.gas -= gas

// Check clause 6
if msg.Value().Sign() > 0 && !st.evm.Context.CanTransfer(st.state, msg.From(), msg.Value()) {
// Note: If this is an L1MessageTx, we will not return a top-level ErrInsufficientFundsForTransfer.
// Instead, we return ErrInsufficientBalance from within st.evm.Call. This means that such transactions
// will revert but they can be included in a valid block. This is necessary for supporting enforced txs.
if msg.Value().Sign() > 0 && !st.evm.Context.CanTransfer(st.state, msg.From(), msg.Value()) && !msg.IsL1MessageTx() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note about backward compatibility:

While this is a concensus rule, this case is impossible to trigger now: EnforcedTxGateway is paused so users cannot construct arbitrary L1 messages.

return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From().Hex())
}

Expand Down
78 changes: 60 additions & 18 deletions miner/scroll_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func TestL1MsgCorrectOrder(t *testing.T) {
}
}

func l1MessageTest(t *testing.T, msgs []types.L1MessageTx, withL2Tx bool, callback func(i int, block *types.Block, db ethdb.Database, w *worker) bool) {
func l1MessageTest(t *testing.T, msgs []types.L1MessageTx, withL2Tx bool, callback func(i int, block *types.Block, db ethdb.Database, w *worker, bc *core.BlockChain) bool) {
var (
engine consensus.Engine
chainConfig *params.ChainConfig
Expand Down Expand Up @@ -558,7 +558,7 @@ func l1MessageTest(t *testing.T, msgs []types.L1MessageTx, withL2Tx bool, callba
w.start()

// call once before first block
callback(0, nil, db, w)
callback(0, nil, db, w, chain)

// timeout for all blocks
globalTimeout := time.After(3 * time.Second)
Expand All @@ -574,7 +574,7 @@ func l1MessageTest(t *testing.T, msgs []types.L1MessageTx, withL2Tx bool, callba
case ev := <-sub.Chan():
block := ev.Data.(core.NewMinedBlockEvent).Block

if done := callback(ii, block, db, w); done {
if done := callback(ii, block, db, w, chain); done {
return
}

Expand All @@ -594,7 +594,7 @@ func TestL1SingleMessageOverGasLimit(t *testing.T) {
{QueueIndex: 2, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}}, // different sender
}

l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker) bool {
l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker, bc *core.BlockChain) bool {
switch blockNum {
case 0:
return false
Expand Down Expand Up @@ -630,7 +630,7 @@ func TestL1CombinedMessagesOverGasLimit(t *testing.T) {
{QueueIndex: 2, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}}, // different sender
}

l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker) bool {
l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker, bc *core.BlockChain) bool {
switch blockNum {
case 0:
return false
Expand Down Expand Up @@ -674,7 +674,7 @@ func TestLargeL1MessageSkipPayloadCheck(t *testing.T) {
{QueueIndex: 2, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}}, // different sender
}

l1MessageTest(t, msgs, true, func(blockNum int, block *types.Block, db ethdb.Database, w *worker) bool {
l1MessageTest(t, msgs, true, func(blockNum int, block *types.Block, db ethdb.Database, w *worker, bc *core.BlockChain) bool {
switch blockNum {
case 0:
return false
Expand Down Expand Up @@ -708,15 +708,17 @@ func TestLargeL1MessageSkipPayloadCheck(t *testing.T) {
func TestSkipMessageWithStrangeError(t *testing.T) {
assert := assert.New(t)

// message #0 is skipped because of `Value`
// TODO: trigger skipping in some other way after this behaviour is changed
msgs := []types.L1MessageTx{
{QueueIndex: 0, Gas: 25100, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}, Value: big.NewInt(1)},
// message #0 is skipped because of `GasLimit`
// (cannot happen in practice, this is checked in the contracts)
{QueueIndex: 0, Gas: 20000000, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}},

// messages #1 and #2 are correct
{QueueIndex: 1, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}},
{QueueIndex: 2, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}},
}

l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker) bool {
l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker, bc *core.BlockChain) bool {
switch blockNum {
case 0:
return false
Expand All @@ -742,18 +744,58 @@ func TestSkipMessageWithStrangeError(t *testing.T) {
})
}

func TestL1MessageWithInsufficientBalanceNotSkipped(t *testing.T) {
assert := assert.New(t)

msgs := []types.L1MessageTx{
// message #0 sends more funds than available in the sender account
{QueueIndex: 0, Gas: 25100, To: &common.Address{1}, Data: make([]byte, 1025), Sender: common.Address{2}, Value: big.NewInt(1)},

// #message #1 is a correct msg
{QueueIndex: 1, Gas: 25100, To: &common.Address{1}, Data: make([]byte, 1025), Sender: common.Address{2}},
}

l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker, bc *core.BlockChain) bool {
switch blockNum {
case 0:
return false
case 1:
// include both #0 and #1
assert.Equal(2, len(block.Transactions()))
assert.True(block.Transactions()[0].IsL1MessageTx())
assert.Equal(uint64(0), block.Transactions()[0].AsL1MessageTx().QueueIndex)
assert.True(block.Transactions()[1].IsL1MessageTx())
assert.Equal(uint64(1), block.Transactions()[1].AsL1MessageTx().QueueIndex)

// #0 fails, #1 succeeds
receipts := bc.GetReceiptsByHash(block.Hash())
assert.Equal(2, len(receipts))
assert.Equal(types.ReceiptStatusFailed, receipts[0].Status)
assert.Equal(types.ReceiptStatusSuccessful, receipts[1].Status)

// db is updated correctly
queueIndex := rawdb.ReadFirstQueueIndexNotInL2Block(db, block.Hash())
assert.NotNil(queueIndex)
assert.Equal(uint64(2), *queueIndex)

return true
default:
return true
}
})
}

func TestSkipAllL1MessagesInBlock(t *testing.T) {
assert := assert.New(t)

// messages are skipped because of `Value`
// TODO: trigger skipping in some other way after this behaviour is changed
// messages are skipped because of `GasLimit`
msgs := []types.L1MessageTx{
{QueueIndex: 0, Gas: 25100, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}, Value: big.NewInt(1)},
{QueueIndex: 1, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}, Value: big.NewInt(1)},
{QueueIndex: 2, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}, Value: big.NewInt(1)},
{QueueIndex: 0, Gas: 20000000, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}},
{QueueIndex: 1, Gas: 20000000, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}},
{QueueIndex: 2, Gas: 20000000, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}},
}

l1MessageTest(t, msgs, true, func(blockNum int, block *types.Block, db ethdb.Database, w *worker) bool {
l1MessageTest(t, msgs, true, func(blockNum int, block *types.Block, db ethdb.Database, w *worker, bc *core.BlockChain) bool {
switch blockNum {
case 0:
return false
Expand Down Expand Up @@ -785,7 +827,7 @@ func TestOversizedTxThenNormal(t *testing.T) {
{QueueIndex: 2, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{3}},
}

l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker) bool {
l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker, bc *core.BlockChain) bool {
switch blockNum {
case 0:
// schedule to skip 2nd call to ccc
Expand Down Expand Up @@ -933,7 +975,7 @@ func TestSkippedTransactionDatabaseEntries(t *testing.T) {
{QueueIndex: 1, Gas: 21016, To: &common.Address{1}, Data: []byte{0x01}, Sender: common.Address{2}},
}

l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker) bool {
l1MessageTest(t, msgs, false, func(blockNum int, block *types.Block, db ethdb.Database, w *worker, bc *core.BlockChain) bool {
switch blockNum {
case 0:
return false
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 8 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release
VersionPatch = 1 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down
Loading