github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/eth/downloader/types.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 ( 21 "fmt" 22 23 "github.com/AigarNetwork/aigar/core/types" 24 ) 25 26 // peerDropFn is a callback type for dropping a peer detected as malicious. 27 type peerDropFn func(id string) 28 29 // dataPack is a data message returned by a peer for some query. 30 type dataPack interface { 31 PeerId() string 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 string 39 headers []*types.Header 40 } 41 42 func (p *headerPack) PeerId() string { 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 string 49 transactions [][]*types.Transaction 50 uncles [][]*types.Header 51 } 52 53 func (p *bodyPack) PeerId() string { return p.peerID } 54 func (p *bodyPack) Items() int { 55 if len(p.transactions) <= len(p.uncles) { 56 return len(p.transactions) 57 } 58 return len(p.uncles) 59 } 60 func (p *bodyPack) Stats() string { return fmt.Sprintf("%d:%d", len(p.transactions), len(p.uncles)) } 61 62 // receiptPack is a batch of receipts returned by a peer. 63 type receiptPack struct { 64 peerID string 65 receipts [][]*types.Receipt 66 } 67 68 func (p *receiptPack) PeerId() string { return p.peerID } 69 func (p *receiptPack) Items() int { return len(p.receipts) } 70 func (p *receiptPack) Stats() string { return fmt.Sprintf("%d", len(p.receipts)) } 71 72 // statePack is a batch of states returned by a peer. 73 type statePack struct { 74 peerID string 75 states [][]byte 76 } 77 78 func (p *statePack) PeerId() string { return p.peerID } 79 func (p *statePack) Items() int { return len(p.states) } 80 func (p *statePack) Stats() string { return fmt.Sprintf("%d", len(p.states)) }