Skip to content

Commit

Permalink
fix race in recent peers (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy authored Jun 30, 2020
1 parent bb8fe43 commit cec9dfa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 9 additions & 3 deletions p2pserver/protocols/recent_peers/recent_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type RecentPeer struct {

func (this *PersistRecentPeerService) saveToFile() {
temp := make(map[uint32][]string)
this.lock.RLock()
for networkId, rps := range this.recentPeers {
temp[networkId] = make([]string, 0)
for _, rp := range rps {
Expand All @@ -91,6 +92,7 @@ func (this *PersistRecentPeerService) saveToFile() {
}
}
}
this.lock.RUnlock()
buf, err := json.Marshal(temp)
if err != nil {
log.Warn("[p2p]package recent peer fail: ", err)
Expand All @@ -104,8 +106,9 @@ func (this *PersistRecentPeerService) saveToFile() {

func NewPersistRecentPeerService(net p2p.P2P) *PersistRecentPeerService {
return &PersistRecentPeerService{
net: net,
quit: make(chan bool),
net: net,
recentPeers: make(map[uint32][]*RecentPeer),
quit: make(chan bool),
}
}

Expand All @@ -114,7 +117,6 @@ func (self *PersistRecentPeerService) Stop() {
}

func (this *PersistRecentPeerService) loadRecentPeers() {
this.recentPeers = make(map[uint32][]*RecentPeer)
if common2.FileExisted(common.RECENT_FILE_NAME) {
buf, err := ioutil.ReadFile(common.RECENT_FILE_NAME)
if err != nil {
Expand All @@ -128,6 +130,8 @@ func (this *PersistRecentPeerService) loadRecentPeers() {
log.Warn("[p2p]parse recent peer file fail: ", err)
return
}
this.lock.Lock()
defer this.lock.Unlock()
for networkId, addrs := range temp {
for _, addr := range addrs {
this.recentPeers[networkId] = append(this.recentPeers[networkId], &RecentPeer{
Expand All @@ -148,6 +152,8 @@ func (this *PersistRecentPeerService) Start() {
//tryRecentPeers try connect recent contact peer when service start
func (this *PersistRecentPeerService) tryRecentPeers() {
netID := config.DefConfig.P2PNode.NetworkMagic
this.lock.RLock()
defer this.lock.RUnlock()
if len(this.recentPeers[netID]) > 0 {
log.Info("[p2p] try to connect recent peer")
}
Expand Down
3 changes: 1 addition & 2 deletions txnpool/common/transaction_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func (tp *TXPool) AddTxList(txEntry *TXEntry) bool {
defer tp.Unlock()
txHash := txEntry.Tx.Hash()
if _, ok := tp.txList[txHash]; ok {
log.Infof("AddTxList: transaction %x is already in the pool",
txHash)
log.Infof("AddTxList: transaction %x is already in the pool", txHash)
return false
}

Expand Down

0 comments on commit cec9dfa

Please sign in to comment.