github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/eth/downloader/modes.go (about)

     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  
    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  	LightSync                 // Download only the headers and terminate afterwards
    28  )
    29  
    30  func FastSyncSupported() bool {
    31  	return false
    32  }
    33  
    34  func DefaultSyncMode() SyncMode {
    35  	if FastSyncSupported() {
    36  		return FastSync
    37  	}
    38  	return FullSync
    39  }
    40  
    41  func FullGcModeSupported() bool {
    42  	return true
    43  }
    44  
    45  func DefaultGcMode() string {
    46  	if FullGcModeSupported() {
    47  		return "full"
    48  	}
    49  	return "archive"
    50  }
    51  
    52  func (mode SyncMode) IsValid() bool {
    53  	return mode >= FullSync && mode <= LightSync
    54  }
    55  
    56  // String implements the stringer interface.
    57  func (mode SyncMode) String() string {
    58  	switch mode {
    59  	case FullSync:
    60  		return "full"
    61  	case FastSync:
    62  		return "fast"
    63  	case LightSync:
    64  		return "light"
    65  	default:
    66  		return "unknown"
    67  	}
    68  }
    69  
    70  func (mode SyncMode) MarshalText() ([]byte, error) {
    71  	switch mode {
    72  	case FullSync:
    73  		return []byte("full"), nil
    74  	case FastSync:
    75  		return []byte("fast"), nil
    76  	case LightSync:
    77  		return []byte("light"), nil
    78  	default:
    79  		return nil, fmt.Errorf("unknown sync mode %d", mode)
    80  	}
    81  }
    82  
    83  func (mode *SyncMode) UnmarshalText(text []byte) error {
    84  	switch string(text) {
    85  	case "full":
    86  		*mode = FullSync
    87  	case "fast":
    88  		*mode = FastSync
    89  	case "light":
    90  		*mode = LightSync
    91  	default:
    92  		return fmt.Errorf(`unknown sync mode %q, want "full", "fast" or "light"`, text)
    93  	}
    94  	return nil
    95  }