Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DA-505 Increase unit test coverage #17

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![build](https://github.com/hyperledger/firefly-tezosconnect/actions/workflows/go.yml/badge.svg?branch=main)
![build](https://github.com/hyperledger/firefly-tezosconnect/actions/workflows/build.yml/badge.svg?branch=main)
[![codecov](https://codecov.io/gh/hyperledger/firefly-tezosconnect/branch/main/graph/badge.svg)](https://codecov.io/gh/hyperledger/firefly-tezosconnect)
[![Go Reference](https://pkg.go.dev/badge/github.com/hyperledger/firefly-tezosconnect.svg)](https://pkg.go.dev/github.com/hyperledger/firefly-tezosconnect)

Expand Down
4 changes: 1 addition & 3 deletions internal/tezos/estimate_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package tezos

import (
"context"
"errors"

"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
)

func (c *tezosConnector) GasEstimate(_ context.Context, _ *ffcapi.TransactionInput) (*ffcapi.GasEstimateResponse, ffcapi.ErrorReason, error) {
// TODO: implement
return nil, ffcapi.ErrorReason("not implemented"), errors.New("not implemented")
return nil, "", nil
}
18 changes: 18 additions & 0 deletions internal/tezos/estimate_gas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tezos

import (
"testing"

"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
"github.com/stretchr/testify/assert"
)

func TestGasEstimate(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

resp, reason, err := c.GasEstimate(ctx, &ffcapi.TransactionInput{})
assert.Nil(t, resp)
assert.Empty(t, reason)
assert.NoError(t, err)
}
5 changes: 2 additions & 3 deletions internal/tezos/get_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ type receiptExtraInfo struct {
func (c *tezosConnector) TransactionReceipt(ctx context.Context, req *ffcapi.TransactionReceiptRequest) (*ffcapi.TransactionReceiptResponse, ffcapi.ErrorReason, error) {
// ensure block observer is running
rpcClient := c.client.(*rpc.Client)
mon := rpcClient.BlockObserver
mon.Listen(rpcClient)
rpcClient.Listen()

// wait for confirmations
res := rpc.NewResult(tezos.MustParseOpHash(req.TransactionHash)) // .WithTTL(op.TTL).WithConfirmations(opts.Confirmations)
res.Listen(mon)
res.Listen(rpcClient.BlockObserver)
res.WaitContext(ctx)
if err := res.Err(); err != nil {
return nil, "", err
Expand Down
11 changes: 4 additions & 7 deletions internal/tezos/prepare_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *tezosConnector) completeOp(ctx context.Context, op *codec.Op, fromStrin
mayNeedReveal := len(op.Contents) > 0 && op.Contents[0].Kind() != tezos.OpTypeReveal
// add reveal if necessary
if mayNeedReveal && !state.IsRevealed() {
key, err := c.getPubKeyFromSignatory(op.Source.String())
key, err := c.getPubKeyFromSignatory(ctx, op.Source.String())
if err != nil {
return err
}
Expand Down Expand Up @@ -178,9 +178,9 @@ func getNetworkParamsByName(name string) *tezos.Params {
}
}

func (c *tezosConnector) getPubKeyFromSignatory(tezosAddress string) (*tezos.Key, error) {
func (c *tezosConnector) getPubKeyFromSignatory(ctx context.Context, tezosAddress string) (*tezos.Key, error) {
url := c.signatoryURL + "/keys/" + tezosAddress
req, err := http.NewRequest("GET", url, nil)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
Expand All @@ -195,10 +195,7 @@ func (c *tezosConnector) getPubKeyFromSignatory(tezosAddress string) (*tezos.Key
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
body, _ := io.ReadAll(resp.Body)

var pubKeyJSON struct {
PubKey string `json:"public_key"`
Expand Down
192 changes: 191 additions & 1 deletion internal/tezos/prepare_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tezos
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"

"blockwatch.cc/tzgo/codec"
Expand All @@ -15,7 +17,7 @@ import (
"github.com/stretchr/testify/mock"
)

func TestTransactionPrepareOk(t *testing.T) {
func TestTransactionPrepareSuccess(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

Expand Down Expand Up @@ -66,6 +68,75 @@ func TestTransactionPrepareOk(t *testing.T) {
assert.NotEmpty(t, res.TransactionData)
}

func TestTransactionPrepareWithRevealSuccess(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: "89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, nil)

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"public_key\":\"edpkvHVuLHkr5eDiTtQKyUPqgYVAk3Sy4m7qBD8r6abemHkZsMU5Kh\"}"))
}))
defer svr.Close()
c.signatoryURL = svr.URL

mRPC.On("Simulate", ctx, mock.Anything, mock.Anything).
Return(&rpc.Receipt{
Op: &rpc.Operation{
Contents: []rpc.TypedOperation{
rpc.Reveal{
Manager: rpc.Manager{
Generic: rpc.Generic{
Metadata: rpc.OperationMetadata{
Result: rpc.OperationResult{
Status: tezos.OpStatusApplied,
},
},
},
},
},
rpc.Transaction{
Manager: rpc.Manager{
Generic: rpc.Generic{
Metadata: rpc.OperationMetadata{
Result: rpc.OperationResult{
Status: tezos.OpStatusApplied,
},
},
},
},
},
},
},
}, 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()
Expand Down Expand Up @@ -318,3 +389,122 @@ func Test_estimateAndAssignExceedMaxLimitError(t *testing.T) {
_, err := c.estimateAndAssignTxCost(ctx, op, opts)
assert.Error(t, err)
}

func TestTransactionPrepareWithRevealEmptyServerError(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: "89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, 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\"}}"),
},
},
}
resp, _, err := c.TransactionPrepare(ctx, req)
assert.Error(t, err)
assert.Nil(t, resp)
}

func Test_getNetworkParamsByName(t *testing.T) {
params := getNetworkParamsByName("ghostnet")
assert.Equal(t, params, tezos.GhostnetParams)

params = getNetworkParamsByName("nairobinet")
assert.Equal(t, params, tezos.NairobinetParams)

params = getNetworkParamsByName("default")
assert.Equal(t, params, tezos.DefaultParams)
}

func Test_getPubKeyFromSignatoryInvalidRespKeySuccess(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"public_key\":\"edpkvHVuLHkr5eDiTtQKyUPqgYVAk3Sy4m7qBD8r6abemHkZsMU5Kh\"}"))
}))
defer svr.Close()
c.signatoryURL = svr.URL

resp, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.NotNil(t, resp)
assert.NoError(t, err)
}

func Test_getPubKeyFromSignatoryyNilContextError(t *testing.T) {
_, c, _, done := newTestConnector(t)
defer done()

_, err := c.getPubKeyFromSignatory(nil, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}

func Test_getPubKeyFromSignatoryHttpError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

_, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}

func Test_getPubKeyFromSignatoryHttpWrongStatusError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
w.Write([]byte("internal error"))
}))
defer svr.Close()
c.signatoryURL = svr.URL

_, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}

func Test_getPubKeyFromSignatoryUnmarshalRespError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(nil)
}))
defer svr.Close()
c.signatoryURL = svr.URL

_, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}

func Test_getPubKeyFromSignatoryInvalidRespKeyError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"public_key\":\"invalid\"}"))
}))
defer svr.Close()
c.signatoryURL = svr.URL

_, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}
13 changes: 7 additions & 6 deletions internal/tezos/send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -49,11 +50,14 @@ func (c *tezosConnector) TransactionSend(ctx context.Context, req *ffcapi.Transa
}, "", nil
}

func (c *tezosConnector) signTxRemotely(_ context.Context, op *codec.Op) error {
func (c *tezosConnector) signTxRemotely(ctx context.Context, op *codec.Op) error {
if op == nil {
return errors.New("operation is empty")
}
url := c.signatoryURL + "/keys/" + op.Source.String()
requestBody, _ := json.Marshal(hex.EncodeToString(op.WatermarkedBytes()))

req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(requestBody))
if err != nil {
return err
}
Expand All @@ -68,10 +72,7 @@ func (c *tezosConnector) signTxRemotely(_ context.Context, op *codec.Op) error {
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
body, _ := io.ReadAll(resp.Body)

var signatureJSON struct {
Signature string
Expand Down
Loading