@@ -14,6 +14,7 @@ import (
1414 "github.com/ethereum/go-ethereum/accounts/abi"
1515 "github.com/ethereum/go-ethereum/accounts/abi/bind"
1616 "github.com/ethereum/go-ethereum/common"
17+ "github.com/ethereum/go-ethereum/core/types"
1718 "github.com/ethereum/go-ethereum/crypto"
1819 "github.com/ethereum/go-ethereum/ethclient"
1920 "github.com/pkg/errors"
@@ -24,8 +25,29 @@ import (
2425 "github.com/flare-foundation/go-flare-common/pkg/contracts/registry"
2526)
2627
28+ // TODO configure addresses and breaking change
29+
2730const (
31+ chainIDCoston = 16
2832 chainIDCoston2 = 114
33+
34+ breakingEpochCoston = 5451
35+ )
36+
37+ const (
38+ newRegistryCostonAddr = "0x4C797636FC2410e1BbA7CF4bf2e397d94e65DfB8"
39+ oldRegistryCostonAddr = "0xE2c06DF29d175Aa0EcfcD10134eB96f8C94448A3"
40+
41+ newPreRegistryCostonAddr = "0x8F8E788d3A019bA0F05aa7d6f596c564EEbeea7c"
42+ oldPreRegistryCostonAddr = "0xD9e8Ee56CB5A06f2070A2793bf76C5dFb3fc3D52"
43+ )
44+
45+ var (
46+ newRegistryCoston = common .HexToAddress (newRegistryCostonAddr )
47+ oldRegistryCoston = common .HexToAddress (oldRegistryCostonAddr )
48+
49+ newPreRegistryCoston = common .HexToAddress (newPreRegistryCostonAddr )
50+ oldPreRegistryCoston = common .HexToAddress (oldPreRegistryCostonAddr )
2951)
3052
3153var (
@@ -112,7 +134,9 @@ type registryContractClientImpl struct {
112134 registryAddress common.Address
113135 preregistryAddress common.Address
114136 registry * registry.Registry
137+ oldRegistry * registry.Registry
115138 preregistry * preregistry.Preregistry
139+ oldPreregistry * preregistry.Preregistry
116140 senderTxOpts * bind.TransactOpts
117141 gasCfg * config.Gas
118142 txVerifier * chain.TxVerifier
@@ -131,19 +155,37 @@ func NewRegistryContractClient(
131155) (* registryContractClientImpl , error ) {
132156 registryBinding , err := registry .NewRegistry (registryAddress , ethClient )
133157 if err != nil {
134- return nil , fmt . Errorf ( "registry binding: %w" , err )
158+ return nil , err
135159 }
136160 preregistryBinding , err := preregistry .NewPreregistry (preregistryAddress , ethClient )
137161 if err != nil {
138- return nil , fmt .Errorf ("pre registry binding: %w" , err )
162+ return nil , err
163+ }
164+
165+ var oldRegistryBinding * registry.Registry
166+ if registryAddress == newRegistryCoston {
167+ oldRegistryBinding , err = registry .NewRegistry (oldRegistryCoston , ethClient )
168+ if err != nil {
169+ return nil , err
170+ }
171+ }
172+
173+ var oldPreRegistryBinding * preregistry.Preregistry
174+ if preregistryAddress == newPreRegistryCoston {
175+ oldPreRegistryBinding , err = preregistry .NewPreregistry (oldPreRegistryCoston , ethClient )
176+ if err != nil {
177+ return nil , err
178+ }
139179 }
140180
141181 return & registryContractClientImpl {
142182 ethClient : ethClient ,
143183 registryAddress : registryAddress ,
144184 preregistryAddress : preregistryAddress ,
145185 registry : registryBinding ,
186+ oldRegistry : oldRegistryBinding ,
146187 preregistry : preregistryBinding ,
188+ oldPreregistry : oldPreRegistryBinding ,
147189 senderTxOpts : senderTxOpts ,
148190 gasCfg : gasCfg ,
149191 txVerifier : chain .NewTxVerifier (ethClient ),
@@ -169,22 +211,9 @@ func (r *registryContractClientImpl) RegisterVoter(ctx context.Context, nextRewa
169211
170212func (r * registryContractClientImpl ) sendRegisterVoter (ctx context.Context , nextRewardEpochID * big.Int , address common.Address ) error {
171213 epochID := uint32 (nextRewardEpochID .Uint64 ())
172-
173- var (
174- signature []byte
175- err error
176- )
177-
178- if r .chainID != chainIDCoston2 {
179- signature , err = r .createSignature (epochID , address )
180- if err != nil {
181- return fmt .Errorf ("creating registry signature old: %w" , err )
182- }
183- } else {
184- signature , err = r .createSignatureNew (r .chainID , epochID , address )
185- if err != nil {
186- return fmt .Errorf ("creating registry signature new: %w" , err )
187- }
214+ signature , err := r .signature (epochID , address )
215+ if err != nil {
216+ return fmt .Errorf ("signature: %w" , err )
188217 }
189218
190219 vrsSignature := registry.IVoterRegistrySignature {
@@ -195,7 +224,7 @@ func (r *registryContractClientImpl) sendRegisterVoter(ctx context.Context, next
195224
196225 err = SetGas (ctx , r .senderTxOpts , r .ethClient , r .gasCfg )
197226 if err != nil {
198- return err
227+ return fmt . Errorf ( "setting gas: %w" , err )
199228 }
200229
201230 estimatedGasLimit , err := chain .DryRunTxAbi (
@@ -211,7 +240,7 @@ func (r *registryContractClientImpl) sendRegisterVoter(ctx context.Context, next
211240 vrsSignature ,
212241 )
213242 if err != nil {
214- return errors . Wrap ( err , "Dry run failed" )
243+ return fmt . Errorf ( "dry run failed: %w" , err )
215244 }
216245
217246 if r .gasCfg .GasLimit != 0 {
@@ -220,9 +249,18 @@ func (r *registryContractClientImpl) sendRegisterVoter(ctx context.Context, next
220249 r .senderTxOpts .GasLimit = estimatedGasLimit
221250 }
222251
223- tx , err := r .registry .RegisterVoter (r .senderTxOpts , address , vrsSignature )
224- if err != nil {
225- return fmt .Errorf ("sending registry tx: %w" , err )
252+ var tx * types.Transaction
253+
254+ if shouldUseOldRegistry (epochID , r .registryAddress ) {
255+ tx , err = r .oldRegistry .RegisterVoter (r .senderTxOpts , address , vrsSignature )
256+ if err != nil {
257+ return fmt .Errorf ("sending registry old tx: %w" , err )
258+ }
259+ } else {
260+ tx , err = r .registry .RegisterVoter (r .senderTxOpts , address , vrsSignature )
261+ if err != nil {
262+ return fmt .Errorf ("sending registry tx: %w" , err )
263+ }
226264 }
227265
228266 err = r .txVerifier .WaitUntilMined (ctx , r .senderTxOpts .From , tx , chain .DefaultTxTimeout )
@@ -250,22 +288,9 @@ func (r *registryContractClientImpl) PreregisterVoter(ctx context.Context, nextR
250288
251289func (r * registryContractClientImpl ) sendPreRegisterVoter (ctx context.Context , nextRewardEpochID * big.Int , address common.Address ) error {
252290 epochID := uint32 (nextRewardEpochID .Uint64 ())
253-
254- var (
255- signature []byte
256- err error
257- )
258-
259- if r .chainID != chainIDCoston2 {
260- signature , err = r .createSignature (epochID , address )
261- if err != nil {
262- return fmt .Errorf ("creating pre registry signature old: %w" , err )
263- }
264- } else {
265- signature , err = r .createSignatureNew (r .chainID , epochID , address )
266- if err != nil {
267- return fmt .Errorf ("creating pre registry signature new: %w" , err )
268- }
291+ signature , err := r .signature (epochID , address )
292+ if err != nil {
293+ return fmt .Errorf ("signature: %w" , err )
269294 }
270295
271296 vrsSignature := preregistry.IVoterRegistrySignature {
@@ -276,7 +301,7 @@ func (r *registryContractClientImpl) sendPreRegisterVoter(ctx context.Context, n
276301
277302 err = SetGas (ctx , r .senderTxOpts , r .ethClient , r .gasCfg )
278303 if err != nil {
279- return fmt .Errorf ("setting gas pre registry:%w" , err )
304+ return fmt .Errorf ("setting gas pre registry: %w" , err )
280305 }
281306
282307 estimatedGasLimit , err := chain .DryRunTxAbi (
@@ -292,7 +317,7 @@ func (r *registryContractClientImpl) sendPreRegisterVoter(ctx context.Context, n
292317 vrsSignature ,
293318 )
294319 if err != nil {
295- return errors . Wrap ( err , "Dry run failed" )
320+ return fmt . Errorf ( "dry run failed, %w" , err )
296321 }
297322
298323 if r .gasCfg .GasLimit != 0 {
@@ -301,9 +326,18 @@ func (r *registryContractClientImpl) sendPreRegisterVoter(ctx context.Context, n
301326 r .senderTxOpts .GasLimit = estimatedGasLimit
302327 }
303328
304- tx , err := r .preregistry .PreRegisterVoter (r .senderTxOpts , address , vrsSignature )
305- if err != nil {
306- return fmt .Errorf ("sending preregistry tx: %w" , err )
329+ var tx * types.Transaction
330+
331+ if shouldUseOldPreRegistry (epochID , r .preregistryAddress ) {
332+ tx , err = r .oldPreregistry .PreRegisterVoter (r .senderTxOpts , address , vrsSignature )
333+ if err != nil {
334+ return fmt .Errorf ("sending preregistry tx: %w" , err )
335+ }
336+ } else {
337+ tx , err = r .preregistry .PreRegisterVoter (r .senderTxOpts , address , vrsSignature )
338+ if err != nil {
339+ return fmt .Errorf ("sending preregistry tx: %w" , err )
340+ }
307341 }
308342
309343 err = r .txVerifier .WaitUntilMined (ctx , r .senderTxOpts .From , tx , chain .DefaultTxTimeout )
@@ -384,3 +418,41 @@ func SetGas(ctx context.Context, txOptions *bind.TransactOpts, client *ethclient
384418 return fmt .Errorf ("unsupported tx type: %d" , gasConfig .TxType )
385419 }
386420}
421+
422+ func shouldUseOldRegistry (epochID uint32 , address common.Address ) bool {
423+ if address == newRegistryCoston && epochID < breakingEpochCoston {
424+ return true
425+ }
426+
427+ return false
428+ }
429+
430+ func shouldUseOldPreRegistry (epochID uint32 , address common.Address ) bool {
431+ if address == newPreRegistryCoston && epochID < breakingEpochCoston {
432+ return true
433+ }
434+
435+ return false
436+ }
437+
438+ func (r * registryContractClientImpl ) signature (epochID uint32 , address common.Address ) ([]byte , error ) {
439+ var (
440+ signature []byte
441+ err error
442+ )
443+ switch {
444+ case r .chainID == chainIDCoston2 ,
445+ r .chainID == chainIDCoston && epochID >= breakingEpochCoston :
446+ signature , err = r .createSignatureNew (r .chainID , epochID , address )
447+ if err != nil {
448+ return nil , fmt .Errorf ("creating pre registry signature new: %w" , err )
449+ }
450+ default :
451+ signature , err = r .createSignature (epochID , address )
452+ if err != nil {
453+ return nil , fmt .Errorf ("creating pre registry signature old: %w" , err )
454+ }
455+ }
456+
457+ return signature , nil
458+ }
0 commit comments