Skip to content

Commit dfdfed5

Browse files
wmitsudasealer3
andauthored
[ots] Fix block rewards calculation on post-merge blocks (#10038)
This is for E2. The block rewards returned by Otterscan API is incorrect since the merge. It replaces very old code with the same calculation used for trace_block. this code probably won't work with Aura consensus, but that's ok since the current one doesn't work as well. It would actually require exposing more code from block execution and I don't want to handle it for now, let's fix only the post-merge calc for now. Co-authored-by: sealer3 <[email protected]>
1 parent d318f11 commit dfdfed5

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

turbo/jsonrpc/otterscan_api.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/ledgerwatch/erigon-lib/kv/iter"
1919
"github.com/ledgerwatch/erigon-lib/kv/order"
2020
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
21-
"github.com/ledgerwatch/erigon/consensus/ethash"
21+
"github.com/ledgerwatch/erigon/consensus"
2222
"github.com/ledgerwatch/erigon/core"
2323
"github.com/ledgerwatch/erigon/core/rawdb"
2424
"github.com/ledgerwatch/erigon/core/types"
@@ -508,24 +508,32 @@ type internalIssuance struct {
508508
Issuance string `json:"issuance,omitempty"`
509509
}
510510

511-
func delegateIssuance(tx kv.Tx, block *types.Block, chainConfig *chain.Config) (internalIssuance, error) {
512-
if chainConfig.Ethash == nil {
513-
// Clique for example has no issuance
514-
return internalIssuance{}, nil
511+
func delegateIssuance(tx kv.Tx, block *types.Block, chainConfig *chain.Config, engine consensus.EngineReader) (internalIssuance, error) {
512+
// TODO: aura seems to be already broken in the original version of this RPC method
513+
rewards, err := engine.CalculateRewards(chainConfig, block.HeaderNoCopy(), block.Uncles(), func(contract common.Address, data []byte) ([]byte, error) {
514+
return nil, nil
515+
})
516+
if err != nil {
517+
return internalIssuance{}, err
515518
}
516519

517-
minerReward, uncleRewards := ethash.AccumulateRewards(chainConfig, block.Header(), block.Uncles())
518-
issuance := minerReward
519-
for _, r := range uncleRewards {
520-
p := r // avoids warning?
521-
issuance.Add(&issuance, &p)
520+
blockReward := uint256.NewInt(0)
521+
uncleReward := uint256.NewInt(0)
522+
for _, r := range rewards {
523+
if r.Kind == consensus.RewardAuthor {
524+
blockReward.Add(blockReward, &r.Amount)
525+
}
526+
if r.Kind == consensus.RewardUncle {
527+
uncleReward.Add(uncleReward, &r.Amount)
528+
}
522529
}
523530

524531
var ret internalIssuance
525-
ret.BlockReward = hexutil2.EncodeBig(minerReward.ToBig())
526-
ret.Issuance = hexutil2.EncodeBig(issuance.ToBig())
527-
issuance.Sub(&issuance, &minerReward)
528-
ret.UncleReward = hexutil2.EncodeBig(issuance.ToBig())
532+
ret.BlockReward = hexutil2.EncodeBig(blockReward.ToBig())
533+
ret.UncleReward = hexutil2.EncodeBig(uncleReward.ToBig())
534+
535+
blockReward.Add(blockReward, uncleReward)
536+
ret.Issuance = hexutil2.EncodeBig(blockReward.ToBig())
529537
return ret, nil
530538
}
531539

turbo/jsonrpc/otterscan_block_details.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jsonrpc
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/ledgerwatch/erigon-lib/common/hexutil"
78

89
"github.com/ledgerwatch/erigon-lib/common"
@@ -65,7 +66,7 @@ func (api *OtterscanAPIImpl) getBlockDetailsImpl(ctx context.Context, tx kv.Tx,
6566
if err != nil {
6667
return nil, err
6768
}
68-
getIssuanceRes, err := delegateIssuance(tx, b, chainConfig)
69+
getIssuanceRes, err := delegateIssuance(tx, b, chainConfig, api.engine())
6970
if err != nil {
7071
return nil, err
7172
}

0 commit comments

Comments
 (0)