github.com/evdatsion/aphelion-dpos-bft@v0.32.1/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 amino "github.com/evdatsion/go-amino" 18 cs "github.com/evdatsion/aphelion-dpos-bft/consensus" 19 "github.com/evdatsion/aphelion-dpos-bft/types" 20 ) 21 22 var cdc = amino.NewCodec() 23 24 func init() { 25 cs.RegisterConsensusMessages(cdc) 26 cs.RegisterWALMessages(cdc) 27 types.RegisterBlockAmino(cdc) 28 } 29 30 func main() { 31 if len(os.Args) < 3 { 32 fmt.Fprintln(os.Stderr, "missing arguments: Usage:json2wal <path-to-JSON> <path-to-wal>") 33 os.Exit(1) 34 } 35 36 f, err := os.Open(os.Args[1]) 37 if err != nil { 38 panic(fmt.Errorf("failed to open WAL file: %v", err)) 39 } 40 defer f.Close() 41 42 walFile, err := os.OpenFile(os.Args[2], os.O_EXCL|os.O_WRONLY|os.O_CREATE, 0666) 43 if err != nil { 44 panic(fmt.Errorf("failed to open WAL file: %v", err)) 45 } 46 defer walFile.Close() 47 48 // the length of tendermint/wal/MsgInfo in the wal.json may exceed the defaultBufSize(4096) of bufio 49 // because of the byte array in BlockPart 50 // leading to unmarshal error: unexpected end of JSON input 51 br := bufio.NewReaderSize(f, 2*types.BlockPartSizeBytes) 52 dec := cs.NewWALEncoder(walFile) 53 54 for { 55 msgJson, _, err := br.ReadLine() 56 if err == io.EOF { 57 break 58 } else if err != nil { 59 panic(fmt.Errorf("failed to read file: %v", err)) 60 } 61 // ignore the ENDHEIGHT in json.File 62 if strings.HasPrefix(string(msgJson), "ENDHEIGHT") { 63 continue 64 } 65 66 var msg cs.TimedWALMessage 67 err = cdc.UnmarshalJSON(msgJson, &msg) 68 if err != nil { 69 panic(fmt.Errorf("failed to unmarshal json: %v", err)) 70 } 71 72 err = dec.Encode(&msg) 73 if err != nil { 74 panic(fmt.Errorf("failed to encode msg: %v", err)) 75 } 76 } 77 }