diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-06-09 19:27:44 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-06-09 19:27:44 +0800 |
commit | 44147d057dd91d8b35dd6f4ed025bdb4baf225eb (patch) | |
tree | 0747516ad8924a1e61ceb97936419d9c3f41ffd1 /eth/peer.go | |
parent | 05cae69d7295a924a4d2aeb49d23869047451e08 (diff) | |
download | go-tangerine-44147d057dd91d8b35dd6f4ed025bdb4baf225eb.tar.gz go-tangerine-44147d057dd91d8b35dd6f4ed025bdb4baf225eb.tar.zst go-tangerine-44147d057dd91d8b35dd6f4ed025bdb4baf225eb.zip |
eth: fix data race accessing peer.recentHash
Diffstat (limited to 'eth/peer.go')
-rw-r--r-- | eth/peer.go | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/eth/peer.go b/eth/peer.go index cf2c58ec9..5f5007891 100644 --- a/eth/peer.go +++ b/eth/peer.go @@ -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 } |