modes.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // Used by raft:
  27. BoundedFullSync SyncMode = 100 // Perform a full sync until the requested hash, and no further
  28. )
  29. func (mode SyncMode) IsValid() bool {
  30. return mode >= FullSync && mode <= LightSync
  31. }
  32. // String implements the stringer interface.
  33. func (mode SyncMode) String() string {
  34. switch mode {
  35. case FullSync:
  36. return "full"
  37. case FastSync:
  38. return "fast"
  39. case SnapSync:
  40. return "snap"
  41. case LightSync:
  42. return "light"
  43. default:
  44. return "unknown"
  45. }
  46. }
  47. func (mode SyncMode) MarshalText() ([]byte, error) {
  48. switch mode {
  49. case FullSync:
  50. return []byte("full"), nil
  51. case FastSync:
  52. return []byte("fast"), nil
  53. case SnapSync:
  54. return []byte("snap"), nil
  55. case LightSync:
  56. return []byte("light"), nil
  57. default:
  58. return nil, fmt.Errorf("unknown sync mode %d", mode)
  59. }
  60. }
  61. func (mode *SyncMode) UnmarshalText(text []byte) error {
  62. switch string(text) {
  63. case "full":
  64. *mode = FullSync
  65. case "fast":
  66. *mode = FastSync
  67. case "snap":
  68. *mode = SnapSync
  69. case "light":
  70. *mode = LightSync
  71. default:
  72. return fmt.Errorf(`unknown sync mode %q, want "full", "fast" or "light"`, text)
  73. }
  74. return nil
  75. }