|
| 1 | +package state |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 8 | + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" |
| 9 | + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "github.com/stretchr/testify/suite" |
| 12 | + tmrand "github.com/tendermint/tendermint/libs/rand" |
| 13 | + rpcclient "github.com/tendermint/tendermint/rpc/client" |
| 14 | + "google.golang.org/grpc" |
| 15 | + |
| 16 | + "github.com/celestiaorg/celestia-app/app" |
| 17 | + "github.com/celestiaorg/celestia-app/testutil/testnode" |
| 18 | + blobtypes "github.com/celestiaorg/celestia-app/x/payment/types" |
| 19 | + |
| 20 | + "github.com/celestiaorg/celestia-node/header" |
| 21 | +) |
| 22 | + |
| 23 | +func TestIntegrationTestSuite(t *testing.T) { |
| 24 | + suite.Run(t, new(IntegrationTestSuite)) |
| 25 | +} |
| 26 | + |
| 27 | +type IntegrationTestSuite struct { |
| 28 | + suite.Suite |
| 29 | + |
| 30 | + cleanups []func() error |
| 31 | + accounts []string |
| 32 | + cctx testnode.Context |
| 33 | + |
| 34 | + accessor *CoreAccessor |
| 35 | +} |
| 36 | + |
| 37 | +func (s *IntegrationTestSuite) SetupSuite() { |
| 38 | + if testing.Short() { |
| 39 | + s.T().Skip("skipping test in unit-tests or race-detector mode.") |
| 40 | + } |
| 41 | + |
| 42 | + s.T().Log("setting up integration test suite") |
| 43 | + require := s.Require() |
| 44 | + |
| 45 | + // we create an arbitrary number of funded accounts |
| 46 | + for i := 0; i < 25; i++ { |
| 47 | + s.accounts = append(s.accounts, tmrand.Str(9)) |
| 48 | + } |
| 49 | + |
| 50 | + tmNode, app, cctx, err := testnode.New( |
| 51 | + s.T(), |
| 52 | + testnode.DefaultParams(), |
| 53 | + testnode.DefaultTendermintConfig(), |
| 54 | + false, |
| 55 | + s.accounts..., |
| 56 | + ) |
| 57 | + require.NoError(err) |
| 58 | + |
| 59 | + cctx, stopNode, err := testnode.StartNode(tmNode, cctx) |
| 60 | + require.NoError(err) |
| 61 | + s.cleanups = append(s.cleanups, stopNode) |
| 62 | + |
| 63 | + cctx, cleanupGRPC, err := testnode.StartGRPCServer(app, testnode.DefaultAppConfig(), cctx) |
| 64 | + require.NoError(err) |
| 65 | + s.cleanups = append(s.cleanups, cleanupGRPC) |
| 66 | + |
| 67 | + s.cctx = cctx |
| 68 | + require.NoError(cctx.WaitForNextBlock()) |
| 69 | + |
| 70 | + signer := blobtypes.NewKeyringSigner(s.cctx.Keyring, s.accounts[0], cctx.ChainID) |
| 71 | + |
| 72 | + accessor := NewCoreAccessor(signer, localHeader{s.cctx.Client}, "", "", "") |
| 73 | + setClients(accessor, s.cctx.GRPCClient, s.cctx.Client) |
| 74 | + s.accessor = accessor |
| 75 | +} |
| 76 | + |
| 77 | +func setClients(ca *CoreAccessor, conn *grpc.ClientConn, abciCli rpcclient.ABCIClient) { |
| 78 | + ca.coreConn = conn |
| 79 | + // create the query client |
| 80 | + queryCli := banktypes.NewQueryClient(ca.coreConn) |
| 81 | + ca.queryCli = queryCli |
| 82 | + // create the staking query client |
| 83 | + stakingCli := stakingtypes.NewQueryClient(ca.coreConn) |
| 84 | + ca.stakingCli = stakingCli |
| 85 | + |
| 86 | + ca.rpcCli = abciCli |
| 87 | +} |
| 88 | + |
| 89 | +func (s *IntegrationTestSuite) TearDownSuite() { |
| 90 | + s.T().Log("tearing down integration test suite") |
| 91 | + require := s.Require() |
| 92 | + require.NoError(s.accessor.Stop(s.cctx.GoContext())) |
| 93 | + for _, c := range s.cleanups { |
| 94 | + err := c() |
| 95 | + require.NoError(err) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func (s *IntegrationTestSuite) getAddress(acc string) sdk.Address { |
| 100 | + rec, err := s.cctx.Keyring.Key(acc) |
| 101 | + require.NoError(s.T(), err) |
| 102 | + |
| 103 | + addr, err := rec.GetAddress() |
| 104 | + require.NoError(s.T(), err) |
| 105 | + |
| 106 | + return addr |
| 107 | +} |
| 108 | + |
| 109 | +type localHeader struct { |
| 110 | + client rpcclient.Client |
| 111 | +} |
| 112 | + |
| 113 | +func (l localHeader) Head(ctx context.Context) (*header.ExtendedHeader, error) { |
| 114 | + latest, err := l.client.Block(ctx, nil) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + h := &header.ExtendedHeader{ |
| 119 | + RawHeader: latest.Block.Header, |
| 120 | + } |
| 121 | + return h, nil |
| 122 | +} |
| 123 | + |
| 124 | +func (s *IntegrationTestSuite) TestGetBalance() { |
| 125 | + require := s.Require() |
| 126 | + expectedBal := sdk.NewCoin(app.BondDenom, sdk.NewInt(int64(99999999999999999))) |
| 127 | + for _, acc := range s.accounts { |
| 128 | + bal, err := s.accessor.BalanceForAddress(context.Background(), s.getAddress(acc)) |
| 129 | + require.NoError(err) |
| 130 | + require.Equal(&expectedBal, bal) |
| 131 | + } |
| 132 | +} |
0 commit comments