modes.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. SnapSync // Download the chain and the state via compact snapshots
  24. LightSync // Download only the headers and terminate afterwards
  25. )
  26. func (mode SyncMode) IsValid() bool {
  27. return mode >= FullSync && mode <= LightSync
  28. }
  29. // String implements the stringer interface.
  30. func (mode SyncMode) String() string {
  31. switch mode {
  32. case FullSync:
  33. return "full"
  34. case SnapSync:
  35. return "snap"
  36. case LightSync:
  37. return "light"
  38. default:
  39. return "unknown"
  40. }
  41. }
  42. func (mode SyncMode) MarshalText() ([]byte, error) {
  43. switch mode {
  44. case FullSync:
  45. return []byte("full"), nil
  46. case SnapSync:
  47. return []byte("snap"), nil
  48. case LightSync:
  49. return []byte("light"), nil
  50. default:
  51. return nil, fmt.Errorf("unknown sync mode %d", mode)
  52. }
  53. }
  54. func (mode *SyncMode) UnmarshalText(text []byte) error {
  55. switch string(text) {
  56. case "full":
  57. *mode = FullSync
  58. case "snap":
  59. *mode = SnapSync
  60. case "light":
  61. *mode = LightSync
  62. default:
  63. return fmt.Errorf(`unknown sync mode %q, want "full", "snap" or "light"`, text)
  64. }
  65. return nil
  66. }