From bd353e004abd50d5e2a63b2ffc2ed12c0a78adf9 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Fri, 28 Oct 2016 12:03:21 -0700 Subject: [PATCH 1/5] QuitService->BaseService --- addrbook.go | 8 ++++---- switch.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/addrbook.go b/addrbook.go index d2e4d48e..2bbae64f 100644 --- a/addrbook.go +++ b/addrbook.go @@ -75,7 +75,7 @@ const ( /* AddrBook - concurrency safe peer address manager */ type AddrBook struct { - QuitService + BaseService mtx sync.Mutex filePath string @@ -106,7 +106,7 @@ func NewAddrBook(filePath string, routabilityStrict bool) *AddrBook { routabilityStrict: routabilityStrict, } am.init() - am.QuitService = *NewQuitService(log, "AddrBook", am) + am.BaseService = *NewBaseService(log, "AddrBook", am) return am } @@ -126,7 +126,7 @@ func (a *AddrBook) init() { } func (a *AddrBook) OnStart() error { - a.QuitService.OnStart() + a.BaseService.OnStart() a.loadFromFile(a.filePath) a.wg.Add(1) go a.saveRoutine() @@ -134,7 +134,7 @@ func (a *AddrBook) OnStart() error { } func (a *AddrBook) OnStop() { - a.QuitService.OnStop() + a.BaseService.OnStop() a.wg.Wait() } diff --git a/switch.go b/switch.go index 61d26949..bbb73805 100644 --- a/switch.go +++ b/switch.go @@ -26,13 +26,13 @@ type Reactor interface { //-------------------------------------- type BaseReactor struct { - QuitService // Provides Start, Stop, .Quit + BaseService // Provides Start, Stop, .Quit Switch *Switch } func NewBaseReactor(log log15.Logger, name string, impl Reactor) *BaseReactor { return &BaseReactor{ - QuitService: *NewQuitService(log, name, impl), + BaseService: *NewBaseService(log, name, impl), Switch: nil, } } From 58e42397f839d35a95dc0ca6d39367f47ae3206f Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Wed, 23 Nov 2016 18:16:40 -0500 Subject: [PATCH 2/5] close conns on filter; fix order in MakeConnectedSwitch --- switch.go | 14 +++++++------- types.go | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/switch.go b/switch.go index bbb73805..d029854e 100644 --- a/switch.go +++ b/switch.go @@ -195,8 +195,10 @@ func (sw *Switch) OnStop() { // NOTE: This performs a blocking handshake before the peer is added. // CONTRACT: Iff error is returned, peer is nil, and conn is immediately closed. func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, error) { + // Filter by addr (ie. ip:port) if err := sw.FilterConnByAddr(conn.RemoteAddr()); err != nil { + conn.Close() return nil, err } @@ -217,6 +219,7 @@ func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, er // Filter by p2p-key if err := sw.FilterConnByPubKey(sconn.(*SecretConnection).RemotePubKey()); err != nil { + sconn.Close() return nil, err } @@ -466,18 +469,15 @@ func MakeConnectedSwitches(n int, initSwitch func(int, *Switch) *Switch, connect switches[i] = makeSwitch(i, "testing", "123.123.123", initSwitch) } + if err := StartSwitches(switches); err != nil { + panic(err) + } + for i := 0; i < n; i++ { for j := i; j < n; j++ { connect(switches, i, j) } } - // Wait for things to happen, peers to get added... - // TODO: better - time.Sleep(100 * time.Millisecond * time.Duration(n*n)) - - if err := StartSwitches(switches); err != nil { - panic(err) - } return switches } diff --git a/types.go b/types.go index 0db2c957..4f3e4c1d 100644 --- a/types.go +++ b/types.go @@ -21,7 +21,7 @@ type NodeInfo struct { Other []string `json:"other"` // other application specific data } -// CONTRACT: two nodes are compactible if the major/minor versions match and network match +// CONTRACT: two nodes are compatible if the major/minor versions match and network match func (info *NodeInfo) CompatibleWith(other *NodeInfo) error { iMajor, iMinor, _, iErr := splitVersion(info.Version) oMajor, oMinor, _, oErr := splitVersion(other.Version) From 2b750ea49f39c0ac113fd60cee52c7f150a6da60 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Tue, 6 Dec 2016 01:13:03 -0800 Subject: [PATCH 3/5] Make Connect2Switches blocking --- switch.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/switch.go b/switch.go index d029854e..f39ed092 100644 --- a/switch.go +++ b/switch.go @@ -483,13 +483,23 @@ func MakeConnectedSwitches(n int, initSwitch func(int, *Switch) *Switch, connect } // Will connect switches i and j via net.Pipe() +// Blocks until a conection is established. // NOTE: caller ensures i and j are within bounds func Connect2Switches(switches []*Switch, i, j int) { switchI := switches[i] switchJ := switches[j] c1, c2 := net.Pipe() - go switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake. - go switchJ.AddPeerWithConnection(c2, true) + doneCh := make(chan struct{}) + go func() { + switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake. + doneCh <- struct{}{} + }() + go func() { + switchJ.AddPeerWithConnection(c2, true) + doneCh <- struct{}{} + }() + <-doneCh + <-doneCh } func StartSwitches(switches []*Switch) error { From e47722ecb2999baaf7a77131a421b8a3788a2267 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 19 Dec 2016 18:24:51 -0500 Subject: [PATCH 4/5] Connect2Switches: panic on err --- switch.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/switch.go b/switch.go index f39ed092..7fabe21a 100644 --- a/switch.go +++ b/switch.go @@ -491,11 +491,17 @@ func Connect2Switches(switches []*Switch, i, j int) { c1, c2 := net.Pipe() doneCh := make(chan struct{}) go func() { - switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake. + _, err := switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake. + if err != nil { + panic(err) + } doneCh <- struct{}{} }() go func() { - switchJ.AddPeerWithConnection(c2, true) + _, err := switchJ.AddPeerWithConnection(c2, true) + if err != nil { + panic(err) + } doneCh <- struct{}{} }() <-doneCh From 67c9086b7458eb45b1970483decd01cd744c477a Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 12 Jan 2017 21:09:24 -0500 Subject: [PATCH 5/5] optional panic on AddPeer err --- switch.go | 6 ++++-- version.go | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/switch.go b/switch.go index 7fabe21a..841c44f8 100644 --- a/switch.go +++ b/switch.go @@ -482,6 +482,8 @@ func MakeConnectedSwitches(n int, initSwitch func(int, *Switch) *Switch, connect return switches } +var PanicOnAddPeerErr = false + // Will connect switches i and j via net.Pipe() // Blocks until a conection is established. // NOTE: caller ensures i and j are within bounds @@ -492,14 +494,14 @@ func Connect2Switches(switches []*Switch, i, j int) { doneCh := make(chan struct{}) go func() { _, err := switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake. - if err != nil { + if PanicOnAddPeerErr && err != nil { panic(err) } doneCh <- struct{}{} }() go func() { _, err := switchJ.AddPeerWithConnection(c2, true) - if err != nil { + if PanicOnAddPeerErr && err != nil { panic(err) } doneCh <- struct{}{} diff --git a/version.go b/version.go index 8608f275..af98bef3 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package p2p -const Version = "0.3.4" // filter by addr or pubkey +const Version = "0.3.5" // minor fixes