github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/qct/downloader/types.go (about) 1 package downloader 2 3 import ( 4 "fmt" 5 6 "github.com/quickchainproject/quickchain/core/types" 7 ) 8 9 // peerDropFn is a callback type for dropping a peer detected as malicious. 10 type peerDropFn func(id string) 11 12 // dataPack is a data message returned by a peer for some query. 13 type dataPack interface { 14 PeerId() string 15 Items() int 16 Stats() string 17 } 18 19 // headerPack is a batch of block headers returned by a peer. 20 type headerPack struct { 21 peerId string 22 headers []*types.Header 23 } 24 25 func (p *headerPack) PeerId() string { return p.peerId } 26 func (p *headerPack) Items() int { return len(p.headers) } 27 func (p *headerPack) Stats() string { return fmt.Sprintf("%d", len(p.headers)) } 28 29 // bodyPack is a batch of block bodies returned by a peer. 30 type bodyPack struct { 31 peerId string 32 transactions [][]*types.Transaction 33 uncles [][]*types.Header 34 } 35 36 func (p *bodyPack) PeerId() string { return p.peerId } 37 func (p *bodyPack) Items() int { 38 if len(p.transactions) <= len(p.uncles) { 39 return len(p.transactions) 40 } 41 return len(p.uncles) 42 } 43 func (p *bodyPack) Stats() string { return fmt.Sprintf("%d:%d", len(p.transactions), len(p.uncles)) } 44 45 // receiptPack is a batch of receipts returned by a peer. 46 type receiptPack struct { 47 peerId string 48 receipts [][]*types.Receipt 49 } 50 51 func (p *receiptPack) PeerId() string { return p.peerId } 52 func (p *receiptPack) Items() int { return len(p.receipts) } 53 func (p *receiptPack) Stats() string { return fmt.Sprintf("%d", len(p.receipts)) } 54 55 // statePack is a batch of states returned by a peer. 56 type statePack struct { 57 peerId string 58 states [][]byte 59 } 60 61 func (p *statePack) PeerId() string { return p.peerId } 62 func (p *statePack) Items() int { return len(p.states) } 63 func (p *statePack) Stats() string { return fmt.Sprintf("%d", len(p.states)) }