fix(rln-relay): sync from deployed block number

This commit is contained in:
Richard Ramos 2023-08-31 17:45:18 -04:00 committed by richΛrd
parent 84fa332e1c
commit f9ed8d973c
2 changed files with 19 additions and 7 deletions

View File

@ -21,10 +21,10 @@ type RegistrationEventHandler = func(*DynamicGroupManager, []*contracts.RLNMembe
// It connects to the eth client, subscribes to the `MemberRegistered` event emitted from the `MembershipContract` // It connects to the eth client, subscribes to the `MemberRegistered` event emitted from the `MembershipContract`
// and collects all the events, for every received event, it calls the `handler` // and collects all the events, for every received event, it calls the `handler`
func (gm *DynamicGroupManager) HandleGroupUpdates(ctx context.Context, handler RegistrationEventHandler) error { func (gm *DynamicGroupManager) HandleGroupUpdates(ctx context.Context, handler RegistrationEventHandler) error {
fromBlock := uint64(0) fromBlock := gm.web3Config.RLNContract.DeployedBlockNumber
metadata, err := gm.GetMetadata() metadata, err := gm.GetMetadata()
if err != nil { if err != nil {
gm.log.Warn("could not load last processed block from metadata. Starting onchain sync from scratch", zap.Error(err)) gm.log.Warn("could not load last processed block from metadata. Starting onchain sync from deployment block", zap.Error(err), zap.Uint64("deploymentBlock", gm.web3Config.RLNContract.DeployedBlockNumber))
} else { } else {
if gm.web3Config.ChainID.Cmp(metadata.ChainID) != 0 { if gm.web3Config.ChainID.Cmp(metadata.ChainID) != 0 {
return errors.New("persisted data: chain id mismatch") return errors.New("persisted data: chain id mismatch")
@ -148,6 +148,8 @@ func (gm *DynamicGroupManager) getEvents(ctx context.Context, from uint64, to *u
end = *toBlock end = *toBlock
} }
gm.log.Info("loading events...", zap.Uint64("fromBlock", start), zap.Uint64("toBlock", end))
evts, err := gm.fetchEvents(ctx, start, &end) evts, err := gm.fetchEvents(ctx, start, &end)
if err != nil { if err != nil {
if tooMuchDataRequestedError(err) { if tooMuchDataRequestedError(err) {
@ -157,6 +159,9 @@ func (gm *DynamicGroupManager) getEvents(ctx context.Context, from uint64, to *u
// multiplicative decrease // multiplicative decrease
batchSize = batchSize / multiplicativeDecreaseDivisor batchSize = batchSize / multiplicativeDecreaseDivisor
gm.log.Warn("too many logs requested!, retrying with a smaller chunk size", zap.Uint64("batchSize", batchSize))
continue continue
} }
return nil, err return nil, err

View File

@ -21,8 +21,9 @@ type RegistryContract struct {
// that represents this contract // that represents this contract
type RLNContract struct { type RLNContract struct {
*contracts.RLN *contracts.RLN
Address common.Address Address common.Address
StorageIndex uint16 StorageIndex uint16
DeployedBlockNumber uint64
} }
// Config is a helper struct that contains attributes for interaction with RLN smart contracts // Config is a helper struct that contains attributes for interaction with RLN smart contracts
@ -78,6 +79,11 @@ func BuildConfig(ctx context.Context, ethClientAddress string, registryAddress c
return nil, err return nil, err
} }
deploymentBlockNumber, err := rlnContract.DeployedBlockNumber(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, err
}
return &Config{ return &Config{
configured: true, configured: true,
ETHClientAddress: ethClientAddress, ETHClientAddress: ethClientAddress,
@ -88,9 +94,10 @@ func BuildConfig(ctx context.Context, ethClientAddress string, registryAddress c
Address: registryAddress, Address: registryAddress,
}, },
RLNContract: RLNContract{ RLNContract: RLNContract{
RLN: rlnContract, RLN: rlnContract,
Address: rlnContractAddress, Address: rlnContractAddress,
StorageIndex: storageIndex, StorageIndex: storageIndex,
DeployedBlockNumber: uint64(deploymentBlockNumber),
}, },
}, nil }, nil
} }