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. package files
  2. import (
  3. "encoding/json"
  4. "os"
  5. "github.com/pkg/errors"
  6. wire "github.com/tendermint/go-wire"
  7. "github.com/tendermint/tendermint/certifiers"
  8. certerr "github.com/tendermint/tendermint/certifiers/errors"
  9. )
  10. const (
  11. // MaxFullCommitSize is the maximum number of bytes we will
  12. // read in for a full commit to avoid excessive allocations
  13. // in the deserializer
  14. MaxFullCommitSize = 1024 * 1024
  15. )
  16. // SaveFullCommit exports the seed in binary / go-wire style
  17. func SaveFullCommit(fc certifiers.FullCommit, path string) error {
  18. f, err := os.Create(path)
  19. if err != nil {
  20. return errors.WithStack(err)
  21. }
  22. defer f.Close()
  23. var n int
  24. wire.WriteBinary(fc, f, &n, &err)
  25. return errors.WithStack(err)
  26. }
  27. // SaveFullCommitJSON exports the seed in a json format
  28. func SaveFullCommitJSON(fc certifiers.FullCommit, path string) error {
  29. f, err := os.Create(path)
  30. if err != nil {
  31. return errors.WithStack(err)
  32. }
  33. defer f.Close()
  34. stream := json.NewEncoder(f)
  35. err = stream.Encode(fc)
  36. return errors.WithStack(err)
  37. }
  38. func LoadFullCommit(path string) (certifiers.FullCommit, error) {
  39. var fc certifiers.FullCommit
  40. f, err := os.Open(path)
  41. if err != nil {
  42. if os.IsNotExist(err) {
  43. return fc, certerr.ErrCommitNotFound()
  44. }
  45. return fc, errors.WithStack(err)
  46. }
  47. defer f.Close()
  48. var n int
  49. wire.ReadBinaryPtr(&fc, f, MaxFullCommitSize, &n, &err)
  50. return fc, errors.WithStack(err)
  51. }
  52. func LoadFullCommitJSON(path string) (certifiers.FullCommit, error) {
  53. var fc certifiers.FullCommit
  54. f, err := os.Open(path)
  55. if err != nil {
  56. if os.IsNotExist(err) {
  57. return fc, certerr.ErrCommitNotFound()
  58. }
  59. return fc, errors.WithStack(err)
  60. }
  61. defer f.Close()
  62. stream := json.NewDecoder(f)
  63. err = stream.Decode(&fc)
  64. return fc, errors.WithStack(err)
  65. }