github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/intprotocol/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  )
    28  
    29  func (mode SyncMode) IsValid() bool {
    30  	return mode >= FullSync
    31  }
    32  
    33  // String implements the stringer interface.
    34  func (mode SyncMode) String() string {
    35  	switch mode {
    36  	case FullSync:
    37  		return "full"
    38  	case FastSync:
    39  		return "fast"
    40  	default:
    41  		return "unknown"
    42  	}
    43  }
    44  
    45  func (mode SyncMode) MarshalText() ([]byte, error) {
    46  	switch mode {
    47  	case FullSync:
    48  		return []byte("full"), nil
    49  	case FastSync:
    50  		return []byte("fast"), 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  	default:
    63  		return fmt.Errorf(`unknown sync mode %q, want "full", "fast" or "light"`, text)
    64  	}
    65  	return nil
    66  }