eth: clean up peer struct a bit, fix double txn bcast

This commit is contained in:
Péter Szilágyi 2015-06-29 12:44:00 +03:00
parent 2c8ed76e01
commit 6fc85f1ec2
4 changed files with 69 additions and 71 deletions

View File

@ -158,9 +158,7 @@ func (pm *ProtocolManager) Stop() {
} }
func (pm *ProtocolManager) newPeer(pv, nv int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { func (pm *ProtocolManager) newPeer(pv, nv int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
td, current, genesis := pm.chainman.Status() return newPeer(pv, nv, p, rw)
return newPeer(pv, nv, genesis, current, td, p, rw)
} }
// handle is the callback invoked to manage the life cycle of an eth peer. When // handle is the callback invoked to manage the life cycle of an eth peer. When
@ -169,7 +167,8 @@ func (pm *ProtocolManager) handle(p *peer) error {
glog.V(logger.Debug).Infof("%v: peer connected", p) glog.V(logger.Debug).Infof("%v: peer connected", p)
// Execute the Ethereum handshake // Execute the Ethereum handshake
if err := p.handleStatus(); err != nil { td, head, genesis := pm.chainman.Status()
if err := p.Handshake(td, head, genesis); err != nil {
glog.V(logger.Debug).Infof("%v: handshake failed: %v", p, err) glog.V(logger.Debug).Infof("%v: handshake failed: %v", p, err)
return err return err
} }
@ -182,7 +181,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
defer pm.removePeer(p.id) defer pm.removePeer(p.id)
// Register the peer in the downloader. If the downloader considers it banned, we disconnect // Register the peer in the downloader. If the downloader considers it banned, we disconnect
if err := pm.downloader.RegisterPeer(p.id, p.Head(), p.requestHashes, p.requestBlocks); err != nil { if err := pm.downloader.RegisterPeer(p.id, p.Head(), p.RequestHashes, p.RequestBlocks); err != nil {
return err return err
} }
// Propagate existing transactions. new transactions appearing // Propagate existing transactions. new transactions appearing
@ -225,9 +224,13 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
} }
propTxnInPacketsMeter.Mark(1) propTxnInPacketsMeter.Mark(1)
for i, tx := range txs { for i, tx := range txs {
// Validate and mark the remote transaction
if tx == nil { if tx == nil {
return errResp(ErrDecode, "transaction %d is nil", i) return errResp(ErrDecode, "transaction %d is nil", i)
} }
p.MarkTransaction(tx.Hash())
// Log it's arrival for later analysis
propTxnInTrafficMeter.Mark(tx.Size().Int64()) propTxnInTrafficMeter.Mark(tx.Size().Int64())
jsonlogger.LogJson(&logger.EthTxReceived{ jsonlogger.LogJson(&logger.EthTxReceived{
TxHash: tx.Hash().Hex(), TxHash: tx.Hash().Hex(),
@ -255,7 +258,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
} }
// returns either requested hashes or nothing (i.e. not found) // returns either requested hashes or nothing (i.e. not found)
return p.sendBlockHashes(hashes) return p.SendBlockHashes(hashes)
case BlockHashesMsg: case BlockHashesMsg:
// A batch of hashes arrived to one of our previous requests // A batch of hashes arrived to one of our previous requests
@ -314,7 +317,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
glog.Infof("%v: no blocks found for requested hashes %s", p, list) glog.Infof("%v: no blocks found for requested hashes %s", p, list)
} }
return p.sendBlocks(blocks) return p.SendBlocks(blocks)
case BlocksMsg: case BlocksMsg:
// Decode the arrived block message // Decode the arrived block message
@ -349,7 +352,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
// Mark the hashes as present at the remote node // Mark the hashes as present at the remote node
for _, hash := range hashes { for _, hash := range hashes {
p.blockHashes.Add(hash) p.MarkBlock(hash)
p.SetHead(hash) p.SetHead(hash)
} }
// Schedule all the unknown hashes for retrieval // Schedule all the unknown hashes for retrieval
@ -360,7 +363,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
} }
} }
for _, hash := range unknown { for _, hash := range unknown {
pm.fetcher.Notify(p.id, hash, time.Now(), p.requestBlocks) pm.fetcher.Notify(p.id, hash, time.Now(), p.RequestBlocks)
} }
case NewBlockMsg: case NewBlockMsg:
@ -387,7 +390,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
RemoteId: p.ID().String(), RemoteId: p.ID().String(),
}) })
// Mark the peer as owning the block and schedule it for import // Mark the peer as owning the block and schedule it for import
p.blockHashes.Add(request.Block.Hash()) p.MarkBlock(request.Block.Hash())
p.SetHead(request.Block.Hash()) p.SetHead(request.Block.Hash())
pm.fetcher.Enqueue(p.id, request.Block) pm.fetcher.Enqueue(p.id, request.Block)
@ -412,14 +415,14 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
if propagate { if propagate {
transfer := peers[:int(math.Sqrt(float64(len(peers))))] transfer := peers[:int(math.Sqrt(float64(len(peers))))]
for _, peer := range transfer { for _, peer := range transfer {
peer.sendNewBlock(block) peer.SendNewBlock(block)
} }
glog.V(logger.Detail).Infof("propagated block %x to %d peers in %v", hash[:4], len(transfer), time.Since(block.ReceivedAt)) glog.V(logger.Detail).Infof("propagated block %x to %d peers in %v", hash[:4], len(transfer), time.Since(block.ReceivedAt))
} }
// Otherwise if the block is indeed in out own chain, announce it // Otherwise if the block is indeed in out own chain, announce it
if pm.chainman.HasBlock(hash) { if pm.chainman.HasBlock(hash) {
for _, peer := range peers { for _, peer := range peers {
peer.sendNewBlockHashes([]common.Hash{hash}) peer.SendNewBlockHashes([]common.Hash{hash})
} }
glog.V(logger.Detail).Infof("announced block %x to %d peers in %v", hash[:4], len(peers), time.Since(block.ReceivedAt)) glog.V(logger.Detail).Infof("announced block %x to %d peers in %v", hash[:4], len(peers), time.Since(block.ReceivedAt))
} }
@ -432,7 +435,7 @@ func (pm *ProtocolManager) BroadcastTx(hash common.Hash, tx *types.Transaction)
peers := pm.peers.PeersWithoutTx(hash) peers := pm.peers.PeersWithoutTx(hash)
//FIXME include this again: peers = peers[:int(math.Sqrt(float64(len(peers))))] //FIXME include this again: peers = peers[:int(math.Sqrt(float64(len(peers))))]
for _, peer := range peers { for _, peer := range peers {
peer.sendTransactions(types.Transactions{tx}) peer.SendTransactions(types.Transactions{tx})
} }
glog.V(logger.Detail).Infoln("broadcast tx to", len(peers), "peers") glog.V(logger.Detail).Infoln("broadcast tx to", len(peers), "peers")
} }

