-
Notifications
You must be signed in to change notification settings - Fork 151
/
snap_merge.go
151 lines (128 loc) · 6.12 KB
/
snap_merge.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
package toolset
import (
"context"
"fmt"
"os"
"time"
flag "github.com/spf13/pflag"
"github.com/iotaledger/hive.go/app/configuration"
"github.com/iotaledger/hornet/v2/pkg/model/utxo"
"github.com/iotaledger/hornet/v2/pkg/snapshot"
iotago "github.com/iotaledger/iota.go/v3"
)
func snapshotMerge(args []string) error {
fs := configuration.NewUnsortedFlagSet("", flag.ContinueOnError)
snapshotPathFullFlag := fs.String(FlagToolSnapshotPathFull, "", "the path to the full snapshot file")
snapshotPathDeltaFlag := fs.String(FlagToolSnapshotPathDelta, "", "the path to the delta snapshot file")
snapshotPathTargetFlag := fs.String(FlagToolSnapshotPathTarget, "", "the path to the target/merged snapshot file")
outputJSONFlag := fs.Bool(FlagToolOutputJSON, false, FlagToolDescriptionOutputJSON)
fs.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", ToolSnapMerge)
fs.PrintDefaults()
println(fmt.Sprintf("\nexample: %s --%s %s --%s %s --%s %s",
ToolSnapMerge,
FlagToolSnapshotPathFull,
"snapshots/mainnet/full_snapshot.bin",
FlagToolSnapshotPathDelta,
"snapshots/mainnet/delta_snapshot.bin",
FlagToolSnapshotPathTarget,
"merged_snapshot.bin"))
}
if err := parseFlagSet(fs, args); err != nil {
return err
}
if len(*snapshotPathFullFlag) == 0 {
return fmt.Errorf("'%s' not specified", FlagToolSnapshotPathFull)
}
if len(*snapshotPathDeltaFlag) == 0 {
return fmt.Errorf("'%s' not specified", FlagToolSnapshotPathDelta)
}
if len(*snapshotPathTargetFlag) == 0 {
return fmt.Errorf("'%s' not specified", FlagToolSnapshotPathTarget)
}
var fullPath, deltaPath, targetPath = *snapshotPathFullFlag, *snapshotPathDeltaFlag, *snapshotPathTargetFlag
if !*outputJSONFlag {
fmt.Println("merging snapshot files ...")
}
ts := time.Now()
mergeInfo, err := snapshot.MergeSnapshotsFiles(context.Background(), fullPath, deltaPath, targetPath)
if err != nil {
return err
}
if !*outputJSONFlag {
fmt.Printf("metadata:\n")
}
_ = printFullSnapshotHeaderInfo("full", fullPath, mergeInfo.FullSnapshotHeader)
_ = printDeltaSnapshotHeaderInfo("delta", deltaPath, mergeInfo.DeltaSnapshotHeader)
_ = printFullSnapshotHeaderInfo("merged", targetPath, mergeInfo.MergedSnapshotHeader)
if !*outputJSONFlag {
fmt.Printf("successfully created merged full snapshot '%s', took %v\n", targetPath, time.Since(ts).Truncate(time.Millisecond))
}
return nil
}
// prints information about the given full snapshot file header.
func printFullSnapshotHeaderInfo(name string, path string, fullHeader *snapshot.FullSnapshotHeader) error {
fullHeaderProtoParams, err := fullHeader.ProtocolParameters()
if err != nil {
return err
}
result := struct {
SnapshotName string `json:"snapshotName,omitempty"`
FilePath string `json:"filePath"`
Version byte `json:"version"`
Type string `json:"type"`
GenesisMilestoneIndex iotago.MilestoneIndex `json:"genesisMilestoneIndex"`
TargetMilestoneIndex iotago.MilestoneIndex `json:"targetMilestoneIndex"`
TargetMilestoneTimestamp time.Time `json:"targetMilestoneTimestamp"`
TargetMilestoneID string `json:"targetMilestoneId"`
LedgerMilestoneIndex iotago.MilestoneIndex `json:"ledgerMilestoneIndex"`
TreasuryOutput *utxo.TreasuryOutput `json:"treasuryOutput"`
ProtocolParameters *iotago.ProtocolParameters `json:"protocolParameters"`
OutputCount uint64 `json:"outputCount"`
MilestoneDiffCount uint32 `json:"milestoneDiffCount"`
SolidEntryPointsCount uint16 `json:"solidEntryPointsCount"`
}{
SnapshotName: name,
FilePath: path,
Version: fullHeader.Version,
Type: "full",
GenesisMilestoneIndex: fullHeader.GenesisMilestoneIndex,
TargetMilestoneIndex: fullHeader.TargetMilestoneIndex,
TargetMilestoneTimestamp: time.Unix(int64(fullHeader.TargetMilestoneTimestamp), 0),
TargetMilestoneID: fullHeader.TargetMilestoneID.ToHex(),
LedgerMilestoneIndex: fullHeader.LedgerMilestoneIndex,
TreasuryOutput: fullHeader.TreasuryOutput,
ProtocolParameters: fullHeaderProtoParams,
OutputCount: fullHeader.OutputCount,
MilestoneDiffCount: fullHeader.MilestoneDiffCount,
SolidEntryPointsCount: fullHeader.SEPCount,
}
return printJSON(result)
}
// prints information about the given delta snapshot file header.
func printDeltaSnapshotHeaderInfo(name string, path string, deltaHeader *snapshot.DeltaSnapshotHeader) error {
result := struct {
SnapshotName string `json:"snapshotName,omitempty"`
FilePath string `json:"filePath"`
Version byte `json:"version"`
Type string `json:"type"`
TargetMilestoneIndex iotago.MilestoneIndex `json:"targetMilestoneIndex"`
TargetMilestoneTimestamp time.Time `json:"targetMilestoneTimestamp"`
FullSnapshotTargetMilestoneID string `json:"fullSnapshotTargetMilestoneId"`
SolidEntryPointsFileOffset int64 `json:"solidEntryPointsFileOffset"`
MilestoneDiffCount uint32 `json:"milestoneDiffCount"`
SolidEntryPointsCount uint16 `json:"solidEntryPointsCount"`
}{
SnapshotName: name,
FilePath: path,
Version: deltaHeader.Version,
Type: "delta",
TargetMilestoneIndex: deltaHeader.TargetMilestoneIndex,
TargetMilestoneTimestamp: time.Unix(int64(deltaHeader.TargetMilestoneTimestamp), 0),
FullSnapshotTargetMilestoneID: deltaHeader.FullSnapshotTargetMilestoneID.ToHex(),
SolidEntryPointsFileOffset: deltaHeader.SEPFileOffset,
MilestoneDiffCount: deltaHeader.MilestoneDiffCount,
SolidEntryPointsCount: deltaHeader.SEPCount,
}
return printJSON(result)
}