github.com/aaa256/atlantis@v0.0.0-20210707112435-42ee889287a2/swarm/network/stream/common_test.go (about)

     1  // Copyright 2018 The go-athereum Authors
     2  // This file is part of the go-athereum library.
     3  //
     4  // The go-athereum 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-athereum 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-athereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package stream
    18  
    19  import (
    20  	"context"
    21  	"encoding/binary"
    22  	"errors"
    23  	"flag"
    24  	"fmt"
    25  	"io"
    26  	"io/ioutil"
    27  	"os"
    28  	"sync/atomic"
    29  	"testing"
    30  	"time"
    31  
    32  	"github.com/athereum/go-athereum/common"
    33  	"github.com/athereum/go-athereum/log"
    34  	"github.com/athereum/go-athereum/node"
    35  	"github.com/athereum/go-athereum/p2p"
    36  	"github.com/athereum/go-athereum/p2p/discover"
    37  	"github.com/athereum/go-athereum/p2p/simulations/adapters"
    38  	p2ptest "github.com/athereum/go-athereum/p2p/testing"
    39  	"github.com/athereum/go-athereum/rpc"
    40  	"github.com/athereum/go-athereum/swarm/network"
    41  	"github.com/athereum/go-athereum/swarm/state"
    42  	"github.com/athereum/go-athereum/swarm/storage"
    43  	"github.com/athereum/go-athereum/swarm/storage/mock"
    44  	"github.com/athereum/go-athereum/swarm/storage/mock/db"
    45  	colorable "github.com/mattn/go-colorable"
    46  )
    47  
    48  var (
    49  	deliveries   map[discover.NodeID]*Delivery
    50  	stores       map[discover.NodeID]storage.ChunkStore
    51  	toAddr       func(discover.NodeID) *network.BzzAddr
    52  	peerCount    func(discover.NodeID) int
    53  	adapter      = flag.String("adapter", "sim", "type of simulation: sim|exec|docker")
    54  	loglevel     = flag.Int("loglevel", 2, "verbosity of logs")
    55  	nodes        = flag.Int("nodes", 0, "number of nodes")
    56  	chunks       = flag.Int("chunks", 0, "number of chunks")
    57  	useMockStore = flag.Bool("mockstore", false, "disabled mock store (default: enabled)")
    58  )
    59  
    60  var (
    61  	defaultSkipCheck  bool
    62  	waitPeerErrC      chan error
    63  	chunkSize         = 4096
    64  	registries        map[discover.NodeID]*TestRegistry
    65  	createStoreFunc   func(id discover.NodeID, addr *network.BzzAddr) (storage.ChunkStore, error)
    66  	getRetrieveFunc   = defaultRetrieveFunc
    67  	subscriptionCount = 0
    68  	globalStore       mock.GlobalStorer
    69  	globalStoreDir    string
    70  )
    71  
    72  var services = adapters.Services{
    73  	"streamer":          NewStreamerService,
    74  	"intervalsStreamer": newIntervalsStreamerService,
    75  }
    76  
    77  func init() {
    78  	flag.Parse()
    79  	// register the Delivery service which will run as a devp2p
    80  	// protocol when using the exec adapter
    81  	adapters.RegisterServices(services)
    82  
    83  	log.PrintOrigins(true)
    84  	log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
    85  }
    86  
    87  func createGlobalStore() {
    88  	var err error
    89  	globalStoreDir, err = ioutil.TempDir("", "global.store")
    90  	if err != nil {
    91  		log.Error("Error initiating global store temp directory!", "err", err)
    92  		return
    93  	}
    94  	globalStore, err = db.NewGlobalStore(globalStoreDir)
    95  	if err != nil {
    96  		log.Error("Error initiating global store!", "err", err)
    97  	}
    98  }
    99  
   100  // NewStreamerService
   101  func NewStreamerService(ctx *adapters.ServiceContext) (node.Service, error) {
   102  	var err error
   103  	id := ctx.Config.ID
   104  	addr := toAddr(id)
   105  	kad := network.NewKademlia(addr.Over(), network.NewKadParams())
   106  	stores[id], err = createStoreFunc(id, addr)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  	store := stores[id].(*storage.LocalStore)
   111  	db := storage.NewDBAPI(store)
   112  	delivery := NewDelivery(kad, db)
   113  	deliveries[id] = delivery
   114  	r := NewRegistry(addr, delivery, db, state.NewInmemoryStore(), &RegistryOptions{
   115  		SkipCheck:  defaultSkipCheck,
   116  		DoRetrieve: false,
   117  	})
   118  	RegisterSwarmSyncerServer(r, db)
   119  	RegisterSwarmSyncerClient(r, db)
   120  	go func() {
   121  		waitPeerErrC <- waitForPeers(r, 1*time.Second, peerCount(id))
   122  	}()
   123  	fileStore := storage.NewFileStore(storage.NewNetStore(store, getRetrieveFunc(id)), storage.NewFileStoreParams())
   124  	testRegistry := &TestRegistry{Registry: r, fileStore: fileStore}
   125  	registries[id] = testRegistry
   126  	return testRegistry, nil
   127  }
   128  
   129  func defaultRetrieveFunc(id discover.NodeID) func(chunk *storage.Chunk) error {
   130  	return nil
   131  }
   132  
   133  func datadirsCleanup() {
   134  	for _, id := range ids {
   135  		os.RemoveAll(datadirs[id])
   136  	}
   137  	if globalStoreDir != "" {
   138  		os.RemoveAll(globalStoreDir)
   139  	}
   140  }
   141  
   142  //local stores need to be cleaned up after the sim is done
   143  func localStoreCleanup() {
   144  	log.Info("Cleaning up...")
   145  	for _, id := range ids {
   146  		registries[id].Close()
   147  		stores[id].Close()
   148  	}
   149  	log.Info("Local store cleanup done")
   150  }
   151  
   152  func newStreamerTester(t *testing.T) (*p2ptest.ProtocolTester, *Registry, *storage.LocalStore, func(), error) {
   153  	// setup
   154  	addr := network.RandomAddr() // tested peers peer address
   155  	to := network.NewKademlia(addr.OAddr, network.NewKadParams())
   156  
   157  	// temp datadir
   158  	datadir, err := ioutil.TempDir("", "streamer")
   159  	if err != nil {
   160  		return nil, nil, nil, func() {}, err
   161  	}
   162  	removeDataDir := func() {
   163  		os.RemoveAll(datadir)
   164  	}
   165  
   166  	params := storage.NewDefaultLocalStoreParams()
   167  	params.Init(datadir)
   168  	params.BaseKey = addr.Over()
   169  
   170  	localStore, err := storage.NewTestLocalStoreForAddr(params)
   171  	if err != nil {
   172  		return nil, nil, nil, removeDataDir, err
   173  	}
   174  
   175  	db := storage.NewDBAPI(localStore)
   176  	delivery := NewDelivery(to, db)
   177  	streamer := NewRegistry(addr, delivery, db, state.NewInmemoryStore(), &RegistryOptions{
   178  		SkipCheck: defaultSkipCheck,
   179  	})
   180  	teardown := func() {
   181  		streamer.Close()
   182  		removeDataDir()
   183  	}
   184  	protocolTester := p2ptest.NewProtocolTester(t, network.NewNodeIDFromAddr(addr), 1, streamer.runProtocol)
   185  
   186  	err = waitForPeers(streamer, 1*time.Second, 1)
   187  	if err != nil {
   188  		return nil, nil, nil, nil, errors.New("timeout: peer is not created")
   189  	}
   190  
   191  	return protocolTester, streamer, localStore, teardown, nil
   192  }
   193  
   194  func waitForPeers(streamer *Registry, timeout time.Duration, expectedPeers int) error {
   195  	ticker := time.NewTicker(10 * time.Millisecond)
   196  	timeoutTimer := time.NewTimer(timeout)
   197  	for {
   198  		select {
   199  		case <-ticker.C:
   200  			if streamer.peersCount() >= expectedPeers {
   201  				return nil
   202  			}
   203  		case <-timeoutTimer.C:
   204  			return errors.New("timeout")
   205  		}
   206  	}
   207  }
   208  
   209  type roundRobinStore struct {
   210  	index  uint32
   211  	stores []storage.ChunkStore
   212  }
   213  
   214  func newRoundRobinStore(stores ...storage.ChunkStore) *roundRobinStore {
   215  	return &roundRobinStore{
   216  		stores: stores,
   217  	}
   218  }
   219  
   220  func (rrs *roundRobinStore) Get(addr storage.Address) (*storage.Chunk, error) {
   221  	return nil, errors.New("get not well defined on round robin store")
   222  }
   223  
   224  func (rrs *roundRobinStore) Put(chunk *storage.Chunk) {
   225  	i := atomic.AddUint32(&rrs.index, 1)
   226  	idx := int(i) % len(rrs.stores)
   227  	rrs.stores[idx].Put(chunk)
   228  }
   229  
   230  func (rrs *roundRobinStore) Close() {
   231  	for _, store := range rrs.stores {
   232  		store.Close()
   233  	}
   234  }
   235  
   236  type TestRegistry struct {
   237  	*Registry
   238  	fileStore *storage.FileStore
   239  }
   240  
   241  func (r *TestRegistry) APIs() []rpc.API {
   242  	a := r.Registry.APIs()
   243  	a = append(a, rpc.API{
   244  		Namespace: "stream",
   245  		Version:   "3.0",
   246  		Service:   r,
   247  		Public:    true,
   248  	})
   249  	return a
   250  }
   251  
   252  func readAll(fileStore *storage.FileStore, hash []byte) (int64, error) {
   253  	r, _ := fileStore.Retrieve(hash)
   254  	buf := make([]byte, 1024)
   255  	var n int
   256  	var total int64
   257  	var err error
   258  	for (total == 0 || n > 0) && err == nil {
   259  		n, err = r.ReadAt(buf, total)
   260  		total += int64(n)
   261  	}
   262  	if err != nil && err != io.EOF {
   263  		return total, err
   264  	}
   265  	return total, nil
   266  }
   267  
   268  func (r *TestRegistry) ReadAll(hash common.Hash) (int64, error) {
   269  	return readAll(r.fileStore, hash[:])
   270  }
   271  
   272  func (r *TestRegistry) Start(server *p2p.Server) error {
   273  	return r.Registry.Start(server)
   274  }
   275  
   276  func (r *TestRegistry) Stop() error {
   277  	return r.Registry.Stop()
   278  }
   279  
   280  type TestExternalRegistry struct {
   281  	*Registry
   282  }
   283  
   284  func (r *TestExternalRegistry) APIs() []rpc.API {
   285  	a := r.Registry.APIs()
   286  	a = append(a, rpc.API{
   287  		Namespace: "stream",
   288  		Version:   "3.0",
   289  		Service:   r,
   290  		Public:    true,
   291  	})
   292  	return a
   293  }
   294  
   295  func (r *TestExternalRegistry) GetHashes(ctx context.Context, peerId discover.NodeID, s Stream) (*rpc.Subscription, error) {
   296  	peer := r.getPeer(peerId)
   297  
   298  	client, err := peer.getClient(ctx, s)
   299  	if err != nil {
   300  		return nil, err
   301  	}
   302  
   303  	c := client.Client.(*testExternalClient)
   304  
   305  	notifier, supported := rpc.NotifierFromContext(ctx)
   306  	if !supported {
   307  		return nil, fmt.Errorf("Subscribe not supported")
   308  	}
   309  
   310  	sub := notifier.CreateSubscription()
   311  
   312  	go func() {
   313  		// if we begin sending event immediately some events
   314  		// will probably be dropped since the subscription ID might not be send to
   315  		// the client.
   316  		// ref: rpc/subscription_test.go#L65
   317  		time.Sleep(1 * time.Second)
   318  		for {
   319  			select {
   320  			case h := <-c.hashes:
   321  				<-c.enableNotificationsC // wait for notification subscription to complete
   322  				if err := notifier.Notify(sub.ID, h); err != nil {
   323  					log.Warn(fmt.Sprintf("rpc sub notifier notify stream %s: %v", s, err))
   324  				}
   325  			case err := <-sub.Err():
   326  				if err != nil {
   327  					log.Warn(fmt.Sprintf("caught subscription error in stream %s: %v", s, err))
   328  				}
   329  			case <-notifier.Closed():
   330  				log.Trace(fmt.Sprintf("rpc sub notifier closed"))
   331  				return
   332  			}
   333  		}
   334  	}()
   335  
   336  	return sub, nil
   337  }
   338  
   339  func (r *TestExternalRegistry) EnableNotifications(peerId discover.NodeID, s Stream) error {
   340  	peer := r.getPeer(peerId)
   341  
   342  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   343  	defer cancel()
   344  
   345  	client, err := peer.getClient(ctx, s)
   346  	if err != nil {
   347  		return err
   348  	}
   349  
   350  	close(client.Client.(*testExternalClient).enableNotificationsC)
   351  
   352  	return nil
   353  }
   354  
   355  // TODO: merge functionalities of testExternalClient and testExternalServer
   356  // with testClient and testServer.
   357  
   358  type testExternalClient struct {
   359  	hashes               chan []byte
   360  	db                   *storage.DBAPI
   361  	enableNotificationsC chan struct{}
   362  }
   363  
   364  func newTestExternalClient(db *storage.DBAPI) *testExternalClient {
   365  	return &testExternalClient{
   366  		hashes:               make(chan []byte),
   367  		db:                   db,
   368  		enableNotificationsC: make(chan struct{}),
   369  	}
   370  }
   371  
   372  func (c *testExternalClient) NeedData(hash []byte) func() {
   373  	chunk, _ := c.db.GetOrCreateRequest(hash)
   374  	if chunk.ReqC == nil {
   375  		return nil
   376  	}
   377  	c.hashes <- hash
   378  	return func() {
   379  		chunk.WaitToStore()
   380  	}
   381  }
   382  
   383  func (c *testExternalClient) BatchDone(Stream, uint64, []byte, []byte) func() (*TakeoverProof, error) {
   384  	return nil
   385  }
   386  
   387  func (c *testExternalClient) Close() {}
   388  
   389  const testExternalServerBatchSize = 10
   390  
   391  type testExternalServer struct {
   392  	t         string
   393  	keyFunc   func(key []byte, index uint64)
   394  	sessionAt uint64
   395  	maxKeys   uint64
   396  	streamer  *TestExternalRegistry
   397  }
   398  
   399  func newTestExternalServer(t string, sessionAt, maxKeys uint64, keyFunc func(key []byte, index uint64)) *testExternalServer {
   400  	if keyFunc == nil {
   401  		keyFunc = binary.BigEndian.PutUint64
   402  	}
   403  	return &testExternalServer{
   404  		t:         t,
   405  		keyFunc:   keyFunc,
   406  		sessionAt: sessionAt,
   407  		maxKeys:   maxKeys,
   408  	}
   409  }
   410  
   411  func (s *testExternalServer) SetNextBatch(from uint64, to uint64) ([]byte, uint64, uint64, *HandoverProof, error) {
   412  	if from == 0 && to == 0 {
   413  		from = s.sessionAt
   414  		to = s.sessionAt + testExternalServerBatchSize
   415  	}
   416  	if to-from > testExternalServerBatchSize {
   417  		to = from + testExternalServerBatchSize - 1
   418  	}
   419  	if from >= s.maxKeys && to > s.maxKeys {
   420  		return nil, 0, 0, nil, io.EOF
   421  	}
   422  	if to > s.maxKeys {
   423  		to = s.maxKeys
   424  	}
   425  	b := make([]byte, HashSize*(to-from+1))
   426  	for i := from; i <= to; i++ {
   427  		s.keyFunc(b[(i-from)*HashSize:(i-from+1)*HashSize], i)
   428  	}
   429  	return b, from, to, nil, nil
   430  }
   431  
   432  func (s *testExternalServer) GetData([]byte) ([]byte, error) {
   433  	return make([]byte, 4096), nil
   434  }
   435  
   436  func (s *testExternalServer) Close() {}
   437  
   438  // Sets the global value defaultSkipCheck.
   439  // It should be used in test function defer to reset the global value
   440  // to the original value.
   441  //
   442  // defer setDefaultSkipCheck(defaultSkipCheck)
   443  // defaultSkipCheck = skipCheck
   444  //
   445  // This works as defer function arguments evaluations are evaluated as ususal,
   446  // but only the function body invocation is deferred.
   447  func setDefaultSkipCheck(skipCheck bool) {
   448  	defaultSkipCheck = skipCheck
   449  }