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.

65 lines
1.3 KiB

  1. /*
  2. cutWALUntil is a small utility for cutting a WAL until the given height
  3. (inclusively). Note it does not include last cs.EndHeightMessage.
  4. Usage:
  5. cutWALUntil <path-to-wal> height-to-stop <output-wal>
  6. */
  7. package main
  8. import (
  9. "fmt"
  10. "io"
  11. "os"
  12. "strconv"
  13. cs "github.com/tendermint/tendermint/consensus"
  14. )
  15. func main() {
  16. if len(os.Args) < 4 {
  17. fmt.Println("3 arguments required: <path-to-wal> <height-to-stop> <output-wal>")
  18. os.Exit(1)
  19. }
  20. var heightToStop uint64
  21. var err error
  22. if heightToStop, err = strconv.ParseUint(os.Args[2], 10, 64); err != nil {
  23. panic(fmt.Errorf("failed to parse height: %v", err))
  24. }
  25. in, err := os.Open(os.Args[1])
  26. if err != nil {
  27. panic(fmt.Errorf("failed to open input WAL file: %v", err))
  28. }
  29. defer in.Close()
  30. out, err := os.Create(os.Args[3])
  31. if err != nil {
  32. panic(fmt.Errorf("failed to open output WAL file: %v", err))
  33. }
  34. defer out.Close()
  35. enc := cs.NewWALEncoder(out)
  36. dec := cs.NewWALDecoder(in)
  37. for {
  38. msg, err := dec.Decode()
  39. if err == io.EOF {
  40. break
  41. } else if err != nil {
  42. panic(fmt.Errorf("failed to decode msg: %v", err))
  43. }
  44. if m, ok := msg.Msg.(cs.EndHeightMessage); ok {
  45. if m.Height == heightToStop {
  46. break
  47. }
  48. }
  49. err = enc.Encode(msg)
  50. if err != nil {
  51. panic(fmt.Errorf("failed to encode msg: %v", err))
  52. }
  53. }
  54. }