github.com/iotexproject/iotex-core@v1.14.1-rc1/e2etest/nodeinfo_test.go (about)

     1  // Copyright (c) 2023 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package e2etest
     7  
     8  import (
     9  	"context"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/iotexproject/iotex-core/blockchain/genesis"
    16  	"github.com/iotexproject/iotex-core/config"
    17  	"github.com/iotexproject/iotex-core/server/itx"
    18  	"github.com/iotexproject/iotex-core/testutil"
    19  )
    20  
    21  func newConfigForNodeInfoTest(triePath, dBPath, idxDBPath, contractIdxDBPath string) (config.Config, func(), error) {
    22  	cfg, err := newTestConfig()
    23  	if err != nil {
    24  		return cfg, nil, err
    25  	}
    26  	cfg.Genesis.PalauBlockHeight = 0
    27  	testTriePath, err := testutil.PathOfTempFile(triePath)
    28  	if err != nil {
    29  		return cfg, nil, err
    30  	}
    31  	testDBPath, err := testutil.PathOfTempFile(dBPath)
    32  	if err != nil {
    33  		return cfg, nil, err
    34  	}
    35  	indexDBPath, err := testutil.PathOfTempFile(idxDBPath)
    36  	if err != nil {
    37  		return cfg, nil, err
    38  	}
    39  	contractIndexDBPath, err := testutil.PathOfTempFile(contractIdxDBPath)
    40  	if err != nil {
    41  		return cfg, nil, err
    42  	}
    43  	sgdIndexDBPath, err := testutil.PathOfTempFile(idxDBPath)
    44  	if err != nil {
    45  		return cfg, nil, err
    46  	}
    47  	cfg.Chain.TrieDBPatchFile = ""
    48  	cfg.Chain.TrieDBPath = testTriePath
    49  	cfg.Chain.ChainDBPath = testDBPath
    50  	cfg.Chain.IndexDBPath = indexDBPath
    51  	cfg.Chain.ContractStakingIndexDBPath = contractIndexDBPath
    52  	cfg.Chain.SGDIndexDBPath = sgdIndexDBPath
    53  	return cfg, func() {
    54  		testutil.CleanupPath(testTriePath)
    55  		testutil.CleanupPath(testDBPath)
    56  		testutil.CleanupPath(indexDBPath)
    57  		testutil.CleanupPath(contractIndexDBPath)
    58  	}, nil
    59  }
    60  
    61  func TestBroadcastNodeInfo(t *testing.T) {
    62  	require := require.New(t)
    63  
    64  	cfgSender, teardown, err := newConfigForNodeInfoTest("trie.test", "db.test", "indexdb.test", "contractidxdb.test")
    65  	require.NoError(err)
    66  	defer teardown()
    67  	cfgSender.NodeInfo.EnableBroadcastNodeInfo = true
    68  	cfgSender.NodeInfo.BroadcastNodeInfoInterval = time.Second
    69  	cfgSender.Network.ReconnectInterval = 2 * time.Second
    70  	srvSender, err := itx.NewServer(cfgSender)
    71  	require.NoError(err)
    72  	ctxSender := genesis.WithGenesisContext(context.Background(), cfgSender.Genesis)
    73  	err = srvSender.Start(ctxSender)
    74  	require.NoError(err)
    75  	defer func() {
    76  		require.NoError(srvSender.Stop(ctxSender))
    77  	}()
    78  	addrsSender, err := srvSender.P2PAgent().Self()
    79  	require.NoError(err)
    80  
    81  	cfgReciever, teardown2, err := newConfigForNodeInfoTest("trie2.test", "db2.test", "indexdb2.test", "contractidxdb2.test")
    82  	require.NoError(err)
    83  	defer teardown2()
    84  	cfgReciever.Network.BootstrapNodes = []string{validNetworkAddr(addrsSender)}
    85  	cfgReciever.Network.ReconnectInterval = 2 * time.Second
    86  	srvReciever, err := itx.NewServer(cfgReciever)
    87  	require.NoError(err)
    88  	ctxReciever := genesis.WithGenesisContext(context.Background(), cfgReciever.Genesis)
    89  	err = srvReciever.Start(ctxReciever)
    90  	require.NoError(err)
    91  	defer func() {
    92  		require.NoError(srvReciever.Stop(ctxReciever))
    93  	}()
    94  
    95  	// check if there is sender's info in reciever delegatemanager
    96  	require.NoError(srvSender.ChainService(cfgSender.Chain.ID).NodeInfoManager().BroadcastNodeInfo(context.Background()))
    97  	addrSender := cfgSender.Chain.ProducerAddress().String()
    98  	require.NoError(testutil.WaitUntil(100*time.Millisecond, 10*time.Second, func() (bool, error) {
    99  		_, ok := srvReciever.ChainService(cfgReciever.Chain.ID).NodeInfoManager().GetNodeInfo(addrSender)
   100  		return ok, nil
   101  	}))
   102  }
   103  
   104  func TestUnicastNodeInfo(t *testing.T) {
   105  	require := require.New(t)
   106  
   107  	cfgReciever, teardown2, err := newConfigForNodeInfoTest("trie2.test", "db2.test", "indexdb2.test", "contractidxdb2.test")
   108  	require.NoError(err)
   109  	defer teardown2()
   110  	cfgReciever.Network.ReconnectInterval = 2 * time.Second
   111  	srvReciever, err := itx.NewServer(cfgReciever)
   112  	require.NoError(err)
   113  	ctxReciever := genesis.WithGenesisContext(context.Background(), cfgReciever.Genesis)
   114  	err = srvReciever.Start(ctxReciever)
   115  	require.NoError(err)
   116  	defer func() {
   117  		require.NoError(srvReciever.Stop(ctxReciever))
   118  	}()
   119  	addrsReciever, err := srvReciever.P2PAgent().Self()
   120  	require.NoError(err)
   121  
   122  	cfgSender, teardown, err := newConfigForNodeInfoTest("trie.test", "db.test", "indexdb.test", "contractidxdb.test")
   123  	require.NoError(err)
   124  	defer teardown()
   125  	cfgSender.Network.ReconnectInterval = 2 * time.Second
   126  	cfgSender.Network.BootstrapNodes = []string{validNetworkAddr(addrsReciever)}
   127  	srvSender, err := itx.NewServer(cfgSender)
   128  	require.NoError(err)
   129  	ctxSender := genesis.WithGenesisContext(context.Background(), cfgSender.Genesis)
   130  	err = srvSender.Start(ctxSender)
   131  	require.NoError(err)
   132  	defer func() {
   133  		require.NoError(srvSender.Stop(ctxSender))
   134  	}()
   135  
   136  	// check if there is reciever's info in sender delegatemanager
   137  	peerReciever, err := srvReciever.P2PAgent().Info()
   138  	require.NoError(err)
   139  	dmSender := srvSender.ChainService(cfgSender.Chain.ID).NodeInfoManager()
   140  	err = dmSender.RequestSingleNodeInfoAsync(context.Background(), peerReciever)
   141  	require.NoError(err)
   142  	addrReciever := cfgReciever.Chain.ProducerAddress().String()
   143  	require.NoError(testutil.WaitUntil(100*time.Millisecond, 10*time.Second, func() (bool, error) {
   144  		_, ok := dmSender.GetNodeInfo(addrReciever)
   145  		return ok, nil
   146  	}))
   147  }