github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/actioninjector.v2/internal/client/client_test.go (about) 1 package client 2 3 import ( 4 "context" 5 "fmt" 6 "math/big" 7 "testing" 8 "time" 9 10 "github.com/golang/mock/gomock" 11 "github.com/stretchr/testify/require" 12 "google.golang.org/protobuf/proto" 13 "google.golang.org/protobuf/types/known/timestamppb" 14 15 "github.com/iotexproject/go-pkgs/hash" 16 "github.com/iotexproject/iotex-proto/golang/iotextypes" 17 18 "github.com/iotexproject/iotex-core/action" 19 "github.com/iotexproject/iotex-core/action/protocol" 20 "github.com/iotexproject/iotex-core/api" 21 "github.com/iotexproject/iotex-core/blockchain/block" 22 "github.com/iotexproject/iotex-core/blockindex" 23 "github.com/iotexproject/iotex-core/config" 24 "github.com/iotexproject/iotex-core/db" 25 "github.com/iotexproject/iotex-core/state" 26 "github.com/iotexproject/iotex-core/test/identityset" 27 "github.com/iotexproject/iotex-core/test/mock/mock_actpool" 28 "github.com/iotexproject/iotex-core/test/mock/mock_blockchain" 29 "github.com/iotexproject/iotex-core/test/mock/mock_factory" 30 "github.com/iotexproject/iotex-core/testutil" 31 ) 32 33 func TestClient(t *testing.T) { 34 require := require.New(t) 35 a := identityset.Address(28).String() 36 priKeyA := identityset.PrivateKey(28) 37 b := identityset.Address(29).String() 38 39 cfg := config.Default 40 cfg.API.GRPCPort = testutil.RandomPort() 41 cfg.API.HTTPPort = testutil.RandomPort() 42 cfg.API.WebSocketPort = testutil.RandomPort() 43 ctx := context.Background() 44 45 mockCtrl := gomock.NewController(t) 46 defer mockCtrl.Finish() 47 48 tx, err := action.NewTransfer(uint64(1), big.NewInt(10), b, nil, uint64(0), big.NewInt(0)) 49 require.NoError(err) 50 bd := &action.EnvelopeBuilder{} 51 elp := bd.SetNonce(1).SetAction(tx).Build() 52 selp, err := action.Sign(elp, priKeyA) 53 require.NoError(err) 54 55 bc := mock_blockchain.NewMockBlockchain(mockCtrl) 56 sf := mock_factory.NewMockFactory(mockCtrl) 57 ap := mock_actpool.NewMockActPool(mockCtrl) 58 59 sf.EXPECT().State(gomock.Any(), gomock.Any()).Do(func(accountState *state.Account, _ protocol.StateOption) { 60 }) 61 sf.EXPECT().Height().Return(uint64(10), nil).AnyTimes() 62 bc.EXPECT().Genesis().Return(cfg.Genesis).AnyTimes() 63 bc.EXPECT().ChainID().Return(uint32(1)).AnyTimes() 64 bc.EXPECT().EvmNetworkID().Return(uint32(0)).AnyTimes() 65 bc.EXPECT().TipHeight().Return(uint64(4)).AnyTimes() 66 bc.EXPECT().AddSubscriber(gomock.Any()).Return(nil).AnyTimes() 67 bh := &iotextypes.BlockHeader{Core: &iotextypes.BlockHeaderCore{ 68 Version: 1, 69 Height: 10, 70 Timestamp: timestamppb.Now(), 71 PrevBlockHash: []byte(""), 72 TxRoot: []byte(""), 73 DeltaStateDigest: []byte(""), 74 ReceiptRoot: []byte(""), 75 }, ProducerPubkey: identityset.PrivateKey(27).PublicKey().Bytes()} 76 blh := block.Header{} 77 require.NoError(blh.LoadFromBlockHeaderProto(bh)) 78 bc.EXPECT().BlockHeaderByHeight(gomock.Any()).Return(&blh, nil).AnyTimes() 79 ap.EXPECT().GetPendingNonce(gomock.Any()).Return(uint64(1), nil).AnyTimes() 80 ap.EXPECT().Add(gomock.Any(), gomock.Any()).Return(nil).AnyTimes() 81 newOption := api.WithBroadcastOutbound(func(_ context.Context, _ uint32, _ proto.Message) error { 82 return nil 83 }) 84 indexer, err := blockindex.NewIndexer(db.NewMemKVStore(), hash.ZeroHash256) 85 require.NoError(err) 86 bfIndexer, err := blockindex.NewBloomfilterIndexer(db.NewMemKVStore(), cfg.Indexer) 87 require.NoError(err) 88 apiServer, err := api.NewServerV2(cfg.API, bc, nil, sf, nil, indexer, bfIndexer, ap, nil, func(u uint64) (time.Time, error) { return time.Time{}, nil }, newOption) 89 require.NoError(err) 90 require.NoError(apiServer.Start(ctx)) 91 // test New() 92 serverAddr := fmt.Sprintf("127.0.0.1:%d", cfg.API.GRPCPort) 93 cli, err := New(serverAddr, true) 94 require.NoError(err) 95 96 // test GetAccount() 97 response, err := cli.GetAccount(ctx, a) 98 require.NotNil(response) 99 require.NoError(err) 100 101 // test SendAction 102 require.NoError(cli.SendAction(ctx, selp)) 103 }