github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/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  	UltraLightSync                 // Synchronise one block per Epoch (Celo-specific mode)
    29  )
    30  
    31  const ultraLightSyncModeAsString = "ultralight"
    32  
    33  func (mode SyncMode) IsValid() bool {
    34  	return mode >= FullSync && mode <= UltraLightSync
    35  }
    36  
    37  // String implements the stringer interface.
    38  func (mode SyncMode) String() string {
    39  	switch mode {
    40  	case FullSync:
    41  		return "full"
    42  	case FastSync:
    43  		return "fast"
    44  	case LightSync:
    45  		return "light"
    46  	case UltraLightSync:
    47  		return ultraLightSyncModeAsString
    48  	default:
    49  		return "unknown"
    50  	}
    51  }
    52  
    53  func (mode SyncMode) MarshalText() ([]byte, error) {
    54  	switch mode {
    55  	case FullSync:
    56  		return []byte("full"), nil
    57  	case FastSync:
    58  		return []byte("fast"), nil
    59  	case LightSync:
    60  		return []byte("light"), nil
    61  	case UltraLightSync:
    62  		return []byte(ultraLightSyncModeAsString), nil
    63  	default:
    64  		return nil, fmt.Errorf("unknown sync mode %d", mode)
    65  	}
    66  }
    67  
    68  func (mode *SyncMode) UnmarshalText(text []byte) error {
    69  	switch string(text) {
    70  	case "full":
    71  		*mode = FullSync
    72  	case "fast":
    73  		*mode = FastSync
    74  	case "light":
    75  		*mode = LightSync
    76  	case ultraLightSyncModeAsString:
    77  		*mode = UltraLightSync
    78  	default:
    79  		return fmt.Errorf(`unknown sync mode %q, want "full", "fast", "light", or "%s"`,
    80  			text, ultraLightSyncModeAsString)
    81  	}
    82  	return nil
    83  }
    84  
    85  // Returns true if the all headers and not just some a small, discontinuous, set of headers are fetched.
    86  func (mode SyncMode) SyncFullHeaderChain() bool {
    87  	switch mode {
    88  	case FullSync:
    89  		return true
    90  	case FastSync:
    91  		return true
    92  	case LightSync:
    93  		return true
    94  	case UltraLightSync:
    95  		return false
    96  	default:
    97  		panic(fmt.Errorf("unknown sync mode %d", mode))
    98  	}
    99  }
   100  
   101  // Returns true if the full blocks (and not just headers) are fetched.
   102  // If a mode returns true here then it will return true for `SyncFullHeaderChain` as well.
   103  func (mode SyncMode) SyncFullBlockChain() bool {
   104  	switch mode {
   105  	case FullSync:
   106  		return true
   107  	case FastSync:
   108  		return true
   109  	case LightSync:
   110  		return false
   111  	case UltraLightSync:
   112  		return false
   113  	default:
   114  		panic(fmt.Errorf("unknown sync mode %d", mode))
   115  	}
   116  }