gitlab.com/gpdionisio/tendermint@v0.34.19-dev2/scripts/json2wal/main.go (about) 1 /* 2 json2wal converts JSON file to binary WAL file. 3 4 Usage: 5 json2wal <path-to-JSON> <path-to-wal> 6 */ 7 8 package main 9 10 import ( 11 "bufio" 12 "fmt" 13 "io" 14 "os" 15 "strings" 16 17 cs "github.com/tendermint/tendermint/consensus" 18 tmjson "github.com/tendermint/tendermint/libs/json" 19 "github.com/tendermint/tendermint/types" 20 ) 21 22 func main() { 23 if len(os.Args) < 3 { 24 fmt.Fprintln(os.Stderr, "missing arguments: Usage:json2wal <path-to-JSON> <path-to-wal>") 25 os.Exit(1) 26 } 27 28 f, err := os.Open(os.Args[1]) 29 if err != nil { 30 panic(fmt.Errorf("failed to open WAL file: %v", err)) 31 } 32 defer f.Close() 33 34 walFile, err := os.OpenFile(os.Args[2], os.O_EXCL|os.O_WRONLY|os.O_CREATE, 0666) 35 if err != nil { 36 panic(fmt.Errorf("failed to open WAL file: %v", err)) 37 } 38 defer walFile.Close() 39 40 // the length of tendermint/wal/MsgInfo in the wal.json may exceed the defaultBufSize(4096) of bufio 41 // because of the byte array in BlockPart 42 // leading to unmarshal error: unexpected end of JSON input 43 br := bufio.NewReaderSize(f, int(2*types.BlockPartSizeBytes)) 44 dec := cs.NewWALEncoder(walFile) 45 46 for { 47 msgJSON, _, err := br.ReadLine() 48 if err == io.EOF { 49 break 50 } else if err != nil { 51 panic(fmt.Errorf("failed to read file: %v", err)) 52 } 53 // ignore the ENDHEIGHT in json.File 54 if strings.HasPrefix(string(msgJSON), "ENDHEIGHT") { 55 continue 56 } 57 58 var msg cs.TimedWALMessage 59 err = tmjson.Unmarshal(msgJSON, &msg) 60 if err != nil { 61 panic(fmt.Errorf("failed to unmarshal json: %v", err)) 62 } 63 64 err = dec.Encode(&msg) 65 if err != nil { 66 panic(fmt.Errorf("failed to encode msg: %v", err)) 67 } 68 } 69 }