-
Notifications
You must be signed in to change notification settings - Fork 20.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd, core, eth: add issuance tracking and querying #24723
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
57dd1ec
cmd, core, eth: add issuance calculation and logging
karalabe 35bebd6
cmd/geth: add initial balance calculation from snapshot
holiman b8b91b2
cmd/geth: integrate the initial balance count
holiman 29025ab
core, eth: track issuance on disk, add rpc sub for it
karalabe bd13c32
core/rawdb, eth: fix lost sign bit, shorten API output fields
karalabe c120a3d
cmd/geth, ore/issuance: integrate total supply calculation
karalabe 02e929a
core/issuance: tiny log tweaks
karalabe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// Copyright 2022 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package issuance | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/consensus/ethash" | ||
"github.com/ethereum/go-ethereum/core/state/snapshot" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/params" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
"github.com/ethereum/go-ethereum/trie" | ||
) | ||
|
||
// Issuance calculates the Ether issuance (or burn) across two state tries. In | ||
// normal mode of operation, the expectation is to calculate the issuance between | ||
// two consecutive blocks. | ||
func Issuance(block *types.Block, parent *types.Header, db *trie.Database, config *params.ChainConfig) (*big.Int, error) { | ||
var ( | ||
issuance = new(big.Int) | ||
start = time.Now() | ||
) | ||
// Open the two tries | ||
if block.ParentHash() != parent.Hash() { | ||
return nil, fmt.Errorf("parent hash mismatch: have %s, want %s", block.ParentHash().Hex(), parent.Hash().Hex()) | ||
} | ||
src, err := trie.New(parent.Root, db) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open source trie: %v", err) | ||
} | ||
dst, err := trie.New(block.Root(), db) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open destination trie: %v", err) | ||
} | ||
// Gather all the changes across from source to destination | ||
fwdDiffIt, _ := trie.NewDifferenceIterator(src.NodeIterator(nil), dst.NodeIterator(nil)) | ||
fwdIt := trie.NewIterator(fwdDiffIt) | ||
|
||
for fwdIt.Next() { | ||
acc := new(types.StateAccount) | ||
if err := rlp.DecodeBytes(fwdIt.Value, acc); err != nil { | ||
panic(err) | ||
} | ||
issuance.Add(issuance, acc.Balance) | ||
} | ||
// Gather all the changes across from destination to source | ||
rewDiffIt, _ := trie.NewDifferenceIterator(dst.NodeIterator(nil), src.NodeIterator(nil)) | ||
rewIt := trie.NewIterator(rewDiffIt) | ||
|
||
for rewIt.Next() { | ||
acc := new(types.StateAccount) | ||
if err := rlp.DecodeBytes(rewIt.Value, acc); err != nil { | ||
panic(err) | ||
} | ||
issuance.Sub(issuance, acc.Balance) | ||
} | ||
// Calculate the block subsidy based on chain rules and progression | ||
subsidy, uncles, burn := Subsidy(block, config) | ||
|
||
// Calculate the difference between the "calculated" and "crawled" issuance | ||
diff := new(big.Int).Set(issuance) | ||
diff.Sub(diff, subsidy) | ||
diff.Sub(diff, uncles) | ||
diff.Add(diff, burn) | ||
|
||
log.Info("Calculated issuance for block", "number", block.Number(), "hash", block.Hash(), "state", issuance, "subsidy", subsidy, "uncles", uncles, "burn", burn, "diff", diff, "elapsed", time.Since(start)) | ||
return issuance, nil | ||
} | ||
|
||
// Subsidy calculates the block mining and uncle subsidy as well as the 1559 burn | ||
// solely based on header fields. This method is a very accurate approximation of | ||
// the true issuance, but cannot take into account Ether burns via selfdestructs, | ||
// so it will always be ever so slightly off. | ||
func Subsidy(block *types.Block, config *params.ChainConfig) (subsidy *big.Int, uncles *big.Int, burn *big.Int) { | ||
// Calculate the block subsidy based on chain rules and progression | ||
subsidy = new(big.Int) | ||
uncles = new(big.Int) | ||
|
||
// Select the correct block reward based on chain progression | ||
if config.Ethash != nil { | ||
if block.Difficulty().BitLen() != 0 { | ||
subsidy = ethash.FrontierBlockReward | ||
if config.IsByzantium(block.Number()) { | ||
subsidy = ethash.ByzantiumBlockReward | ||
} | ||
if config.IsConstantinople(block.Number()) { | ||
subsidy = ethash.ConstantinopleBlockReward | ||
} | ||
} | ||
// Accumulate the rewards for inclded uncles | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. *included (: |
||
var ( | ||
big8 = big.NewInt(8) | ||
big32 = big.NewInt(32) | ||
r = new(big.Int) | ||
) | ||
for _, uncle := range block.Uncles() { | ||
// Add the reward for the side blocks | ||
r.Add(uncle.Number, big8) | ||
r.Sub(r, block.Number()) | ||
r.Mul(r, subsidy) | ||
r.Div(r, big8) | ||
uncles.Add(uncles, r) | ||
|
||
// Add the reward for accumulating the side blocks | ||
r.Div(subsidy, big32) | ||
uncles.Add(uncles, r) | ||
} | ||
} | ||
// Calculate the burn based on chain rules and progression | ||
burn = new(big.Int) | ||
if block.BaseFee() != nil { | ||
burn = new(big.Int).Mul(new(big.Int).SetUint64(block.GasUsed()), block.BaseFee()) | ||
} | ||
return subsidy, uncles, burn | ||
} | ||
|
||
// Supply crawls the state snapshot at a given header and gatheres all the account | ||
// balances to sum into the total Ether supply. | ||
func Supply(header *types.Header, snaptree *snapshot.Tree) (*big.Int, error) { | ||
accIt, err := snaptree.AccountIterator(header.Root, common.Hash{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer accIt.Release() | ||
|
||
log.Info("Ether supply counting started", "block", header.Number, "hash", header.Hash(), "root", header.Root) | ||
var ( | ||
start = time.Now() | ||
logged = time.Now() | ||
accounts uint64 | ||
) | ||
supply := big.NewInt(0) | ||
for accIt.Next() { | ||
account, err := snapshot.FullAccount(accIt.Account()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
supply.Add(supply, account.Balance) | ||
accounts++ | ||
if time.Since(logged) > 8*time.Second { | ||
log.Info("Ether supply counting in progress", "at", accIt.Hash(), | ||
"accounts", accounts, "supply", supply, "elapsed", common.PrettyDuration(time.Since(start))) | ||
logged = time.Now() | ||
} | ||
} | ||
log.Info("Ether supply counting complete", "block", header.Number, "hash", header.Hash(), "root", header.Root, | ||
"accounts", accounts, "supply", supply, "elapsed", common.PrettyDuration(time.Since(start))) | ||
|
||
return supply, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this is not issuance, but "EtherDelta". Issuance is "how much is issued", which IMO should be mining-rewards.
This is how I think of the terms:
rewards
: mining rewardsburn
: 1559 burnissuance
:rewards
-burn
delta
:issuance
-destroyed ether
And the
supply
is thedelta
betweenempty-state
(before genesis) and chain head.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comment here :) #24723 (comment)
"delta" is a great word, probably better than "diff" or "change". I'll edit my comment above to use "delta".