Skip to content

Commit fd47246

Browse files
make batch gas costs part of mel consensus
1 parent 9a6e52b commit fd47246

File tree

9 files changed

+33
-55
lines changed

9 files changed

+33
-55
lines changed

arbnode/mel/extraction/message_extraction_function.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,6 @@ func extractMessagesImpl(
151151
if delayed.Message.Header.Kind == arbostypes.L1MessageType_BatchPostingReport || delayed.Message.Header.Kind == arbostypes.L1MessageType_Initialize { // Let's consider the init message as a batch posting report, since it is seen as a batch as well, we can later ignore filling its batchGasCost anyway
152152
batchPostingReports = append(batchPostingReports, delayed)
153153
}
154-
if err = state.AccumulateDelayedMessage(delayed); err != nil {
155-
return nil, nil, nil, nil, err
156-
}
157-
state.DelayedMessagesSeen += 1
158-
}
159-
if len(delayedMessages) > 0 {
160-
// Only need to calculate partials once, after all the delayed messages are `seen`
161-
if err := state.GenerateDelayedMessagesSeenMerklePartialsAndRoot(); err != nil {
162-
return nil, nil, nil, nil, err
163-
}
164154
}
165155

166156
// Batch posting reports are included in the same transaction as a batch, so there should
@@ -175,6 +165,7 @@ func extractMessagesImpl(
175165

176166
var batchMetas []*mel.BatchMetadata
177167
var messages []*arbostypes.MessageWithMetadata
168+
var serializedBatches [][]byte
178169
for i, batch := range batches {
179170
batchTx := batchTxs[i]
180171
serialized, err := serialize(
@@ -209,7 +200,26 @@ func extractMessagesImpl(
209200
} else if !(inputState.DelayedMessagesSeen == 0 && i == 0 && delayedMessages[i] == batchPostReport) {
210201
return nil, nil, nil, nil, errors.New("encountered initialize message that is not the first delayed message and the first batch ")
211202
}
203+
serializedBatches = append(serializedBatches, serialized)
204+
}
205+
206+
// Update the delayed message accumulator in the MEL state.
207+
for _, delayed := range delayedMessages {
208+
if err = state.AccumulateDelayedMessage(delayed); err != nil {
209+
return nil, nil, nil, nil, err
210+
}
211+
state.DelayedMessagesSeen += 1
212+
}
213+
if len(delayedMessages) > 0 {
214+
// Only need to calculate partials once, after all the delayed messages are `seen`
215+
if err := state.GenerateDelayedMessagesSeenMerklePartialsAndRoot(); err != nil {
216+
return nil, nil, nil, nil, err
217+
}
218+
}
212219

220+
// Extract L2 messages from batches
221+
for i, batch := range batches {
222+
serialized := serializedBatches[i]
213223
rawSequencerMsg, err := parseSequencerMessage(
214224
ctx,
215225
batch.SequenceNumber,

arbnode/mel/messages.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,13 @@ func (m *DelayedInboxMessage) AfterInboxAcc() common.Hash {
5959
}
6060

6161
func (m *DelayedInboxMessage) Hash() common.Hash {
62-
encoded, err := rlp.EncodeToBytes(m.WithMELRelevantFields())
62+
encoded, err := rlp.EncodeToBytes(m)
6363
if err != nil {
6464
panic(err)
6565
}
6666
return crypto.Keccak256Hash(encoded)
6767
}
6868

69-
func (m *DelayedInboxMessage) WithMELRelevantFields() *DelayedInboxMessage {
70-
return &DelayedInboxMessage{
71-
BlockHash: m.BlockHash,
72-
Message: &arbostypes.L1IncomingMessage{
73-
Header: m.Message.Header,
74-
L2msg: m.Message.L2msg,
75-
},
76-
ParentChainBlockNumber: m.ParentChainBlockNumber,
77-
}
78-
}
79-
8069
type BatchMetadata struct {
8170
Accumulator common.Hash
8271
MessageCount arbutil.MessageIndex

arbnode/mel/recording/delayed_msg_database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (r *DelayedMsgDatabase) ReadDelayedMessage(ctx context.Context, state *mel.
6363
if err != nil {
6464
return nil, err
6565
}
66-
delayedMsgBytes, err := rlp.EncodeToBytes(delayed.WithMELRelevantFields())
66+
delayedMsgBytes, err := rlp.EncodeToBytes(delayed)
6767
if err != nil {
6868
return nil, err
6969
}

arbnode/mel/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (s *State) AccumulateMessage(msg *arbostypes.MessageWithMetadata) error {
168168
}
169169
}
170170
}
171-
msgBytes, err := rlp.EncodeToBytes(msg.WithMELRelevantFields())
171+
msgBytes, err := rlp.EncodeToBytes(msg)
172172
if err != nil {
173173
return err
174174
}

arbos/arbostypes/messagewithmeta.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,13 @@ type MessageWithMetadata struct {
1616
}
1717

1818
func (m *MessageWithMetadata) Hash() common.Hash {
19-
encoded, err := rlp.EncodeToBytes(m.WithMELRelevantFields())
19+
encoded, err := rlp.EncodeToBytes(m)
2020
if err != nil {
2121
panic(err)
2222
}
2323
return crypto.Keccak256Hash(encoded)
2424
}
2525

26-
func (m *MessageWithMetadata) WithMELRelevantFields() *MessageWithMetadata {
27-
return &MessageWithMetadata{
28-
Message: &L1IncomingMessage{
29-
Header: m.Message.Header,
30-
L2msg: m.Message.L2msg,
31-
},
32-
DelayedMessagesRead: m.DelayedMessagesRead,
33-
}
34-
}
35-
3626
// lint:require-exhaustive-initialization
3727
type MessageWithMetadataAndBlockInfo struct {
3828
MessageWithMeta MessageWithMetadata

staker/block_validation_entry_creator.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@ import (
1717
type MELEnabledValidationEntryCreator struct {
1818
melValidator MELValidatorInterface
1919
txStreamer TransactionStreamerInterface
20-
melRunner MELRunnerInterface
2120
}
2221

2322
// NewMELEnabledValidationEntryCreator creates a new instance of MELEnabledValidationEntryCreator.
2423
func NewMELEnabledValidationEntryCreator(
2524
melValidator MELValidatorInterface,
2625
txStreamer TransactionStreamerInterface,
27-
melRunner MELRunnerInterface,
2826
) *MELEnabledValidationEntryCreator {
2927
return &MELEnabledValidationEntryCreator{
3028
melValidator: melValidator,
3129
txStreamer: txStreamer,
32-
melRunner: melRunner,
3330
}
3431
}
3532

@@ -113,7 +110,7 @@ func (m *MELEnabledValidationEntryCreator) CreateBlockValidationEntry(
113110
created = true
114111
return &validationEntry{
115112
Stage: ReadyForRecord,
116-
Pos: arbutil.MessageIndex(position),
113+
Pos: position,
117114
Start: startGs,
118115
End: endGlobalState,
119116
msg: msg,

staker/block_validator.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ func NewBlockValidator(
383383
validationEntryCreator := NewMELEnabledValidationEntryCreator(
384384
melValidator,
385385
streamer,
386-
melRunner,
387386
)
388387
ret.melEnabledEntryCreator = validationEntryCreator
389388
}

system_tests/message_extraction_layer_test.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,19 +289,15 @@ func TestMessageExtractionLayer_SequencerBatchMessageEquivalence_Blobs(t *testin
289289
numDelayedMessages,
290290
)
291291
}
292+
292293
// Start from 1 to ignore the init message.
293-
readHelperState := &mel.State{DelayedMessagesSeen: 1}
294-
readHelperState.SetDelayedMessageBacklog(&mel.DelayedMessageBacklog{})
295-
readHelperState.SetReadCountFromBacklog(numDelayedMessages) // skip checking against accumulator- not the purpose of this test
296294
for i := uint64(1); i < numDelayedMessages; i++ {
297295
fromInboxTracker, err := builder.L2.ConsensusNode.InboxTracker.GetDelayedMessage(ctx, i)
298296
Require(t, err)
299-
Require(t, readHelperState.AccumulateDelayedMessage(&mel.DelayedInboxMessage{Message: fromInboxTracker}))
300-
readHelperState.DelayedMessagesSeen++
301-
fromMelDB, err := melDB.ReadDelayedMessage(ctx, readHelperState, i)
297+
delayedMsg, err := extractor.GetDelayedMessage(i)
302298
Require(t, err)
303299
// Check the messages we extracted from MEL and the inbox tracker are the same.
304-
if !fromInboxTracker.Equals(fromMelDB.Message) {
300+
if !fromInboxTracker.Equals(delayedMsg.Message) {
305301
t.Fatal("Messages from MEL and inbox tracker do not match")
306302
}
307303
}
@@ -386,18 +382,13 @@ func TestMessageExtractionLayer_DelayedMessageEquivalence_Simple(t *testing.T) {
386382
}
387383

388384
// Start from 1 to ignore the init message.
389-
readHelperState := &mel.State{DelayedMessagesSeen: 1}
390-
readHelperState.SetDelayedMessageBacklog(&mel.DelayedMessageBacklog{})
391-
readHelperState.SetReadCountFromBacklog(numDelayedMessages) // skip checking against accumulator- not the purpose of this test
392385
for i := uint64(1); i < numDelayedMessages; i++ {
393386
fromInboxTracker, err := builder.L2.ConsensusNode.InboxTracker.GetDelayedMessage(ctx, i)
394387
Require(t, err)
395-
Require(t, readHelperState.AccumulateDelayedMessage(&mel.DelayedInboxMessage{Message: fromInboxTracker}))
396-
readHelperState.DelayedMessagesSeen++
397-
fromMelDB, err := melDB.ReadDelayedMessage(ctx, readHelperState, i)
388+
delayedMsg, err := extractor.GetDelayedMessage(i)
398389
Require(t, err)
399390
// Check the messages we extracted from MEL and the inbox tracker are the same.
400-
if !fromInboxTracker.Equals(fromMelDB.Message) {
391+
if !fromInboxTracker.Equals(delayedMsg.Message) {
401392
t.Fatal("Messages from MEL and inbox tracker do not match")
402393
}
403394
}

system_tests/message_extraction_layer_validation_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package arbtest
22

33
import (
44
"context"
5+
"fmt"
56
"math/big"
67
"testing"
78
"time"
@@ -83,7 +84,7 @@ func TestUnifiedReplayBinary_ValidationOfMELAndBlockExecution(t *testing.T) {
8384
latestValidatedState: endMELState,
8485
}
8586
entryCreator := staker.NewMELEnabledValidationEntryCreator(
86-
mockMElV, builder.L2.ConsensusNode.TxStreamer, builder.L2.ConsensusNode.MessageExtractor,
87+
mockMElV, builder.L2.ConsensusNode.TxStreamer,
8788
)
8889
Require(t, err)
8990

@@ -144,6 +145,7 @@ func TestUnifiedReplayBinary_ValidationOfMELAndBlockExecution(t *testing.T) {
144145
t.Fatalf("Expected to compute %s block hash but computed %s", blockValidatorEntry.End.BlockHash, lastStep.GlobalState.BlockHash)
145146
}
146147
t.Logf("Validated block execution of message index %+v\n", lastStep.GlobalState)
148+
fmt.Println("Validated block execution of message index ", lastStep.GlobalState.PosInBatch)
147149

148150
// Update the computed global state to the one just computed by Arbitrator.
149151
computedGlobalState = lastStep.GlobalState

0 commit comments

Comments
 (0)