erigon-pulse/cmd/rpcdaemon/commands/erigon_issuance.go

86 lines
2.5 KiB
Go
Raw Normal View History

package commands
import (
"context"
2021-07-29 11:53:13 +00:00
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/rpc"
)
// BlockReward returns the block reward for this block
// func (api *ErigonImpl) BlockReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
// tx, err := api.db.Begin(ctx, ethdb.RO)
// if err != nil {
// return Issuance{}, err
// }
// defer tx.Rollback()
//
// return api.rewardCalc(tx, blockNr, "block") // nolint goconst
//}
// UncleReward returns the uncle reward for this block
// func (api *ErigonImpl) UncleReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
// tx, err := api.db.Begin(ctx, ethdb.RO)
// if err != nil {
// return Issuance{}, err
// }
// defer tx.Rollback()
//
// return api.rewardCalc(tx, blockNr, "uncle") // nolint goconst
//}
// Issuance implements erigon_issuance. Returns the total issuance (block reward plus uncle reward) for the given block.
func (api *ErigonImpl) Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
2021-04-03 06:26:00 +00:00
tx, err := api.db.BeginRo(ctx)
if err != nil {
return Issuance{}, err
}
defer tx.Rollback()
2021-04-08 11:03:45 +00:00
chainConfig, err := api.chainConfig(tx)
if err != nil {
return Issuance{}, err
}
if chainConfig.Ethash == nil {
// Clique for example has no issuance
return Issuance{}, nil
}
block, err := api.getBlockByRPCNumber(tx, blockNr)
if err != nil {
return Issuance{}, err
}
minerReward, uncleRewards := ethash.AccumulateRewards(chainConfig, block.Header(), block.Uncles())
issuance := minerReward
for _, r := range uncleRewards {
p := r // avoids warning?
issuance.Add(&issuance, &p)
}
var ret Issuance
ret.BlockReward = hexutil.EncodeBig(minerReward.ToBig())
ret.Issuance = hexutil.EncodeBig(issuance.ToBig())
issuance.Sub(&issuance, &minerReward)
ret.UncleReward = hexutil.EncodeBig(issuance.ToBig())
return ret, nil
}
func (api *ErigonImpl) getBlockByRPCNumber(tx kv.Tx, blockNr rpc.BlockNumber) (*types.Block, error) {
2021-03-30 09:53:54 +00:00
blockNum, err := getBlockNumber(blockNr, tx)
if err != nil {
return nil, err
}
return rawdb.ReadBlockByNumber(tx, blockNum)
}
// Issuance structure to return information about issuance
type Issuance struct {
BlockReward string `json:"blockReward,omitempty"`
UncleReward string `json:"uncleReward,omitempty"`
Issuance string `json:"issuance,omitempty"`
}