github.com/ylsGit/go-ethereum@v1.6.5/eth/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 "math/big" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/core/types" 25 ) 26 27 // headerCheckFn is a callback type for verifying a header's presence in the local chain. 28 type headerCheckFn func(common.Hash) bool 29 30 // blockAndStateCheckFn is a callback type for verifying block and associated states' presence in the local chain. 31 type blockAndStateCheckFn func(common.Hash) bool 32 33 // headerRetrievalFn is a callback type for retrieving a header from the local chain. 34 type headerRetrievalFn func(common.Hash) *types.Header 35 36 // blockRetrievalFn is a callback type for retrieving a block from the local chain. 37 type blockRetrievalFn func(common.Hash) *types.Block 38 39 // headHeaderRetrievalFn is a callback type for retrieving the head header from the local chain. 40 type headHeaderRetrievalFn func() *types.Header 41 42 // headBlockRetrievalFn is a callback type for retrieving the head block from the local chain. 43 type headBlockRetrievalFn func() *types.Block 44 45 // headFastBlockRetrievalFn is a callback type for retrieving the head fast block from the local chain. 46 type headFastBlockRetrievalFn func() *types.Block 47 48 // headBlockCommitterFn is a callback for directly committing the head block to a certain entity. 49 type headBlockCommitterFn func(common.Hash) error 50 51 // tdRetrievalFn is a callback type for retrieving the total difficulty of a local block. 52 type tdRetrievalFn func(common.Hash) *big.Int 53 54 // headerChainInsertFn is a callback type to insert a batch of headers into the local chain. 55 type headerChainInsertFn func([]*types.Header, int) (int, error) 56 57 // blockChainInsertFn is a callback type to insert a batch of blocks into the local chain. 58 type blockChainInsertFn func(types.Blocks) (int, error) 59 60 // receiptChainInsertFn is a callback type to insert a batch of receipts into the local chain. 61 type receiptChainInsertFn func(types.Blocks, []types.Receipts) (int, error) 62 63 // chainRollbackFn is a callback type to remove a few recently added elements from the local chain. 64 type chainRollbackFn func([]common.Hash) 65 66 // peerDropFn is a callback type for dropping a peer detected as malicious. 67 type peerDropFn func(id string) 68 69 // dataPack is a data message returned by a peer for some query. 70 type dataPack interface { 71 PeerId() string 72 Items() int 73 Stats() string 74 } 75 76 // headerPack is a batch of block headers returned by a peer. 77 type headerPack struct { 78 peerId string 79 headers []*types.Header 80 } 81 82 func (p *headerPack) PeerId() string { return p.peerId } 83 func (p *headerPack) Items() int { return len(p.headers) } 84 func (p *headerPack) Stats() string { return fmt.Sprintf("%d", len(p.headers)) } 85 86 // bodyPack is a batch of block bodies returned by a peer. 87 type bodyPack struct { 88 peerId string 89 transactions [][]*types.Transaction 90 uncles [][]*types.Header 91 } 92 93 func (p *bodyPack) PeerId() string { return p.peerId } 94 func (p *bodyPack) Items() int { 95 if len(p.transactions) <= len(p.uncles) { 96 return len(p.transactions) 97 } 98 return len(p.uncles) 99 } 100 func (p *bodyPack) Stats() string { return fmt.Sprintf("%d:%d", len(p.transactions), len(p.uncles)) } 101 102 // receiptPack is a batch of receipts returned by a peer. 103 type receiptPack struct { 104 peerId string 105 receipts [][]*types.Receipt 106 } 107 108 func (p *receiptPack) PeerId() string { return p.peerId } 109 func (p *receiptPack) Items() int { return len(p.receipts) } 110 func (p *receiptPack) Stats() string { return fmt.Sprintf("%d", len(p.receipts)) } 111 112 // statePack is a batch of states returned by a peer. 113 type statePack struct { 114 peerId string 115 states [][]byte 116 } 117 118 func (p *statePack) PeerId() string { return p.peerId } 119 func (p *statePack) Items() int { return len(p.states) } 120 func (p *statePack) Stats() string { return fmt.Sprintf("%d", len(p.states)) }