-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
74 lines (61 loc) · 1.44 KB
/
main.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
/*
json2wal converts JSON file to binary WAL file.
Usage:
json2wal <path-to-JSON> <path-to-wal>
*/
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/tendermint/go-amino"
cs "github.com/tendermint/tendermint/consensus"
"github.com/tendermint/tendermint/types"
)
var cdc = amino.NewCodec()
func init() {
cs.RegisterConsensusMessages(cdc)
cs.RegisterWALMessages(cdc)
types.RegisterBlockAmino(cdc)
}
func main() {
if len(os.Args) < 3 {
fmt.Fprintln(os.Stderr, "missing arguments: Usage:json2wal <path-to-JSON> <path-to-wal>")
os.Exit(1)
}
f, err := os.Open(os.Args[1])
if err != nil {
panic(fmt.Errorf("failed to open WAL file: %v", err))
}
defer f.Close()
walFile, err := os.OpenFile(os.Args[2], os.O_EXCL|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(fmt.Errorf("failed to open WAL file: %v", err))
}
defer walFile.Close()
br := bufio.NewReader(f)
dec := cs.NewWALEncoder(walFile)
for {
msgJson, _, err := br.ReadLine()
if err == io.EOF {
break
} else if err != nil {
panic(fmt.Errorf("failed to read file: %v", err))
}
// ignore the ENDHEIGHT in json.File
if strings.HasPrefix(string(msgJson), "ENDHEIGHT") {
continue
}
var msg cs.TimedWALMessage
err = cdc.UnmarshalJSON(msgJson, &msg)
if err != nil {
panic(fmt.Errorf("failed to unmarshal json: %v", err))
}
err = dec.Encode(&msg)
if err != nil {
panic(fmt.Errorf("failed to encode msg: %v", err))
}
}
}