2020-09-25 12:12:36 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-16 22:08:27 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2020-10-20 21:16:28 +00:00
|
|
|
|
2021-12-22 02:14:18 +00:00
|
|
|
"github.com/holiman/uint256"
|
2022-01-30 06:42:44 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/hexutil"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus/ethash"
|
2021-12-16 22:08:27 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
2021-05-20 18:25:53 +00:00
|
|
|
"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.
|
2021-12-16 22:08:27 +00:00
|
|
|
func (api *ErigonImpl) WatchTheBurn(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
|
|
|
|
}
|
2021-12-16 22:08:27 +00:00
|
|
|
hash, err := rawdb.ReadCanonicalHash(tx, uint64(blockNr))
|
2020-09-25 12:12:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return Issuance{}, err
|
|
|
|
}
|
2021-12-16 22:08:27 +00:00
|
|
|
header := rawdb.ReadHeader(tx, hash, uint64(blockNr))
|
|
|
|
if header == nil {
|
|
|
|
return Issuance{}, fmt.Errorf("could not find block header")
|
|
|
|
}
|
|
|
|
|
2022-01-07 13:52:38 +00:00
|
|
|
body := rawdb.ReadCanonicalBodyWithTransactions(tx, hash, uint64(blockNr))
|
2021-12-22 02:14:18 +00:00
|
|
|
|
2021-12-16 22:08:27 +00:00
|
|
|
if body == nil {
|
|
|
|
return Issuance{}, fmt.Errorf("could not find block body")
|
|
|
|
}
|
|
|
|
|
|
|
|
minerReward, uncleRewards := ethash.AccumulateRewards(chainConfig, header, body.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
|
2022-01-30 06:42:44 +00:00
|
|
|
ret.BlockReward = (*hexutil.Big)(minerReward.ToBig())
|
|
|
|
ret.Issuance = (*hexutil.Big)(issuance.ToBig())
|
2020-10-24 17:03:52 +00:00
|
|
|
issuance.Sub(&issuance, &minerReward)
|
2022-01-30 06:42:44 +00:00
|
|
|
ret.UncleReward = (*hexutil.Big)(issuance.ToBig())
|
2021-12-16 22:08:27 +00:00
|
|
|
// Compute how much was burnt
|
|
|
|
if header.BaseFee != nil {
|
2022-01-30 06:42:44 +00:00
|
|
|
burnt := header.BaseFee
|
|
|
|
burnt.Mul(burnt, big.NewInt(int64(header.GasUsed)))
|
|
|
|
ret.Burnt = (*hexutil.Big)(burnt)
|
2021-12-16 22:08:27 +00:00
|
|
|
} else {
|
2022-01-30 06:42:44 +00:00
|
|
|
ret.Burnt = (*hexutil.Big)(big.NewInt(0))
|
2021-12-16 22:08:27 +00:00
|
|
|
}
|
|
|
|
// Compute totalIssued, totalBurnt and the supply of eth
|
2022-01-30 06:42:44 +00:00
|
|
|
totalIssued, err := rawdb.ReadTotalIssued(tx, uint64(blockNr))
|
2020-10-20 21:16:28 +00:00
|
|
|
if err != nil {
|
2021-12-16 22:08:27 +00:00
|
|
|
return Issuance{}, err
|
2020-10-20 21:16:28 +00:00
|
|
|
}
|
2022-01-30 06:42:44 +00:00
|
|
|
totalBurnt, err := rawdb.ReadTotalBurnt(tx, uint64(blockNr))
|
2021-12-16 22:08:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return Issuance{}, err
|
|
|
|
}
|
2022-01-30 06:42:44 +00:00
|
|
|
|
|
|
|
ret.TotalIssued = (*hexutil.Big)(totalIssued)
|
|
|
|
ret.TotalBurnt = (*hexutil.Big)(totalBurnt)
|
|
|
|
|
2021-12-22 02:14:18 +00:00
|
|
|
// Compute tips
|
2022-01-30 06:42:44 +00:00
|
|
|
tips := big.NewInt(0)
|
2021-12-22 02:14:18 +00:00
|
|
|
|
|
|
|
if header.BaseFee != nil {
|
|
|
|
receipts, err := rawdb.ReadReceiptsByHash(tx, hash)
|
|
|
|
if err != nil {
|
|
|
|
return Issuance{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
baseFee, overflow := uint256.FromBig(header.BaseFee)
|
|
|
|
if overflow {
|
|
|
|
return Issuance{}, fmt.Errorf("baseFee overflow")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, transaction := range body.Transactions {
|
|
|
|
tip := transaction.GetEffectiveGasTip(baseFee).ToBig()
|
2022-01-30 06:42:44 +00:00
|
|
|
tips.Add(tips, tip.Mul(tip, big.NewInt(int64(receipts[i].GasUsed))))
|
2021-12-22 02:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-30 06:42:44 +00:00
|
|
|
ret.Tips = (*hexutil.Big)(tips)
|
2021-12-16 22:08:27 +00:00
|
|
|
return ret, nil
|
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 {
|
2022-01-30 06:42:44 +00:00
|
|
|
BlockReward *hexutil.Big `json:"blockReward"` // Block reward for given block
|
|
|
|
UncleReward *hexutil.Big `json:"uncleReward"` // Uncle reward for gived block
|
|
|
|
Issuance *hexutil.Big `json:"issuance"` // Total amount of wei created in the block
|
|
|
|
Burnt *hexutil.Big `json:"burnt"` // Total amount of wei burned in the block
|
|
|
|
TotalIssued *hexutil.Big `json:"totalIssued"` // Total amount of wei created in total so far
|
|
|
|
TotalBurnt *hexutil.Big `json:"totalBurnt"` // Total amount of wei burnt so far
|
|
|
|
Tips *hexutil.Big `json:"tips"` // Total Tips generated by the block
|
2020-09-25 12:12:36 +00:00
|
|
|
}
|