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.

69 lines
1.6 KiB

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