From 2c3dba3f998724328525d44a5264a0be8c90a748 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 4 Nov 2015 03:50:12 +0100 Subject: [PATCH] cleanup and minor fixes --- core/types/block.go | 7 ++++ eth/downloader/downloader.go | 4 +-- les/handler.go | 33 ------------------ les/peer.go | 67 ------------------------------------ miner/worker.go | 1 + 5 files changed, 10 insertions(+), 102 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 1d1cfa5151..2c6d94e0c2 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -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) SafeNumber() *big.Int { + if b == nil { + return big.NewInt(0) + } + return b.Number() +} + func (b *Block) HashNoNonce() common.Hash { return b.header.HashNoNonce() } diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 33c4a8ef92..0ab284b6ae 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -1163,10 +1163,10 @@ func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error { for i, header := range rollback { 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) 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 rollback[len(rollback)-1].Number.Uint64() > pivot { diff --git a/les/handler.go b/les/handler.go index 9af619bfd1..d0e2342a31 100644 --- a/les/handler.go +++ b/les/handler.go @@ -502,39 +502,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { 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: 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) diff --git a/les/peer.go b/les/peer.go index 3bc7b73948..9b33267328 100644 --- a/les/peer.go +++ b/les/peer.go @@ -81,14 +81,6 @@ func (p *peer) Head() (hash common.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. func (p *peer) Td() *big.Int { p.lock.RLock() @@ -97,48 +89,11 @@ func (p *peer) Td() *big.Int { 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. func (p *peer) SendBlockHeaders(headers []*types.Header) error { 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 // an already RLP encoded format. 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) } -// 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 // 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 { @@ -316,21 +264,6 @@ func (ps *peerSet) Len() int { 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. func (ps *peerSet) BestPeer() *peer { ps.lock.RLock() diff --git a/miner/worker.go b/miner/worker.go index dacaa1d97a..62e4900462 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -162,6 +162,7 @@ func newDummyWorker(coinbase common.Address, eth core.Backend) *worker { coinbase: coinbase, txQueue: make(map[common.Hash]*types.Transaction), quit: make(chan struct{}), + agents: make(map[Agent]struct{}), fullValidation: false, }