cleanup and minor fixes

This commit is contained in:
zsfelfoldi 2015-11-04 03:50:12 +01:00
parent d48f195ea6
commit 2c3dba3f99
5 changed files with 10 additions and 102 deletions

View file

@ -326,6 +326,13 @@ func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Ext
func (b *Block) Header() *Header { return CopyHeader(b.header) } func (b *Block) Header() *Header { return CopyHeader(b.header) }
func (b *Block) SafeNumber() *big.Int {
if b == nil {
return big.NewInt(0)
}
return b.Number()
}
func (b *Block) HashNoNonce() common.Hash { func (b *Block) HashNoNonce() common.Hash {
return b.header.HashNoNonce() return b.header.HashNoNonce()
} }

View file

@ -1163,10 +1163,10 @@ func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error {
for i, header := range rollback { for i, header := range rollback {
hashes[i] = header.Hash() hashes[i] = header.Hash()
} }
lh, lfb, lb := d.headHeader().Number, d.headFastBlock().Number(), d.headBlock().Number() lh, lfb, lb := d.headHeader().Number, d.headFastBlock().SafeNumber(), d.headBlock().SafeNumber()
d.rollback(hashes) d.rollback(hashes)
glog.V(logger.Warn).Infof("Rolled back %d headers (LH: %d->%d, FB: %d->%d, LB: %d->%d)", glog.V(logger.Warn).Infof("Rolled back %d headers (LH: %d->%d, FB: %d->%d, LB: %d->%d)",
len(hashes), lh, d.headHeader().Number, lfb, d.headFastBlock().Number(), lb, d.headBlock().Number()) len(hashes), lh, d.headHeader().Number, lfb, d.headFastBlock().SafeNumber(), lb, d.headBlock().SafeNumber())
// If we're already past the pivot point, this could be an attack, disable fast sync // If we're already past the pivot point, this could be an attack, disable fast sync
if rollback[len(rollback)-1].Number.Uint64() > pivot { if rollback[len(rollback)-1].Number.Uint64() > pivot {

View file

@ -502,39 +502,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
Obj: data, Obj: data,
}) })
case NewBlockHashesMsg:
glog.V(access.LogLevel).Infof("LES: received NewBlockHashesMsg from peer %v", p.id)
// Retrieve and deseralize the remote new block hashes notification
type announce struct {
Hash common.Hash
Number uint64
}
var announces = []announce{}
// We're running the old protocol, make block number unknown (0)
var hashes []common.Hash
if err := msg.Decode(&hashes); err != nil {
return errResp(ErrDecode, "%v: %v", msg, err)
}
for _, hash := range hashes {
announces = append(announces, announce{hash, 0})
}
// Mark the hashes as present at the remote node
for _, block := range announces {
p.MarkBlock(block.Hash)
p.SetHead(block.Hash)
}
// Schedule all the unknown hashes for retrieval
unknown := make([]announce, 0, len(announces))
for _, block := range announces {
if !pm.blockchain.HasBlock(block.Hash) {
unknown = append(unknown, block)
}
}
/*for _, block := range unknown {
pm.fetcher.Notify(p.id, block.Hash, block.Number, time.Now(), nil, p.RequestOneHeader, p.RequestBodies)
}*/
default: default:
glog.V(access.LogLevel).Infof("LES: received unknown message with code %d from peer %v", msg.Code, p.id) glog.V(access.LogLevel).Infof("LES: received unknown message with code %d from peer %v", msg.Code, p.id)
return errResp(ErrInvalidMsgCode, "%v", msg.Code) return errResp(ErrInvalidMsgCode, "%v", msg.Code)

View file

