github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/eth/downloader/types.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:38</date> 10 //</624342634407661568> 11 12 13 package downloader 14 15 import ( 16 "fmt" 17 18 "github.com/ethereum/go-ethereum/core/types" 19 ) 20 21 //peerDropFn是一种回调类型,用于删除被检测为恶意的对等机。 22 type peerDropFn func(id string) 23 24 //DATAPACK是对等体为某些查询返回的数据消息。 25 type dataPack interface { 26 PeerId() string 27 Items() int 28 Stats() string 29 } 30 31 //HeaderPack是由对等机返回的一批块头。 32 type headerPack struct { 33 peerID string 34 headers []*types.Header 35 } 36 37 func (p *headerPack) PeerId() string { return p.peerID } 38 func (p *headerPack) Items() int { return len(p.headers) } 39 func (p *headerPack) Stats() string { return fmt.Sprintf("%d", len(p.headers)) } 40 41 //bodypack是对等机返回的一批块体。 42 type bodyPack struct { 43 peerID string 44 transactions [][]*types.Transaction 45 uncles [][]*types.Header 46 } 47 48 func (p *bodyPack) PeerId() string { return p.peerID } 49 func (p *bodyPack) Items() int { 50 if len(p.transactions) <= len(p.uncles) { 51 return len(p.transactions) 52 } 53 return len(p.uncles) 54 } 55 func (p *bodyPack) Stats() string { return fmt.Sprintf("%d:%d", len(p.transactions), len(p.uncles)) } 56 57 //ReceiptPack是由对等方返回的一批收据。 58 type receiptPack struct { 59 peerID string 60 receipts [][]*types.Receipt 61 } 62 63 func (p *receiptPack) PeerId() string { return p.peerID } 64 func (p *receiptPack) Items() int { return len(p.receipts) } 65 func (p *receiptPack) Stats() string { return fmt.Sprintf("%d", len(p.receipts)) } 66 67 //statepack是对等机返回的一批状态。 68 type statePack struct { 69 peerID string 70 states [][]byte 71 } 72 73 func (p *statePack) PeerId() string { return p.peerID } 74 func (p *statePack) Items() int { return len(p.states) } 75 func (p *statePack) Stats() string { return fmt.Sprintf("%d", len(p.states)) } 76