forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_writer.go
119 lines (104 loc) · 3.36 KB
/
block_writer.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
package blockio
import (
"context"
"encoding/binary"
"github.com/nebojsa94/erigon/erigon-lib/kv/dbutils"
"github.com/nebojsa94/erigon/erigon-lib/common"
"github.com/nebojsa94/erigon/erigon-lib/common/hexutility"
"github.com/nebojsa94/erigon/erigon-lib/etl"
"github.com/nebojsa94/erigon/erigon-lib/kv"
"github.com/nebojsa94/erigon/erigon-lib/kv/rawdbv3"
"github.com/nebojsa94/erigon/core/rawdb"
"github.com/nebojsa94/erigon/turbo/backup"
"github.com/ledgerwatch/log/v3"
)
//Naming:
// Prune: delete old data
// Unwind: delete recent data
// BlockReader can read blocks from db and snapshots
type BlockWriter struct {
historyV3 bool
// adding Auto-Increment BlockID
// allow store non-canonical Txs/Senders
txsV3 bool
}
func NewBlockWriter(historyV3 bool) *BlockWriter {
return &BlockWriter{historyV3: historyV3, txsV3: true}
}
func (w *BlockWriter) FillHeaderNumberIndex(logPrefix string, tx kv.RwTx, tmpDir string, from, to uint64, ctx context.Context, logger log.Logger) error {
startKey := make([]byte, 8)
binary.BigEndian.PutUint64(startKey, from)
endKey := dbutils.HeaderKey(to, common.Hash{}) // etl.Tranform uses ExractEndKey as exclusive bound, therefore +1
return etl.Transform(
logPrefix,
tx,
kv.Headers,
kv.HeaderNumber,
tmpDir,
extractHeaders,
etl.IdentityLoadFunc,
etl.TransformArgs{
ExtractStartKey: startKey,
ExtractEndKey: endKey,
Quit: ctx.Done(),
},
logger,
)
}
func (w *BlockWriter) MakeBodiesCanonical(tx kv.RwTx, from uint64) error {
if w.historyV3 {
if err := rawdb.AppendCanonicalTxNums(tx, from); err != nil {
return err
}
}
return nil
}
func (w *BlockWriter) MakeBodiesNonCanonical(tx kv.RwTx, from uint64) error {
if w.historyV3 {
if err := rawdbv3.TxNums.Truncate(tx, from); err != nil {
return err
}
}
return nil
}
func extractHeaders(k []byte, _ []byte, next etl.ExtractNextFunc) error {
// We only want to extract entries composed by Block Number + Header Hash
if len(k) != 40 {
return nil
}
return next(k, k[8:], k[:8])
}
func (w *BlockWriter) TruncateBodies(db kv.RoDB, tx kv.RwTx, from uint64) error {
fromB := hexutility.EncodeTs(from)
if err := tx.ForEach(kv.BlockBody, fromB, func(k, _ []byte) error { return tx.Delete(kv.BlockBody, k) }); err != nil {
return err
}
if err := backup.ClearTables(context.Background(), db, tx,
kv.NonCanonicalTxs,
kv.EthTx,
kv.MaxTxNum,
); err != nil {
return err
}
if err := rawdb.ResetSequence(tx, kv.EthTx, 0); err != nil {
return err
}
if err := rawdb.ResetSequence(tx, kv.NonCanonicalTxs, 0); err != nil {
return err
}
return nil
}
// PruneBlocks - [1, to) old blocks after moving it to snapshots.
// keeps genesis in db
// doesn't change sequences of kv.EthTx and kv.NonCanonicalTxs
// doesn't delete Receipts, Senders, Canonical markers, TotalDifficulty
func (w *BlockWriter) PruneBlocks(ctx context.Context, tx kv.RwTx, blockTo uint64, blocksDeleteLimit int) error {
return rawdb.PruneBlocks(tx, blockTo, blocksDeleteLimit)
}
// PruneBorBlocks - [1, to) old blocks after moving it to snapshots.
// keeps genesis in db
// doesn't change sequences of kv.EthTx and kv.NonCanonicalTxs
// doesn't delete Receipts, Senders, Canonical markers, TotalDifficulty
func (w *BlockWriter) PruneBorBlocks(ctx context.Context, tx kv.RwTx, blockTo uint64, blocksDeleteLimit int) error {
return rawdb.PruneBorBlocks(tx, blockTo, blocksDeleteLimit)
}