@ -81,14 +81,6 @@ func (p *peer) Head() (hash common.Hash) {
return hash return hash
} }
// SetHead updates the head (most recent) hash of the peer.
func (p *peer) SetHead(hash common.Hash) {
p.lock.Lock()
defer p.lock.Unlock()
copy(p.head[:], hash[:])
}
// Td retrieves the current total difficulty of a peer. // Td retrieves the current total difficulty of a peer.
func (p *peer) Td() *big.Int { func (p *peer) Td() *big.Int {
p.lock.RLock() p.lock.RLock()
@ -97,48 +89,11 @@ func (p *peer) Td() *big.Int {
return new(big.Int).Set(p.td) return new(big.Int).Set(p.td)
} }
// SetTd updates the current total difficulty of a peer.
func (p *peer) SetTd(td *big.Int) {
p.lock.Lock()
defer p.lock.Unlock()
p.td.Set(td)
}
// MarkBlock marks a block as known for the peer, ensuring that the block will
// never be propagated to this particular peer.
func (p *peer) MarkBlock(hash common.Hash) {
// If we reached the memory allowance, drop a previously known block hash
for p.knownBlocks.Size() >= maxKnownBlocks {
p.knownBlocks.Pop()
}
p.knownBlocks.Add(hash)
}
// SendNewBlockHashes announces the availability of a number of blocks through
// a hash notification.
func (p *peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error {
for _, hash := range hashes {
p.knownBlocks.Add(hash)
}
request := make(newBlockHashesData, len(hashes))
for i := 0; i < len(hashes); i++ {
request[i].Hash = hashes[i]
request[i].Number = numbers[i]
}
return p2p.Send(p.rw, NewBlockHashesMsg, request)
}
// SendBlockHeaders sends a batch of block headers to the remote peer. // SendBlockHeaders sends a batch of block headers to the remote peer.
func (p *peer) SendBlockHeaders(headers []*types.Header) error { func (p *peer) SendBlockHeaders(headers []*types.Header) error {
return p2p.Send(p.rw, BlockHeadersMsg, headers) return p2p.Send(p.rw, BlockHeadersMsg, headers)
} }
// SendBlockBodies sends a batch of block contents to the remote peer.
func (p *peer) SendBlockBodies(bodies []*types.Body) error {
return p2p.Send(p.rw, BlockBodiesMsg, blockBodiesData(bodies))
}
// SendBlockBodiesRLP sends a batch of block contents to the remote peer from // SendBlockBodiesRLP sends a batch of block contents to the remote peer from
// an already RLP encoded format. // an already RLP encoded format.
func (p *peer) SendBlockBodiesRLP(bodies []rlp.RawValue) error { func (p *peer) SendBlockBodiesRLP(bodies []rlp.RawValue) error {
@ -162,13 +117,6 @@ func (p *peer) SendProofs(proofs proofsData) error {
return p2p.Send(p.rw, ProofsMsg, proofs) return p2p.Send(p.rw, ProofsMsg, proofs)
} }
// RequestHeaders is a wrapper around the header query functions to fetch a
// single header. It is used solely by the fetcher.
func (p *peer) RequestOneHeader(hash common.Hash) error {
glog.V(logger.Debug).Infof("%v fetching a single header: %x", p, hash)
return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Hash: hash}, Amount: uint64(1), Skip: uint64(0), Reverse: false})
}
// RequestHeadersByHash fetches a batch of blocks' headers corresponding to the // RequestHeadersByHash fetches a batch of blocks' headers corresponding to the
// specified header query, based on the hash of an origin block. // specified header query, based on the hash of an origin block.
func (p *peer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error { func (p *peer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error {
@ -316,21 +264,6 @@ func (ps *peerSet) Len() int {
return len(ps.peers) return len(ps.peers)
} }
// PeersWithoutBlock retrieves a list of peers that do not have a given block in
// their set of known hashes.
func (ps *peerSet) PeersWithoutBlock(hash common.Hash) []*peer {
ps.lock.RLock()
defer ps.lock.RUnlock()
list := make([]*peer, 0, len(ps.peers))
for _, p := range ps.peers {
if !p.knownBlocks.Has(hash) {
list = append(list, p)
}
}
return list
}
// BestPeer retrieves the known peer with the currently highest total difficulty. // BestPeer retrieves the known peer with the currently highest total difficulty.
func (ps *peerSet) BestPeer() *peer { func (ps *peerSet) BestPeer() *peer {
ps.lock.RLock() ps.lock.RLock()

View file

@ -162,6 +162,7 @@ func newDummyWorker(coinbase common.Address, eth core.Backend) *worker {
coinbase: coinbase, coinbase: coinbase,
txQueue: make(map[common.Hash]*types.Transaction), txQueue: make(map[common.Hash]*types.Transaction),
quit: make(chan struct{}), quit: make(chan struct{}),
agents: make(map[Agent]struct{}),
fullValidation: false, fullValidation: false,
} }