github.com/annchain/OG@v0.0.9/og/downloader/mode.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package downloader
    15  
    16  import "fmt"
    17  
    18  // SyncMode represents the synchronisation mode of the downloader.
    19  type SyncMode int
    20  
    21  const (
    22  	FullSync  SyncMode = iota // Synchronise the entire blockchain history from full blocks
    23  	FastSync                  // Quickly download the headers, full sync only at the chain head
    24  	LightSync                 // Download only the headers and terminate afterwards
    25  )
    26  
    27  func (mode SyncMode) IsValid() bool {
    28  	return mode >= FullSync && mode <= LightSync
    29  }
    30  
    31  // String implements the stringer interface.
    32  func (mode SyncMode) String() string {
    33  	switch mode {
    34  	case FullSync:
    35  		return "full"
    36  	case FastSync:
    37  		return "fast"
    38  	case LightSync:
    39  		return "light"
    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  	case LightSync:
    52  		return []byte("light"), nil
    53  	default:
    54  		return nil, fmt.Errorf("unknown sync mode %d", mode)
    55  	}
    56  }
    57  
    58  func (mode *SyncMode) UnmarshalText(text []byte) error {
    59  	switch string(text) {
    60  	case "full":
    61  		*mode = FullSync
    62  	case "fast":
    63  		*mode = FastSync
    64  	case "light":
    65  		*mode = LightSync
    66  	default:
    67  		return fmt.Errorf(`unknown sync mode %q, want "full", "fast" or "light"`, text)
    68  	}
    69  	return nil
    70  }