|
|
@ -63,35 +63,45 @@ func TestPEXReactorRunning(t *testing.T) { |
|
|
|
N := 3 |
|
|
|
switches := make([]*p2p.Switch, N) |
|
|
|
|
|
|
|
// directory to store address books
|
|
|
|
dir, err := ioutil.TempDir("", "pex_reactor") |
|
|
|
require.Nil(t, err) |
|
|
|
defer os.RemoveAll(dir) // nolint: errcheck
|
|
|
|
book := NewAddrBook(filepath.Join(dir, "addrbook.json"), false) |
|
|
|
book.SetLogger(log.TestingLogger()) |
|
|
|
|
|
|
|
books := make([]*addrBook, N) |
|
|
|
logger := log.TestingLogger() |
|
|
|
|
|
|
|
// create switches
|
|
|
|
for i := 0; i < N; i++ { |
|
|
|
switches[i] = p2p.MakeSwitch(config, i, "127.0.0.1", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch { |
|
|
|
sw.SetLogger(log.TestingLogger().With("switch", i)) |
|
|
|
books[i] = NewAddrBook(filepath.Join(dir, fmt.Sprintf("addrbook%d.json", i)), false) |
|
|
|
books[i].SetLogger(logger.With("pex", i)) |
|
|
|
sw.SetAddrBook(books[i]) |
|
|
|
|
|
|
|
r := NewPEXReactor(book, &PEXReactorConfig{}) |
|
|
|
r.SetLogger(log.TestingLogger()) |
|
|
|
sw.SetLogger(logger.With("pex", i)) |
|
|
|
|
|
|
|
r := NewPEXReactor(books[i], &PEXReactorConfig{}) |
|
|
|
r.SetLogger(logger.With("pex", i)) |
|
|
|
r.SetEnsurePeersPeriod(250 * time.Millisecond) |
|
|
|
sw.AddReactor("pex", r) |
|
|
|
|
|
|
|
return sw |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// fill the address book and add listeners
|
|
|
|
for _, s := range switches { |
|
|
|
addr := s.NodeInfo().NetAddress() |
|
|
|
book.AddAddress(addr, addr) |
|
|
|
s.AddListener(p2p.NewDefaultListener("tcp", s.NodeInfo().ListenAddr, true, log.TestingLogger())) |
|
|
|
addOtherNodeAddrToAddrBook := func(switchIndex, otherSwitchIndex int) { |
|
|
|
addr := switches[otherSwitchIndex].NodeInfo().NetAddress() |
|
|
|
books[switchIndex].AddAddress(addr, addr) |
|
|
|
} |
|
|
|
|
|
|
|
// start switches
|
|
|
|
for _, s := range switches { |
|
|
|
err := s.Start() // start switch and reactors
|
|
|
|
addOtherNodeAddrToAddrBook(0, 1) |
|
|
|
addOtherNodeAddrToAddrBook(1, 0) |
|
|
|
addOtherNodeAddrToAddrBook(2, 1) |
|
|
|
|
|
|
|
for i, sw := range switches { |
|
|
|
sw.AddListener(p2p.NewDefaultListener("tcp", sw.NodeInfo().ListenAddr, true, logger.With("pex", i))) |
|
|
|
|
|
|
|
err := sw.Start() // start switch and reactors
|
|
|
|
require.Nil(t, err) |
|
|
|
} |
|
|
|
|
|
|
@ -127,6 +137,7 @@ func TestPEXReactorRequestMessageAbuse(t *testing.T) { |
|
|
|
defer teardownReactor(book) |
|
|
|
|
|
|
|
sw := createSwitchAndAddReactors(r) |
|
|
|
sw.SetAddrBook(book) |
|
|
|
|
|
|
|
peer := newMockPeer() |
|
|
|
p2p.AddPeerToSwitch(sw, peer) |
|
|
@ -156,6 +167,7 @@ func TestPEXReactorAddrsMessageAbuse(t *testing.T) { |
|
|
|
defer teardownReactor(book) |
|
|
|
|
|
|
|
sw := createSwitchAndAddReactors(r) |
|
|
|
sw.SetAddrBook(book) |
|
|
|
|
|
|
|
peer := newMockPeer() |
|
|
|
p2p.AddPeerToSwitch(sw, peer) |
|
|
@ -182,13 +194,11 @@ func TestPEXReactorAddrsMessageAbuse(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestPEXReactorUsesSeedsIfNeeded(t *testing.T) { |
|
|
|
// directory to store address books
|
|
|
|
dir, err := ioutil.TempDir("", "pex_reactor") |
|
|
|
require.Nil(t, err) |
|
|
|
defer os.RemoveAll(dir) // nolint: errcheck
|
|
|
|
|
|
|
|
book := NewAddrBook(filepath.Join(dir, "addrbook.json"), false) |
|
|
|
book.SetLogger(log.TestingLogger()) |
|
|
|
|
|
|
|
// 1. create seed
|
|
|
|
seed := p2p.MakeSwitch( |
|
|
|
config, |
|
|
@ -196,6 +206,10 @@ func TestPEXReactorUsesSeedsIfNeeded(t *testing.T) { |
|
|
|
"127.0.0.1", |
|
|
|
"123.123.123", |
|
|
|
func(i int, sw *p2p.Switch) *p2p.Switch { |
|
|
|
book := NewAddrBook(filepath.Join(dir, "addrbook0.json"), false) |
|
|
|
book.SetLogger(log.TestingLogger()) |
|
|
|
sw.SetAddrBook(book) |
|
|
|
|
|
|
|
sw.SetLogger(log.TestingLogger()) |
|
|
|
|
|
|
|
r := NewPEXReactor(book, &PEXReactorConfig{}) |
|
|
@ -222,6 +236,10 @@ func TestPEXReactorUsesSeedsIfNeeded(t *testing.T) { |
|
|
|
"127.0.0.1", |
|
|
|
"123.123.123", |
|
|
|
func(i int, sw *p2p.Switch) *p2p.Switch { |
|
|
|
book := NewAddrBook(filepath.Join(dir, "addrbook1.json"), false) |
|
|
|
book.SetLogger(log.TestingLogger()) |
|
|
|
sw.SetAddrBook(book) |
|
|
|
|
|
|
|
sw.SetLogger(log.TestingLogger()) |
|
|
|
|
|
|
|
r := NewPEXReactor( |
|
|
@ -247,7 +265,8 @@ func TestPEXReactorCrawlStatus(t *testing.T) { |
|
|
|
defer teardownReactor(book) |
|
|
|
|
|
|
|
// Seed/Crawler mode uses data from the Switch
|
|
|
|
_ = createSwitchAndAddReactors(pexR) |
|
|
|
sw := createSwitchAndAddReactors(pexR) |
|
|
|
sw.SetAddrBook(book) |
|
|
|
|
|
|
|
// Create a peer, add it to the peer set and the addrbook.
|
|
|
|
peer := p2p.CreateRandomPeer(false) |
|
|
@ -291,7 +310,8 @@ func TestPEXReactorDialPeer(t *testing.T) { |
|
|
|
pexR, book := createReactor(&PEXReactorConfig{}) |
|
|
|
defer teardownReactor(book) |
|
|
|
|
|
|
|
_ = createSwitchAndAddReactors(pexR) |
|
|
|
sw := createSwitchAndAddReactors(pexR) |
|
|
|
sw.SetAddrBook(book) |
|
|
|
|
|
|
|
peer := newMockPeer() |
|
|
|
addr := peer.NodeInfo().NetAddress() |
|
|
@ -397,6 +417,7 @@ func assertPeersWithTimeout( |
|
|
|
} |
|
|
|
|
|
|
|
func createReactor(config *PEXReactorConfig) (r *PEXReactor, book *addrBook) { |
|
|
|
// directory to store address book
|
|
|
|
dir, err := ioutil.TempDir("", "pex_reactor") |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|