github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/scripts/wal2json/main.go (about) 1 /* 2 wal2json converts binary WAL file to JSON. 3 4 Usage: 5 wal2json <path-to-wal> 6 */ 7 8 package main 9 10 import ( 11 "encoding/json" 12 "fmt" 13 "io" 14 "os" 15 16 "github.com/ari-anchor/sei-tendermint/internal/consensus" 17 ) 18 19 func main() { 20 if len(os.Args) < 2 { 21 fmt.Println("missing one argument: <path-to-wal>") 22 os.Exit(1) 23 } 24 25 f, err := os.Open(os.Args[1]) 26 if err != nil { 27 panic(fmt.Errorf("failed to open WAL file: %w", err)) 28 } 29 defer f.Close() 30 31 dec := consensus.NewWALDecoder(f) 32 for { 33 msg, err := dec.Decode() 34 if err == io.EOF { 35 break 36 } else if err != nil { 37 panic(fmt.Errorf("failed to decode msg: %w", err)) 38 } 39 40 json, err := json.Marshal(msg) 41 if err != nil { 42 panic(fmt.Errorf("failed to marshal msg: %w", err)) 43 } 44 45 _, err = os.Stdout.Write(json) 46 if err == nil { 47 _, err = os.Stdout.Write([]byte("\n")) 48 } 49 50 if err == nil { 51 if endMsg, ok := msg.Msg.(consensus.EndHeightMessage); ok { 52 _, err = os.Stdout.Write([]byte(fmt.Sprintf("ENDHEIGHT %d\n", endMsg.Height))) 53 } 54 } 55 56 if err != nil { 57 fmt.Println("Failed to write message", err) 58 os.Exit(1) //nolint:gocritic 59 } 60 61 } 62 }