Skip to content

Commit

Permalink
Fix edge case goldToken lookup for epoch rewards at synced head (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eela Nagaraj authored Sep 12, 2023
1 parent 7bcfd8f commit cfcefa0
Showing 1 changed file with 12 additions and 22 deletions.
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

0 comments on commit cfcefa0

Please sign in to comment.