github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/swarm/network/hive_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package network
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/ethereum/go-ethereum/crypto"
    26  	p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
    27  	"github.com/ethereum/go-ethereum/swarm/state"
    28  )
    29  
    30  func newHiveTester(t *testing.T, params *HiveParams, n int, store state.Store) (*bzzTester, *Hive, error) {
    31  	// setup
    32  	prvkey, err := crypto.GenerateKey()
    33  	if err != nil {
    34  		return nil, nil, err
    35  	}
    36  	addr := PrivateKeyToBzzKey(prvkey)
    37  	to := NewKademlia(addr, NewKadParams())
    38  	pp := NewHive(params, to, store) // hive
    39  
    40  	bt, err := newBzzBaseTester(t, n, prvkey, DiscoverySpec, pp.Run)
    41  	if err != nil {
    42  		return nil, nil, err
    43  	}
    44  	return bt, pp, nil
    45  }
    46  
    47  // TestRegisterAndConnect verifies that the protocol runs successfully
    48  // and that the peer connection exists afterwards
    49  func TestRegisterAndConnect(t *testing.T) {
    50  	params := NewHiveParams()
    51  	s, pp, err := newHiveTester(t, params, 1, nil)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	node := s.Nodes[0]
    57  	raddr := NewAddr(node)
    58  	pp.Register(raddr)
    59  
    60  	// start the hive
    61  	err = pp.Start(s.Server)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	defer pp.Stop()
    66  
    67  	// both hive connect and disconect check have time delays
    68  	// therefore we need to verify that peer is connected
    69  	// so that we are sure that the disconnect timeout doesn't complete
    70  	// before the hive connect method is run at least once
    71  	timeout := time.After(time.Second)
    72  	for {
    73  		select {
    74  		case <-timeout:
    75  			t.Fatalf("expected connection")
    76  		default:
    77  		}
    78  		i := 0
    79  		pp.Kademlia.EachConn(nil, 256, func(addr *Peer, po int) bool {
    80  			i++
    81  			return true
    82  		})
    83  		if i > 0 {
    84  			break
    85  		}
    86  		time.Sleep(time.Millisecond)
    87  	}
    88  
    89  	// check that the connection actually exists
    90  	// the timeout error means no disconnection events
    91  	// were received within the a certain timeout
    92  	err = s.TestDisconnected(&p2ptest.Disconnect{
    93  		Peer:  s.Nodes[0].ID(),
    94  		Error: nil,
    95  	})
    96  
    97  	if err == nil || err.Error() != "timed out waiting for peers to disconnect" {
    98  		t.Fatalf("expected no disconnection event")
    99  	}
   100  }
   101  
   102  // TestHiveStatePersistance creates a protocol simulation with n peers for a node
   103  // After protocols complete, the node is shut down and the state is stored.
   104  // Another simulation is created, where 0 nodes are created, but where the stored state is passed
   105  // The test succeeds if all the peers from the stored state are known after the protocols of the
   106  // second simulation have completed
   107  //
   108  // Actual connectivity is not in scope for this test, as the peers loaded from state are not known to
   109  // the simulation; the test only verifies that the peers are known to the node
   110  func TestHiveStatePersistance(t *testing.T) {
   111  
   112  	dir, err := ioutil.TempDir("", "hive_test_store")
   113  	if err != nil {
   114  		panic(err)
   115  	}
   116  	defer os.RemoveAll(dir)
   117  
   118  	store, err := state.NewDBStore(dir) //start the hive with an empty dbstore
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	params := NewHiveParams()
   124  	s, pp, err := newHiveTester(t, params, 5, store)
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  	peers := make(map[string]bool)
   129  	for _, node := range s.Nodes {
   130  		raddr := NewAddr(node)
   131  		pp.Register(raddr)
   132  		peers[raddr.String()] = true
   133  	}
   134  
   135  	// start and stop the hive
   136  	// the known peers should be saved upon stopping
   137  	err = pp.Start(s.Server)
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  	pp.Stop()
   142  	store.Close()
   143  
   144  	// start the hive with an empty dbstore
   145  	persistedStore, err := state.NewDBStore(dir)
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  
   150  	s1, pp, err := newHiveTester(t, params, 0, persistedStore)
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  
   155  	// start the hive and check that we know of all expected peers
   156  	pp.Start(s1.Server)
   157  	i := 0
   158  	pp.Kademlia.EachAddr(nil, 256, func(addr *BzzAddr, po int) bool {
   159  		delete(peers, addr.String())
   160  		i++
   161  		return true
   162  	})
   163  	// TODO remove this line when verified that test passes
   164  	time.Sleep(time.Second)
   165  	if i != 5 {
   166  		t.Fatalf("invalid number of entries: got %v, want %v", i, 5)
   167  	}
   168  	if len(peers) != 0 {
   169  		t.Fatalf("%d peers left over: %v", len(peers), peers)
   170  	}
   171  
   172  }