2020-09-25 12:12:36 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-20 21:16:28 +00:00
|
|
|
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"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"
|
2020-09-25 12:12:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BlockReward returns the block reward for this block
|
2021-05-26 10:35:39 +00:00
|
|
|
// func (api *ErigonImpl) BlockReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
|
2021-06-11 08:34:47 +00:00
|
|
|
// tx, err := api.db.Begin(ctx, ethdb.RO)
|
2020-10-24 17:03:52 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return Issuance{}, err
|
|
|
|
// }
|
|
|
|
// defer tx.Rollback()
|
|
|
|
//
|
|
|
|
// return api.rewardCalc(tx, blockNr, "block") // nolint goconst
|
|
|
|
//}
|
2020-09-25 12:12:36 +00:00
|
|
|
|
|
|
|
// UncleReward returns the uncle reward for this block
|
2021-05-26 10:35:39 +00:00
|
|
|
// func (api *ErigonImpl) UncleReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
|
2021-06-11 08:34:47 +00:00
|
|
|
// tx, err := api.db.Begin(ctx, ethdb.RO)
|
2020-10-24 17:03:52 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return Issuance{}, err
|
|
|
|
// }
|
|
|
|
// defer tx.Rollback()
|
|
|
|
//
|
|
|
|
// return api.rewardCalc(tx, blockNr, "uncle") // nolint goconst
|
|
|
|
//}
|
2020-09-25 12:12:36 +00:00
|
|
|
|
2021-05-26 10:35:39 +00:00
|
|
|
// 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)
|
2020-10-10 12:24:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return Issuance{}, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2021-04-08 11:03:45 +00:00
|
|
|
chainConfig, err := api.chainConfig(tx)
|
2020-10-24 06:57:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return Issuance{}, err
|
|
|
|
}
|
2020-10-03 08:07:49 +00:00
|
|
|
if chainConfig.Ethash == nil {
|
2020-09-26 06:41:34 +00:00
|
|
|
// Clique for example has no issuance
|
|
|
|
return Issuance{}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
block, err := api.getBlockByRPCNumber(tx, blockNr)
|
2020-09-25 12:12:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return Issuance{}, err
|
|
|
|
}
|
2020-10-03 08:07:49 +00:00
|
|
|
minerReward, uncleRewards := ethash.AccumulateRewards(chainConfig, block.Header(), block.Uncles())
|
2020-09-25 12:12:36 +00:00
|
|
|
issuance := minerReward
|
|
|
|
for _, r := range uncleRewards {
|
|
|
|
p := r // avoids warning?
|
|
|
|
issuance.Add(&issuance, &p)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret Issuance
|
2020-10-24 17:03:52 +00:00
|
|
|
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
|
2020-09-25 12:12:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
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)
|
2020-10-20 21:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-01 07:42:23 +00:00
|
|
|
return rawdb.ReadBlockByNumber(tx, blockNum)
|
2020-10-20 21:16:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 12:12:36 +00:00
|
|
|
// 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"`
|
|
|
|
}
|