github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/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 "encoding/json" 13 "fmt" 14 "io" 15 "os" 16 "strings" 17 18 "github.com/ari-anchor/sei-tendermint/internal/consensus" 19 "github.com/ari-anchor/sei-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: %w", 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: %w", 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 := consensus.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: %w", err)) 52 } 53 // ignore the ENDHEIGHT in json.File 54 if strings.HasPrefix(string(msgJSON), "ENDHEIGHT") { 55 continue 56 } 57 58 var msg consensus.TimedWALMessage 59 err = json.Unmarshal(msgJSON, &msg) 60 if err != nil { 61 panic(fmt.Errorf("failed to unmarshal json: %w", err)) 62 } 63 64 err = dec.Encode(&msg) 65 if err != nil { 66 panic(fmt.Errorf("failed to encode msg: %w", err)) 67 } 68 } 69 }