github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/vnt/downloader/types.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 (
    20  	"fmt"
    21  
    22  	libp2p "github.com/libp2p/go-libp2p-peer"
    23  	"github.com/vntchain/go-vnt/core/types"
    24  )
    25  
    26  // peerDropFn is a callback type for dropping a peer detected as malicious.
    27  type peerDropFn func(id libp2p.ID)
    28  
    29  // dataPack is a data message returned by a peer for some query.
    30  type dataPack interface {
    31  	PeerId() libp2p.ID
    32  	Items() int
    33  	Stats() string
    34  }
    35  
    36  // headerPack is a batch of block headers returned by a peer.
    37  type headerPack struct {
    38  	peerId  libp2p.ID
    39  	headers []*types.Header
    40  }
    41  
    42  func (p *headerPack) PeerId() libp2p.ID { return p.peerId }
    43  func (p *headerPack) Items() int        { return len(p.headers) }
    44  func (p *headerPack) Stats() string     { return fmt.Sprintf("%d", len(p.headers)) }
    45  
    46  // bodyPack is a batch of block bodies returned by a peer.
    47  type bodyPack struct {
    48  	peerId       libp2p.ID
    49  	transactions [][]*types.Transaction
    50  }
    51  
    52  func (p *bodyPack) PeerId() libp2p.ID { return p.peerId }
    53  func (p *bodyPack) Items() int {
    54  	return len(p.transactions)
    55  }
    56  func (p *bodyPack) Stats() string { return fmt.Sprintf("%d", len(p.transactions)) }
    57  
    58  // receiptPack is a batch of receipts returned by a peer.
    59  type receiptPack struct {
    60  	peerId   libp2p.ID
    61  	receipts [][]*types.Receipt
    62  }
    63  
    64  func (p *receiptPack) PeerId() libp2p.ID { return p.peerId }
    65  func (p *receiptPack) Items() int        { return len(p.receipts) }
    66  func (p *receiptPack) Stats() string     { return fmt.Sprintf("%d", len(p.receipts)) }
    67  
    68  // statePack is a batch of states returned by a peer.
    69  type statePack struct {
    70  	peerId libp2p.ID
    71  	states [][]byte
    72  }
    73  
    74  func (p *statePack) PeerId() libp2p.ID { return p.peerId }
    75  func (p *statePack) Items() int        { return len(p.states) }
    76  func (p *statePack) Stats() string     { return fmt.Sprintf("%d", len(p.states)) }