| 
									
										
										
										
											2015-10-05 19:37:56 +03:00
										 |  |  | // Copyright 2015 The go-ethereum Authors | 
					
						
							|  |  |  | // This file is part of the go-ethereum library. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Lesser General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							|  |  |  | // GNU Lesser General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Lesser General Public License | 
					
						
							|  |  |  | // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package downloader | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // peerDropFn is a callback type for dropping a peer detected as malicious. | 
					
						
							|  |  |  | type peerDropFn func(id string) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // dataPack is a data message returned by a peer for some query. | 
					
						
							|  |  |  | type dataPack interface { | 
					
						
							|  |  |  | 	PeerId() string | 
					
						
							|  |  |  | 	Items() int | 
					
						
							|  |  |  | 	Stats() string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // headerPack is a batch of block headers returned by a peer. | 
					
						
							|  |  |  | type headerPack struct { | 
					
						
							| 
									
										
										
										
											2018-06-14 03:14:52 -07:00
										 |  |  | 	peerID  string | 
					
						
							| 
									
										
										
										
											2015-10-05 19:37:56 +03:00
										 |  |  | 	headers []*types.Header | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-14 03:14:52 -07:00
										 |  |  | func (p *headerPack) PeerId() string { return p.peerID } | 
					
						
							| 
									
										
										
										
											2015-10-05 19:37:56 +03:00
										 |  |  | func (p *headerPack) Items() int     { return len(p.headers) } | 
					
						
							|  |  |  | func (p *headerPack) Stats() string  { return fmt.Sprintf("%d", len(p.headers)) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // bodyPack is a batch of block bodies returned by a peer. | 
					
						
							|  |  |  | type bodyPack struct { | 
					
						
							| 
									
										
										
										
											2018-06-14 03:14:52 -07:00
										 |  |  | 	peerID       string | 
					
						
							| 
									
										
										
										
											2015-10-05 19:37:56 +03:00
										 |  |  | 	transactions [][]*types.Transaction | 
					
						
							|  |  |  | 	uncles       [][]*types.Header | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-14 03:14:52 -07:00
										 |  |  | func (p *bodyPack) PeerId() string { return p.peerID } | 
					
						
							| 
									
										
										
										
											2015-10-05 19:37:56 +03:00
										 |  |  | func (p *bodyPack) Items() int { | 
					
						
							|  |  |  | 	if len(p.transactions) <= len(p.uncles) { | 
					
						
							|  |  |  | 		return len(p.transactions) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return len(p.uncles) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func (p *bodyPack) Stats() string { return fmt.Sprintf("%d:%d", len(p.transactions), len(p.uncles)) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // receiptPack is a batch of receipts returned by a peer. | 
					
						
							|  |  |  | type receiptPack struct { | 
					
						
							| 
									
										
										
										
											2018-06-14 03:14:52 -07:00
										 |  |  | 	peerID   string | 
					
						
							| 
									
										
										
										
											2015-10-05 19:37:56 +03:00
										 |  |  | 	receipts [][]*types.Receipt | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-14 03:14:52 -07:00
										 |  |  | func (p *receiptPack) PeerId() string { return p.peerID } | 
					
						
							| 
									
										
										
										
											2015-10-05 19:37:56 +03:00
										 |  |  | func (p *receiptPack) Items() int     { return len(p.receipts) } | 
					
						
							|  |  |  | func (p *receiptPack) Stats() string  { return fmt.Sprintf("%d", len(p.receipts)) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // statePack is a batch of states returned by a peer. | 
					
						
							|  |  |  | type statePack struct { | 
					
						
							| 
									
										
										
										
											2018-06-14 03:14:52 -07:00
										 |  |  | 	peerID string | 
					
						
							| 
									
										
										
										
											2015-10-05 19:37:56 +03:00
										 |  |  | 	states [][]byte | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-14 03:14:52 -07:00
										 |  |  | func (p *statePack) PeerId() string { return p.peerID } | 
					
						
							| 
									
										
										
										
											2015-10-05 19:37:56 +03:00
										 |  |  | func (p *statePack) Items() int     { return len(p.states) } | 
					
						
							|  |  |  | func (p *statePack) Stats() string  { return fmt.Sprintf("%d", len(p.states)) } |