2020-09-25 12:12:36 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-20 21:16:28 +00:00
|
|
|
|
2020-09-25 12:12:36 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/consensus/ethash"
|
2020-10-03 08:07:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core/rawdb"
|
2020-10-20 21:16:28 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
2020-10-25 08:38:55 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2020-09-25 12:12:36 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BlockReward returns the block reward for this block
|
2020-10-24 17:03:52 +00:00
|
|
|
// func (api *TgImpl) BlockReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
|
2020-10-25 08:38:55 +00:00
|
|
|
// tx, err := api.dbReader.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
|
2020-10-24 17:03:52 +00:00
|
|
|
// func (api *TgImpl) UncleReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
|
2020-10-25 08:38:55 +00:00
|
|
|
// tx, err := api.dbReader.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
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// Issuance implements tg_issuance. Returns the total issuance (block reward plus uncle reward) for the given block.
|
2020-10-20 21:16:28 +00:00
|
|
|
func (api *TgImpl) Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
|
2021-03-30 09:53:54 +00:00
|
|
|
tx, err := api.db.Begin(ctx)
|
2020-10-10 12:24:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return Issuance{}, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2021-03-30 09:53:54 +00:00
|
|
|
genesis, err := rawdb.ReadBlockByNumber(ethdb.NewRoTxDb(tx), 0)
|
2020-10-24 06:57:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return Issuance{}, err
|
|
|
|
}
|
|
|
|
genesisHash := genesis.Hash()
|
2021-03-30 09:53:54 +00:00
|
|
|
chainConfig, err := rawdb.ReadChainConfig(ethdb.NewRoTxDb(tx), genesisHash)
|
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-03-30 09:53:54 +00:00
|
|
|
func (api *TgImpl) getBlockByRPCNumber(tx ethdb.Tx, blockNr rpc.BlockNumber) (*types.Block, error) {
|
|
|
|
blockNum, err := getBlockNumber(blockNr, tx)
|
2020-10-20 21:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-30 09:53:54 +00:00
|
|
|
return rawdb.ReadBlockByNumber(ethdb.NewRoTxDb(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"`
|
|
|
|
}
|