You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.1 KiB

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