Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix edge case goldToken lookup for epoch rewards at synced head #206

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions analyzer/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ func ComputeEpochRewards(ctx context.Context, cc *client.CeloClient, db db.Roset
}

rewards := make(map[common.Address]*big.Int)
// Epoch rewards are distributed in the last tx of each block.
txIndex, err := rctx.cc.Eth.TransactionCount(rctx.ctx, rctx.header.Hash())
if err != nil {
return nil, err
}

goldToken, err := rctx.getGoldToken()
// Ensure that we fetch the goldToken contract event from the current
goldToken, err := rctx.getGoldToken(txIndex)
if err != nil {
return nil, err
}
Expand All @@ -53,7 +59,7 @@ func ComputeEpochRewards(ctx context.Context, cc *client.CeloClient, db db.Roset
return NewEpochRewards(rewards), nil
}

if err := rctx.computeRewards(rewards, goldToken); err != nil {
if err := rctx.computeRewards(rewards, goldToken, txIndex); err != nil {
return nil, err
}

Expand All @@ -66,16 +72,7 @@ func (rctx *rewardsContext) blockNumber() *big.Int {
return rctx.header.Number
}

func (rctx *rewardsContext) nextBlockNumber() *big.Int {
return new(big.Int).Add(rctx.header.Number, big.NewInt(1))
}

//nolint:unused
func (rctx *rewardsContext) prevBlockNumber() *big.Int {
return new(big.Int).Sub(rctx.header.Number, big.NewInt(1))
}

func (rctx *rewardsContext) computeRewards(rewardsMap map[common.Address]*big.Int, token *contracts.GoldToken) error {
func (rctx *rewardsContext) computeRewards(rewardsMap map[common.Address]*big.Int, token *contracts.GoldToken, txIndex uint) error {
blockNumber := rctx.blockNumber().Uint64()

iter, err := token.FilterTransfer(&bind.FilterOpts{
Expand All @@ -87,24 +84,17 @@ func (rctx *rewardsContext) computeRewards(rewardsMap map[common.Address]*big.In
return err
}

// Epoch rewards are distributed in the last tx of each block.

txCount, err := rctx.cc.Eth.TransactionCount(rctx.ctx, rctx.header.Hash())
if err != nil {
return err
}

for iter.Next() {
if iter.Event.Raw.TxIndex == txCount {
if iter.Event.Raw.TxIndex == txIndex {
rewardsMap[iter.Event.To] = iter.Event.Value
}
}

return nil
}

func (rctx *rewardsContext) getGoldToken() (*contracts.GoldToken, error) {
address, err := rctx.db.RegistryAddressStartOf(rctx.ctx, rctx.nextBlockNumber(), 0, "GoldToken")
func (rctx *rewardsContext) getGoldToken(txIndex uint) (*contracts.GoldToken, error) {
address, err := rctx.db.RegistryAddressStartOf(rctx.ctx, rctx.blockNumber(), txIndex, "GoldToken")
if err != nil && err != db.ErrContractNotFound {
return nil, err
}
Expand Down