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.

93 lines
2.0 KiB

  1. package files
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "github.com/pkg/errors"
  6. "github.com/tendermint/tendermint/lite"
  7. liteErr "github.com/tendermint/tendermint/lite/errors"
  8. )
  9. const (
  10. // MaxFullCommitSize is the maximum number of bytes we will
  11. // read in for a full commit to avoid excessive allocations
  12. // in the deserializer
  13. MaxFullCommitSize = 1024 * 1024
  14. )
  15. // SaveFullCommit exports the seed in binary / go-amino style
  16. func SaveFullCommit(fc lite.FullCommit, path string) error {
  17. f, err := os.Create(path)
  18. if err != nil {
  19. return errors.WithStack(err)
  20. }
  21. defer f.Close()
  22. _, err = cdc.MarshalBinaryWriter(f, fc)
  23. if err != nil {
  24. return errors.WithStack(err)
  25. }
  26. return nil
  27. }
  28. // SaveFullCommitJSON exports the seed in a json format
  29. func SaveFullCommitJSON(fc lite.FullCommit, path string) error {
  30. f, err := os.Create(path)
  31. if err != nil {
  32. return errors.WithStack(err)
  33. }
  34. defer f.Close()
  35. bz, err := cdc.MarshalJSON(fc)
  36. if err != nil {
  37. return errors.WithStack(err)
  38. }
  39. _, err = f.Write(bz)
  40. if err != nil {
  41. return errors.WithStack(err)
  42. }
  43. return nil
  44. }
  45. // LoadFullCommit loads the full commit from the file system.
  46. func LoadFullCommit(path string) (lite.FullCommit, error) {
  47. var fc lite.FullCommit
  48. f, err := os.Open(path)
  49. if err != nil {
  50. if os.IsNotExist(err) {
  51. return fc, liteErr.ErrCommitNotFound()
  52. }
  53. return fc, errors.WithStack(err)
  54. }
  55. defer f.Close()
  56. _, err = cdc.UnmarshalBinaryReader(f, &fc, 0)
  57. if err != nil {
  58. return fc, errors.WithStack(err)
  59. }
  60. return fc, nil
  61. }
  62. // LoadFullCommitJSON loads the commit from the file system in JSON format.
  63. func LoadFullCommitJSON(path string) (lite.FullCommit, error) {
  64. var fc lite.FullCommit
  65. f, err := os.Open(path)
  66. if err != nil {
  67. if os.IsNotExist(err) {
  68. return fc, liteErr.ErrCommitNotFound()
  69. }
  70. return fc, errors.WithStack(err)
  71. }
  72. defer f.Close()
  73. bz, err := ioutil.ReadAll(f)
  74. if err != nil {
  75. return fc, errors.WithStack(err)
  76. }
  77. err = cdc.UnmarshalJSON(bz, &fc)
  78. if err != nil {
  79. return fc, errors.WithStack(err)
  80. }
  81. return fc, nil
  82. }