modes.go 2.0 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. type SyncMode int
  20. const (
  21. FullSync SyncMode = iota // Synchronise the entire blockchain history from full blocks
  22. FastSync // Quickly download the headers, full sync only at the chain head
  23. LightSync // Download only the headers and terminate afterwards
  24. )
  25. func (mode SyncMode) IsValid() bool {
  26. return mode >= FullSync && mode <= LightSync
  27. }
  28. // String implements the stringer interface.
  29. func (mode SyncMode) String() string {
  30. switch mode {
  31. case FullSync:
  32. return "full"
  33. case FastSync:
  34. return "fast"
  35. case LightSync:
  36. return "light"
  37. default:
  38. return "unknown"
  39. }
  40. }
  41. func (mode SyncMode) MarshalText() ([]byte, error) {
  42. switch mode {
  43. case FullSync:
  44. return []byte("full"), nil
  45. case FastSync:
  46. return []byte("fast"), nil
  47. case LightSync:
  48. return []byte("light"), nil
  49. default:
  50. return nil, fmt.Errorf("unknown sync mode %d", mode)
  51. }
  52. }
  53. func (mode *SyncMode) UnmarshalText(text []byte) error {
  54. switch string(text) {
  55. case "full":
  56. *mode = FullSync
  57. case "fast":
  58. *mode = FastSync
  59. case "light":
  60. *mode = LightSync
  61. default:
  62. return fmt.Errorf(`unknown sync mode %q, want "full", "fast" or "light"`, text)
  63. }
  64. return nil
  65. }