github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/eth/downloader/types.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package downloader
    13  
    14  import (
    15  	"fmt"
    16  
    17  	"github.com/Sberex/go-sberex/core/types"
    18  )
    19  
    20  // peerDropFn is a callback type for dropping a peer detected as malicious.
    21  type peerDropFn func(id string)
    22  
    23  // dataPack is a data message returned by a peer for some query.
    24  type dataPack interface {
    25  	PeerId() string
    26  	Items() int
    27  	Stats() string
    28  }
    29  
    30  // headerPack is a batch of block headers returned by a peer.
    31  type headerPack struct {
    32  	peerId  string
    33  	headers []*types.Header
    34  }
    35  
    36  func (p *headerPack) PeerId() string { return p.peerId }
    37  func (p *headerPack) Items() int     { return len(p.headers) }
    38  func (p *headerPack) Stats() string  { return fmt.Sprintf("%d", len(p.headers)) }
    39  
    40  // bodyPack is a batch of block bodies returned by a peer.
    41  type bodyPack struct {
    42  	peerId       string
    43  	transactions [][]*types.Transaction
    44  	uncles       [][]*types.Header
    45  }
    46  
    47  func (p *bodyPack) PeerId() string { return p.peerId }
    48  func (p *bodyPack) Items() int {
    49  	if len(p.transactions) <= len(p.uncles) {
    50  		return len(p.transactions)
    51  	}
    52  	return len(p.uncles)
    53  }
    54  func (p *bodyPack) Stats() string { return fmt.Sprintf("%d:%d", len(p.transactions), len(p.uncles)) }
    55  
    56  // receiptPack is a batch of receipts returned by a peer.
    57  type receiptPack struct {
    58  	peerId   string
    59  	receipts [][]*types.Receipt
    60  }
    61  
    62  func (p *receiptPack) PeerId() string { return p.peerId }
    63  func (p *receiptPack) Items() int     { return len(p.receipts) }
    64  func (p *receiptPack) Stats() string  { return fmt.Sprintf("%d", len(p.receipts)) }
    65  
    66  // statePack is a batch of states returned by a peer.
    67  type statePack struct {
    68  	peerId string
    69  	states [][]byte
    70  }
    71  
    72  func (p *statePack) PeerId() string { return p.peerId }
    73  func (p *statePack) Items() int     { return len(p.states) }
    74  func (p *statePack) Stats() string  { return fmt.Sprintf("%d", len(p.states)) }