@@ -19,120 +19,87 @@ package supply
1919import (
2020 "fmt"
2121 "math/big"
22- "time"
2322
2423 "github.com/ethereum/go-ethereum/consensus/ethash"
2524 "github.com/ethereum/go-ethereum/core/types"
26- "github.com/ethereum/go-ethereum/log"
2725 "github.com/ethereum/go-ethereum/params"
2826 "github.com/ethereum/go-ethereum/rlp"
2927 "github.com/ethereum/go-ethereum/trie"
3028)
3129
32- // Delta calculates the Ether delta across two state tries. That is, the
30+ // Delta calculates the ether delta across two state tries. That is, the
3331// issuance minus the ether destroyed.
34- func Delta (block * types.Block , parent * types.Header , db * trie.Database , config * params.ChainConfig ) (* big.Int , error ) {
35- var (
36- supplyDelta = new (big.Int )
37- start = time .Now ()
38- )
39- // Open the two tries.
40- if block .ParentHash () != parent .Hash () {
41- return nil , fmt .Errorf ("parent hash mismatch: have %s, want %s" , block .ParentHash ().Hex (), parent .Hash ().Hex ())
42- }
43- src , err := trie .New (trie .StateTrieID (parent .Root ), db )
32+ func Delta (src , dst * types.Header , db * trie.Database ) (* big.Int , error ) {
33+ // Open src and dst tries.
34+ srcTrie , err := trie .New (trie .StateTrieID (src .Root ), db )
4435 if err != nil {
4536 return nil , fmt .Errorf ("failed to open source trie: %v" , err )
4637 }
47- dst , err := trie .New (trie .StateTrieID (block .Root () ), db )
38+ dstTrie , err := trie .New (trie .StateTrieID (dst .Root ), db )
4839 if err != nil {
4940 return nil , fmt .Errorf ("failed to open destination trie: %v" , err )
5041 }
42+
43+ delta := new (big.Int )
44+
5145 // Gather all the changes across from source to destination.
52- fwdDiffIt , _ := trie .NewDifferenceIterator (src .MustNodeIterator (nil ), dst .MustNodeIterator (nil ))
46+ fwdDiffIt , _ := trie .NewDifferenceIterator (srcTrie .MustNodeIterator (nil ), dstTrie .MustNodeIterator (nil ))
5347 fwdIt := trie .NewIterator (fwdDiffIt )
5448
5549 for fwdIt .Next () {
5650 acc := new (types.StateAccount )
5751 if err := rlp .DecodeBytes (fwdIt .Value , acc ); err != nil {
5852 panic (err )
5953 }
60- supplyDelta .Add (supplyDelta , acc .Balance )
54+ delta .Add (delta , acc .Balance )
6155 }
6256 // Gather all the changes across from destination to source.
63- rewDiffIt , _ := trie .NewDifferenceIterator (dst .MustNodeIterator (nil ), src .MustNodeIterator (nil ))
64- rewIt := trie .NewIterator (rewDiffIt )
57+ revDiffIt , _ := trie .NewDifferenceIterator (dstTrie .MustNodeIterator (nil ), srcTrie .MustNodeIterator (nil ))
58+ revIt := trie .NewIterator (revDiffIt )
6559
66- for rewIt .Next () {
60+ for revIt .Next () {
6761 acc := new (types.StateAccount )
68- if err := rlp .DecodeBytes (rewIt .Value , acc ); err != nil {
62+ if err := rlp .DecodeBytes (revIt .Value , acc ); err != nil {
6963 panic (err )
7064 }
71- supplyDelta .Sub (supplyDelta , acc .Balance )
65+ delta .Sub (delta , acc .Balance )
7266 }
73- // Calculate the block fixedReward based on chain rules and progression.
74- fixedReward , unclesReward , burn , withdrawals := Subsidy (block , config )
75-
76- // Calculate the difference between the "calculated" and "crawled" supply
77- // delta.
78- diff := new (big.Int ).Set (supplyDelta )
79- diff .Sub (diff , fixedReward )
80- diff .Sub (diff , unclesReward )
81- diff .Add (diff , burn )
8267
83- log .Info ("Calculated supply delta for block" , "number" , block .Number (), "hash" , block .Hash (), "supplydelta" , supplyDelta , "fixedreward" , fixedReward , "unclesreward" , unclesReward , "burn" , burn , "withdrawals" , withdrawals , "diff" , diff , "elapsed" , time .Since (start ))
84- return supplyDelta , nil
68+ return delta , nil
8569}
8670
87- // Subsidy calculates the block mining (fixed) and uncle subsidy as well as the
88- // 1559 burn solely based on header fields. This method is a very accurate
89- // approximation of the true supply delta, but cannot take into account Ether
90- // burns via selfdestructs, so it will always be ever so slightly off.
91- func Subsidy (block * types.Block , config * params.ChainConfig ) (fixedReward * big.Int , unclesReward * big.Int , burn * big.Int , withdrawals * big.Int ) {
92- // Calculate the block rewards based on chain rules and progression.
93- fixedReward = new (big.Int )
94- unclesReward = new (big.Int )
95- withdrawals = new (big.Int )
96-
97- // Select the correct block reward based on chain progression.
98- if config .Ethash != nil {
99- if block .Difficulty ().BitLen () != 0 {
100- fixedReward = ethash .FrontierBlockReward
101- if config .IsByzantium (block .Number ()) {
102- fixedReward = ethash .ByzantiumBlockReward
103- }
104- if config .IsConstantinople (block .Number ()) {
105- fixedReward = ethash .ConstantinopleBlockReward
106- }
71+ // Subsidy calculates the coinbase subsidy and uncle subsidy as well as the
72+ // EIP-1559 burn. This method is a very accurate approximation of the true
73+ // supply delta, but cannot take into account ether burns via selfdestructs, so
74+ // it will always be slightly off.
75+ func Subsidy (block * types.Block , config * params.ChainConfig ) (* big.Int , * big.Int , * big.Int , * big.Int ) {
76+ var (
77+ coinbaseReward = new (big.Int )
78+ unclesReward = new (big.Int )
79+ withdrawals = new (big.Int )
80+ )
81+ // If block is ethash, calculate the coinbase and uncle rewards.
82+ if config .Ethash != nil && block .Difficulty ().BitLen () != 0 {
83+ accCoinbase := func (h * types.Header , amt * big.Int ) {
84+ coinbaseReward .Add (coinbaseReward , amt )
10785 }
108- // Accumulate the rewards for included uncles.
109- var (
110- big8 = big .NewInt (8 )
111- big32 = big .NewInt (32 )
112- r = new (big.Int )
113- )
114- for _ , uncle := range block .Uncles () {
115- // Add the reward for the side blocks.
116- r .Add (uncle .Number , big8 )
117- r .Sub (r , block .Number ())
118- r .Mul (r , fixedReward )
119- r .Div (r , big8 )
120- unclesReward .Add (unclesReward , r )
121-
122- // Add the reward for accumulating the side blocks.
123- r .Div (fixedReward , big32 )
124- unclesReward .Add (unclesReward , r )
86+ accUncles := func (h * types.Header , amt * big.Int ) {
87+ unclesReward .Add (unclesReward , amt )
12588 }
89+ ethash .AccumulateRewards (config , block .Header (), block .Uncles (), accCoinbase , accUncles )
12690 }
12791 // Calculate the burn based on chain rules and progression.
128- burn = new (big.Int )
92+ burn : = new (big.Int )
12993 if block .BaseFee () != nil {
13094 burn = new (big.Int ).Mul (new (big.Int ).SetUint64 (block .GasUsed ()), block .BaseFee ())
13195 }
132-
96+ // Sum up withdrawals.
13397 for _ , w := range block .Withdrawals () {
134- withdrawals .Add (withdrawals , big . NewInt ( int64 ( w .Amount ) ))
98+ withdrawals .Add (withdrawals , newGwei ( w .Amount ))
13599 }
100+ return coinbaseReward , unclesReward , burn , withdrawals
101+ }
136102
137- return fixedReward , unclesReward , burn , withdrawals
103+ func newGwei (n uint64 ) * big.Int {
104+ return new (big.Int ).Mul (big .NewInt (int64 (n )), big .NewInt (params .GWei ))
138105}
0 commit comments