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