github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/eth/downloader/modes.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:37</date> 10 //</624450088206340096> 11 12 13 package downloader 14 15 import "fmt" 16 17 //SyncMode表示下载程序的同步模式。 18 type SyncMode int 19 20 const ( 21 FullSync SyncMode = iota //从完整块同步整个区块链历史 22 FastSync //快速下载邮件头,仅在链头完全同步 23 LightSync //只下载邮件头,然后终止 24 ) 25 26 func (mode SyncMode) IsValid() bool { 27 return mode >= FullSync && mode <= LightSync 28 } 29 30 //字符串实现字符串接口。 31 func (mode SyncMode) String() string { 32 switch mode { 33 case FullSync: 34 return "full" 35 case FastSync: 36 return "fast" 37 case LightSync: 38 return "light" 39 default: 40 return "unknown" 41 } 42 } 43 44 func (mode SyncMode) MarshalText() ([]byte, error) { 45 switch mode { 46 case FullSync: 47 return []byte("full"), nil 48 case FastSync: 49 return []byte("fast"), nil 50 case LightSync: 51 return []byte("light"), nil 52 default: 53 return nil, fmt.Errorf("unknown sync mode %d", mode) 54 } 55 } 56 57 func (mode *SyncMode) UnmarshalText(text []byte) error { 58 switch string(text) { 59 case "full": 60 *mode = FullSync 61 case "fast": 62 *mode = FastSync 63 case "light": 64 *mode = LightSync 65 default: 66 return fmt.Errorf(`unknown sync mode %q, want "full", "fast" or "light"`, text) 67 } 68 return nil 69 } 70