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.

83 lines
1.8 KiB

7 years ago
7 years ago
7 years ago
  1. package pex
  2. import (
  3. "encoding/json"
  4. "os"
  5. cmn "github.com/tendermint/tendermint/libs/common"
  6. )
  7. /* Loading & Saving */
  8. type addrBookJSON struct {
  9. Key string `json:"key"`
  10. Addrs []*knownAddress `json:"addrs"`
  11. }
  12. func (a *addrBook) saveToFile(filePath string) {
  13. a.Logger.Info("Saving AddrBook to file", "size", a.Size())
  14. a.mtx.Lock()
  15. defer a.mtx.Unlock()
  16. // Compile Addrs
  17. addrs := []*knownAddress{}
  18. for _, ka := range a.addrLookup {
  19. addrs = append(addrs, ka)
  20. }
  21. aJSON := &addrBookJSON{
  22. Key: a.key,
  23. Addrs: addrs,
  24. }
  25. jsonBytes, err := json.MarshalIndent(aJSON, "", "\t")
  26. if err != nil {
  27. a.Logger.Error("Failed to save AddrBook to file", "err", err)
  28. return
  29. }
  30. err = cmn.WriteFileAtomic(filePath, jsonBytes, 0644)
  31. if err != nil {
  32. a.Logger.Error("Failed to save AddrBook to file", "file", filePath, "err", err)
  33. }
  34. }
  35. // Returns false if file does not exist.
  36. // cmn.Panics if file is corrupt.
  37. func (a *addrBook) loadFromFile(filePath string) bool {
  38. // If doesn't exist, do nothing.
  39. _, err := os.Stat(filePath)
  40. if os.IsNotExist(err) {
  41. return false
  42. }
  43. // Load addrBookJSON{}
  44. r, err := os.Open(filePath)
  45. if err != nil {
  46. cmn.PanicCrisis(cmn.Fmt("Error opening file %s: %v", filePath, err))
  47. }
  48. defer r.Close() // nolint: errcheck
  49. aJSON := &addrBookJSON{}
  50. dec := json.NewDecoder(r)
  51. err = dec.Decode(aJSON)
  52. if err != nil {
  53. cmn.PanicCrisis(cmn.Fmt("Error reading file %s: %v", filePath, err))
  54. }
  55. // Restore all the fields...
  56. // Restore the key
  57. a.key = aJSON.Key
  58. // Restore .bucketsNew & .bucketsOld
  59. for _, ka := range aJSON.Addrs {
  60. for _, bucketIndex := range ka.Buckets {
  61. bucket := a.getBucket(ka.BucketType, bucketIndex)
  62. bucket[ka.Addr.String()] = ka
  63. }
  64. a.addrLookup[ka.ID()] = ka
  65. if ka.BucketType == bucketTypeNew {
  66. a.nNew++
  67. } else {
  68. a.nOld++
  69. }
  70. }
  71. return true
  72. }