View File

@ -47,27 +47,21 @@ type peer struct {
td *big.Int td *big.Int
lock sync.RWMutex lock sync.RWMutex
genesis, ourHash common.Hash knownTxs *set.Set // Set of transaction hashes known to be known by this peer
ourTd *big.Int knownBlocks *set.Set // Set of block hashes known to be known by this peer
txHashes *set.Set
blockHashes *set.Set
} }
func newPeer(version, network int, genesis, head common.Hash, td *big.Int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { func newPeer(version, network int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
id := p.ID() id := p.ID()
return &peer{ return &peer{
Peer: p, Peer: p,
rw: rw, rw: rw,
genesis: genesis,
ourHash: head,
ourTd: td,
version: version, version: version,
network: network, network: network,
id: fmt.Sprintf("%x", id[:8]), id: fmt.Sprintf("%x", id[:8]),
txHashes: set.New(), knownTxs: set.New(),
blockHashes: set.New(), knownBlocks: set.New(),
} }
} }
@ -104,27 +98,39 @@ func (p *peer) SetTd(td *big.Int) {
p.td.Set(td) p.td.Set(td)
} }
// sendTransactions sends transactions to the peer and includes the hashes // 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) {
p.knownBlocks.Add(hash)
}
// MarkTransaction marks a transaction as known for the peer, ensuring that it
// will never be propagated to this particular peer.
func (p *peer) MarkTransaction(hash common.Hash) {
p.knownTxs.Add(hash)
}
// SendTransactions sends transactions to the peer and includes the hashes
// in its transaction hash set for future reference. // in its transaction hash set for future reference.
func (p *peer) sendTransactions(txs types.Transactions) error { func (p *peer) SendTransactions(txs types.Transactions) error {
propTxnOutPacketsMeter.Mark(1) propTxnOutPacketsMeter.Mark(1)
for _, tx := range txs { for _, tx := range txs {
propTxnOutTrafficMeter.Mark(tx.Size().Int64()) propTxnOutTrafficMeter.Mark(tx.Size().Int64())
p.txHashes.Add(tx.Hash()) p.knownTxs.Add(tx.Hash())
} }
return p2p.Send(p.rw, TxMsg, txs) return p2p.Send(p.rw, TxMsg, txs)
} }
// sendBlockHashes sends a batch of known hashes to the remote peer. // SendBlockHashes sends a batch of known hashes to the remote peer.
func (p *peer) sendBlockHashes(hashes []common.Hash) error { func (p *peer) SendBlockHashes(hashes []common.Hash) error {
reqHashOutPacketsMeter.Mark(1) reqHashOutPacketsMeter.Mark(1)
reqHashOutTrafficMeter.Mark(int64(32 * len(hashes))) reqHashOutTrafficMeter.Mark(int64(32 * len(hashes)))
return p2p.Send(p.rw, BlockHashesMsg, hashes) return p2p.Send(p.rw, BlockHashesMsg, hashes)
} }
// sendBlocks sends a batch of blocks to the remote peer. // SendBlocks sends a batch of blocks to the remote peer.
func (p *peer) sendBlocks(blocks []*types.Block) error { func (p *peer) SendBlocks(blocks []*types.Block) error {
reqBlockOutPacketsMeter.Mark(1) reqBlockOutPacketsMeter.Mark(1)
for _, block := range blocks { for _, block := range blocks {
reqBlockOutTrafficMeter.Mark(block.Size().Int64()) reqBlockOutTrafficMeter.Mark(block.Size().Int64())
@ -132,52 +138,55 @@ func (p *peer) sendBlocks(blocks []*types.Block) error {
return p2p.Send(p.rw, BlocksMsg, blocks) return p2p.Send(p.rw, BlocksMsg, blocks)
} }
// sendNewBlockHashes announces the availability of a number of blocks through // SendNewBlockHashes announces the availability of a number of blocks through
// a hash notification. // a hash notification.
func (p *peer) sendNewBlockHashes(hashes []common.Hash) error { func (p *peer) SendNewBlockHashes(hashes []common.Hash) error {
propHashOutPacketsMeter.Mark(1) propHashOutPacketsMeter.Mark(1)
propHashOutTrafficMeter.Mark(int64(32 * len(hashes))) propHashOutTrafficMeter.Mark(int64(32 * len(hashes)))
for _, hash := range hashes { for _, hash := range hashes {
p.blockHashes.Add(hash) p.knownBlocks.Add(hash)
} }
return p2p.Send(p.rw, NewBlockHashesMsg, hashes) return p2p.Send(p.rw, NewBlockHashesMsg, hashes)
} }
// sendNewBlock propagates an entire block to a remote peer. // SendNewBlock propagates an entire block to a remote peer.
func (p *peer) sendNewBlock(block *types.Block) error { func (p *peer) SendNewBlock(block *types.Block) error {
propBlockOutPacketsMeter.Mark(1) propBlockOutPacketsMeter.Mark(1)
propBlockOutTrafficMeter.Mark(block.Size().Int64()) propBlockOutTrafficMeter.Mark(block.Size().Int64())
p.blockHashes.Add(block.Hash()) p.knownBlocks.Add(block.Hash())
return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, block.Td}) return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, block.Td})
} }
// requestHashes fetches a batch of hashes from a peer, starting at from, going // RequestHashes fetches a batch of hashes from a peer, starting at from, going
// towards the genesis block. // towards the genesis block.
func (p *peer) requestHashes(from common.Hash) error { func (p *peer) RequestHashes(from common.Hash) error {
glog.V(logger.Debug).Infof("Peer [%s] fetching hashes (%d) %x...\n", p.id, downloader.MaxHashFetch, from[:4]) glog.V(logger.Debug).Infof("Peer [%s] fetching hashes (%d) %x...\n", p.id, downloader.MaxHashFetch, from[:4])
return p2p.Send(p.rw, GetBlockHashesMsg, getBlockHashesMsgData{from, uint64(downloader.MaxHashFetch)}) return p2p.Send(p.rw, GetBlockHashesMsg, getBlockHashesMsgData{from, uint64(downloader.MaxHashFetch)})
} }
func (p *peer) requestBlocks(hashes []common.Hash) error { // RequestBlocks fetches a batch of blocks corresponding to the specified hashes.
func (p *peer) RequestBlocks(hashes []common.Hash) error {
glog.V(logger.Debug).Infof("[%s] fetching %v blocks\n", p.id, len(hashes)) glog.V(logger.Debug).Infof("[%s] fetching %v blocks\n", p.id, len(hashes))
return p2p.Send(p.rw, GetBlocksMsg, hashes) return p2p.Send(p.rw, GetBlocksMsg, hashes)
} }
func (p *peer) handleStatus() error { // Handshake executes the eth protocol handshake, negotiating version number,
// network IDs, difficulties, head and genesis blocks.
func (p *peer) Handshake(td *big.Int, head common.Hash, genesis common.Hash) error {
// Send out own handshake in a new thread
errc := make(chan error, 1) errc := make(chan error, 1)
go func() { go func() {
errc <- p2p.Send(p.rw, StatusMsg, &statusMsgData{ errc <- p2p.Send(p.rw, StatusMsg, &statusMsgData{
ProtocolVersion: uint32(p.version), ProtocolVersion: uint32(p.version),
NetworkId: uint32(p.network), NetworkId: uint32(p.network),
TD: p.ourTd, TD: td,
CurrentBlock: p.ourHash, CurrentBlock: head,
GenesisBlock: p.genesis, GenesisBlock: genesis,
}) })
}() }()
// In the mean time retrieve the remote status message
// read and handle remote status
msg, err := p.rw.ReadMsg() msg, err := p.rw.ReadMsg()
if err != nil { if err != nil {
return err return err
@ -188,28 +197,22 @@ func (p *peer) handleStatus() error {
if msg.Size > ProtocolMaxMsgSize { if msg.Size > ProtocolMaxMsgSize {
return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize)
} }
// Decode the handshake and make sure everything matches
var status statusMsgData var status statusMsgData
if err := msg.Decode(&status); err != nil { if err := msg.Decode(&status); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err) return errResp(ErrDecode, "msg %v: %v", msg, err)
} }
if status.GenesisBlock != genesis {
if status.GenesisBlock != p.genesis { return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock, genesis)
return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock, p.genesis)
} }
if int(status.NetworkId) != p.network { if int(status.NetworkId) != p.network {
return errResp(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, p.network) return errResp(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, p.network)
} }
if int(status.ProtocolVersion) != p.version { if int(status.ProtocolVersion) != p.version {
return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, p.version) return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, p.version)
} }
// Set the total difficulty of the peer // Configure the remote peer, and sanity check out handshake too
p.td = status.TD p.td, p.head = status.TD, status.CurrentBlock
// set the best hash of the peer
p.head = status.CurrentBlock
return <-errc return <-errc
} }
@ -284,7 +287,7 @@ func (ps *peerSet) PeersWithoutBlock(hash common.Hash) []*peer {
list := make([]*peer, 0, len(ps.peers)) list := make([]*peer, 0, len(ps.peers))
for _, p := range ps.peers { for _, p := range ps.peers {
if !p.blockHashes.Has(hash) { if !p.knownBlocks.Has(hash) {
list = append(list, p) list = append(list, p)
} }
} }
@ -299,7 +302,7 @@ func (ps *peerSet) PeersWithoutTx(hash common.Hash) []*peer {
list := make([]*peer, 0, len(ps.peers)) list := make([]*peer, 0, len(ps.peers))
for _, p := range ps.peers { for _, p := range ps.peers {
if !p.txHashes.Has(hash) { if !p.knownTxs.Has(hash) {
list = append(list, p) list = append(list, p)
} }
} }

View File

@ -43,11 +43,11 @@ func TestStatusMsgErrors(t *testing.T) {
wantError: errResp(ErrProtocolVersionMismatch, "10 (!= 0)"), wantError: errResp(ErrProtocolVersionMismatch, "10 (!= 0)"),
}, },
{ {
code: StatusMsg, data: statusMsgData{ProtocolVersion, 999, td, currentBlock, genesis}, code: StatusMsg, data: statusMsgData{uint32(ProtocolVersions[0]), 999, td, currentBlock, genesis},
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 0)"), wantError: errResp(ErrNetworkIdMismatch, "999 (!= 0)"),
}, },
{ {
code: StatusMsg, data: statusMsgData{ProtocolVersion, NetworkId, td, currentBlock, common.Hash{3}}, code: StatusMsg, data: statusMsgData{uint32(ProtocolVersions[0]), NetworkId, td, currentBlock, common.Hash{3}},
wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000000000000000000000000000000000000000000000000000 (!= %x)", genesis), wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000000000000000000000000000000000000000000000000000 (!= %x)", genesis),
}, },
} }
@ -167,7 +167,7 @@ func newProtocolManagerForTesting(txAdded chan<- []*types.Transaction) *Protocol
db, _ = ethdb.NewMemDatabase() db, _ = ethdb.NewMemDatabase()
chain, _ = core.NewChainManager(core.GenesisBlock(0, db), db, db, core.FakePow{}, em) chain, _ = core.NewChainManager(core.GenesisBlock(0, db), db, db, core.FakePow{}, em)
txpool = &fakeTxPool{added: txAdded} txpool = &fakeTxPool{added: txAdded}
pm = NewProtocolManager(ProtocolVersion, 0, em, txpool, core.FakePow{}, chain) pm = NewProtocolManager(0, em, txpool, core.FakePow{}, chain)
) )
pm.Start() pm.Start()
return pm return pm

View File

@ -20,14 +20,6 @@ const (
txsyncPackSize = 100 * 1024 txsyncPackSize = 100 * 1024
) )
// blockAnnounce is the hash notification of the availability of a new block in
// the network.
type blockAnnounce struct {
hash common.Hash
peer *peer
time time.Time
}
type txsync struct { type txsync struct {
p *peer p *peer
txs []*types.Transaction txs []*types.Transaction
@ -75,7 +67,7 @@ func (pm *ProtocolManager) txsyncLoop() {
// Send the pack in the background. // Send the pack in the background.
glog.V(logger.Detail).Infof("%v: sending %d transactions (%v)", s.p.Peer, len(pack.txs), size) glog.V(logger.Detail).Infof("%v: sending %d transactions (%v)", s.p.Peer, len(pack.txs), size)
sending = true sending = true
go func() { done <- pack.p.sendTransactions(pack.txs) }() go func() { done <- pack.p.SendTransactions(pack.txs) }()
} }
// pick chooses the next pending sync. // pick chooses the next pending sync.