eth: fix data race accessing peer.recentHash

This commit is contained in:
Péter Szilágyi
2015-06-09 14:27:44 +03:00
parent 05cae69d72
commit 44147d057d
3 changed files with 32 additions and 12 deletions

View File

@ -40,9 +40,11 @@ type peer struct {
protv, netid int
recentHash common.Hash
id string
td *big.Int
id string
td *big.Int
head common.Hash
headLock sync.RWMutex
genesis, ourHash common.Hash
ourTd *big.Int
@ -51,14 +53,14 @@ type peer struct {
blockHashes *set.Set
}
func newPeer(protv, netid int, genesis, recentHash common.Hash, td *big.Int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
func newPeer(protv, netid int, genesis, head common.Hash, td *big.Int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
id := p.ID()
return &peer{
Peer: p,
rw: rw,
genesis: genesis,
ourHash: recentHash,
ourHash: head,
ourTd: td,
protv: protv,
netid: netid,
@ -68,6 +70,23 @@ func newPeer(protv, netid int, genesis, recentHash common.Hash, td *big.Int, p *
}
}
// Head retrieves a copy of the current head (most recent) hash of the peer.
func (p *peer) Head() (hash common.Hash) {
p.headLock.RLock()
defer p.headLock.RUnlock()
copy(hash[:], p.head[:])
return hash
}
// SetHead updates the head (most recent) hash of the peer.
func (p *peer) SetHead(hash common.Hash) {
p.headLock.Lock()
defer p.headLock.Unlock()
copy(p.head[:], hash[:])
}
// sendTransactions sends transactions to the peer and includes the hashes
// in it's tx hash set for future reference. The tx hash will allow the
// manager to check whether the peer has already received this particular
@ -160,7 +179,7 @@ func (p *peer) handleStatus() error {
// Set the total difficulty of the peer
p.td = status.TD
// set the best hash of the peer
p.recentHash = status.CurrentBlock
p.head = status.CurrentBlock
return <-errc
}