@@ -40,23 +40,6 @@ func NewEthereumWriter(
40
40
}
41
41
42
42
func (wr * EthereumWriter ) Start (ctx context.Context , eg * errgroup.Group , requests <- chan Request ) error {
43
- address := common .HexToAddress (wr .config .Contracts .BeefyClient )
44
- contract , err := contracts .NewBeefyClient (address , wr .conn .Client ())
45
- if err != nil {
46
- return fmt .Errorf ("create beefy client: %w" , err )
47
- }
48
- wr .contract = contract
49
-
50
- callOpts := bind.CallOpts {
51
- Context : ctx ,
52
- }
53
- blockWaitPeriod , err := wr .contract .RandaoCommitDelay (& callOpts )
54
- if err != nil {
55
- return fmt .Errorf ("create randao commit delay: %w" , err )
56
- }
57
- wr .blockWaitPeriod = blockWaitPeriod .Uint64 ()
58
- log .WithField ("randaoCommitDelay" , wr .blockWaitPeriod ).Trace ("Fetched randaoCommitDelay" )
59
-
60
43
// launch task processor
61
44
eg .Go (func () error {
62
45
for {
@@ -68,7 +51,24 @@ func (wr *EthereumWriter) Start(ctx context.Context, eg *errgroup.Group, request
68
51
return nil
69
52
}
70
53
71
- err := wr .submit (ctx , task )
54
+ state , err := wr .queryBeefyClientState (ctx )
55
+ if err != nil {
56
+ return fmt .Errorf ("query beefy client state: %w" , err )
57
+ }
58
+
59
+ if task .SignedCommitment .Commitment .BlockNumber < uint32 (state .LatestBeefyBlock ) {
60
+ log .WithFields (logrus.Fields {
61
+ "beefyBlockNumber" : task .SignedCommitment .Commitment .BlockNumber ,
62
+ "latestBeefyBlock" : state .LatestBeefyBlock ,
63
+ }).Info ("Commitment already synced" )
64
+ continue
65
+ }
66
+
67
+ // Mandatory commitments are always signed by the next validator set recorded in
68
+ // the beefy light client
69
+ task .ValidatorsRoot = state .NextValidatorSetRoot
70
+
71
+ err = wr .submit (ctx , task )
72
72
if err != nil {
73
73
return fmt .Errorf ("submit request: %w" , err )
74
74
}
@@ -79,32 +79,43 @@ func (wr *EthereumWriter) Start(ctx context.Context, eg *errgroup.Group, request
79
79
return nil
80
80
}
81
81
82
- func (wr * EthereumWriter ) submit (ctx context.Context , task Request ) error {
82
+ type BeefyClientState struct {
83
+ LatestBeefyBlock uint64
84
+ CurrentValidatorSetID uint64
85
+ CurrentValidatorSetRoot [32 ]byte
86
+ NextValidatorSetID uint64
87
+ NextValidatorSetRoot [32 ]byte
88
+ }
89
+
90
+ func (wr * EthereumWriter ) queryBeefyClientState (ctx context.Context ) (* BeefyClientState , error ) {
83
91
callOpts := bind.CallOpts {
84
92
Context : ctx ,
85
93
}
86
94
87
95
latestBeefyBlock , err := wr .contract .LatestBeefyBlock (& callOpts )
88
96
if err != nil {
89
- return err
90
- }
91
- if uint32 (latestBeefyBlock ) >= task .SignedCommitment .Commitment .BlockNumber {
92
- return nil
97
+ return nil , err
93
98
}
94
99
95
100
currentValidatorSet , err := wr .contract .CurrentValidatorSet (& callOpts )
96
101
if err != nil {
97
- return err
102
+ return nil , err
98
103
}
99
104
nextValidatorSet , err := wr .contract .NextValidatorSet (& callOpts )
100
105
if err != nil {
101
- return err
102
- }
103
- task .ValidatorsRoot = currentValidatorSet .Root
104
- if task .IsHandover {
105
- task .ValidatorsRoot = nextValidatorSet .Root
106
+ return nil , err
106
107
}
107
108
109
+ return & BeefyClientState {
110
+ LatestBeefyBlock : latestBeefyBlock ,
111
+ CurrentValidatorSetID : currentValidatorSet .Id .Uint64 (),
112
+ CurrentValidatorSetRoot : currentValidatorSet .Root ,
113
+ NextValidatorSetID : nextValidatorSet .Id .Uint64 (),
114
+ NextValidatorSetRoot : nextValidatorSet .Root ,
115
+ }, nil
116
+ }
117
+
118
+ func (wr * EthereumWriter ) submit (ctx context.Context , task Request ) error {
108
119
// Initial submission
109
120
tx , initialBitfield , err := wr .doSubmitInitial (ctx , & task )
110
121
if err != nil {
@@ -131,6 +142,7 @@ func (wr *EthereumWriter) submit(ctx context.Context, task Request) error {
131
142
wr .conn .MakeTxOpts (ctx ),
132
143
* commitmentHash ,
133
144
)
145
+
134
146
_ , err = wr .conn .WatchTransaction (ctx , tx , 1 )
135
147
if err != nil {
136
148
log .WithError (err ).Error ("Failed to CommitPrevRandao" )
@@ -153,7 +165,6 @@ func (wr *EthereumWriter) submit(ctx context.Context, task Request) error {
153
165
log .WithFields (logrus.Fields {
154
166
"tx" : tx .Hash ().Hex (),
155
167
"blockNumber" : task .SignedCommitment .Commitment .BlockNumber ,
156
- "IsHandover" : task .IsHandover ,
157
168
}).Debug ("Transaction SubmitFinal succeeded" )
158
169
159
170
return nil
@@ -267,3 +278,24 @@ func (wr *EthereumWriter) doSubmitFinal(ctx context.Context, commitmentHash [32]
267
278
268
279
return tx , nil
269
280
}
281
+
282
+ func (wr * EthereumWriter ) initialize (ctx context.Context ) error {
283
+ address := common .HexToAddress (wr .config .Contracts .BeefyClient )
284
+ contract , err := contracts .NewBeefyClient (address , wr .conn .Client ())
285
+ if err != nil {
286
+ return fmt .Errorf ("create beefy client: %w" , err )
287
+ }
288
+ wr .contract = contract
289
+
290
+ callOpts := bind.CallOpts {
291
+ Context : ctx ,
292
+ }
293
+ blockWaitPeriod , err := wr .contract .RandaoCommitDelay (& callOpts )
294
+ if err != nil {
295
+ return fmt .Errorf ("create randao commit delay: %w" , err )
296
+ }
297
+ wr .blockWaitPeriod = blockWaitPeriod .Uint64 ()
298
+ log .WithField ("randaoCommitDelay" , wr .blockWaitPeriod ).Trace ("Fetched randaoCommitDelay" )
299
+
300
+ return nil
301
+ }
0 commit comments