-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstage_difficulty.go
158 lines (139 loc) · 3.7 KB
/
stage_difficulty.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
package stagedsync
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"math/big"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/interfaces"
"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"
)
type DifficultyCfg struct {
tmpDir string
terminalTotalDifficulty *big.Int
db kv.RwDB
blockReader interfaces.FullBlockReader
}
func StageDifficultyCfg(db kv.RwDB, tmpDir string, terminalTotalDifficulty *big.Int, blockReader interfaces.FullBlockReader) DifficultyCfg {
return DifficultyCfg{
db: db,
tmpDir: tmpDir,
terminalTotalDifficulty: terminalTotalDifficulty,
blockReader: blockReader,
}
}
func SpawnDifficultyStage(s *StageState, tx kv.RwTx, cfg DifficultyCfg, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
}
quit := ctx.Done()
headNumber, err := stages.GetStageProgress(tx, stages.Headers)
if err != nil {
return fmt.Errorf("getting headers progress: %w", err)
}
td := big.NewInt(0)
if s.BlockNumber > 0 {
td, err = rawdb.ReadTd(tx, rawdb.ReadHeaderByNumber(tx, s.BlockNumber).Hash(), s.BlockNumber)
if err != nil {
return err
}
}
// If the chain does not have a proof of stake config or has reached terminalTotalDifficulty then we can skip this stage
if cfg.terminalTotalDifficulty == nil || td.Cmp(cfg.terminalTotalDifficulty) >= 0 {
if err = s.Update(tx, headNumber); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
startKey := make([]byte, 8)
binary.BigEndian.PutUint64(startKey, s.BlockNumber)
header := new(types.Header)
if err := etl.Transform(
s.LogPrefix(),
tx,
kv.Headers,
kv.HeaderTD,
cfg.tmpDir,
func(k []byte, v []byte, next etl.ExtractNextFunc) error {
if len(k) != 40 {
return nil
}
blockNum := binary.BigEndian.Uint64(k)
canonical, err := rawdb.ReadCanonicalHash(tx, blockNum)
if err != nil {
return err
}
if !bytes.Equal(k[8:], canonical[:]) {
return nil
}
if err := rlp.Decode(bytes.NewReader(v), header); err != nil {
return err
}
if header.Difficulty.Cmp(serenity.SerenityDifficulty) == 0 {
// Proof-of-Stake block
// TODO(yperbasis): double check that it's secure
return nil
}
td.Add(td, header.Difficulty)
data, err := rlp.EncodeToBytes(td)
if err != nil {
return fmt.Errorf("failed to RLP encode block total difficulty: %w", err)
}
return next(k, k, data)
},
etl.IdentityLoadFunc,
etl.TransformArgs{
ExtractStartKey: startKey,
Quit: quit,
},
); err != nil {
return err
}
if err = s.Update(tx, headNumber); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func UnwindDifficultyStage(u *UnwindState, tx kv.RwTx, ctx context.Context) (err error) {
useExternalTx := tx != nil
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 PruneDifficultyStage(p *PruneState, tx kv.RwTx, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}