forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtransaction_streamer.go
1321 lines (1182 loc) · 42.1 KB
/
transaction_streamer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package arbnode
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"fmt"
"math/big"
"reflect"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
espressoClient "github.com/EspressoSystems/espresso-sequencer-go/client"
espressoTypes "github.com/EspressoSystems/espresso-sequencer-go/types"
"errors"
"github.com/cockroachdb/pebble"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
flag "github.com/spf13/pflag"
"github.com/syndtr/goleveldb/leveldb"
"github.com/offchainlabs/nitro/arbos"
"github.com/offchainlabs/nitro/arbos/arbostypes"
"github.com/offchainlabs/nitro/arbutil"
"github.com/offchainlabs/nitro/broadcaster"
m "github.com/offchainlabs/nitro/broadcaster/message"
"github.com/offchainlabs/nitro/execution"
"github.com/offchainlabs/nitro/staker"
"github.com/offchainlabs/nitro/util/arbmath"
"github.com/offchainlabs/nitro/util/sharedmetrics"
"github.com/offchainlabs/nitro/util/stopwaiter"
)
// TransactionStreamer produces blocks from a node's L1 messages, storing the results in the blockchain and recording their positions
// The streamer is notified when there's new batches to process
type TransactionStreamer struct {
stopwaiter.StopWaiter
chainConfig *params.ChainConfig
exec execution.ExecutionSequencer
execLastMsgCount arbutil.MessageIndex
validator *staker.BlockValidator
db ethdb.Database
fatalErrChan chan<- error
config TransactionStreamerConfigFetcher
snapSyncConfig *SnapSyncConfig
insertionMutex sync.Mutex // cannot be acquired while reorgMutex is held
reorgMutex sync.RWMutex
newMessageNotifier chan struct{}
newSovereignTxNotifier chan struct{}
nextAllowedFeedReorgLog time.Time
broadcasterQueuedMessages []arbostypes.MessageWithMetadataAndBlockHash
broadcasterQueuedMessagesPos uint64
broadcasterQueuedMessagesActiveReorg bool
coordinator *SeqCoordinator
broadcastServer *broadcaster.Broadcaster
inboxReader *InboxReader
delayedBridge *DelayedBridge
espressoClient *espressoClient.Client
pendingTxnsQueueMutex sync.Mutex // cannot be acquired while reorgMutex is held
pendingTxnsPos []arbutil.MessageIndex
submittedTxnPos *arbutil.MessageIndex
submittedTxHash *espressoTypes.TaggedBase64
}
type TransactionStreamerConfig struct {
MaxBroadcasterQueueSize int `koanf:"max-broadcaster-queue-size"`
MaxReorgResequenceDepth int64 `koanf:"max-reorg-resequence-depth" reload:"hot"`
ExecuteMessageLoopDelay time.Duration `koanf:"execute-message-loop-delay" reload:"hot"`
// Espresso specific fields
SovereignSequencerEnabled bool `koanf:"sovereign-sequencer-enabled"`
HotShotUrl string `koanf:"hotshot-url"`
EspressoNamespace uint64 `koanf:"espresso-namespace"`
EspressoTxnsPollingInterval time.Duration `koanf:"espresso-txns-polling-interval"`
}
type TransactionStreamerConfigFetcher func() *TransactionStreamerConfig
var DefaultTransactionStreamerConfig = TransactionStreamerConfig{
MaxBroadcasterQueueSize: 50_000,
MaxReorgResequenceDepth: 1024,
ExecuteMessageLoopDelay: time.Millisecond * 100,
SovereignSequencerEnabled: false,
HotShotUrl: "",
EspressoTxnsPollingInterval: time.Millisecond * 100,
}
var TestTransactionStreamerConfig = TransactionStreamerConfig{
MaxBroadcasterQueueSize: 10_000,
MaxReorgResequenceDepth: 128 * 1024,
ExecuteMessageLoopDelay: time.Millisecond,
SovereignSequencerEnabled: false,
HotShotUrl: "",
EspressoTxnsPollingInterval: time.Millisecond * 100,
}
func TransactionStreamerConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Int(prefix+".max-broadcaster-queue-size", DefaultTransactionStreamerConfig.MaxBroadcasterQueueSize, "maximum cache of pending broadcaster messages")
f.Int64(prefix+".max-reorg-resequence-depth", DefaultTransactionStreamerConfig.MaxReorgResequenceDepth, "maximum number of messages to attempt to resequence on reorg (0 = never resequence, -1 = always resequence)")
f.Duration(prefix+".execute-message-loop-delay", DefaultTransactionStreamerConfig.ExecuteMessageLoopDelay, "delay when polling calls to execute messages")
f.Bool(prefix+".sovereign-sequencer-enabled", DefaultTransactionStreamerConfig.SovereignSequencerEnabled, "if true, transactions will be sent to espresso's sovereign sequencer to be notarized by espresso network")
f.String(prefix+".hotshot-url", DefaultTransactionStreamerConfig.HotShotUrl, "url of the hotshot sequencer")
f.Uint64(prefix+".espresso-namespace", DefaultTransactionStreamerConfig.EspressoNamespace, "espresso namespace that corresponds the L2 chain")
f.Duration(prefix+".espresso-txns-polling-interval", DefaultTransactionStreamerConfig.EspressoTxnsPollingInterval, "interval between polling for transactions to be included in the block")
}
func NewTransactionStreamer(
db ethdb.Database,
chainConfig *params.ChainConfig,
exec execution.ExecutionSequencer,
broadcastServer *broadcaster.Broadcaster,
fatalErrChan chan<- error,
config TransactionStreamerConfigFetcher,
snapSyncConfig *SnapSyncConfig,
) (*TransactionStreamer, error) {
streamer := &TransactionStreamer{
exec: exec,
chainConfig: chainConfig,
db: db,
newMessageNotifier: make(chan struct{}, 1),
broadcastServer: broadcastServer,
fatalErrChan: fatalErrChan,
config: config,
snapSyncConfig: snapSyncConfig,
}
if config().SovereignSequencerEnabled {
espressoClient := espressoClient.NewClient(config().HotShotUrl)
streamer.espressoClient = espressoClient
}
err := streamer.cleanupInconsistentState()
if err != nil {
return nil, err
}
return streamer, nil
}
// Represents a block's hash in the database.
// Necessary because RLP decoder doesn't produce nil values by default.
type blockHashDBValue struct {
BlockHash *common.Hash `rlp:"nil"`
}
const (
BlockHashMismatchLogMsg = "BlockHash from feed doesn't match locally computed hash. Check feed source."
)
// Encodes a uint64 as bytes in a lexically sortable manner for database iteration.
// Generally this is only used for database keys, which need sorted.
// A shorter RLP encoding is usually used for database values.
func uint64ToKey(x uint64) []byte {
data := make([]byte, 8)
binary.BigEndian.PutUint64(data, x)
return data
}
func (s *TransactionStreamer) SetBlockValidator(validator *staker.BlockValidator) {
if s.Started() {
panic("trying to set coordinator after start")
}
if s.validator != nil {
panic("trying to set coordinator when already set")
}
s.validator = validator
}
func (s *TransactionStreamer) SetSeqCoordinator(coordinator *SeqCoordinator) {
if s.Started() {
panic("trying to set coordinator after start")
}
if s.coordinator != nil {
panic("trying to set coordinator when already set")
}
s.coordinator = coordinator
}
func (s *TransactionStreamer) SetInboxReaders(inboxReader *InboxReader, delayedBridge *DelayedBridge) {
if s.Started() {
panic("trying to set inbox reader after start")
}
if s.inboxReader != nil || s.delayedBridge != nil {
panic("trying to set inbox reader when already set")
}
s.inboxReader = inboxReader
s.delayedBridge = delayedBridge
}
func (s *TransactionStreamer) ChainConfig() *params.ChainConfig {
return s.chainConfig
}
func (s *TransactionStreamer) cleanupInconsistentState() error {
// If it doesn't exist yet, set the message count to 0
hasMessageCount, err := s.db.Has(messageCountKey)
if err != nil {
return err
}
if !hasMessageCount {
err := setMessageCount(s.db, 0)
if err != nil {
return err
}
}
// TODO remove trailing messageCountToMessage and messageCountToBlockPrefix entries
return nil
}
func (s *TransactionStreamer) ReorgTo(count arbutil.MessageIndex) error {
return s.ReorgToAndEndBatch(s.db.NewBatch(), count)
}
func (s *TransactionStreamer) ReorgToAndEndBatch(batch ethdb.Batch, count arbutil.MessageIndex) error {
s.insertionMutex.Lock()
defer s.insertionMutex.Unlock()
err := s.reorg(batch, count, nil)
if err != nil {
return err
}
err = batch.Write()
if err != nil {
return err
}
return nil
}
func deleteStartingAt(db ethdb.Database, batch ethdb.Batch, prefix []byte, minKey []byte) error {
iter := db.NewIterator(prefix, minKey)
defer iter.Release()
for iter.Next() {
err := batch.Delete(iter.Key())
if err != nil {
return err
}
}
return iter.Error()
}
// deleteFromRange deletes key ranging from startMinKey(inclusive) to endMinKey(exclusive)
// might have deleted some keys even if returning an error
func deleteFromRange(ctx context.Context, db ethdb.Database, prefix []byte, startMinKey uint64, endMinKey uint64) ([]uint64, error) {
batch := db.NewBatch()
startIter := db.NewIterator(prefix, uint64ToKey(startMinKey))
defer startIter.Release()
var prunedKeysRange []uint64
for startIter.Next() {
if ctx.Err() != nil {
return nil, ctx.Err()
}
currentKey := binary.BigEndian.Uint64(bytes.TrimPrefix(startIter.Key(), prefix))
if currentKey >= endMinKey {
break
}
if len(prunedKeysRange) == 0 || len(prunedKeysRange) == 1 {
prunedKeysRange = append(prunedKeysRange, currentKey)
} else {
prunedKeysRange[1] = currentKey
}
err := batch.Delete(startIter.Key())
if err != nil {
return nil, err
}
if batch.ValueSize() >= ethdb.IdealBatchSize {
if err := batch.Write(); err != nil {
return nil, err
}
batch.Reset()
}
}
if batch.ValueSize() > 0 {
if err := batch.Write(); err != nil {
return nil, err
}
}
return prunedKeysRange, nil
}
// The insertion mutex must be held. This acquires the reorg mutex.
// Note: oldMessages will be empty if reorgHook is nil
func (s *TransactionStreamer) reorg(batch ethdb.Batch, count arbutil.MessageIndex, newMessages []arbostypes.MessageWithMetadataAndBlockHash) error {
if count == 0 {
return errors.New("cannot reorg out init message")
}
lastDelayedSeqNum, err := s.getPrevPrevDelayedRead(count)
if err != nil {
return err
}
var oldMessages []*arbostypes.MessageWithMetadata
targetMsgCount, err := s.GetMessageCount()
if err != nil {
return err
}
config := s.config()
maxResequenceMsgCount := count + arbutil.MessageIndex(config.MaxReorgResequenceDepth)
if config.MaxReorgResequenceDepth >= 0 && maxResequenceMsgCount < targetMsgCount {
log.Error(
"unable to re-sequence all old messages because there are too many",
"reorgingToCount", count,
"removingMessages", targetMsgCount-count,
"maxReorgResequenceDepth", config.MaxReorgResequenceDepth,
)
targetMsgCount = maxResequenceMsgCount
}
for i := count; i < targetMsgCount; i++ {
oldMessage, err := s.GetMessage(i)
if err != nil {
log.Error("unable to lookup old message for re-sequencing", "position", i, "err", err)
break
}
if oldMessage.Message == nil || oldMessage.Message.Header == nil {
continue
}
header := oldMessage.Message.Header
if header.RequestId != nil {
// This is a delayed message
delayedSeqNum := header.RequestId.Big().Uint64()
if delayedSeqNum+1 != oldMessage.DelayedMessagesRead {
log.Error("delayed message header RequestId doesn't match database DelayedMessagesRead", "header", oldMessage.Message.Header, "delayedMessagesRead", oldMessage.DelayedMessagesRead)
continue
}
if delayedSeqNum != lastDelayedSeqNum {
// This is the wrong position for the delayed message
continue
}
if s.inboxReader != nil {
// this is a delayed message. Should be resequenced if all 3 agree:
// oldMessage, accumulator stored in tracker, and the message re-read from l1
expectedAcc, err := s.inboxReader.tracker.GetDelayedAcc(delayedSeqNum)
if err != nil {
if !strings.Contains(err.Error(), "not found") {
log.Error("reorg-resequence: failed to read expected accumulator", "err", err)
}
continue
}
msgBlockNum := new(big.Int).SetUint64(oldMessage.Message.Header.BlockNumber)
delayedInBlock, err := s.delayedBridge.LookupMessagesInRange(s.GetContext(), msgBlockNum, msgBlockNum, nil)
if err != nil {
log.Error("reorg-resequence: failed to serialize old delayed message from database", "err", err)
continue
}
messageFound := false
delayedInBlockLoop:
for _, delayedFound := range delayedInBlock {
if delayedFound.Message.Header.RequestId.Big().Uint64() != delayedSeqNum {
continue delayedInBlockLoop
}
if expectedAcc == delayedFound.AfterInboxAcc() && delayedFound.Message.Equals(oldMessage.Message) {
messageFound = true
}
break delayedInBlockLoop
}
if !messageFound {
continue
}
}
lastDelayedSeqNum++
}
oldMessages = append(oldMessages, oldMessage)
}
s.reorgMutex.Lock()
defer s.reorgMutex.Unlock()
messagesResults, err := s.exec.Reorg(count, newMessages, oldMessages)
if err != nil {
return err
}
messagesWithComputedBlockHash := make([]arbostypes.MessageWithMetadataAndBlockHash, 0, len(messagesResults))
for i := 0; i < len(messagesResults); i++ {
messagesWithComputedBlockHash = append(messagesWithComputedBlockHash, arbostypes.MessageWithMetadataAndBlockHash{
MessageWithMeta: newMessages[i].MessageWithMeta,
BlockHash: &messagesResults[i].BlockHash,
})
}
s.broadcastMessages(messagesWithComputedBlockHash, count)
if s.validator != nil {
err = s.validator.Reorg(s.GetContext(), count)
if err != nil {
return err
}
}
err = deleteStartingAt(s.db, batch, blockHashInputFeedPrefix, uint64ToKey(uint64(count)))
if err != nil {
return err
}
err = deleteStartingAt(s.db, batch, messagePrefix, uint64ToKey(uint64(count)))
if err != nil {
return err
}
return setMessageCount(batch, count)
}
func setMessageCount(batch ethdb.KeyValueWriter, count arbutil.MessageIndex) error {
countBytes, err := rlp.EncodeToBytes(count)
if err != nil {
return err
}
err = batch.Put(messageCountKey, countBytes)
if err != nil {
return err
}
sharedmetrics.UpdateSequenceNumberGauge(count)
return nil
}
func dbKey(prefix []byte, pos uint64) []byte {
var key []byte
key = append(key, prefix...)
key = append(key, uint64ToKey(pos)...)
return key
}
func isErrNotFound(err error) bool {
return errors.Is(err, leveldb.ErrNotFound) || errors.Is(err, pebble.ErrNotFound)
}
// Note: if changed to acquire the mutex, some internal users may need to be updated to a non-locking version.
func (s *TransactionStreamer) GetMessage(seqNum arbutil.MessageIndex) (*arbostypes.MessageWithMetadata, error) {
key := dbKey(messagePrefix, uint64(seqNum))
data, err := s.db.Get(key)
if err != nil {
return nil, err
}
var message arbostypes.MessageWithMetadata
err = rlp.DecodeBytes(data, &message)
if err != nil {
return nil, err
}
return &message, nil
}
func (s *TransactionStreamer) getMessageWithMetadataAndBlockHash(seqNum arbutil.MessageIndex) (*arbostypes.MessageWithMetadataAndBlockHash, error) {
msg, err := s.GetMessage(seqNum)
if err != nil {
return nil, err
}
// Get block hash.
// To keep it backwards compatible, since it is possible that a message related
// to a sequence number exists in the database, but the block hash doesn't.
key := dbKey(blockHashInputFeedPrefix, uint64(seqNum))
var blockHash *common.Hash
data, err := s.db.Get(key)
if err == nil {
var blockHashDBVal blockHashDBValue
err = rlp.DecodeBytes(data, &blockHashDBVal)
if err != nil {
return nil, err
}
blockHash = blockHashDBVal.BlockHash
} else if !isErrNotFound(err) {
return nil, err
}
msgWithBlockHash := arbostypes.MessageWithMetadataAndBlockHash{
MessageWithMeta: *msg,
BlockHash: blockHash,
}
return &msgWithBlockHash, nil
}
// Note: if changed to acquire the mutex, some internal users may need to be updated to a non-locking version.
func (s *TransactionStreamer) GetMessageCount() (arbutil.MessageIndex, error) {
posBytes, err := s.db.Get(messageCountKey)
if err != nil {
return 0, err
}
var pos uint64
err = rlp.DecodeBytes(posBytes, &pos)
if err != nil {
return 0, err
}
return arbutil.MessageIndex(pos), nil
}
func (s *TransactionStreamer) GetProcessedMessageCount() (arbutil.MessageIndex, error) {
msgCount, err := s.GetMessageCount()
if err != nil {
return 0, err
}
digestedHead, err := s.exec.HeadMessageNumber()
if err != nil {
return 0, err
}
if msgCount > digestedHead+1 {
return digestedHead + 1, nil
}
return msgCount, nil
}
func (s *TransactionStreamer) AddMessages(pos arbutil.MessageIndex, messagesAreConfirmed bool, messages []arbostypes.MessageWithMetadata) error {
return s.AddMessagesAndEndBatch(pos, messagesAreConfirmed, messages, nil)
}
func (s *TransactionStreamer) FeedPendingMessageCount() arbutil.MessageIndex {
pos := atomic.LoadUint64(&s.broadcasterQueuedMessagesPos)
if pos == 0 {
return 0
}
s.insertionMutex.Lock()
defer s.insertionMutex.Unlock()
pos = atomic.LoadUint64(&s.broadcasterQueuedMessagesPos)
if pos == 0 {
return 0
}
return arbutil.MessageIndex(pos + uint64(len(s.broadcasterQueuedMessages)))
}
func (s *TransactionStreamer) AddBroadcastMessages(feedMessages []*m.BroadcastFeedMessage) error {
if len(feedMessages) == 0 {
return nil
}
broadcastStartPos := feedMessages[0].SequenceNumber
var messages []arbostypes.MessageWithMetadataAndBlockHash
broadcastAfterPos := broadcastStartPos
for _, feedMessage := range feedMessages {
if broadcastAfterPos != feedMessage.SequenceNumber {
return fmt.Errorf("invalid sequence number %v, expected %v", feedMessage.SequenceNumber, broadcastAfterPos)
}
if feedMessage.Message.Message == nil || feedMessage.Message.Message.Header == nil {
return fmt.Errorf("invalid feed message at sequence number %v", feedMessage.SequenceNumber)
}
msgWithBlockHash := arbostypes.MessageWithMetadataAndBlockHash{
MessageWithMeta: feedMessage.Message,
BlockHash: feedMessage.BlockHash,
}
messages = append(messages, msgWithBlockHash)
broadcastAfterPos++
}
s.insertionMutex.Lock()
defer s.insertionMutex.Unlock()
var feedReorg bool
var err error
// Skip any messages already in the database
// prevDelayedRead set to 0 because it's only used to compute the output prevDelayedRead which is not used here
// Messages from feed are not confirmed, so confirmedMessageCount is 0 and confirmedReorg can be ignored
dups, feedReorg, oldMsg, err := s.countDuplicateMessages(broadcastStartPos, messages, nil)
if err != nil {
return err
}
messages = messages[dups:]
broadcastStartPos += arbutil.MessageIndex(dups)
if oldMsg != nil {
s.logReorg(broadcastStartPos, oldMsg, &messages[0].MessageWithMeta, false)
}
if len(messages) == 0 {
// No new messages received
return nil
}
if len(s.broadcasterQueuedMessages) == 0 || (feedReorg && !s.broadcasterQueuedMessagesActiveReorg) {
// Empty cache or feed different from database, save current feed messages until confirmed L1 messages catch up.
s.broadcasterQueuedMessages = messages
atomic.StoreUint64(&s.broadcasterQueuedMessagesPos, uint64(broadcastStartPos))
s.broadcasterQueuedMessagesActiveReorg = feedReorg
} else {
broadcasterQueuedMessagesPos := arbutil.MessageIndex(atomic.LoadUint64(&s.broadcasterQueuedMessagesPos))
if broadcasterQueuedMessagesPos >= broadcastStartPos {
// Feed messages older than cache
s.broadcasterQueuedMessages = messages
atomic.StoreUint64(&s.broadcasterQueuedMessagesPos, uint64(broadcastStartPos))
s.broadcasterQueuedMessagesActiveReorg = feedReorg
} else if broadcasterQueuedMessagesPos+arbutil.MessageIndex(len(s.broadcasterQueuedMessages)) == broadcastStartPos {
// Feed messages can be added directly to end of cache
maxQueueSize := s.config().MaxBroadcasterQueueSize
if maxQueueSize == 0 || len(s.broadcasterQueuedMessages) <= maxQueueSize {
s.broadcasterQueuedMessages = append(s.broadcasterQueuedMessages, messages...)
}
broadcastStartPos = broadcasterQueuedMessagesPos
// Do not change existing reorg state
} else {
if len(s.broadcasterQueuedMessages) > 0 {
log.Warn(
"broadcaster queue jumped positions",
"queuedMessages", len(s.broadcasterQueuedMessages),
"expectedNextPos", broadcasterQueuedMessagesPos+arbutil.MessageIndex(len(s.broadcasterQueuedMessages)),
"gotPos", broadcastStartPos,
)
}
s.broadcasterQueuedMessages = messages
atomic.StoreUint64(&s.broadcasterQueuedMessagesPos, uint64(broadcastStartPos))
s.broadcasterQueuedMessagesActiveReorg = feedReorg
}
}
if s.broadcasterQueuedMessagesActiveReorg || len(s.broadcasterQueuedMessages) == 0 {
// Broadcaster never triggered reorg or no messages to add
return nil
}
if broadcastStartPos > 0 {
_, err := s.GetMessage(broadcastStartPos - 1)
if err != nil {
if !isErrNotFound(err) {
return err
}
// Message before current message doesn't exist in database, so don't add current messages yet
return nil
}
}
err = s.addMessagesAndEndBatchImpl(broadcastStartPos, false, nil, nil)
if err != nil {
return fmt.Errorf("error adding pending broadcaster messages: %w", err)
}
return nil
}
// AddFakeInitMessage should only be used for testing or running a local dev node
func (s *TransactionStreamer) AddFakeInitMessage() error {
chainConfigJson, err := json.Marshal(s.chainConfig)
if err != nil {
return fmt.Errorf("failed to serialize chain config: %w", err)
}
chainIdBytes := arbmath.U256Bytes(s.chainConfig.ChainID)
msg := append(append(chainIdBytes, 0), chainConfigJson...)
return s.AddMessages(0, false, []arbostypes.MessageWithMetadata{{
Message: &arbostypes.L1IncomingMessage{
Header: &arbostypes.L1IncomingMessageHeader{
Kind: arbostypes.L1MessageType_Initialize,
RequestId: &common.Hash{},
L1BaseFee: common.Big0,
},
L2msg: msg,
},
DelayedMessagesRead: 1,
}})
}
// Used in redis tests
func (s *TransactionStreamer) GetMessageCountSync(t *testing.T) (arbutil.MessageIndex, error) {
s.insertionMutex.Lock()
defer s.insertionMutex.Unlock()
return s.GetMessageCount()
}
func endBatch(batch ethdb.Batch) error {
if batch == nil {
return nil
}
return batch.Write()
}
func (s *TransactionStreamer) AddMessagesAndEndBatch(pos arbutil.MessageIndex, messagesAreConfirmed bool, messages []arbostypes.MessageWithMetadata, batch ethdb.Batch) error {
messagesWithBlockHash := make([]arbostypes.MessageWithMetadataAndBlockHash, 0, len(messages))
for _, message := range messages {
messagesWithBlockHash = append(messagesWithBlockHash, arbostypes.MessageWithMetadataAndBlockHash{
MessageWithMeta: message,
})
}
if messagesAreConfirmed {
// Trim confirmed messages from l1pricedataCache
s.exec.MarkFeedStart(pos + arbutil.MessageIndex(len(messages)))
s.reorgMutex.RLock()
dups, _, _, err := s.countDuplicateMessages(pos, messagesWithBlockHash, &batch)
s.reorgMutex.RUnlock()
if err != nil {
return err
}
if dups == len(messages) {
return endBatch(batch)
}
// cant keep reorg lock when catching insertionMutex.
// we have to re-evaluate all messages
// happy cases for confirmed messages:
// 1: were previously in feed. We saved work
// 2: are new (syncing). We wasted very little work.
}
s.insertionMutex.Lock()
defer s.insertionMutex.Unlock()
return s.addMessagesAndEndBatchImpl(pos, messagesAreConfirmed, messagesWithBlockHash, batch)
}
func (s *TransactionStreamer) getPrevPrevDelayedRead(pos arbutil.MessageIndex) (uint64, error) {
if s.snapSyncConfig.Enabled && uint64(pos) == s.snapSyncConfig.PrevBatchMessageCount {
return s.snapSyncConfig.PrevDelayedRead, nil
}
var prevDelayedRead uint64
if pos > 0 {
prevMsg, err := s.GetMessage(pos - 1)
if err != nil {
return 0, fmt.Errorf("failed to get previous message for pos %d: %w", pos, err)
}
prevDelayedRead = prevMsg.DelayedMessagesRead
}
return prevDelayedRead, nil
}
func (s *TransactionStreamer) countDuplicateMessages(
pos arbutil.MessageIndex,
messages []arbostypes.MessageWithMetadataAndBlockHash,
batch *ethdb.Batch,
) (int, bool, *arbostypes.MessageWithMetadata, error) {
curMsg := 0
for {
if len(messages) == curMsg {
break
}
key := dbKey(messagePrefix, uint64(pos))
hasMessage, err := s.db.Has(key)
if err != nil {
return 0, false, nil, err
}
if !hasMessage {
break
}
haveMessage, err := s.db.Get(key)
if err != nil {
return 0, false, nil, err
}
nextMessage := messages[curMsg]
wantMessage, err := rlp.EncodeToBytes(nextMessage.MessageWithMeta)
if err != nil {
return 0, false, nil, err
}
if !bytes.Equal(haveMessage, wantMessage) {
// Current message does not exactly match message in database
var dbMessageParsed arbostypes.MessageWithMetadata
if err := rlp.DecodeBytes(haveMessage, &dbMessageParsed); err != nil {
log.Warn("TransactionStreamer: Reorg detected! (failed parsing db message)",
"pos", pos,
"err", err,
)
return curMsg, true, nil, nil
}
var duplicateMessage bool
if nextMessage.MessageWithMeta.Message != nil {
if dbMessageParsed.Message.BatchGasCost == nil || nextMessage.MessageWithMeta.Message.BatchGasCost == nil {
// Remove both of the batch gas costs and see if the messages still differ
nextMessageCopy := nextMessage.MessageWithMeta
nextMessageCopy.Message = new(arbostypes.L1IncomingMessage)
*nextMessageCopy.Message = *nextMessage.MessageWithMeta.Message
batchGasCostBkup := dbMessageParsed.Message.BatchGasCost
dbMessageParsed.Message.BatchGasCost = nil
nextMessageCopy.Message.BatchGasCost = nil
if reflect.DeepEqual(dbMessageParsed, nextMessageCopy) {
// Actually this isn't a reorg; only the batch gas costs differed
duplicateMessage = true
// If possible - update the message in the database to add the gas cost cache.
if batch != nil && nextMessage.MessageWithMeta.Message.BatchGasCost != nil {
if *batch == nil {
*batch = s.db.NewBatch()
}
if err := s.writeMessage(pos, nextMessage, *batch); err != nil {
return 0, false, nil, err
}
}
}
dbMessageParsed.Message.BatchGasCost = batchGasCostBkup
}
msg := nextMessage.MessageWithMeta.Message
if arbos.IsEspressoMsg(dbMessageParsed.Message) &&
arbos.IsEspressoMsg(msg) &&
!bytes.Equal(msg.L2msg, dbMessageParsed.Message.L2msg) {
// Check to see if the difference is the existence of block merkle proof in the new message,
// The batcher can append this on the fly (see AddEspressoBlockMerkleProof).
// If this is the case, update the database with the block merkle justification.
_, newJst, err := arbos.ParseEspressoMsg(msg)
if err != nil {
return 0, false, nil, err
}
_, oldJst, err := arbos.ParseEspressoMsg(dbMessageParsed.Message)
if err != nil {
return 0, false, nil, err
}
if oldJst.BlockMerkleJustification == nil && newJst.BlockMerkleJustification != nil {
duplicateMessage = true
if batch != nil {
log.Info("Writing new block justification to DB")
if *batch == nil {
*batch = s.db.NewBatch()
}
if err := s.writeMessage(pos, nextMessage, *batch); err != nil {
return 0, false, nil, err
}
}
}
}
}
if !duplicateMessage {
return curMsg, true, &dbMessageParsed, nil
}
}
curMsg++
pos++
}
return curMsg, false, nil, nil
}
func (s *TransactionStreamer) logReorg(pos arbutil.MessageIndex, dbMsg *arbostypes.MessageWithMetadata, newMsg *arbostypes.MessageWithMetadata, confirmed bool) {
sendLog := confirmed
if time.Now().After(s.nextAllowedFeedReorgLog) {
sendLog = true
}
if sendLog {
s.nextAllowedFeedReorgLog = time.Now().Add(time.Minute)
log.Warn("TransactionStreamer: Reorg detected!",
"confirmed", confirmed,
"pos", pos,
"got-delayed", newMsg.DelayedMessagesRead,
"got-header", newMsg.Message.Header,
"db-delayed", dbMsg.DelayedMessagesRead,
"db-header", dbMsg.Message.Header,
)
}
}
func (s *TransactionStreamer) addMessagesAndEndBatchImpl(messageStartPos arbutil.MessageIndex, messagesAreConfirmed bool, messages []arbostypes.MessageWithMetadataAndBlockHash, batch ethdb.Batch) error {
var confirmedReorg bool
var oldMsg *arbostypes.MessageWithMetadata
var lastDelayedRead uint64
var hasNewConfirmedMessages bool
var cacheClearLen int
messagesAfterPos := messageStartPos + arbutil.MessageIndex(len(messages))
broadcastStartPos := arbutil.MessageIndex(atomic.LoadUint64(&s.broadcasterQueuedMessagesPos))
if messagesAreConfirmed {
var duplicates int
var err error
duplicates, confirmedReorg, oldMsg, err = s.countDuplicateMessages(messageStartPos, messages, &batch)
if err != nil {
return err
}
if duplicates > 0 {
lastDelayedRead = messages[duplicates-1].MessageWithMeta.DelayedMessagesRead
messages = messages[duplicates:]
messageStartPos += arbutil.MessageIndex(duplicates)
}
if len(messages) > 0 {
hasNewConfirmedMessages = true
}
}
clearQueueOnSuccess := false
if (s.broadcasterQueuedMessagesActiveReorg && messageStartPos <= broadcastStartPos) ||
(!s.broadcasterQueuedMessagesActiveReorg && broadcastStartPos <= messagesAfterPos) {
// Active broadcast reorg and L1 messages at or before start of broadcast messages
// Or no active broadcast reorg and broadcast messages start before or immediately after last L1 message
if messagesAfterPos >= broadcastStartPos {
broadcastSliceIndex := int(messagesAfterPos - broadcastStartPos)
messagesOldLen := len(messages)
if broadcastSliceIndex < len(s.broadcasterQueuedMessages) {
// Some cached feed messages can be used
messages = append(messages, s.broadcasterQueuedMessages[broadcastSliceIndex:]...)
}
// This calculation gives the exact length of cache which was appended to messages
cacheClearLen = broadcastSliceIndex + len(messages) - messagesOldLen
}
// L1 used or replaced broadcast cache items
clearQueueOnSuccess = true
}
var feedReorg bool
if !hasNewConfirmedMessages {
var duplicates int
var err error
duplicates, feedReorg, oldMsg, err = s.countDuplicateMessages(messageStartPos, messages, nil)
if err != nil {
return err
}
if duplicates > 0 {
lastDelayedRead = messages[duplicates-1].MessageWithMeta.DelayedMessagesRead
messages = messages[duplicates:]
messageStartPos += arbutil.MessageIndex(duplicates)
}
}
if oldMsg != nil {
s.logReorg(messageStartPos, oldMsg, &messages[0].MessageWithMeta, confirmedReorg)
}
if feedReorg {
// Never allow feed to reorg confirmed messages
// Note that any remaining messages must be feed messages, so we're done here
return endBatch(batch)
}
if lastDelayedRead == 0 {
var err error
lastDelayedRead, err = s.getPrevPrevDelayedRead(messageStartPos)
if err != nil {
return err
}
}
// Validate delayed message counts of remaining messages
for i, msg := range messages {
msgPos := messageStartPos + arbutil.MessageIndex(i)
diff := msg.MessageWithMeta.DelayedMessagesRead - lastDelayedRead
if diff != 0 && diff != 1 {
return fmt.Errorf("attempted to insert jump from %v delayed messages read to %v delayed messages read at message index %v", lastDelayedRead, msg.MessageWithMeta.DelayedMessagesRead, msgPos)
}
lastDelayedRead = msg.MessageWithMeta.DelayedMessagesRead
if msg.MessageWithMeta.Message == nil {
return fmt.Errorf("attempted to insert nil message at position %v", msgPos)
}
}
if confirmedReorg {
reorgBatch := s.db.NewBatch()
err := s.reorg(reorgBatch, messageStartPos, messages)
if err != nil {
return err
}
err = reorgBatch.Write()
if err != nil {
return err
}
}
if len(messages) == 0 {
return endBatch(batch)
}
err := s.writeMessages(messageStartPos, messages, batch)
if err != nil {
return err
}
if clearQueueOnSuccess {
// Check if new messages were added at the end of cache, if they were, then dont remove those particular messages
if len(s.broadcasterQueuedMessages) > cacheClearLen {
s.broadcasterQueuedMessages = s.broadcasterQueuedMessages[cacheClearLen:]
atomic.StoreUint64(&s.broadcasterQueuedMessagesPos, uint64(broadcastStartPos)+uint64(cacheClearLen))
} else {
s.broadcasterQueuedMessages = s.broadcasterQueuedMessages[:0]
atomic.StoreUint64(&s.broadcasterQueuedMessagesPos, 0)
}
s.broadcasterQueuedMessagesActiveReorg = false
}
return nil
}
// The caller must hold the insertionMutex
func (s *TransactionStreamer) ExpectChosenSequencer() error {
if s.coordinator != nil {
if !s.coordinator.CurrentlyChosen() {
return fmt.Errorf("%w: not main sequencer", execution.ErrRetrySequencer)
}
}
return nil
}
func (s *TransactionStreamer) WriteMessageFromSequencer(
pos arbutil.MessageIndex,
msgWithMeta arbostypes.MessageWithMetadata,
msgResult execution.MessageResult,
) error {
if err := s.ExpectChosenSequencer(); err != nil {
return err
}
if !s.insertionMutex.TryLock() {
return execution.ErrSequencerInsertLockTaken
}