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.

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