@@ -19,120 +19,87 @@ package supply
19
19
import (
20
20
"fmt"
21
21
"math/big"
22
- "time"
23
22
24
23
"github.com/ethereum/go-ethereum/consensus/ethash"
25
24
"github.com/ethereum/go-ethereum/core/types"
26
- "github.com/ethereum/go-ethereum/log"
27
25
"github.com/ethereum/go-ethereum/params"
28
26
"github.com/ethereum/go-ethereum/rlp"
29
27
"github.com/ethereum/go-ethereum/trie"
30
28
)
31
29
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
33
31
// 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 )
44
35
if err != nil {
45
36
return nil , fmt .Errorf ("failed to open source trie: %v" , err )
46
37
}
47
- dst , err := trie .New (trie .StateTrieID (block .Root () ), db )
38
+ dstTrie , err := trie .New (trie .StateTrieID (dst .Root ), db )
48
39
if err != nil {
49
40
return nil , fmt .Errorf ("failed to open destination trie: %v" , err )
50
41
}
42
+
43
+ delta := new (big.Int )
44
+
51
45
// 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 ))
53
47
fwdIt := trie .NewIterator (fwdDiffIt )
54
48
55
49
for fwdIt .Next () {
56
50
acc := new (types.StateAccount )
57
51
if err := rlp .DecodeBytes (fwdIt .Value , acc ); err != nil {
58
52
panic (err )
59
53
}
60
- supplyDelta .Add (supplyDelta , acc .Balance )
54
+ delta .Add (delta , acc .Balance )
61
55
}
62
56
// 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 )
65
59
66
- for rewIt .Next () {
60
+ for revIt .Next () {
67
61
acc := new (types.StateAccount )
68
- if err := rlp .DecodeBytes (rewIt .Value , acc ); err != nil {
62
+ if err := rlp .DecodeBytes (revIt .Value , acc ); err != nil {
69
63
panic (err )
70
64
}
71
- supplyDelta .Sub (supplyDelta , acc .Balance )
65
+ delta .Sub (delta , acc .Balance )
72
66
}
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 )
82
67
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
85
69
}
86
70
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 )
107
85
}
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 )
125
88
}
89
+ ethash .AccumulateRewards (config , block .Header (), block .Uncles (), accCoinbase , accUncles )
126
90
}
127
91
// Calculate the burn based on chain rules and progression.
128
- burn = new (big.Int )
92
+ burn : = new (big.Int )
129
93
if block .BaseFee () != nil {
130
94
burn = new (big.Int ).Mul (new (big.Int ).SetUint64 (block .GasUsed ()), block .BaseFee ())
131
95
}
132
-
96
+ // Sum up withdrawals.
133
97
for _ , w := range block .Withdrawals () {
134
- withdrawals .Add (withdrawals , big . NewInt ( int64 ( w .Amount ) ))
98
+ withdrawals .Add (withdrawals , newGwei ( w .Amount ))
135
99
}
100
+ return coinbaseReward , unclesReward , burn , withdrawals
101
+ }
136
102
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 ))
138
105
}
0 commit comments