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.

79 lines
1.8 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/lite"
  8. liteErr "github.com/tendermint/tendermint/lite/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 lite.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 lite.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. // LoadFullCommit loads the full commit from the file system.
  39. func LoadFullCommit(path string) (lite.FullCommit, error) {
  40. var fc lite.FullCommit
  41. f, err := os.Open(path)
  42. if err != nil {
  43. if os.IsNotExist(err) {
  44. return fc, liteErr.ErrCommitNotFound()
  45. }
  46. return fc, errors.WithStack(err)
  47. }
  48. defer f.Close()
  49. var n int
  50. wire.ReadBinaryPtr(&fc, f, MaxFullCommitSize, &n, &err)
  51. return fc, errors.WithStack(err)
  52. }
  53. // LoadFullCommitJSON loads the commit from the file system in JSON format.
  54. func LoadFullCommitJSON(path string) (lite.FullCommit, error) {
  55. var fc lite.FullCommit
  56. f, err := os.Open(path)
  57. if err != nil {
  58. if os.IsNotExist(err) {
  59. return fc, liteErr.ErrCommitNotFound()
  60. }
  61. return fc, errors.WithStack(err)
  62. }
  63. defer f.Close()
  64. stream := json.NewDecoder(f)
  65. err = stream.Decode(&fc)
  66. return fc, errors.WithStack(err)
  67. }