Skip to content

Commit 2b3ef77

Browse files
committed
miner && block reward
1 parent ac43172 commit 2b3ef77

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

turbo/jsonrpc/eth_receipts.go

+55-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"github.com/ledgerwatch/erigon/eth/ethutils"
2222

2323
"github.com/ledgerwatch/erigon/consensus"
24+
"github.com/ledgerwatch/erigon/consensus/ethash"
25+
"github.com/ledgerwatch/erigon/consensus/misc"
2426
"github.com/ledgerwatch/erigon/core"
2527
"github.com/ledgerwatch/erigon/core/rawdb"
2628
"github.com/ledgerwatch/erigon/core/state"
@@ -803,11 +805,15 @@ func (api *APIImpl) GetERCBlockReceipts(ctx context.Context, to rpc.BlockNumber,
803805

804806
blocks := make([]map[string]interface{}, 0)
805807
for ; start <= end; start++ {
806-
blockNum, _, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(start)), tx, api.filters)
808+
var (
809+
totalFees uint64
810+
)
811+
812+
blockNum, hash, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(start)), tx, api.filters)
807813
if err != nil {
808814
return nil, err
809815
}
810-
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
816+
block, err := api.blockWithSenders(ctx, tx, hash, blockNum)
811817
if err != nil {
812818
return nil, err
813819
}
@@ -831,6 +837,18 @@ func (api *APIImpl) GetERCBlockReceipts(ctx context.Context, to rpc.BlockNumber,
831837
for _, receipt := range receipts {
832838
marhaledReciept := queryERCTransaction(engine, ctx, chainConfig, signer, rules, blockCtx, ibs, receipt, block)
833839
result = append(result, marhaledReciept)
840+
841+
// Gas Fee Calculation
842+
txn := block.Transactions()[receipt.TransactionIndex]
843+
effectiveGasPrice := uint64(0)
844+
if !chainConfig.IsLondon(block.NumberU64()) {
845+
effectiveGasPrice = txn.GetPrice().Uint64()
846+
} else {
847+
baseFee, _ := uint256.FromBig(block.BaseFee())
848+
gasPrice := new(big.Int).Add(block.BaseFee(), txn.GetEffectiveGasTip(baseFee).ToBig())
849+
effectiveGasPrice = gasPrice.Uint64()
850+
}
851+
totalFees += effectiveGasPrice * receipt.GasUsed
834852
}
835853

836854
if chainConfig.Bor != nil {
@@ -846,17 +864,52 @@ func (api *APIImpl) GetERCBlockReceipts(ctx context.Context, to rpc.BlockNumber,
846864
}
847865
}
848866

867+
issuance, _ := api.delegateIssuance(tx, block, chainConfig)
868+
849869
blocks = append(blocks, map[string]interface{}{
850870
"number": block.Number().Int64(),
851871
"timestamp": block.Time(),
852872
"baseFeePerGas": (*hexutil.Big)(block.BaseFee()),
873+
"gasUsed": hexutil.Uint64(block.GasUsed()),
874+
"miner": block.Coinbase(),
875+
"issuance": issuance.Issuance,
876+
"totalFees": hexutil.Uint64(totalFees),
853877
"transactions": result,
854878
})
855879
}
856880

857881
return blocks, nil
858882
}
859883

884+
func (api *APIImpl) delegateIssuance(tx kv.Tx, block *types.Block, chainConfig *chain.Config) (internalIssuance, error) {
885+
if chainConfig.Ethash == nil {
886+
// Clique for example has no issuance
887+
return internalIssuance{}, nil
888+
}
889+
890+
if chainConfig.TerminalTotalDifficulty != nil {
891+
isPos := block.HeaderNoCopy().Difficulty.Cmp(common.Big0) == 0 || block.HeaderNoCopy().Difficulty.Cmp(chainConfig.TerminalTotalDifficulty) >= 0
892+
if isPos {
893+
// No execution layer issuance in PoS
894+
return internalIssuance{}, nil
895+
}
896+
}
897+
898+
minerReward, uncleRewards := ethash.AccumulateRewards(chainConfig, block.Header(), block.Uncles())
899+
issuance := minerReward
900+
for _, r := range uncleRewards {
901+
p := r // avoids warning?
902+
issuance.Add(&issuance, &p)
903+
}
904+
905+
var ret internalIssuance
906+
ret.BlockReward = hexutil.EncodeBig(minerReward.ToBig())
907+
ret.Issuance = hexutil.EncodeBig(issuance.ToBig())
908+
issuance.Sub(&issuance, &minerReward)
909+
ret.UncleReward = hexutil.EncodeBig(issuance.ToBig())
910+
return ret, nil
911+
}
912+
860913
func (api *APIImpl) GetERCTransactionReceipt(ctx context.Context, txnHash common.Hash) (map[string]any, error) {
861914
tx, err := api.db.BeginRo(ctx)
862915
if err != nil {

0 commit comments

Comments
 (0)