forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaker.go
1234 lines (1153 loc) · 44.3 KB
/
staker.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"
"errors"
"fmt"
"math/big"
"runtime/debug"
"strings"
"time"
"github.com/google/btree"
flag "github.com/spf13/pflag"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/rpc"
"github.com/offchainlabs/nitro/arbnode/dataposter"
"github.com/offchainlabs/nitro/arbutil"
"github.com/offchainlabs/nitro/cmd/genericconf"
"github.com/offchainlabs/nitro/solgen/go/rollupgen"
"github.com/offchainlabs/nitro/staker/txbuilder"
"github.com/offchainlabs/nitro/util"
"github.com/offchainlabs/nitro/util/arbmath"
"github.com/offchainlabs/nitro/util/headerreader"
"github.com/offchainlabs/nitro/util/stopwaiter"
"github.com/offchainlabs/nitro/validator"
)
var (
stakerBalanceGauge = metrics.NewRegisteredGaugeFloat64("arb/staker/balance", nil)
stakerAmountStakedGauge = metrics.NewRegisteredGauge("arb/staker/amount_staked", nil)
stakerLatestStakedNodeGauge = metrics.NewRegisteredGauge("arb/staker/staked_node", nil)
stakerLatestConfirmedNodeGauge = metrics.NewRegisteredGauge("arb/staker/confirmed_node", nil)
stakerLastSuccessfulActionGauge = metrics.NewRegisteredGauge("arb/staker/action/last_success", nil)
stakerActionSuccessCounter = metrics.NewRegisteredCounter("arb/staker/action/success", nil)
stakerActionFailureCounter = metrics.NewRegisteredCounter("arb/staker/action/failure", nil)
validatorGasRefunderBalance = metrics.NewRegisteredGaugeFloat64("arb/validator/gasrefunder/balanceether", nil)
)
type StakerStrategy uint8
const (
// Watchtower: don't do anything on L1, but log if there's a bad assertion
WatchtowerStrategy StakerStrategy = iota
// Defensive: stake if there's a bad assertion
DefensiveStrategy
// Stake latest: stay staked on the latest node, challenging bad assertions
StakeLatestStrategy
// Resolve nodes: stay staked on the latest node and resolve any unconfirmed nodes, challenging bad assertions
ResolveNodesStrategy
// Make nodes: continually create new nodes, challenging bad assertions
MakeNodesStrategy
)
type L1PostingStrategy struct {
HighGasThreshold float64 `koanf:"high-gas-threshold"`
HighGasDelayBlocks int64 `koanf:"high-gas-delay-blocks"`
}
var DefaultL1PostingStrategy = L1PostingStrategy{
HighGasThreshold: 0,
HighGasDelayBlocks: 0,
}
func L1PostingStrategyAddOptions(prefix string, f *flag.FlagSet) {
f.Float64(prefix+".high-gas-threshold", DefaultL1PostingStrategy.HighGasThreshold, "high gas threshold")
f.Int64(prefix+".high-gas-delay-blocks", DefaultL1PostingStrategy.HighGasDelayBlocks, "high gas delay blocks")
}
type L1ValidatorConfig struct {
Enable bool `koanf:"enable"`
Strategy string `koanf:"strategy"`
StakerInterval time.Duration `koanf:"staker-interval"`
MakeAssertionInterval time.Duration `koanf:"make-assertion-interval"`
PostingStrategy L1PostingStrategy `koanf:"posting-strategy"`
DisableChallenge bool `koanf:"disable-challenge"`
ConfirmationBlocks int64 `koanf:"confirmation-blocks"`
UseSmartContractWallet bool `koanf:"use-smart-contract-wallet"`
OnlyCreateWalletContract bool `koanf:"only-create-wallet-contract"`
StartValidationFromStaked bool `koanf:"start-validation-from-staked"`
ContractWalletAddress string `koanf:"contract-wallet-address"`
GasRefunderAddress string `koanf:"gas-refunder-address"`
DataPoster dataposter.DataPosterConfig `koanf:"data-poster" reload:"hot"`
RedisUrl string `koanf:"redis-url"`
ExtraGas uint64 `koanf:"extra-gas" reload:"hot"`
Dangerous DangerousConfig `koanf:"dangerous"`
ParentChainWallet genericconf.WalletConfig `koanf:"parent-chain-wallet"`
LogQueryBatchSize uint64 `koanf:"log-query-batch-size" reload:"hot"`
EnableFastConfirmation bool `koanf:"enable-fast-confirmation"`
strategy StakerStrategy
gasRefunder common.Address
}
func (c *L1ValidatorConfig) ParseStrategy() (StakerStrategy, error) {
switch strings.ToLower(c.Strategy) {
case "watchtower":
return WatchtowerStrategy, nil
case "defensive":
return DefensiveStrategy, nil
case "stakelatest":
return StakeLatestStrategy, nil
case "resolvenodes":
return ResolveNodesStrategy, nil
case "makenodes":
return MakeNodesStrategy, nil
default:
return WatchtowerStrategy, fmt.Errorf("unknown staker strategy \"%v\"", c.Strategy)
}
}
func (c *L1ValidatorConfig) ValidatorRequired() bool {
if !c.Enable {
return false
}
if c.Dangerous.WithoutBlockValidator {
return false
}
if c.strategy == WatchtowerStrategy && !c.EnableFastConfirmation {
return false
}
return true
}
func (c *L1ValidatorConfig) Validate() error {
strategy, err := c.ParseStrategy()
if err != nil {
return err
}
c.strategy = strategy
if len(c.GasRefunderAddress) > 0 && !common.IsHexAddress(c.GasRefunderAddress) {
return errors.New("invalid validator gas refunder address")
}
c.gasRefunder = common.HexToAddress(c.GasRefunderAddress)
return nil
}
type L1ValidatorConfigFetcher func() *L1ValidatorConfig
var DefaultL1ValidatorConfig = L1ValidatorConfig{
Enable: true,
Strategy: "Watchtower",
StakerInterval: time.Minute,
MakeAssertionInterval: time.Hour,
PostingStrategy: L1PostingStrategy{},
DisableChallenge: false,
ConfirmationBlocks: 12,
UseSmartContractWallet: false,
OnlyCreateWalletContract: false,
StartValidationFromStaked: true,
ContractWalletAddress: "",
GasRefunderAddress: "",
DataPoster: dataposter.DefaultDataPosterConfigForValidator,
RedisUrl: "",
ExtraGas: 50000,
Dangerous: DefaultDangerousConfig,
ParentChainWallet: DefaultValidatorL1WalletConfig,
LogQueryBatchSize: 0,
EnableFastConfirmation: false,
}
var TestL1ValidatorConfig = L1ValidatorConfig{
Enable: true,
Strategy: "Watchtower",
StakerInterval: time.Millisecond * 10,
MakeAssertionInterval: -time.Hour * 1000,
PostingStrategy: L1PostingStrategy{},
DisableChallenge: false,
ConfirmationBlocks: 0,
UseSmartContractWallet: false,
OnlyCreateWalletContract: false,
StartValidationFromStaked: true,
ContractWalletAddress: "",
GasRefunderAddress: "",
DataPoster: dataposter.TestDataPosterConfigForValidator,
RedisUrl: "",
ExtraGas: 50000,
Dangerous: DefaultDangerousConfig,
ParentChainWallet: DefaultValidatorL1WalletConfig,
LogQueryBatchSize: 0,
EnableFastConfirmation: false,
}
var DefaultValidatorL1WalletConfig = genericconf.WalletConfig{
Pathname: "validator-wallet",
Password: genericconf.WalletConfigDefault.Password,
PrivateKey: genericconf.WalletConfigDefault.PrivateKey,
Account: genericconf.WalletConfigDefault.Account,
OnlyCreateKey: genericconf.WalletConfigDefault.OnlyCreateKey,
}
func L1ValidatorConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Bool(prefix+".enable", DefaultL1ValidatorConfig.Enable, "enable validator")
f.String(prefix+".strategy", DefaultL1ValidatorConfig.Strategy, "L1 validator strategy, either watchtower, defensive, stakeLatest, or makeNodes")
f.Duration(prefix+".staker-interval", DefaultL1ValidatorConfig.StakerInterval, "how often the L1 validator should check the status of the L1 rollup and maybe take action with its stake")
f.Duration(prefix+".make-assertion-interval", DefaultL1ValidatorConfig.MakeAssertionInterval, "if configured with the makeNodes strategy, how often to create new assertions (bypassed in case of a dispute)")
L1PostingStrategyAddOptions(prefix+".posting-strategy", f)
f.Bool(prefix+".disable-challenge", DefaultL1ValidatorConfig.DisableChallenge, "disable validator challenge")
f.Int64(prefix+".confirmation-blocks", DefaultL1ValidatorConfig.ConfirmationBlocks, "confirmation blocks")
f.Bool(prefix+".use-smart-contract-wallet", DefaultL1ValidatorConfig.UseSmartContractWallet, "use a smart contract wallet instead of an EOA address")
f.Bool(prefix+".only-create-wallet-contract", DefaultL1ValidatorConfig.OnlyCreateWalletContract, "only create smart wallet contract and exit")
f.Bool(prefix+".start-validation-from-staked", DefaultL1ValidatorConfig.StartValidationFromStaked, "assume staked nodes are valid")
f.String(prefix+".contract-wallet-address", DefaultL1ValidatorConfig.ContractWalletAddress, "validator smart contract wallet public address")
f.String(prefix+".gas-refunder-address", DefaultL1ValidatorConfig.GasRefunderAddress, "The gas refunder contract address (optional)")
f.String(prefix+".redis-url", DefaultL1ValidatorConfig.RedisUrl, "redis url for L1 validator")
f.Uint64(prefix+".extra-gas", DefaultL1ValidatorConfig.ExtraGas, "use this much more gas than estimation says is necessary to post transactions")
f.Uint64(prefix+".log-query-batch-size", DefaultL1ValidatorConfig.LogQueryBatchSize, "range ro query from eth_getLogs")
dataposter.DataPosterConfigAddOptions(prefix+".data-poster", f, dataposter.DefaultDataPosterConfigForValidator)
DangerousConfigAddOptions(prefix+".dangerous", f)
genericconf.WalletConfigAddOptions(prefix+".parent-chain-wallet", f, DefaultL1ValidatorConfig.ParentChainWallet.Pathname)
f.Bool(prefix+".enable-fast-confirmation", DefaultL1ValidatorConfig.EnableFastConfirmation, "enable fast confirmation")
}
type DangerousConfig struct {
IgnoreRollupWasmModuleRoot bool `koanf:"ignore-rollup-wasm-module-root"`
WithoutBlockValidator bool `koanf:"without-block-validator"`
}
var DefaultDangerousConfig = DangerousConfig{
IgnoreRollupWasmModuleRoot: false,
WithoutBlockValidator: false,
}
func DangerousConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Bool(prefix+".ignore-rollup-wasm-module-root", DefaultL1ValidatorConfig.Dangerous.IgnoreRollupWasmModuleRoot, "DANGEROUS! make assertions even when the wasm module root is wrong")
f.Bool(prefix+".without-block-validator", DefaultL1ValidatorConfig.Dangerous.WithoutBlockValidator, "DANGEROUS! allows running an L1 validator without a block validator")
}
type nodeAndHash struct {
id uint64
hash common.Hash
}
type LatestStakedNotifier interface {
UpdateLatestStaked(count arbutil.MessageIndex, globalState validator.GoGlobalState)
}
type LatestConfirmedNotifier interface {
UpdateLatestConfirmed(count arbutil.MessageIndex, globalState validator.GoGlobalState)
}
type validatedNode struct {
number uint64
hash common.Hash
}
type Staker struct {
*L1Validator
stopwaiter.StopWaiter
l1Reader *headerreader.HeaderReader
stakedNotifiers []LatestStakedNotifier
confirmedNotifiers []LatestConfirmedNotifier
activeChallenge *ChallengeManager
baseCallOpts bind.CallOpts
config L1ValidatorConfigFetcher
highGasBlocksBuffer *big.Int
lastActCalledBlock *big.Int
inactiveLastCheckedNode *nodeAndHash
inactiveValidatedNodes *btree.BTreeG[validatedNode]
bringActiveUntilNode uint64
inboxReader InboxReaderInterface
statelessBlockValidator *StatelessBlockValidator
fatalErr chan<- error
fastConfirmSafe *FastConfirmSafe
}
type ValidatorWalletInterface interface {
Initialize(context.Context) error
// Address must be able to be called concurrently with other functions
Address() *common.Address
// Address must be able to be called concurrently with other functions
AddressOrZero() common.Address
TxSenderAddress() *common.Address
RollupAddress() common.Address
ChallengeManagerAddress() common.Address
L1Client() *ethclient.Client
TestTransactions(context.Context, []*types.Transaction) error
ExecuteTransactions(context.Context, *txbuilder.Builder, common.Address) (*types.Transaction, error)
TimeoutChallenges(context.Context, []uint64) (*types.Transaction, error)
CanBatchTxs() bool
AuthIfEoa() *bind.TransactOpts
Start(context.Context)
StopAndWait()
// May be nil
DataPoster() *dataposter.DataPoster
}
func NewStaker(
l1Reader *headerreader.HeaderReader,
wallet ValidatorWalletInterface,
callOpts bind.CallOpts,
config L1ValidatorConfigFetcher,
blockValidator *BlockValidator,
statelessBlockValidator *StatelessBlockValidator,
stakedNotifiers []LatestStakedNotifier,
confirmedNotifiers []LatestConfirmedNotifier,
validatorUtilsAddress common.Address,
fatalErr chan<- error,
) (*Staker, error) {
if err := config().Validate(); err != nil {
return nil, err
}
client := l1Reader.Client()
val, err := NewL1Validator(client, wallet, validatorUtilsAddress, callOpts,
statelessBlockValidator.inboxTracker, statelessBlockValidator.streamer, blockValidator)
if err != nil {
return nil, err
}
stakerLastSuccessfulActionGauge.Update(time.Now().Unix())
if config().StartValidationFromStaked && blockValidator != nil {
stakedNotifiers = append(stakedNotifiers, blockValidator)
}
inactiveValidatedNodes := btree.NewG(2, func(a, b validatedNode) bool {
return a.number < b.number || (a.number == b.number && a.hash.Cmp(b.hash) < 0)
})
return &Staker{
L1Validator: val,
l1Reader: l1Reader,
stakedNotifiers: stakedNotifiers,
confirmedNotifiers: confirmedNotifiers,
baseCallOpts: callOpts,
config: config,
highGasBlocksBuffer: big.NewInt(config().PostingStrategy.HighGasDelayBlocks),
lastActCalledBlock: nil,
inboxReader: statelessBlockValidator.inboxReader,
statelessBlockValidator: statelessBlockValidator,
fatalErr: fatalErr,
inactiveValidatedNodes: inactiveValidatedNodes,
}, nil
}
func (s *Staker) Initialize(ctx context.Context) error {
err := s.L1Validator.Initialize(ctx)
if err != nil {
return err
}
walletAddressOrZero := s.wallet.AddressOrZero()
if walletAddressOrZero != (common.Address{}) {
s.updateStakerBalanceMetric(ctx)
}
if s.blockValidator != nil && s.config().StartValidationFromStaked {
latestStaked, _, err := s.validatorUtils.LatestStaked(&s.baseCallOpts, s.rollupAddress, walletAddressOrZero)
if err != nil {
return err
}
// #nosec G115
stakerLatestStakedNodeGauge.Update(int64(latestStaked))
if latestStaked == 0 {
return nil
}
stakedInfo, err := s.rollup.LookupNode(ctx, latestStaked)
if err != nil {
return err
}
err = s.blockValidator.InitAssumeValid(stakedInfo.AfterState().GlobalState)
if err != nil {
return err
}
}
return s.setupFastConfirmation(ctx)
}
// setupFastConfirmation sets the enableFastConfirmation and fastConfirmSafe variables of staker
// based on the config, the wallet address, and the on-chain rollup designated fast confirmer.
// Before this function, both variables should be their default (i.e. fast confirmation is disabled).
func (s *Staker) setupFastConfirmation(ctx context.Context) error {
cfg := s.config()
if !cfg.EnableFastConfirmation {
return nil
}
if s.wallet.Address() == nil {
return errors.New("fast confirmation requires wallet setup")
}
walletAddress := *s.wallet.Address()
client := s.l1Reader.Client()
rollup, err := rollupgen.NewRollupUserLogic(s.rollupAddress, client)
if err != nil {
return err
}
callOpts := s.getCallOpts(ctx)
fastConfirmer, err := rollup.AnyTrustFastConfirmer(callOpts)
if err != nil {
return fmt.Errorf("getting rollup fast confirmer address: %w", err)
}
log.Info("Setting up fast confirmation", "wallet", walletAddress, "fastConfirmer", fastConfirmer)
if fastConfirmer == walletAddress {
// We can directly fast confirm nodes
return nil
} else if fastConfirmer == (common.Address{}) {
// No fast confirmer enabled
return errors.New("fast confirmation enabled in config, but no fast confirmer set in rollup contract")
}
// The fast confirmer address is a contract address, not sure if it's a safe contract yet.
fastConfirmSafe, err := NewFastConfirmSafe(
callOpts,
fastConfirmer,
s.builder,
s.wallet,
cfg.gasRefunder,
s.l1Reader,
)
if err != nil {
// Unknown while loading the safe contract.
return fmt.Errorf("loading fast confirm safe: %w", err)
}
// Fast confirmer address implements getOwners() and is probably a safe.
isOwner, err := fastConfirmSafe.safe.IsOwner(callOpts, walletAddress)
if err != nil {
return fmt.Errorf("checking if wallet is owner of safe: %w", err)
}
if !isOwner {
return fmt.Errorf("staker wallet address %v is not an owner of the fast confirm safe %v", walletAddress, fastConfirmer)
}
s.fastConfirmSafe = fastConfirmSafe
return nil
}
func (s *Staker) tryFastConfirmationNodeNumber(ctx context.Context, number uint64, hash common.Hash) error {
if !s.config().EnableFastConfirmation {
return nil
}
nodeInfo, err := s.rollup.LookupNode(ctx, number)
if err != nil {
return err
}
return s.tryFastConfirmation(ctx, nodeInfo.AfterState().GlobalState.BlockHash, nodeInfo.AfterState().GlobalState.SendRoot, hash)
}
func (s *Staker) tryFastConfirmation(ctx context.Context, blockHash common.Hash, sendRoot common.Hash, nodeHash common.Hash) error {
if !s.config().EnableFastConfirmation {
return nil
}
if s.fastConfirmSafe != nil {
return s.fastConfirmSafe.tryFastConfirmation(ctx, blockHash, sendRoot, nodeHash)
}
auth, err := s.builder.Auth(ctx)
if err != nil {
return err
}
log.Info("Fast confirming node with wallet", "wallet", auth.From, "nodeHash", nodeHash)
_, err = s.rollup.FastConfirmNextNode(auth, blockHash, sendRoot, nodeHash)
return err
}
func (s *Staker) getLatestStakedState(ctx context.Context, staker common.Address) (uint64, arbutil.MessageIndex, *validator.GoGlobalState, error) {
callOpts := s.getCallOpts(ctx)
if s.l1Reader.UseFinalityData() {
callOpts.BlockNumber = big.NewInt(int64(rpc.FinalizedBlockNumber))
}
latestStaked, _, err := s.validatorUtils.LatestStaked(s.getCallOpts(ctx), s.rollupAddress, staker)
if err != nil {
return 0, 0, nil, fmt.Errorf("couldn't get LatestStaked(%v): %w", staker, err)
}
if latestStaked == 0 {
return latestStaked, 0, nil, nil
}
stakedInfo, err := s.rollup.LookupNode(ctx, latestStaked)
if err != nil {
return 0, 0, nil, fmt.Errorf("couldn't look up latest assertion of %v (%v): %w", staker, latestStaked, err)
}
globalState := stakedInfo.AfterState().GlobalState
caughtUp, count, err := GlobalStateToMsgCount(s.inboxTracker, s.txStreamer, globalState)
if err != nil {
if errors.Is(err, ErrGlobalStateNotInChain) && s.fatalErr != nil {
fatal := fmt.Errorf("latest assertion of %v (%v) not in chain: %w", staker, latestStaked, err)
s.fatalErr <- fatal
}
return 0, 0, nil, fmt.Errorf("latest assertion of %v (%v): %w", staker, latestStaked, err)
}
if !caughtUp {
log.Info("latest assertion not yet in our node", "staker", staker, "assertion", latestStaked, "state", globalState)
return latestStaked, 0, nil, nil
}
processedCount, err := s.txStreamer.GetProcessedMessageCount()
if err != nil {
return 0, 0, nil, err
}
if processedCount < count {
log.Info("execution catching up to rollup", "staker", staker, "rollupCount", count, "processedCount", processedCount)
return latestStaked, 0, nil, nil
}
return latestStaked, count, &globalState, nil
}
func (s *Staker) StopAndWait() {
s.StopWaiter.StopAndWait()
if s.Strategy() != WatchtowerStrategy {
s.wallet.StopAndWait()
}
}
func (s *Staker) Start(ctxIn context.Context) {
if s.Strategy() != WatchtowerStrategy {
s.wallet.Start(ctxIn)
}
s.StopWaiter.Start(ctxIn, s)
backoff := time.Second
isAheadOfOnChainNonceEphemeralErrorHandler := util.NewEphemeralErrorHandler(10*time.Minute, "is ahead of on-chain nonce", 0)
exceedsMaxMempoolSizeEphemeralErrorHandler := util.NewEphemeralErrorHandler(10*time.Minute, dataposter.ErrExceedsMaxMempoolSize.Error(), 0)
blockValidationPendingEphemeralErrorHandler := util.NewEphemeralErrorHandler(10*time.Minute, "block validation is still pending", 0)
s.CallIteratively(func(ctx context.Context) (returningWait time.Duration) {
defer func() {
panicErr := recover()
if panicErr != nil {
log.Error("staker Act call panicked", "panic", panicErr, "backtrace", string(debug.Stack()))
s.builder.ClearTransactions()
returningWait = time.Minute
}
}()
var err error
cfg := s.config()
if common.HexToAddress(cfg.GasRefunderAddress) != (common.Address{}) {
gasRefunderBalance, err := s.client.BalanceAt(ctx, common.HexToAddress(cfg.GasRefunderAddress), nil)
if err != nil {
log.Warn("error fetching validator gas refunder balance", "err", err)
} else {
validatorGasRefunderBalance.Update(arbmath.BalancePerEther(gasRefunderBalance))
}
}
err = s.updateBlockValidatorModuleRoot(ctx)
if err != nil {
log.Warn("error updating latest wasm module root", "err", err)
}
arbTx, err := s.Act(ctx)
if err == nil && arbTx != nil {
_, err = s.l1Reader.WaitForTxApproval(ctx, arbTx)
if err == nil {
log.Info("successfully executed staker transaction", "hash", arbTx.Hash())
} else {
err = fmt.Errorf("error waiting for tx receipt: %w", err)
}
}
if err == nil {
isAheadOfOnChainNonceEphemeralErrorHandler.Reset()
exceedsMaxMempoolSizeEphemeralErrorHandler.Reset()
blockValidationPendingEphemeralErrorHandler.Reset()
backoff = time.Second
stakerLastSuccessfulActionGauge.Update(time.Now().Unix())
stakerActionSuccessCounter.Inc(1)
if arbTx != nil && !s.wallet.CanBatchTxs() {
// Try to create another tx
return 0
}
return cfg.StakerInterval
}
stakerActionFailureCounter.Inc(1)
backoff *= 2
logLevel := log.Error
if backoff > time.Minute {
backoff = time.Minute
} else {
logLevel = log.Warn
}
logLevel = isAheadOfOnChainNonceEphemeralErrorHandler.LogLevel(err, logLevel)
logLevel = exceedsMaxMempoolSizeEphemeralErrorHandler.LogLevel(err, logLevel)
logLevel = blockValidationPendingEphemeralErrorHandler.LogLevel(err, logLevel)
logLevel("error acting as staker", "err", err)
return backoff
})
s.CallIteratively(func(ctx context.Context) time.Duration {
wallet := s.wallet.AddressOrZero()
staked, stakedMsgCount, stakedGlobalState, err := s.getLatestStakedState(ctx, wallet)
if err != nil && ctx.Err() == nil {
log.Error("staker: error checking latest staked", "err", err)
}
// #nosec G115
stakerLatestStakedNodeGauge.Update(int64(staked))
if stakedGlobalState != nil {
for _, notifier := range s.stakedNotifiers {
notifier.UpdateLatestStaked(stakedMsgCount, *stakedGlobalState)
}
}
confirmed := staked
confirmedMsgCount := stakedMsgCount
confirmedGlobalState := stakedGlobalState
if wallet != (common.Address{}) {
confirmed, confirmedMsgCount, confirmedGlobalState, err = s.getLatestStakedState(ctx, common.Address{})
if err != nil && ctx.Err() == nil {
log.Error("staker: error checking latest confirmed", "err", err)
}
}
// #nosec G115
stakerLatestConfirmedNodeGauge.Update(int64(confirmed))
if confirmedGlobalState != nil {
for _, notifier := range s.confirmedNotifiers {
notifier.UpdateLatestConfirmed(confirmedMsgCount, *confirmedGlobalState)
}
}
return s.config().StakerInterval
})
}
func (s *Staker) IsWhitelisted(ctx context.Context) (bool, error) {
callOpts := s.getCallOpts(ctx)
whitelistDisabled, err := s.rollup.ValidatorWhitelistDisabled(callOpts)
if err != nil {
return false, err
}
if whitelistDisabled {
return true, nil
}
addr := s.wallet.Address()
if addr != nil {
return s.rollup.IsValidator(callOpts, *addr)
}
return false, nil
}
func (s *Staker) shouldAct(ctx context.Context) bool {
cfg := s.config()
var gasPriceHigh = false
var gasPriceFloat float64
gasPrice, err := s.client.SuggestGasPrice(ctx)
if err != nil {
log.Warn("error getting gas price", "err", err)
} else {
gasPriceFloat = float64(gasPrice.Int64()) / 1e9
if gasPriceFloat >= cfg.PostingStrategy.HighGasThreshold {
gasPriceHigh = true
}
}
latestBlockInfo, err := s.client.HeaderByNumber(ctx, nil)
if err != nil {
log.Warn("error getting latest block", "err", err)
return true
}
latestBlockNum := latestBlockInfo.Number
if s.lastActCalledBlock == nil {
s.lastActCalledBlock = latestBlockNum
}
blocksSinceActCalled := new(big.Int).Sub(latestBlockNum, s.lastActCalledBlock)
s.lastActCalledBlock = latestBlockNum
if gasPriceHigh {
// We're eating into the high gas buffer to delay our tx
s.highGasBlocksBuffer.Sub(s.highGasBlocksBuffer, blocksSinceActCalled)
} else {
// We'll try to make a tx if necessary, so we can add to the buffer for future high gas
s.highGasBlocksBuffer.Add(s.highGasBlocksBuffer, blocksSinceActCalled)
}
// Clamp `s.highGasBlocksBuffer` to between 0 and HighGasDelayBlocks
if s.highGasBlocksBuffer.Sign() < 0 {
s.highGasBlocksBuffer.SetInt64(0)
} else if s.highGasBlocksBuffer.Cmp(big.NewInt(cfg.PostingStrategy.HighGasDelayBlocks)) > 0 {
s.highGasBlocksBuffer.SetInt64(cfg.PostingStrategy.HighGasDelayBlocks)
}
if gasPriceHigh && s.highGasBlocksBuffer.Sign() > 0 {
log.Warn(
"not acting yet as gas price is high",
"gasPrice", gasPriceFloat,
"highGasPriceConfig", cfg.PostingStrategy.HighGasThreshold,
"highGasBuffer", s.highGasBlocksBuffer,
)
return false
}
return true
}
func (s *Staker) confirmDataPosterIsReady(ctx context.Context) error {
dp := s.wallet.DataPoster()
if dp == nil {
return nil
}
dataPosterNonce, _, err := dp.GetNextNonceAndMeta(ctx)
if err != nil {
return err
}
latestNonce, err := s.l1Reader.Client().NonceAt(ctx, dp.Sender(), nil)
if err != nil {
return err
}
if dataPosterNonce > latestNonce {
return fmt.Errorf("data poster nonce %v is ahead of on-chain nonce %v -- probably waiting for a pending transaction to be included in a block", dataPosterNonce, latestNonce)
}
if dataPosterNonce < latestNonce {
return fmt.Errorf("data poster nonce %v is behind on-chain nonce %v -- is something else making transactions on this address?", dataPosterNonce, latestNonce)
}
return nil
}
func (s *Staker) Act(ctx context.Context) (*types.Transaction, error) {
cfg := s.config()
if cfg.strategy != WatchtowerStrategy {
err := s.confirmDataPosterIsReady(ctx)
if err != nil {
return nil, err
}
whitelisted, err := s.IsWhitelisted(ctx)
if err != nil {
return nil, fmt.Errorf("error checking if whitelisted: %w", err)
}
if !whitelisted {
log.Warn("validator address isn't whitelisted", "address", s.wallet.Address(), "txSender", s.wallet.TxSenderAddress())
}
}
if !s.shouldAct(ctx) {
// The fact that we're delaying acting is already logged in `shouldAct`
return nil, nil
}
callOpts := s.getCallOpts(ctx)
s.builder.ClearTransactions()
var rawInfo *StakerInfo
walletAddressOrZero := s.wallet.AddressOrZero()
if walletAddressOrZero != (common.Address{}) {
var err error
rawInfo, err = s.rollup.StakerInfo(ctx, walletAddressOrZero)
if err != nil {
return nil, fmt.Errorf("error getting own staker (%v) info: %w", walletAddressOrZero, err)
}
if rawInfo != nil {
stakerAmountStakedGauge.Update(rawInfo.AmountStaked.Int64())
} else {
stakerAmountStakedGauge.Update(0)
}
s.updateStakerBalanceMetric(ctx)
}
// If the wallet address is zero, or the wallet address isn't staked,
// this will return the latest node and its hash (atomically).
latestStakedNodeNum, latestStakedNodeInfo, err := s.validatorUtils.LatestStaked(
callOpts, s.rollupAddress, walletAddressOrZero,
)
if err != nil {
return nil, fmt.Errorf("error getting latest staked node of own wallet %v: %w", walletAddressOrZero, err)
}
// #nosec G115
stakerLatestStakedNodeGauge.Update(int64(latestStakedNodeNum))
if rawInfo != nil {
rawInfo.LatestStakedNode = latestStakedNodeNum
}
info := OurStakerInfo{
CanProgress: true,
LatestStakedNode: latestStakedNodeNum,
LatestStakedNodeHash: latestStakedNodeInfo.NodeHash,
StakerInfo: rawInfo,
StakeExists: rawInfo != nil,
}
effectiveStrategy := cfg.strategy
nodesLinear, err := s.validatorUtils.AreUnresolvedNodesLinear(callOpts, s.rollupAddress)
if err != nil {
return nil, fmt.Errorf("error checking for rollup assertion fork: %w", err)
}
if !nodesLinear {
log.Warn("rollup assertion fork detected")
if effectiveStrategy == DefensiveStrategy {
effectiveStrategy = StakeLatestStrategy
}
s.inactiveLastCheckedNode = nil
}
if s.bringActiveUntilNode != 0 {
if info.LatestStakedNode < s.bringActiveUntilNode {
if effectiveStrategy == DefensiveStrategy {
effectiveStrategy = StakeLatestStrategy
}
} else {
log.Info("defensive validator staked past incorrect node; waiting here")
s.bringActiveUntilNode = 0
}
s.inactiveLastCheckedNode = nil
}
if effectiveStrategy <= DefensiveStrategy && s.inactiveLastCheckedNode != nil {
info.LatestStakedNode = s.inactiveLastCheckedNode.id
info.LatestStakedNodeHash = s.inactiveLastCheckedNode.hash
}
if cfg.EnableFastConfirmation {
firstUnresolvedNode, err := s.rollup.FirstUnresolvedNode(callOpts)
if err != nil {
return nil, err
}
if info.LatestStakedNode >= firstUnresolvedNode {
lastHeader, err := s.l1Reader.LastHeader(ctx)
if err != nil {
return nil, err
}
// To check if a node is correct, we simply check if we're staked on it.
// Since we're staked on it or a later node, this will tell us if it's correct.
// To keep this call consistent with the GetNode call, we pin a specific parent chain block hash.
checkNodeCorrectCallOpts := s.getCallOpts(ctx)
checkNodeCorrectCallOpts.BlockHash = lastHeader.ParentHash
nodeInfo, err := s.rollup.GetNode(checkNodeCorrectCallOpts, firstUnresolvedNode)
if err != nil {
return nil, err
}
validatedNode, haveValidated := s.inactiveValidatedNodes.Get(validatedNode{
number: firstUnresolvedNode,
hash: nodeInfo.NodeHash,
})
confirmedCorrect := haveValidated && validatedNode.hash == nodeInfo.NodeHash
if !confirmedCorrect {
stakedOnNode, err := s.rollup.NodeHasStaker(checkNodeCorrectCallOpts, firstUnresolvedNode, walletAddressOrZero)
if err != nil {
return nil, err
}
confirmedCorrect = stakedOnNode
}
if confirmedCorrect {
log.Info("trying to fast confirm previous node", "node", firstUnresolvedNode, "nodeHash", nodeInfo.NodeHash)
err = s.tryFastConfirmationNodeNumber(ctx, firstUnresolvedNode, nodeInfo.NodeHash)
if err != nil {
return nil, err
}
if s.builder.BuildingTransactionCount() > 0 {
// Try to fast confirm previous nodes before working on new ones
return s.wallet.ExecuteTransactions(ctx, s.builder, cfg.gasRefunder)
}
}
}
}
latestConfirmedNode, err := s.rollup.LatestConfirmed(callOpts)
if err != nil {
return nil, fmt.Errorf("error getting latest confirmed node: %w", err)
}
// Clear s.inactiveValidatedNodes of any entries before or equal to latestConfirmedNode
for {
validatedNode, ok := s.inactiveValidatedNodes.Min()
if !ok {
break
}
if validatedNode.number > latestConfirmedNode {
break
}
s.inactiveValidatedNodes.DeleteMin()
}
requiredStakeElevated, err := s.isRequiredStakeElevated(ctx)
if err != nil {
return nil, fmt.Errorf("error checking if required stake is elevated: %w", err)
}
// Resolve nodes if either we're on the make nodes strategy,
// or we're on the stake latest strategy but don't have a stake
// (attempt to reduce the current required stake).
shouldResolveNodes := effectiveStrategy >= ResolveNodesStrategy ||
(effectiveStrategy >= StakeLatestStrategy && rawInfo == nil && requiredStakeElevated)
resolvingNode := false
if shouldResolveNodes {
arbTx, err := s.resolveTimedOutChallenges(ctx)
if err != nil {
return nil, fmt.Errorf("error resolving timed out challenges: %w", err)
}
if arbTx != nil {
return arbTx, nil
}
resolvingNode, err = s.resolveNextNode(ctx, rawInfo, &latestConfirmedNode)
if err != nil {
return nil, fmt.Errorf("error resolving node %v: %w", latestConfirmedNode+1, err)
}
if resolvingNode && rawInfo == nil && latestConfirmedNode > info.LatestStakedNode {
// If we hit this condition, we've resolved what was previously the latest confirmed node,
// and we don't have a stake yet. That means we were planning to enter the rollup on
// the latest confirmed node, which has now changed. We fix this by updating our staker info
// to indicate that we're now entering the rollup on the newly confirmed node.
nodeInfo, err := s.rollup.GetNode(callOpts, latestConfirmedNode)
if err != nil {
return nil, fmt.Errorf("error getting latest confirmed node %v info: %w", latestConfirmedNode, err)
}
info.LatestStakedNode = latestConfirmedNode
info.LatestStakedNodeHash = nodeInfo.NodeHash
}
}
canActFurther := func() bool {
return s.wallet.CanBatchTxs() || s.builder.BuildingTransactionCount() == 0
}
// If we have an old stake, remove it
if rawInfo != nil && rawInfo.LatestStakedNode <= latestConfirmedNode && canActFurther() {
stakeIsTooOutdated := rawInfo.LatestStakedNode < latestConfirmedNode
// We're not trying to stake anyways
stakeIsUnwanted := effectiveStrategy < StakeLatestStrategy
if stakeIsTooOutdated || stakeIsUnwanted {
// Note: we must have an address if rawInfo != nil
auth, err := s.builder.Auth(ctx)
if err != nil {
return nil, err
}
_, err = s.rollup.ReturnOldDeposit(auth, walletAddressOrZero)
if err != nil {
return nil, fmt.Errorf("error returning old deposit (from our staker %v): %w", walletAddressOrZero, err)
}
auth, err = s.builder.Auth(ctx)
if err != nil {
return nil, err
}
_, err = s.rollup.WithdrawStakerFunds(auth)
if err != nil {
return nil, fmt.Errorf("error withdrawing staker funds from our staker %v: %w", walletAddressOrZero, err)
}
log.Info("removing old stake and withdrawing funds")
return s.wallet.ExecuteTransactions(ctx, s.builder, cfg.gasRefunder)
}
}
if walletAddressOrZero != (common.Address{}) && canActFurther() {
withdrawable, err := s.rollup.WithdrawableFunds(callOpts, walletAddressOrZero)
if err != nil {
return nil, fmt.Errorf("error checking withdrawable funds of our staker %v: %w", walletAddressOrZero, err)
}
if withdrawable.Sign() > 0 {
auth, err := s.builder.Auth(ctx)
if err != nil {
return nil, err
}
_, err = s.rollup.WithdrawStakerFunds(auth)
if err != nil {
return nil, fmt.Errorf("error withdrawing our staker %v funds: %w", walletAddressOrZero, err)
}
}
}
if rawInfo != nil && canActFurther() {
if err = s.handleConflict(ctx, rawInfo); err != nil {
return nil, fmt.Errorf("error handling conflict: %w", err)
}
}
// Don't attempt to create a new stake if we're resolving a node and the stake is elevated,
// as that might affect the current required stake.
if (rawInfo != nil || !resolvingNode || !requiredStakeElevated) && canActFurther() {
// Advance stake up to 20 times in one transaction
for i := 0; info.CanProgress && i < 20; i++ {
if err := s.advanceStake(ctx, &info, effectiveStrategy); err != nil {
return nil, fmt.Errorf("error advancing stake from node %v (hash %v): %w", info.LatestStakedNode, info.LatestStakedNodeHash, err)
}
if !s.wallet.CanBatchTxs() && effectiveStrategy >= StakeLatestStrategy {
info.CanProgress = false
}
}
}
if rawInfo != nil && s.builder.BuildingTransactionCount() == 0 && canActFurther() {
if err := s.createConflict(ctx, rawInfo); err != nil {
return nil, fmt.Errorf("error creating conflict: %w", err)
}
}
if s.builder.BuildingTransactionCount() == 0 {
return nil, nil
}
if info.StakerInfo == nil && info.StakeExists {
log.Info("staking to execute transactions")
}
return s.wallet.ExecuteTransactions(ctx, s.builder, cfg.gasRefunder)
}
func (s *Staker) handleConflict(ctx context.Context, info *StakerInfo) error {
if info.CurrentChallenge == nil {
s.activeChallenge = nil
return nil
}
if s.activeChallenge == nil || s.activeChallenge.ChallengeIndex() != *info.CurrentChallenge {
log.Error("entered challenge", "challenge", *info.CurrentChallenge)
latestConfirmedCreated, err := s.rollup.LatestConfirmedCreationBlock(ctx)
if err != nil {
return fmt.Errorf("error getting latest confirmed creation block: %w", err)
}
newChallengeManager, err := NewChallengeManager(
ctx,
s.builder,
s.builder.BuilderAuth(),
*s.builder.WalletAddress(),
s.wallet.ChallengeManagerAddress(),
*info.CurrentChallenge,
s.statelessBlockValidator,
latestConfirmedCreated,
s.config().ConfirmationBlocks,
)
if err != nil {
return fmt.Errorf("error creating challenge manager: %w", err)
}
s.activeChallenge = newChallengeManager
}
_, err := s.activeChallenge.Act(ctx)
return err
}