Browse Source

various bug fixes

pull/139/merge
Jae Kwon 9 years ago
parent
commit
21d45023d3
4 changed files with 19 additions and 18 deletions
  1. +4
    -1
      blockchain/pool.go
  2. +1
    -1
      blockchain/pool_test.go
  3. +2
    -2
      p2p/peer.go
  4. +12
    -14
      p2p/peer_set_test.go

+ 4
- 1
blockchain/pool.go View File

@ -107,7 +107,9 @@ func (pool *BlockPool) removeTimedoutPeers() {
} }
} }
if peer.didTimeout { if peer.didTimeout {
pool.peersMtx.Lock() // Lock
pool.removePeer(peer.id) pool.removePeer(peer.id)
pool.peersMtx.Unlock()
} }
} }
} }
@ -224,7 +226,7 @@ func (pool *BlockPool) removePeer(peerID string) {
for _, request := range pool.requests { for _, request := range pool.requests {
if request.getPeerID() == peerID { if request.getPeerID() == peerID {
pool.numPending++ pool.numPending++
request.redo() // pick another peer and ...
go request.redo() // pick another peer and ...
} }
} }
delete(pool.peers, peerID) delete(pool.peers, peerID)
@ -430,6 +432,7 @@ func (bpr *bpRequester) reset() {
} }
// Tells bpRequester to pick another peer and try again. // Tells bpRequester to pick another peer and try again.
// NOTE: blocking
func (bpr *bpRequester) redo() { func (bpr *bpRequester) redo() {
bpr.redoCh <- struct{}{} bpr.redoCh <- struct{}{}
} }


+ 1
- 1
blockchain/pool_test.go View File

@ -85,7 +85,7 @@ func TestTimeout(t *testing.T) {
pool.Start() pool.Start()
for _, peer := range peers { for _, peer := range peers {
log.Info("Peer", peer.id)
log.Info("Peer", "id", peer.id)
} }
// Introduce each peer. // Introduce each peer.


+ 2
- 2
p2p/peer.go View File

@ -119,9 +119,9 @@ func (p *Peer) WriteTo(w io.Writer) (n int64, err error) {
func (p *Peer) String() string { func (p *Peer) String() string {
if p.outbound { if p.outbound {
return fmt.Sprintf("Peer{->%v}", p.mconn)
return fmt.Sprintf("Peer{%v out}", p.Key)
} else { } else {
return fmt.Sprintf("Peer{%v->}", p.mconn)
return fmt.Sprintf("Peer{%v in}", p.Key)
} }
} }


+ 12
- 14
p2p/peer_set_test.go View File

@ -73,11 +73,9 @@ func newPeerInIPRange(ipBytes ...string) *Peer {
for i, ipByte := range ipBytes { for i, ipByte := range ipBytes {
ips[i] = ipByte ips[i] = ipByte
} }
for i := len(ipBytes); i < 4; i++ { for i := len(ipBytes); i < 4; i++ {
ips[i] = Fmt("%v", rand.Int()%256) ips[i] = Fmt("%v", rand.Int()%256)
} }
ipS := strings.Join(ips, ".") ipS := strings.Join(ips, ".")
return &Peer{ return &Peer{
Key: RandStr(12), Key: RandStr(12),
@ -92,19 +90,19 @@ func TestIPRanges(t *testing.T) {
// test /8 // test /8
maxPeersPerIPRange = [4]int{2, 2, 2, 2} maxPeersPerIPRange = [4]int{2, 2, 2, 2}
peer := newPeerInIPRange("54")
peer := newPeerInIPRange("54", "1")
if err := peerSet.Add(peer); err != nil { if err := peerSet.Add(peer); err != nil {
t.Errorf("Failed to add new peer") t.Errorf("Failed to add new peer")
} }
peer = newPeerInIPRange("54")
peer = newPeerInIPRange("54", "2")
if err := peerSet.Add(peer); err != nil { if err := peerSet.Add(peer); err != nil {
t.Errorf("Failed to add new peer") t.Errorf("Failed to add new peer")
} }
peer = newPeerInIPRange("54")
peer = newPeerInIPRange("54", "3")
if err := peerSet.Add(peer); err == nil { if err := peerSet.Add(peer); err == nil {
t.Errorf("Added peer when we shouldn't have") t.Errorf("Added peer when we shouldn't have")
} }
peer = newPeerInIPRange("55")
peer = newPeerInIPRange("55", "1")
if err := peerSet.Add(peer); err != nil { if err := peerSet.Add(peer); err != nil {
t.Errorf("Failed to add new peer") t.Errorf("Failed to add new peer")
} }
@ -112,19 +110,19 @@ func TestIPRanges(t *testing.T) {
// test /16 // test /16
peerSet = NewPeerSet() peerSet = NewPeerSet()
maxPeersPerIPRange = [4]int{3, 2, 1, 1} maxPeersPerIPRange = [4]int{3, 2, 1, 1}
peer = newPeerInIPRange("54", "112")
peer = newPeerInIPRange("54", "112", "1")
if err := peerSet.Add(peer); err != nil { if err := peerSet.Add(peer); err != nil {
t.Errorf("Failed to add new peer") t.Errorf("Failed to add new peer")
} }
peer = newPeerInIPRange("54", "112")
peer = newPeerInIPRange("54", "112", "2")
if err := peerSet.Add(peer); err != nil { if err := peerSet.Add(peer); err != nil {
t.Errorf("Failed to add new peer") t.Errorf("Failed to add new peer")
} }
peer = newPeerInIPRange("54", "112")
peer = newPeerInIPRange("54", "112", "3")
if err := peerSet.Add(peer); err == nil { if err := peerSet.Add(peer); err == nil {
t.Errorf("Added peer when we shouldn't have") t.Errorf("Added peer when we shouldn't have")
} }
peer = newPeerInIPRange("54", "113")
peer = newPeerInIPRange("54", "113", "1")
if err := peerSet.Add(peer); err != nil { if err := peerSet.Add(peer); err != nil {
t.Errorf("Failed to add new peer") t.Errorf("Failed to add new peer")
} }
@ -132,19 +130,19 @@ func TestIPRanges(t *testing.T) {
// test /24 // test /24
peerSet = NewPeerSet() peerSet = NewPeerSet()
maxPeersPerIPRange = [4]int{5, 3, 2, 1} maxPeersPerIPRange = [4]int{5, 3, 2, 1}
peer = newPeerInIPRange("54", "112", "11")
peer = newPeerInIPRange("54", "112", "11", "1")
if err := peerSet.Add(peer); err != nil { if err := peerSet.Add(peer); err != nil {
t.Errorf("Failed to add new peer") t.Errorf("Failed to add new peer")
} }
peer = newPeerInIPRange("54", "112", "11")
peer = newPeerInIPRange("54", "112", "11", "2")
if err := peerSet.Add(peer); err != nil { if err := peerSet.Add(peer); err != nil {
t.Errorf("Failed to add new peer") t.Errorf("Failed to add new peer")
} }
peer = newPeerInIPRange("54", "112", "11")
peer = newPeerInIPRange("54", "112", "11", "3")
if err := peerSet.Add(peer); err == nil { if err := peerSet.Add(peer); err == nil {
t.Errorf("Added peer when we shouldn't have") t.Errorf("Added peer when we shouldn't have")
} }
peer = newPeerInIPRange("54", "112", "12")
peer = newPeerInIPRange("54", "112", "12", "1")
if err := peerSet.Add(peer); err != nil { if err := peerSet.Add(peer); err != nil {
t.Errorf("Failed to add new peer") t.Errorf("Failed to add new peer")
} }


Loading…
Cancel
Save