forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 8
/
stage_issuance.go
220 lines (200 loc) · 5.59 KB
/
stage_issuance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package stagedsync
import (
"bytes"
"context"
"fmt"
"math/big"
"time"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/consensus/serenity"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/services"
)
type IssuanceCfg struct {
db kv.RwDB
chainConfig *chain.Config
blockReader services.FullBlockReader
enabledIssuance bool
}
func StageIssuanceCfg(db kv.RwDB, chainConfig *chain.Config, blockReader services.FullBlockReader, enabledIssuance bool) IssuanceCfg {
return IssuanceCfg{
db: db,
chainConfig: chainConfig,
blockReader: blockReader,
enabledIssuance: enabledIssuance,
}
}
func SpawnStageIssuance(cfg IssuanceCfg, s *StageState, tx kv.RwTx, ctx context.Context) error {
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
}
headNumber, err := stages.GetStageProgress(tx, stages.Bodies)
if err != nil {
return fmt.Errorf("getting headers progress: %w", err)
}
if cfg.chainConfig.Consensus != chain.EtHashConsensus || !cfg.enabledIssuance || headNumber == s.BlockNumber {
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
// Log timer
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
// Read previous issuance
totalIssued, err := rawdb.ReadTotalIssued(tx, s.BlockNumber)
if err != nil {
return err
}
totalBurnt, err := rawdb.ReadTotalBurnt(tx, s.BlockNumber)
if err != nil {
return err
}
stopped := false
prevProgress := s.BlockNumber
currentBlockNumber := s.BlockNumber + 1
headerC, err := tx.Cursor(kv.Headers)
if err != nil {
return err
}
for k, v, err := headerC.Seek(hexutility.EncodeTs(currentBlockNumber)); k != nil && !stopped; k, v, err = headerC.Next() {
if err != nil {
return err
}
if len(k) != 40 {
continue
}
currentBlockNumber, err = dbutils.DecodeBlockNumber(k[:8])
if err != nil {
return err
}
if currentBlockNumber > headNumber {
currentBlockNumber = headNumber
break
}
// read body without transactions
hash, err := rawdb.ReadCanonicalHash(tx, currentBlockNumber)
if err != nil {
return err
}
if hash != libcommon.BytesToHash(k[8:]) {
continue
}
var header types.Header
if err := rlp.Decode(bytes.NewReader(v), &header); err != nil {
log.Error("Invalid block header RLP", "hash", hash, "err", err)
return nil
}
burnt := big.NewInt(0)
// burnt: len(Transactions) * baseFee * gasUsed
if header.BaseFee != nil {
burnt.Set(header.BaseFee)
burnt.Mul(burnt, big.NewInt(int64(header.GasUsed)))
}
// TotalIssued, BlockReward and UncleReward, depends on consensus engine
if header.Difficulty.Cmp(serenity.SerenityDifficulty) == 0 {
// Proof-of-stake is 0.3 ether per block
totalIssued.Add(totalIssued, serenity.RewardSerenity)
} else {
var blockReward uint256.Int
var uncleRewards []uint256.Int
if header.UncleHash == types.EmptyUncleHash {
blockReward, uncleRewards = ethash.AccumulateRewards(cfg.chainConfig, &header, nil)
} else {
body, _, err := cfg.blockReader.Body(ctx, tx, hash, currentBlockNumber)
if err != nil {
return err
}
blockReward, uncleRewards = ethash.AccumulateRewards(cfg.chainConfig, &header, body.Uncles)
}
// Set BlockReward
totalIssued.Add(totalIssued, blockReward.ToBig())
// Compute uncleRewards
for _, uncleReward := range uncleRewards {
totalIssued.Add(totalIssued, uncleReward.ToBig())
}
}
totalBurnt.Add(totalBurnt, burnt)
// Write to database
if err := rawdb.WriteTotalIssued(tx, currentBlockNumber, totalIssued); err != nil {
return err
}
if err := rawdb.WriteTotalBurnt(tx, currentBlockNumber, totalBurnt); err != nil {
return err
}
// Sleep and check for logs
select {
case <-ctx.Done():
stopped = true
case <-logEvery.C:
log.Info(fmt.Sprintf("[%s] Wrote Block Issuance", s.LogPrefix()),
"now", currentBlockNumber, "blk/sec", float64(currentBlockNumber-prevProgress)/float64(logInterval/time.Second))
prevProgress = currentBlockNumber
default:
log.Trace("RequestQueueTime (header) ticked")
}
// Cleanup timer
}
if err = s.Update(tx, currentBlockNumber); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func UnwindIssuanceStage(u *UnwindState, cfg IssuanceCfg, tx kv.RwTx, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
if err = u.Done(tx); err != nil {
return fmt.Errorf(" reset: %w", err)
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return fmt.Errorf("failed to write db commit: %w", err)
}
}
return nil
}
func PruneIssuanceStage(p *PruneState, cfg IssuanceCfg, tx kv.RwTx, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}