gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/aqua/downloader/modes.go (about) 1 // Copyright 2015 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package downloader 18 19 import "fmt" 20 21 // SyncMode represents the synchronisation mode of the downloader. 22 type SyncMode int 23 24 const ( 25 FullSync SyncMode = iota // Synchronise the entire blockchain history from full blocks 26 FastSync // Quickly download the headers, full sync only at the chain head 27 OfflineSync // no p2p 28 ) 29 30 func (mode SyncMode) IsValid() bool { 31 return mode >= FullSync && mode <= OfflineSync 32 } 33 34 // String implements the stringer interface. 35 func (mode SyncMode) String() string { 36 switch mode { 37 case FullSync: 38 return "full" 39 case FastSync: 40 return "fast" 41 case OfflineSync: 42 return "offline" 43 default: 44 return "unknown" 45 } 46 } 47 48 func (mode SyncMode) MarshalText() ([]byte, error) { 49 switch mode { 50 case FullSync: 51 return []byte("full"), nil 52 case FastSync: 53 return []byte("fast"), nil 54 default: 55 return nil, fmt.Errorf("unknown sync mode %d", mode) 56 } 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 "none", "offline": 66 *mode = OfflineSync 67 default: 68 return fmt.Errorf(`unknown sync mode %q, want "full", "fast"`, text) 69 } 70 return nil 71 }