From 473b33153f9fa4111816a8f2cd0f24d2f6ccadbf Mon Sep 17 00:00:00 2001 From: Dzianis Andreyenka Date: Mon, 2 Oct 2023 14:29:33 +0200 Subject: [PATCH 1/2] DA-505 Increase unit test coverage Signed-off-by: Dzianis Andreyenka --- internal/tezos/prepare_transaction.go | 6 +- internal/tezos/prepare_transaction_test.go | 135 +++++++++++++++++++++ internal/tezos/tezos_test.go | 4 + 3 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 internal/tezos/prepare_transaction_test.go diff --git a/internal/tezos/prepare_transaction.go b/internal/tezos/prepare_transaction.go index 164d7a0..2c2b3d3 100644 --- a/internal/tezos/prepare_transaction.go +++ b/internal/tezos/prepare_transaction.go @@ -29,7 +29,7 @@ func (c *tezosConnector) TransactionPrepare(ctx context.Context, req *ffcapi.Tra op, err := c.buildOp(ctx, params, req.From, req.To, req.Nonce) if err != nil { - return nil, ffcapi.ErrorReasonInvalidInputs, err + return nil, "", err } return &ffcapi.TransactionPrepareResponse{ @@ -116,10 +116,6 @@ func (c *tezosConnector) completeOp(ctx context.Context, op *codec.Op, fromStrin nextCounter = state.Counter + 1 } for _, op := range op.Contents { - // skip non-manager ops - if op.GetCounter() < 0 { - continue - } op.WithCounter(nextCounter) nextCounter++ } diff --git a/internal/tezos/prepare_transaction_test.go b/internal/tezos/prepare_transaction_test.go new file mode 100644 index 0000000..314ff5b --- /dev/null +++ b/internal/tezos/prepare_transaction_test.go @@ -0,0 +1,135 @@ +package tezos + +import ( + "errors" + "testing" + + "blockwatch.cc/tzgo/rpc" + "blockwatch.cc/tzgo/tezos" + "github.com/hyperledger/firefly-common/pkg/fftypes" + "github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" +) + +func TestTransactionPrepareOk(t *testing.T) { + ctx, c, mRPC, done := newTestConnector(t) + defer done() + + mRPC.On("GetBlockHash", ctx, mock.Anything). + Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil) + + mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything). + Return(&rpc.ContractInfo{ + Counter: 10, + Manager: "edpkv89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6", + }, nil) + + req := &ffcapi.TransactionPrepareRequest{ + TransactionInput: ffcapi.TransactionInput{ + TransactionHeaders: ffcapi.TransactionHeaders{ + From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN", + To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s", + }, + Method: fftypes.JSONAnyPtr("\"pause\""), + Params: []*fftypes.JSONAny{ + fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"), + }, + }, + } + res, reason, err := c.TransactionPrepare(ctx, req) + assert.NoError(t, err) + assert.Empty(t, reason) + assert.NotNil(t, res) + assert.NotEmpty(t, res.TransactionData) +} + +func TestTransactionPrepareWrongParamsError(t *testing.T) { + ctx, c, _, done := newTestConnector(t) + defer done() + + req := &ffcapi.TransactionPrepareRequest{ + TransactionInput: ffcapi.TransactionInput{ + TransactionHeaders: ffcapi.TransactionHeaders{ + From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN", + To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s", + }, + Method: fftypes.JSONAnyPtr("\"pause\""), + Params: []*fftypes.JSONAny{ + fftypes.JSONAnyPtr("wrong"), + }, + }, + } + _, reason, err := c.TransactionPrepare(ctx, req) + assert.Error(t, err) + assert.Regexp(t, "FF23014", err) + assert.Equal(t, ffcapi.ErrorReasonInvalidInputs, reason) +} + +func TestTransactionPrepareWrongToAddressError(t *testing.T) { + ctx, c, _, done := newTestConnector(t) + defer done() + + req := &ffcapi.TransactionPrepareRequest{ + TransactionInput: ffcapi.TransactionInput{ + TransactionHeaders: ffcapi.TransactionHeaders{ + From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN", + To: "wrong", + }, + Method: fftypes.JSONAnyPtr("\"pause\""), + Params: []*fftypes.JSONAny{ + fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"), + }, + }, + } + _, _, err := c.TransactionPrepare(ctx, req) + assert.Error(t, err) + assert.Regexp(t, "FF23020", err) +} + +func TestTransactionPrepareWrongFromAddressError(t *testing.T) { + ctx, c, _, done := newTestConnector(t) + defer done() + + req := &ffcapi.TransactionPrepareRequest{ + TransactionInput: ffcapi.TransactionInput{ + TransactionHeaders: ffcapi.TransactionHeaders{ + From: "wrong", + To: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN", + }, + Method: fftypes.JSONAnyPtr("\"pause\""), + Params: []*fftypes.JSONAny{ + fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"), + }, + }, + } + _, _, err := c.TransactionPrepare(ctx, req) + assert.Error(t, err) + assert.Regexp(t, "FF23019", err) +} + +func TestTransactionPrepareGetContractExtError(t *testing.T) { + ctx, c, mRPC, done := newTestConnector(t) + defer done() + + mRPC.On("GetBlockHash", ctx, mock.Anything). + Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil) + + mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything). + Return(nil, errors.New("error")) + + req := &ffcapi.TransactionPrepareRequest{ + TransactionInput: ffcapi.TransactionInput{ + TransactionHeaders: ffcapi.TransactionHeaders{ + From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN", + To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s", + }, + Method: fftypes.JSONAnyPtr("\"pause\""), + Params: []*fftypes.JSONAny{ + fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"), + }, + }, + } + _, _, err := c.TransactionPrepare(ctx, req) + assert.Error(t, err) +} diff --git a/internal/tezos/tezos_test.go b/internal/tezos/tezos_test.go index 4143c7e..acffbab 100644 --- a/internal/tezos/tezos_test.go +++ b/internal/tezos/tezos_test.go @@ -48,6 +48,10 @@ func TestConnectorInit(t *testing.T) { assert.NoError(t, err) assert.Equal(t, int64(500), cc.(*tezosConnector).catchupThreshold) // set to page size + conf.Set(BlockchainRPC, "wrong rpc") + cc, err = NewTezosConnector(context.Background(), conf) + assert.Regexp(t, "FF23052", err) + conf.Set(ConfigDataFormat, "map") conf.Set(BlockCacheSize, "-1") cc, err = NewTezosConnector(context.Background(), conf) From 9644783c98fa4cdd4396064b2abbfefc1c2c7c7c Mon Sep 17 00:00:00 2001 From: Dzianis Andreyenka Date: Mon, 9 Oct 2023 20:04:16 +0200 Subject: [PATCH 2/2] DA-505 Increase unit test coverage Signed-off-by: Dzianis Andreyenka --- internal/tezos/send_transaction_test.go | 151 ++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 internal/tezos/send_transaction_test.go diff --git a/internal/tezos/send_transaction_test.go b/internal/tezos/send_transaction_test.go new file mode 100644 index 0000000..26031af --- /dev/null +++ b/internal/tezos/send_transaction_test.go @@ -0,0 +1,151 @@ +package tezos + +import ( + "encoding/json" + "errors" + "testing" + + "blockwatch.cc/tzgo/rpc" + "blockwatch.cc/tzgo/tezos" + "github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" +) + +func TestTransactionSendDecodeStrError(t *testing.T) { + ctx, c, _, done := newTestConnector(t) + defer done() + + req := &ffcapi.TransactionSendRequest{ + TransactionData: "1", + } + res, reason, err := c.TransactionSend(ctx, req) + assert.Error(t, err) + assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs) + assert.Nil(t, res) +} + +func TestTransactionSendDecodeOpError(t *testing.T) { + ctx, c, _, done := newTestConnector(t) + defer done() + + req := &ffcapi.TransactionSendRequest{} + res, reason, err := c.TransactionSend(ctx, req) + assert.Error(t, err) + assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs) + assert.Nil(t, res) +} + +func TestTransactionSendWrongFromAddressError(t *testing.T) { + ctx, c, _, done := newTestConnector(t) + defer done() + + req := &ffcapi.TransactionSendRequest{ + TransactionHeaders: ffcapi.TransactionHeaders{ + From: "wrong", + }, + TransactionData: "424d426559724d4a704c577271437337555463466155514365574271736a434c6c00889816a17ae688c971be1ad34bfe1990f8fa5e0f000b0000000130a980e6e41028da2cacfca4ddefea252d18bed900ffff05706175736500000002030a", + } + res, _, err := c.TransactionSend(ctx, req) + assert.Error(t, err) + assert.Regexp(t, "FF23019", err) + assert.Nil(t, res) +} + +func TestTransactionSendGetContractExtError(t *testing.T) { + ctx, c, mRPC, done := newTestConnector(t) + defer done() + + mRPC.On("GetBlockHash", ctx, mock.Anything). + Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil) + + mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything). + Return(nil, errors.New("error")) + + req := &ffcapi.TransactionSendRequest{ + TransactionHeaders: ffcapi.TransactionHeaders{ + From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN", + To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s", + }, + TransactionData: "424d426559724d4a704c577271437337555463466155514365574271736a434c6c00889816a17ae688c971be1ad34bfe1990f8fa5e0f000b0000000130a980e6e41028da2cacfca4ddefea252d18bed900ffff05706175736500000002030a", + } + _, _, err := c.TransactionSend(ctx, req) + assert.Error(t, err) +} + +func TestTransactionSendSimulateError(t *testing.T) { + ctx, c, mRPC, done := newTestConnector(t) + defer done() + + mRPC.On("GetBlockHash", ctx, mock.Anything). + Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil) + + mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything). + Return(&rpc.ContractInfo{ + Counter: 10, + Manager: "edpkv89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6", + }, nil) + + mRPC.On("Simulate", ctx, mock.Anything, mock.Anything).Return(nil, errors.New("error")) + + req := &ffcapi.TransactionSendRequest{ + TransactionHeaders: ffcapi.TransactionHeaders{ + From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN", + To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s", + }, + TransactionData: "424d426559724d4a704c577271437337555463466155514365574271736a434c6c00889816a17ae688c971be1ad34bfe1990f8fa5e0f000b0000000130a980e6e41028da2cacfca4ddefea252d18bed900ffff05706175736500000002030a", + } + _, _, err := c.TransactionSend(ctx, req) + assert.Error(t, err) +} + +func TestTransactionSendWrongSimulateStatusError(t *testing.T) { + ctx, c, mRPC, done := newTestConnector(t) + defer done() + + mRPC.On("GetBlockHash", ctx, mock.Anything). + Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil) + + mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything). + Return(&rpc.ContractInfo{ + Counter: 10, + Manager: "edpkv89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6", + }, nil) + + mRPC.On("Simulate", ctx, mock.Anything, mock.Anything). + Return(&rpc.Receipt{ + Op: &rpc.Operation{ + Contents: []rpc.TypedOperation{ + rpc.Transaction{ + Manager: rpc.Manager{ + Generic: rpc.Generic{ + Metadata: rpc.OperationMetadata{ + Result: rpc.OperationResult{ + Errors: []rpc.OperationError{ + { + GenericError: rpc.GenericError{ + ID: "error id", + Kind: "error", + }, + Raw: json.RawMessage{}, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, nil) + + req := &ffcapi.TransactionSendRequest{ + TransactionHeaders: ffcapi.TransactionHeaders{ + From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN", + To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s", + }, + TransactionData: "424d426559724d4a704c577271437337555463466155514365574271736a434c6c00889816a17ae688c971be1ad34bfe1990f8fa5e0f000b0000000130a980e6e41028da2cacfca4ddefea252d18bed900ffff05706175736500000002030a", + } + _, _, err := c.TransactionSend(ctx, req) + assert.Error(t, err) +}