forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_validator.go
1402 lines (1305 loc) · 46.9 KB
/
block_validator.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 staker
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"regexp"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/spf13/pflag"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/rlp"
"github.com/offchainlabs/nitro/arbnode/resourcemanager"
"github.com/offchainlabs/nitro/arbutil"
"github.com/offchainlabs/nitro/util/containers"
"github.com/offchainlabs/nitro/util/rpcclient"
"github.com/offchainlabs/nitro/util/stopwaiter"
"github.com/offchainlabs/nitro/validator"
"github.com/offchainlabs/nitro/validator/client/redis"
"github.com/offchainlabs/nitro/validator/inputs"
"github.com/offchainlabs/nitro/validator/server_api"
)
var (
validatorPendingValidationsGauge = metrics.NewRegisteredGauge("arb/validator/validations/pending", nil)
validatorValidValidationsCounter = metrics.NewRegisteredCounter("arb/validator/validations/valid", nil)
validatorFailedValidationsCounter = metrics.NewRegisteredCounter("arb/validator/validations/failed", nil)
validatorProfileWaitToRecordHist = metrics.NewRegisteredHistogram("arb/validator/profile/wait_to_record", nil, metrics.NewBoundedHistogramSample())
validatorProfileRecordingHist = metrics.NewRegisteredHistogram("arb/validator/profile/recording", nil, metrics.NewBoundedHistogramSample())
validatorProfileWaitToLaunchHist = metrics.NewRegisteredHistogram("arb/validator/profile/wait_to_launch", nil, metrics.NewBoundedHistogramSample())
validatorProfileLaunchingHist = metrics.NewRegisteredHistogram("arb/validator/profile/launching", nil, metrics.NewBoundedHistogramSample())
validatorProfileRunningHist = metrics.NewRegisteredHistogram("arb/validator/profile/running", nil, metrics.NewBoundedHistogramSample())
validatorMsgCountCurrentBatch = metrics.NewRegisteredGauge("arb/validator/msg_count_current_batch", nil)
validatorMsgCountCreatedGauge = metrics.NewRegisteredGauge("arb/validator/msg_count_created", nil)
validatorMsgCountRecordSentGauge = metrics.NewRegisteredGauge("arb/validator/msg_count_record_sent", nil)
validatorMsgCountValidatedGauge = metrics.NewRegisteredGauge("arb/validator/msg_count_validated", nil)
)
type BlockValidator struct {
stopwaiter.StopWaiter
*StatelessBlockValidator
reorgMutex sync.RWMutex
chainCaughtUp bool
// can only be accessed from creation thread or if holding reorg-write
nextCreateBatch *FullBatchInfo
nextCreateBatchReread bool
prevBatchCache map[uint64][]byte
nextCreateStartGS validator.GoGlobalState
nextCreatePrevDelayed uint64
// can only be accessed from from validation thread or if holding reorg-write
lastValidGS validator.GoGlobalState
valLoopPos arbutil.MessageIndex
legacyValidInfo *legacyLastBlockValidatedDbInfo
// only from logger thread
lastValidInfoPrinted *GlobalStateValidatedInfo
// can be read (atomic.Load) by anyone holding reorg-read
// written (atomic.Set) by appropriate thread or (any way) holding reorg-write
createdA atomic.Uint64
recordSentA atomic.Uint64
validatedA atomic.Uint64
validations containers.SyncMap[arbutil.MessageIndex, *validationStatus]
config BlockValidatorConfigFetcher
createNodesChan chan struct{}
sendRecordChan chan struct{}
progressValidationsChan chan struct{}
chosenValidator map[common.Hash]validator.ValidationSpawner
// wasmModuleRoot
moduleMutex sync.Mutex
currentWasmModuleRoot common.Hash
pendingWasmModuleRoot common.Hash
// for testing only
testingProgressMadeChan chan struct{}
// For troubleshooting failed validations
validationInputsWriter *inputs.Writer
fatalErr chan<- error
MemoryFreeLimitChecker resourcemanager.LimitChecker
}
type BlockValidatorConfig struct {
Enable bool `koanf:"enable"`
RedisValidationClientConfig redis.ValidationClientConfig `koanf:"redis-validation-client-config"`
ValidationServer rpcclient.ClientConfig `koanf:"validation-server" reload:"hot"`
ValidationServerConfigs []rpcclient.ClientConfig `koanf:"validation-server-configs"`
ValidationPoll time.Duration `koanf:"validation-poll" reload:"hot"`
PrerecordedBlocks uint64 `koanf:"prerecorded-blocks" reload:"hot"`
RecordingIterLimit uint64 `koanf:"recording-iter-limit"`
ForwardBlocks uint64 `koanf:"forward-blocks" reload:"hot"`
BatchCacheLimit uint32 `koanf:"batch-cache-limit"`
CurrentModuleRoot string `koanf:"current-module-root"` // TODO(magic) requires reinitialization on hot reload
PendingUpgradeModuleRoot string `koanf:"pending-upgrade-module-root"` // TODO(magic) requires StatelessBlockValidator recreation on hot reload
FailureIsFatal bool `koanf:"failure-is-fatal" reload:"hot"`
Dangerous BlockValidatorDangerousConfig `koanf:"dangerous"`
MemoryFreeLimit string `koanf:"memory-free-limit" reload:"hot"`
ValidationServerConfigsList string `koanf:"validation-server-configs-list"`
// The directory to which the BlockValidator will write the
// block_inputs_<id>.json files when WriteToFile() is called.
BlockInputsFilePath string `koanf:"block-inputs-file-path"`
memoryFreeLimit int
}
func (c *BlockValidatorConfig) Validate() error {
if c.MemoryFreeLimit == "default" {
c.memoryFreeLimit = 1073741824 // 1GB
} else if c.MemoryFreeLimit != "" {
limit, err := resourcemanager.ParseMemLimit(c.MemoryFreeLimit)
if err != nil {
return fmt.Errorf("failed to parse block-validator config memory-free-limit string: %w", err)
}
c.memoryFreeLimit = limit
}
if err := c.RedisValidationClientConfig.Validate(); err != nil {
return fmt.Errorf("failed to validate redis validation client config: %w", err)
}
streamsEnabled := c.RedisValidationClientConfig.Enabled()
if len(c.ValidationServerConfigs) == 0 {
c.ValidationServerConfigs = []rpcclient.ClientConfig{c.ValidationServer}
if c.ValidationServerConfigsList != "default" {
var executionServersConfigs []rpcclient.ClientConfig
if err := json.Unmarshal([]byte(c.ValidationServerConfigsList), &executionServersConfigs); err != nil && !streamsEnabled {
return fmt.Errorf("failed to parse block-validator validation-server-configs-list string: %w", err)
}
c.ValidationServerConfigs = executionServersConfigs
}
}
for i := range c.ValidationServerConfigs {
if err := c.ValidationServerConfigs[i].Validate(); err != nil {
return fmt.Errorf("failed to validate one of the block-validator validation-server-configs. url: %s, err: %w", c.ValidationServerConfigs[i].URL, err)
}
serverUrl := c.ValidationServerConfigs[i].URL
if len(serverUrl) > 0 && serverUrl != "self" && serverUrl != "self-auth" {
u, err := url.Parse(serverUrl)
if err != nil {
return fmt.Errorf("failed parsing validation server's url:%s err: %w", serverUrl, err)
}
if u.Scheme != "ws" && u.Scheme != "wss" {
return fmt.Errorf("validation server's url scheme is unsupported, it should either be ws or wss, url:%s", serverUrl)
}
}
}
return nil
}
type BlockValidatorDangerousConfig struct {
ResetBlockValidation bool `koanf:"reset-block-validation"`
}
type BlockValidatorConfigFetcher func() *BlockValidatorConfig
func BlockValidatorConfigAddOptions(prefix string, f *pflag.FlagSet) {
f.Bool(prefix+".enable", DefaultBlockValidatorConfig.Enable, "enable block-by-block validation")
rpcclient.RPCClientAddOptions(prefix+".validation-server", f, &DefaultBlockValidatorConfig.ValidationServer)
redis.ValidationClientConfigAddOptions(prefix+".redis-validation-client-config", f)
f.String(prefix+".validation-server-configs-list", DefaultBlockValidatorConfig.ValidationServerConfigsList, "array of execution rpc configs given as a json string. time duration should be supplied in number indicating nanoseconds")
f.Duration(prefix+".validation-poll", DefaultBlockValidatorConfig.ValidationPoll, "poll time to check validations")
f.Uint64(prefix+".forward-blocks", DefaultBlockValidatorConfig.ForwardBlocks, "prepare entries for up to that many blocks ahead of validation (stores batch-copy per block)")
f.Uint64(prefix+".prerecorded-blocks", DefaultBlockValidatorConfig.PrerecordedBlocks, "record that many blocks ahead of validation (larger footprint)")
f.Uint32(prefix+".batch-cache-limit", DefaultBlockValidatorConfig.BatchCacheLimit, "limit number of old batches to keep in block-validator")
f.String(prefix+".current-module-root", DefaultBlockValidatorConfig.CurrentModuleRoot, "current wasm module root ('current' read from chain, 'latest' from machines/latest dir, or provide hash)")
f.Uint64(prefix+".recording-iter-limit", DefaultBlockValidatorConfig.RecordingIterLimit, "limit on block recordings sent per iteration")
f.String(prefix+".pending-upgrade-module-root", DefaultBlockValidatorConfig.PendingUpgradeModuleRoot, "pending upgrade wasm module root to additionally validate (hash, 'latest' or empty)")
f.Bool(prefix+".failure-is-fatal", DefaultBlockValidatorConfig.FailureIsFatal, "failing a validation is treated as a fatal error")
BlockValidatorDangerousConfigAddOptions(prefix+".dangerous", f)
f.String(prefix+".memory-free-limit", DefaultBlockValidatorConfig.MemoryFreeLimit, "minimum free-memory limit after reaching which the blockvalidator pauses validation. Enabled by default as 1GB, to disable provide empty string")
f.String(prefix+".block-inputs-file-path", DefaultBlockValidatorConfig.BlockInputsFilePath, "directory to write block validation inputs files")
}
func BlockValidatorDangerousConfigAddOptions(prefix string, f *pflag.FlagSet) {
f.Bool(prefix+".reset-block-validation", DefaultBlockValidatorDangerousConfig.ResetBlockValidation, "resets block-by-block validation, starting again at genesis")
}
var DefaultBlockValidatorConfig = BlockValidatorConfig{
Enable: false,
ValidationServerConfigsList: "default",
ValidationServer: rpcclient.DefaultClientConfig,
RedisValidationClientConfig: redis.DefaultValidationClientConfig,
ValidationPoll: time.Second,
ForwardBlocks: 128,
PrerecordedBlocks: uint64(2 * runtime.NumCPU()),
BatchCacheLimit: 20,
CurrentModuleRoot: "current",
PendingUpgradeModuleRoot: "latest",
FailureIsFatal: true,
Dangerous: DefaultBlockValidatorDangerousConfig,
BlockInputsFilePath: "./target/validation_inputs",
MemoryFreeLimit: "default",
RecordingIterLimit: 20,
}
var TestBlockValidatorConfig = BlockValidatorConfig{
Enable: false,
ValidationServer: rpcclient.TestClientConfig,
ValidationServerConfigs: []rpcclient.ClientConfig{rpcclient.TestClientConfig},
RedisValidationClientConfig: redis.TestValidationClientConfig,
ValidationPoll: 100 * time.Millisecond,
ForwardBlocks: 128,
BatchCacheLimit: 20,
PrerecordedBlocks: uint64(2 * runtime.NumCPU()),
RecordingIterLimit: 20,
CurrentModuleRoot: "latest",
PendingUpgradeModuleRoot: "latest",
FailureIsFatal: true,
Dangerous: DefaultBlockValidatorDangerousConfig,
BlockInputsFilePath: "./target/validation_inputs",
MemoryFreeLimit: "default",
}
var DefaultBlockValidatorDangerousConfig = BlockValidatorDangerousConfig{
ResetBlockValidation: false,
}
type valStatusField uint32
const (
Created valStatusField = iota
RecordSent
RecordFailed
Prepared
SendingValidation
ValidationSent
)
type validationStatus struct {
Status atomic.Uint32 // atomic: value is one of validationStatus*
Cancel func() // non-atomic: only read/written to with reorg mutex
Entry *validationEntry // non-atomic: only read if Status >= validationStatusPrepared
Runs []validator.ValidationRun // if status >= ValidationSent
profileTS int64 // time-stamp for profiling
}
func (s *validationStatus) getStatus() valStatusField {
uintStat := s.Status.Load()
return valStatusField(uintStat)
}
func (s *validationStatus) replaceStatus(old, new valStatusField) bool {
return s.Status.CompareAndSwap(uint32(old), uint32(new))
}
// gets how many miliseconds last step took, and starts measuring a new step
func (s *validationStatus) profileStep() int64 {
start := s.profileTS
s.profileTS = time.Now().UnixMilli()
return s.profileTS - start
}
func NewBlockValidator(
statelessBlockValidator *StatelessBlockValidator,
inbox InboxTrackerInterface,
streamer TransactionStreamerInterface,
config BlockValidatorConfigFetcher,
fatalErr chan<- error,
) (*BlockValidator, error) {
ret := &BlockValidator{
StatelessBlockValidator: statelessBlockValidator,
createNodesChan: make(chan struct{}, 1),
sendRecordChan: make(chan struct{}, 1),
progressValidationsChan: make(chan struct{}, 1),
config: config,
fatalErr: fatalErr,
prevBatchCache: make(map[uint64][]byte),
}
valInputsWriter, err := inputs.NewWriter(
inputs.WithBaseDir(ret.stack.InstanceDir()),
inputs.WithSlug("BlockValidator"))
if err != nil {
return nil, err
}
ret.validationInputsWriter = valInputsWriter
if !config().Dangerous.ResetBlockValidation {
validated, err := ret.ReadLastValidatedInfo()
if err != nil {
return nil, err
}
if validated != nil {
ret.lastValidGS = validated.GlobalState
} else {
legacyInfo, err := ret.legacyReadLastValidatedInfo()
if err != nil {
return nil, err
}
ret.legacyValidInfo = legacyInfo
}
}
// genesis block is impossible to validate unless genesis state is empty
if ret.lastValidGS.Batch == 0 && ret.legacyValidInfo == nil {
genesis, err := streamer.ResultAtCount(1)
if err != nil {
return nil, err
}
ret.lastValidGS = validator.GoGlobalState{
BlockHash: genesis.BlockHash,
SendRoot: genesis.SendRoot,
Batch: 1,
PosInBatch: 0,
}
}
streamer.SetBlockValidator(ret)
inbox.SetBlockValidator(ret)
if config().MemoryFreeLimit != "" {
limtchecker, err := resourcemanager.NewCgroupsMemoryLimitCheckerIfSupported(config().memoryFreeLimit)
if err != nil {
if config().MemoryFreeLimit == "default" {
log.Warn("Cgroups V1 or V2 is unsupported, memory-free-limit feature inside block-validator is disabled")
} else {
return nil, fmt.Errorf("failed to create MemoryFreeLimitChecker, Cgroups V1 or V2 is unsupported")
}
} else {
ret.MemoryFreeLimitChecker = limtchecker
}
}
return ret, nil
}
func atomicStorePos(addr *atomic.Uint64, val arbutil.MessageIndex, metr metrics.Gauge) {
addr.Store(uint64(val))
// #nosec G115
metr.Update(int64(val))
}
func atomicLoadPos(addr *atomic.Uint64) arbutil.MessageIndex {
return arbutil.MessageIndex(addr.Load())
}
func (v *BlockValidator) created() arbutil.MessageIndex {
return atomicLoadPos(&v.createdA)
}
func (v *BlockValidator) recordSent() arbutil.MessageIndex {
return atomicLoadPos(&v.recordSentA)
}
func (v *BlockValidator) validated() arbutil.MessageIndex {
return atomicLoadPos(&v.validatedA)
}
func (v *BlockValidator) Validated(t *testing.T) arbutil.MessageIndex {
return v.validated()
}
func (v *BlockValidator) possiblyFatal(err error) {
if v.Stopped() {
return
}
if err == nil {
return
}
log.Error("Error during validation", "err", err)
if v.config().FailureIsFatal {
select {
case v.fatalErr <- err:
default:
}
}
}
func nonBlockingTrigger(channel chan struct{}) {
select {
case channel <- struct{}{}:
default:
}
}
func (v *BlockValidator) GetModuleRootsToValidate() []common.Hash {
v.moduleMutex.Lock()
defer v.moduleMutex.Unlock()
validatingModuleRoots := []common.Hash{v.currentWasmModuleRoot}
if v.currentWasmModuleRoot != v.pendingWasmModuleRoot && v.pendingWasmModuleRoot != (common.Hash{}) {
validatingModuleRoots = append(validatingModuleRoots, v.pendingWasmModuleRoot)
}
return validatingModuleRoots
}
// called from NewBlockValidator, doesn't need to catch locks
func ReadLastValidatedInfo(db ethdb.Database) (*GlobalStateValidatedInfo, error) {
exists, err := db.Has(lastGlobalStateValidatedInfoKey)
if err != nil {
return nil, err
}
var validated GlobalStateValidatedInfo
if !exists {
return nil, nil
}
gsBytes, err := db.Get(lastGlobalStateValidatedInfoKey)
if err != nil {
return nil, err
}
err = rlp.DecodeBytes(gsBytes, &validated)
if err != nil {
return nil, err
}
return &validated, nil
}
func (v *BlockValidator) ReadLastValidatedInfo() (*GlobalStateValidatedInfo, error) {
return ReadLastValidatedInfo(v.db)
}
func (v *BlockValidator) legacyReadLastValidatedInfo() (*legacyLastBlockValidatedDbInfo, error) {
exists, err := v.db.Has(legacyLastBlockValidatedInfoKey)
if err != nil {
return nil, err
}
var validated legacyLastBlockValidatedDbInfo
if !exists {
return nil, nil
}
gsBytes, err := v.db.Get(legacyLastBlockValidatedInfoKey)
if err != nil {
return nil, err
}
err = rlp.DecodeBytes(gsBytes, &validated)
if err != nil {
return nil, err
}
return &validated, nil
}
var ErrGlobalStateNotInChain = errors.New("globalstate not in chain")
// false if chain not caught up to globalstate
// error is ErrGlobalStateNotInChain if globalstate not in chain (and chain caught up)
func GlobalStateToMsgCount(tracker InboxTrackerInterface, streamer TransactionStreamerInterface, gs validator.GoGlobalState) (bool, arbutil.MessageIndex, error) {
batchCount, err := tracker.GetBatchCount()
if err != nil {
return false, 0, err
}
requiredBatchCount := gs.Batch + 1
if gs.PosInBatch == 0 {
requiredBatchCount -= 1
}
if batchCount < requiredBatchCount {
return false, 0, nil
}
var prevBatchMsgCount arbutil.MessageIndex
if gs.Batch > 0 {
prevBatchMsgCount, err = tracker.GetBatchMessageCount(gs.Batch - 1)
if err != nil {
return false, 0, err
}
}
count := prevBatchMsgCount
if gs.PosInBatch > 0 {
curBatchMsgCount, err := tracker.GetBatchMessageCount(gs.Batch)
if err != nil {
return false, 0, fmt.Errorf("%w: getBatchMsgCount %d batchCount %d", err, gs.Batch, batchCount)
}
count += arbutil.MessageIndex(gs.PosInBatch)
if curBatchMsgCount < count {
return false, 0, fmt.Errorf("%w: batch %d posInBatch %d, maxPosInBatch %d", ErrGlobalStateNotInChain, gs.Batch, gs.PosInBatch, curBatchMsgCount-prevBatchMsgCount)
}
}
processed, err := streamer.GetProcessedMessageCount()
if err != nil {
return false, 0, err
}
if processed < count {
return false, 0, nil
}
res, err := streamer.ResultAtCount(count)
if err != nil {
return false, 0, err
}
if res.BlockHash != gs.BlockHash || res.SendRoot != gs.SendRoot {
return false, 0, fmt.Errorf("%w: count %d hash %v expected %v, sendroot %v expected %v", ErrGlobalStateNotInChain, count, gs.BlockHash, res.BlockHash, gs.SendRoot, res.SendRoot)
}
return true, count, nil
}
func (v *BlockValidator) sendRecord(s *validationStatus) error {
if !v.Started() {
return nil
}
if !s.replaceStatus(Created, RecordSent) {
return fmt.Errorf("failed status check for send record. Status: %v", s.getStatus())
}
validatorProfileWaitToRecordHist.Update(s.profileStep())
v.LaunchThread(func(ctx context.Context) {
err := v.ValidationEntryRecord(ctx, s.Entry)
if ctx.Err() != nil {
return
}
if err != nil {
s.replaceStatus(RecordSent, RecordFailed) // after that - could be removed from validations map
log.Error("Error while recording", "err", err, "status", s.getStatus())
return
}
validatorProfileRecordingHist.Update(s.profileStep())
if !s.replaceStatus(RecordSent, Prepared) {
log.Error("Fault trying to update validation with recording", "entry", s.Entry, "status", s.getStatus())
return
}
nonBlockingTrigger(v.progressValidationsChan)
})
return nil
}
//nolint:gosec
func (v *BlockValidator) writeToFile(validationEntry *validationEntry) error {
input, err := validationEntry.ToInput([]ethdb.WasmTarget{rawdb.TargetWavm})
if err != nil {
return err
}
inputJson := server_api.ValidationInputToJson(input)
if err := v.validationInputsWriter.Write(inputJson); err != nil {
return err
}
return nil
}
func (v *BlockValidator) SetCurrentWasmModuleRoot(hash common.Hash) error {
v.moduleMutex.Lock()
defer v.moduleMutex.Unlock()
if (hash == common.Hash{}) {
return errors.New("trying to set zero as wasmModuleRoot")
}
if hash == v.currentWasmModuleRoot {
return nil
}
if (v.currentWasmModuleRoot == common.Hash{}) {
v.currentWasmModuleRoot = hash
return nil
}
if v.pendingWasmModuleRoot == hash {
log.Info("Block validator: detected progressing to pending machine", "hash", hash)
v.currentWasmModuleRoot = hash
return nil
}
if v.config().CurrentModuleRoot != "current" {
return nil
}
return fmt.Errorf(
"unexpected wasmModuleRoot! cannot validate! found %v , current %v, pending %v",
hash, v.currentWasmModuleRoot, v.pendingWasmModuleRoot,
)
}
func (v *BlockValidator) createNextValidationEntry(ctx context.Context) (bool, error) {
v.reorgMutex.RLock()
defer v.reorgMutex.RUnlock()
pos := v.created()
if pos > v.validated()+arbutil.MessageIndex(v.config().ForwardBlocks) {
log.Trace("create validation entry: nothing to do", "pos", pos, "validated", v.validated())
return false, nil
}
streamerMsgCount, err := v.streamer.GetProcessedMessageCount()
if err != nil {
return false, err
}
if pos >= streamerMsgCount {
log.Trace("create validation entry: nothing to do", "pos", pos, "streamerMsgCount", streamerMsgCount)
return false, nil
}
msg, err := v.streamer.GetMessage(pos)
if err != nil {
return false, err
}
endRes, err := v.streamer.ResultAtCount(pos + 1)
if err != nil {
return false, err
}
if v.nextCreateStartGS.PosInBatch == 0 || v.nextCreateBatchReread {
// new batch
found, fullBatchInfo, err := v.readFullBatch(ctx, v.nextCreateStartGS.Batch)
if !found {
return false, err
}
if v.nextCreateBatch != nil {
v.prevBatchCache[v.nextCreateBatch.Number] = v.nextCreateBatch.PostedData
}
v.nextCreateBatch = fullBatchInfo
// #nosec G115
validatorMsgCountCurrentBatch.Update(int64(fullBatchInfo.MsgCount))
batchCacheLimit := v.config().BatchCacheLimit
if len(v.prevBatchCache) > int(batchCacheLimit) {
for num := range v.prevBatchCache {
if num+uint64(batchCacheLimit) < v.nextCreateStartGS.Batch {
delete(v.prevBatchCache, num)
}
}
}
v.nextCreateBatchReread = false
}
endGS := validator.GoGlobalState{
BlockHash: endRes.BlockHash,
SendRoot: endRes.SendRoot,
}
if pos+1 < v.nextCreateBatch.MsgCount {
endGS.Batch = v.nextCreateStartGS.Batch
endGS.PosInBatch = v.nextCreateStartGS.PosInBatch + 1
} else if pos+1 == v.nextCreateBatch.MsgCount {
endGS.Batch = v.nextCreateStartGS.Batch + 1
endGS.PosInBatch = 0
} else {
return false, fmt.Errorf("illegal batch msg count %d pos %d batch %d", v.nextCreateBatch.MsgCount, pos, endGS.Batch)
}
chainConfig := v.streamer.ChainConfig()
prevBatchNums, err := msg.Message.PastBatchesRequired()
if err != nil {
return false, err
}
prevBatches := make([]validator.BatchInfo, 0, len(prevBatchNums))
// prevBatchNums are only used for batch reports, each is only used once
for _, batchNum := range prevBatchNums {
data, found := v.prevBatchCache[batchNum]
if found {
delete(v.prevBatchCache, batchNum)
} else {
data, err = v.readPostedBatch(ctx, batchNum)
if err != nil {
return false, err
}
}
prevBatches = append(prevBatches, validator.BatchInfo{
Number: batchNum,
Data: data,
})
}
entry, err := newValidationEntry(
pos, v.nextCreateStartGS, endGS, msg, v.nextCreateBatch, prevBatches, v.nextCreatePrevDelayed, chainConfig,
)
if err != nil {
return false, err
}
status := &validationStatus{
Entry: entry,
profileTS: time.Now().UnixMilli(),
}
status.Status.Store(uint32(Created))
v.validations.Store(pos, status)
v.nextCreateStartGS = endGS
v.nextCreatePrevDelayed = msg.DelayedMessagesRead
atomicStorePos(&v.createdA, pos+1, validatorMsgCountCreatedGauge)
log.Trace("create validation entry: created", "pos", pos)
return true, nil
}
func (v *BlockValidator) iterativeValidationEntryCreator(ctx context.Context, ignored struct{}) time.Duration {
moreWork, err := v.createNextValidationEntry(ctx)
if err != nil {
processed, processedErr := v.streamer.GetProcessedMessageCount()
log.Error("error trying to create validation node", "err", err, "created", v.created()+1, "processed", processed, "processedErr", processedErr)
}
if moreWork {
return 0
}
return v.config().ValidationPoll
}
func (v *BlockValidator) isMemoryLimitExceeded() bool {
if v.MemoryFreeLimitChecker == nil {
return false
}
exceeded, err := v.MemoryFreeLimitChecker.IsLimitExceeded()
if err != nil {
log.Error("error checking if free-memory limit exceeded using MemoryFreeLimitChecker", "err", err)
}
return exceeded
}
func (v *BlockValidator) sendNextRecordRequests(ctx context.Context) (bool, error) {
if v.isMemoryLimitExceeded() {
log.Warn("sendNextRecordRequests: aborting due to running low on memory")
return false, nil
}
v.reorgMutex.RLock()
pos := v.recordSent()
created := v.created()
validated := v.validated()
v.reorgMutex.RUnlock()
recordUntil := validated + arbutil.MessageIndex(v.config().PrerecordedBlocks) - 1
if recordUntil > created-1 {
recordUntil = created - 1
}
if recordUntil < pos {
return false, nil
}
recordUntilLimit := pos + arbutil.MessageIndex(v.config().RecordingIterLimit)
if recordUntil > recordUntilLimit {
recordUntil = recordUntilLimit
}
log.Trace("preparing to record", "pos", pos, "until", recordUntil)
// prepare could take a long time so we do it without a lock
err := v.recorder.PrepareForRecord(ctx, pos, recordUntil)
if err != nil {
return false, err
}
v.reorgMutex.RLock()
defer v.reorgMutex.RUnlock()
createdNew := v.created()
recordSentNew := v.recordSent()
if createdNew < created || recordSentNew < pos {
// there was a relevant reorg - quit and restart
return true, nil
}
for pos <= recordUntil {
if v.isMemoryLimitExceeded() {
log.Warn("sendNextRecordRequests: aborting due to running low on memory")
return false, nil
}
validationStatus, found := v.validations.Load(pos)
if !found {
return false, fmt.Errorf("not found entry for pos %d", pos)
}
currentStatus := validationStatus.getStatus()
if currentStatus != Created {
return false, fmt.Errorf("bad status trying to send recordings for pos %d status: %v", pos, currentStatus)
}
err := v.sendRecord(validationStatus)
if err != nil {
return false, err
}
pos += 1
atomicStorePos(&v.recordSentA, pos, validatorMsgCountRecordSentGauge)
log.Trace("next record request: sent", "pos", pos)
}
return true, nil
}
func (v *BlockValidator) iterativeValidationEntryRecorder(ctx context.Context, ignored struct{}) time.Duration {
moreWork, err := v.sendNextRecordRequests(ctx)
if err != nil {
log.Error("error trying to record for validation node", "err", err)
}
if moreWork {
return 0
}
return v.config().ValidationPoll
}
func (v *BlockValidator) iterativeValidationPrint(ctx context.Context) time.Duration {
validated, err := v.ReadLastValidatedInfo()
if err != nil {
log.Error("cannot read last validated data from database", "err", err)
return time.Second * 30
}
if validated == nil {
return time.Second
}
if v.lastValidInfoPrinted != nil {
if v.lastValidInfoPrinted.GlobalState.BlockHash == validated.GlobalState.BlockHash {
return time.Second
}
}
var batchMsgs arbutil.MessageIndex
var printedCount int64
if validated.GlobalState.Batch > 0 {
batchMsgs, err = v.inboxTracker.GetBatchMessageCount(validated.GlobalState.Batch - 1)
}
if err != nil {
printedCount = -1
} else {
// #nosec G115
printedCount = int64(batchMsgs) + int64(validated.GlobalState.PosInBatch)
}
log.Info("validated execution", "messageCount", printedCount, "globalstate", validated.GlobalState, "WasmRoots", validated.WasmRoots)
v.lastValidInfoPrinted = validated
return time.Second
}
// return val:
// *MessageIndex - pointer to bad entry if there is one (requires reorg)
func (v *BlockValidator) advanceValidations(ctx context.Context) (*arbutil.MessageIndex, error) {
v.reorgMutex.RLock()
defer v.reorgMutex.RUnlock()
wasmRoots := v.GetModuleRootsToValidate()
pos := v.validated() - 1 // to reverse the first +1 in the loop
validationsLoop:
for {
if ctx.Err() != nil {
return nil, ctx.Err()
}
v.valLoopPos = pos + 1
v.reorgMutex.RUnlock()
v.reorgMutex.RLock()
pos = v.valLoopPos
if pos >= v.recordSent() {
log.Trace("advanceValidations: nothing to validate", "pos", pos)
return nil, nil
}
validationStatus, found := v.validations.Load(pos)
if !found {
return nil, fmt.Errorf("not found entry for pos %d", pos)
}
currentStatus := validationStatus.getStatus()
if currentStatus == RecordFailed {
// retry
log.Warn("Recording for validation failed, retrying..", "pos", pos)
return &pos, nil
}
if currentStatus == ValidationSent && pos == v.validated() {
if validationStatus.Entry.Start != v.lastValidGS {
log.Warn("Validation entry has wrong start state", "pos", pos, "start", validationStatus.Entry.Start, "expected", v.lastValidGS)
validationStatus.Cancel()
return &pos, nil
}
var wasmRoots []common.Hash
for i, run := range validationStatus.Runs {
if !run.Ready() {
log.Trace("advanceValidations: validation not ready", "pos", pos, "run", i)
continue validationsLoop
}
wasmRoots = append(wasmRoots, run.WasmModuleRoot())
runEnd, err := run.Current()
if err == nil && runEnd != validationStatus.Entry.End {
err = fmt.Errorf("validation failed: expected %v got %v", validationStatus.Entry.End, runEnd)
writeErr := v.writeToFile(validationStatus.Entry)
if writeErr != nil {
log.Warn("failed to write debug results file", "err", writeErr)
}
}
if err != nil {
validatorFailedValidationsCounter.Inc(1)
v.possiblyFatal(err)
return &pos, nil // if not fatal - retry
}
validatorValidValidationsCounter.Inc(1)
}
err := v.writeLastValidated(validationStatus.Entry.End, wasmRoots)
if err != nil {
log.Error("failed writing new validated to database", "pos", pos, "err", err)
}
go v.recorder.MarkValid(pos, v.lastValidGS.BlockHash)
atomicStorePos(&v.validatedA, pos+1, validatorMsgCountValidatedGauge)
v.validations.Delete(pos)
nonBlockingTrigger(v.createNodesChan)
nonBlockingTrigger(v.sendRecordChan)
if v.testingProgressMadeChan != nil {
nonBlockingTrigger(v.testingProgressMadeChan)
}
log.Trace("result validated", "count", v.validated(), "blockHash", v.lastValidGS.BlockHash)
continue
}
for _, moduleRoot := range wasmRoots {
spawner := v.chosenValidator[moduleRoot]
if spawner == nil {
notFoundErr := fmt.Errorf("did not find spawner for moduleRoot :%v", moduleRoot)
v.possiblyFatal(notFoundErr)
return nil, notFoundErr
}
if spawner.Room() == 0 {
log.Trace("advanceValidations: no more room", "moduleRoot", moduleRoot)
return nil, nil
}
}
if v.isMemoryLimitExceeded() {
log.Warn("advanceValidations: aborting due to running low on memory")
return nil, nil
}
if currentStatus == Prepared {
replaced := validationStatus.replaceStatus(Prepared, SendingValidation)
if !replaced {
v.possiblyFatal(errors.New("failed to set SendingValidation status"))
}
validatorProfileWaitToLaunchHist.Update(validationStatus.profileStep())
validatorPendingValidationsGauge.Inc(1)
var runs []validator.ValidationRun
for _, moduleRoot := range wasmRoots {
spawner := v.chosenValidator[moduleRoot]
input, err := validationStatus.Entry.ToInput(spawner.StylusArchs())
if err != nil && ctx.Err() == nil {
v.possiblyFatal(fmt.Errorf("%w: error preparing validation", err))
continue
}
if ctx.Err() != nil {
return nil, ctx.Err()
}
run := spawner.Launch(input, moduleRoot)
log.Trace("advanceValidations: launched", "pos", validationStatus.Entry.Pos, "moduleRoot", moduleRoot)
runs = append(runs, run)
}
validatorProfileLaunchingHist.Update(validationStatus.profileStep())
validationCtx, cancel := context.WithCancel(ctx)
validationStatus.Runs = runs
validationStatus.Cancel = cancel
v.LaunchUntrackedThread(func() {
defer validatorPendingValidationsGauge.Dec(1)
defer cancel()
startTsMilli := validationStatus.profileTS
replaced = validationStatus.replaceStatus(SendingValidation, ValidationSent)
if !replaced {
v.possiblyFatal(errors.New("failed to set status to ValidationSent"))
}
// validationStatus might be removed from under us
// trigger validation progress when done
for _, run := range runs {
_, err := run.Await(validationCtx)
if err != nil {
return
}
}
validatorProfileRunningHist.Update(time.Now().UnixMilli() - startTsMilli)
nonBlockingTrigger(v.progressValidationsChan)
})
}
}
}
func (v *BlockValidator) iterativeValidationProgress(ctx context.Context, ignored struct{}) time.Duration {
reorg, err := v.advanceValidations(ctx)
if err != nil {
log.Error("error trying to record for validation node", "err", err)
} else if reorg != nil {
err := v.Reorg(ctx, *reorg)
if err != nil {
log.Error("error trying to reorg validation", "pos", *reorg-1, "err", err)
v.possiblyFatal(err)
}
}
return v.config().ValidationPoll
}
var ErrValidationCanceled = errors.New("validation of block cancelled")
func (v *BlockValidator) writeLastValidated(gs validator.GoGlobalState, wasmRoots []common.Hash) error {
v.lastValidGS = gs
info := GlobalStateValidatedInfo{
GlobalState: gs,
WasmRoots: wasmRoots,
}
encoded, err := rlp.EncodeToBytes(info)
if err != nil {
return err
}
err = v.db.Put(lastGlobalStateValidatedInfoKey, encoded)
if err != nil {
return err
}
return nil
}
func (v *BlockValidator) validGSIsNew(globalState validator.GoGlobalState) bool {
if v.legacyValidInfo != nil {
if v.legacyValidInfo.AfterPosition.BatchNumber > globalState.Batch {
return false
}
if v.legacyValidInfo.AfterPosition.BatchNumber == globalState.Batch && v.legacyValidInfo.AfterPosition.PosInBatch >= globalState.PosInBatch {
return false
}
return true
}
if v.lastValidGS.Batch > globalState.Batch {
return false
}
if v.lastValidGS.Batch == globalState.Batch && v.lastValidGS.PosInBatch >= globalState.PosInBatch {
return false
}
return true
}
// this accepts globalstate even if not caught up
func (v *BlockValidator) InitAssumeValid(globalState validator.GoGlobalState) error {
if v.Started() {
return fmt.Errorf("cannot handle InitAssumeValid while running")
}
// don't do anything if we already validated past that
if !v.validGSIsNew(globalState) {
return nil
}
v.legacyValidInfo = nil