From fb3db9a74acf22904fce0c3399d52bda2e5e1d1d Mon Sep 17 00:00:00 2001 From: Kacper Sawicki Date: Thu, 16 May 2024 13:49:03 +0200 Subject: [PATCH] Add ParseTransaction test --- api/grpcserver/v2alpha1/transaction_test.go | 86 ++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/api/grpcserver/v2alpha1/transaction_test.go b/api/grpcserver/v2alpha1/transaction_test.go index 0ce10a75ea..10ba4d2e5b 100644 --- a/api/grpcserver/v2alpha1/transaction_test.go +++ b/api/grpcserver/v2alpha1/transaction_test.go @@ -43,8 +43,6 @@ func TestTransactionService_EstimateGas(t *testing.T) { _, _, err := vminst.Apply(vm.ApplyContext{Layer: types.GetEffectiveGenesis().Add(1)}, []types.Transaction{{RawTx: types.NewRawTx(wallet.SelfSpawn(keys[0], 0))}}, nil) require.NoError(t, err) - mangled := wallet.Spend(keys[0], accounts[3].Address, 100, 0) - mangled[len(mangled)-1] -= 1 conn := dialGrpc(ctx, t, cfg) client := spacemeshv2alpha1.NewTransactionServiceClient(conn) @@ -84,3 +82,87 @@ func TestTransactionService_EstimateGas(t *testing.T) { assert.Contains(t, s.Message(), "not spawned") }) } + +func TestTransactionService_ParseTransaction(t *testing.T) { + types.SetLayersPerEpoch(5) + db := sql.InMemory() + vminst := vm.New(db) + ctx := context.Background() + + svc := NewTransactionService(db, txs.NewConservativeState(vminst, db), nil, nil, nil) + cfg, cleanup := launchServer(t, svc) + t.Cleanup(cleanup) + + keys := make([]signing.PrivateKey, 4) + accounts := make([]types.Account, len(keys)) + rng := rand.New(rand.NewSource(10101)) + for i := range keys { + pub, priv, err := ed25519.GenerateKey(rng) + require.NoError(t, err) + keys[i] = priv + accounts[i] = types.Account{Address: wallet.Address(pub), Balance: 1e12} + } + require.NoError(t, vminst.ApplyGenesis(accounts)) + _, _, err := vminst.Apply(vm.ApplyContext{Layer: types.GetEffectiveGenesis().Add(1)}, + []types.Transaction{{RawTx: types.NewRawTx(wallet.SelfSpawn(keys[0], 0))}}, nil) + require.NoError(t, err) + + mangled := wallet.Spend(keys[0], accounts[3].Address, 100, 0) + mangled[len(mangled)-1] -= 1 + + conn := dialGrpc(ctx, t, cfg) + client := spacemeshv2alpha1.NewTransactionServiceClient(conn) + + t.Run("valid tx", func(t *testing.T) { + resp, err := client.ParseTransaction(ctx, &spacemeshv2alpha1.ParseTransactionRequest{ + Transaction: wallet.Spend(keys[0], accounts[3].Address, 100, 0), + }) + require.NoError(t, err) + require.NotEmpty(t, resp) + }) + t.Run("valid tx with verify set to true", func(t *testing.T) { + resp, err := client.ParseTransaction(ctx, &spacemeshv2alpha1.ParseTransactionRequest{ + Transaction: wallet.Spend(keys[0], accounts[3].Address, 100, 0), + Verify: true, + }) + require.NoError(t, err) + require.NotEmpty(t, resp) + }) + t.Run("malformed tx", func(t *testing.T) { + _, err := client.ParseTransaction(ctx, &spacemeshv2alpha1.ParseTransactionRequest{ + Transaction: []byte("malformed"), + }) + s, ok := status.FromError(err) + require.True(t, ok) + assert.Equal(t, codes.InvalidArgument, s.Code()) + assert.Contains(t, s.Message(), "malformed tx") + }) + t.Run("empty", func(t *testing.T) { + _, err := client.ParseTransaction(ctx, &spacemeshv2alpha1.ParseTransactionRequest{ + Transaction: nil, + }) + s, ok := status.FromError(err) + require.True(t, ok) + assert.Equal(t, codes.InvalidArgument, s.Code()) + assert.Contains(t, s.Message(), "empty") + }) + t.Run("not spawned", func(t *testing.T) { + _, err := client.ParseTransaction(ctx, &spacemeshv2alpha1.ParseTransactionRequest{ + Transaction: wallet.Spend(keys[2], accounts[3].Address, 100, 0), + }) + s, ok := status.FromError(err) + require.True(t, ok) + assert.Equal(t, codes.NotFound, s.Code()) + assert.Contains(t, s.Message(), "not spawned") + }) + t.Run("mangled signature", func(t *testing.T) { + _, err := client.ParseTransaction(ctx, &spacemeshv2alpha1.ParseTransactionRequest{ + Transaction: mangled, + Verify: true, + }) + s, ok := status.FromError(err) + require.True(t, ok) + assert.Equal(t, codes.InvalidArgument, s.Code()) + assert.Contains(t, s.Message(), "signature is invalid") + }) +}