modes.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package downloader
  17. import "fmt"
  18. // SyncMode represents the synchronisation mode of the downloader.
  19. // It is a uint32 as it is used with atomic operations.
  20. type SyncMode uint32
  21. const (
  22. FullSync SyncMode = iota // Synchronise the entire blockchain history from full blocks
  23. FastSync // Quickly download the headers, full sync only at the chain
  24. SnapSync // Download the chain and the state via compact snapshots
  25. LightSync // Download only the headers and terminate afterwards
  26. )
  27. func (mode SyncMode) IsValid() bool {
  28. return mode >= FullSync && mode <= LightSync
  29. }
  30. // String implements the stringer interface.
  31. func (mode SyncMode) String() string {
  32. switch mode {
  33. case FullSync:
  34. return "full"
  35. case FastSync:
  36. return "fast"
  37. case SnapSync:
  38. return "snap"
  39. case LightSync:
  40. return "light"
  41. default:
  42. return "unknown"
  43. }
  44. }
  45. func (mode SyncMode) MarshalText() ([]byte, error) {
  46. switch mode {
  47. case FullSync:
  48. return []byte("full"), nil
  49. case FastSync:
  50. return []byte("fast"), nil
  51. case SnapSync:
  52. return []byte("snap"), nil
  53. case LightSync:
  54. return []byte("light"), nil
  55. default:
  56. return nil, fmt.Errorf("unknown sync mode %d", mode)
  57. }
  58. }
  59. func (mode *SyncMode) UnmarshalText(text []byte) error {
  60. switch string(text) {
  61. case "full":
  62. *mode = FullSync
  63. case "fast":
  64. *mode = FastSync
  65. case "snap":
  66. *mode = SnapSync
  67. case "light":
  68. *mode = LightSync
  69. default:
  70. return fmt.Errorf(`unknown sync mode %q, want "full", "fast" or "light"`, text)
  71. }
  72. return nil
  73. }