github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/eth/downloader/modes.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package downloader
    19  
    20  import "fmt"
    21  
    22  // SyncMode represents the synchronisation mode of the downloader.
    23  type SyncMode int
    24  
    25  const (
    26  	FullSync  SyncMode = iota // Synchronise the entire blockchain history from full blocks
    27  	FastSync                  // Quickly download the headers, full sync only at the chain head
    28  	LightSync                 // Download only the headers and terminate afterwards
    29  )
    30  
    31  func (mode SyncMode) IsValid() bool {
    32  	return mode >= FullSync && mode <= LightSync
    33  }
    34  
    35  // String implements the stringer interface.
    36  func (mode SyncMode) String() string {
    37  	switch mode {
    38  	case FullSync:
    39  		return "full"
    40  	case FastSync:
    41  		return "fast"
    42  	case LightSync:
    43  		return "light"
    44  	default:
    45  		return "unknown"
    46  	}
    47  }
    48  
    49  func (mode SyncMode) MarshalText() ([]byte, error) {
    50  	switch mode {
    51  	case FullSync:
    52  		return []byte("full"), nil
    53  	case FastSync:
    54  		return []byte("fast"), nil
    55  	case LightSync:
    56  		return []byte("light"), nil
    57  	default:
    58  		return nil, fmt.Errorf("unknown sync mode %d", mode)
    59  	}
    60  }
    61  
    62  func (mode *SyncMode) UnmarshalText(text []byte) error {
    63  	switch string(text) {
    64  	case "full":
    65  		*mode = FullSync
    66  	case "fast":
    67  		*mode = FastSync
    68  	case "light":
    69  		*mode = LightSync
    70  	default:
    71  		return fmt.Errorf(`unknown sync mode %q, want "full", "fast" or "light"`, text)
    72  	}
    73  	return nil
    74  }