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 #4

Merged
merged 1 commit into from
Sep 20, 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
47 changes: 47 additions & 0 deletions internal/tezos/blocklistener_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tezos

import (
"errors"
"testing"

"blockwatch.cc/tzgo/rpc"
"blockwatch.cc/tzgo/tezos"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestBlockListenerStartGettingHighestBlockRetry(t *testing.T) {
_, c, mRPC, done := newTestConnector(t)
bl := c.blockListener

mRPC.On("GetHeadBlock", mock.Anything).Return(nil, errors.New("err")).Once()
mRPC.On("GetHeadBlock", mock.Anything).Return(
&rpc.Block{
Hash: tezos.MustParseBlockHash("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg"),
Header: rpc.BlockHeader{
Predecessor: tezos.MustParseBlockHash("BLc1BjmZ7WevMoMoj8jxh4k2wLoRqoMUxjrQuDmKzAsApfRRjFL"),
Level: 12345,
},
}, nil)

assert.Equal(t, int64(12345), bl.getHighestBlock(bl.ctx))
done() // Stop immediately in this case, while we're in the polling interval

<-bl.listenLoopDone

mRPC.AssertExpectations(t)
}

func TestBlockListenerStartGettingHighestBlockFailBeforeStop(t *testing.T) {
_, c, mRPC, done := newTestConnector(t)
done() // Stop before we start
bl := c.blockListener

mRPC.On("GetHeadBlock", mock.Anything).Return(nil, errors.New("err")).Maybe()

assert.Equal(t, int64(-1), bl.getHighestBlock(bl.ctx))

<-bl.listenLoopDone

mRPC.AssertExpectations(t)
}
53 changes: 52 additions & 1 deletion internal/tezos/get_address_balance_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,72 @@
package tezos

import (
"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 TestGetAddressBalanceOK(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("GetHeadBlock", mock.Anything).Return(&rpc.Block{
Hash: tezos.MustParseBlockHash("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg"),
}, nil)
mRPC.On("GetContractBalance", mock.Anything, mock.Anything, mock.Anything).
Return(tezos.NewZ(999), nil)

req := ffcapi.AddressBalanceRequest{
Address: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
}
res, _, err := c.AddressBalance(ctx, &req)
assert.NoError(t, err)
assert.Equal(t, int64(999), res.Balance.Int64())
}

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

req := &ffcapi.AddressBalanceRequest{
Address: "wrong",
}

_, reason, err := c.AddressBalance(ctx, req)
assert.Error(t, err)
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs)
}

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

mRPC.On("GetHeadBlock", mock.Anything).Return(nil, errors.New("err"))

req := ffcapi.AddressBalanceRequest{
Address: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
}
_, _, err := c.AddressBalance(ctx, &req)
assert.Error(t, err)
}

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

mRPC.On("GetHeadBlock", mock.Anything).Return(&rpc.Block{
Hash: tezos.MustParseBlockHash("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg"),
}, nil)
mRPC.On("GetContractBalance", mock.Anything, mock.Anything, mock.Anything).
Return(tezos.Zero, errors.New("err"))

req := ffcapi.AddressBalanceRequest{
Address: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
}
_, _, err := c.AddressBalance(ctx, &req)
assert.Error(t, err)
}
196 changes: 196 additions & 0 deletions internal/tezos/get_block_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
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 TestGetBlockInfoByNumberOK(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("GetBlock", mock.Anything, mock.MatchedBy(
func(blockNumber *fftypes.FFBigInt) bool {
return blockNumber.String() == "12345"
})).
Return(&rpc.Block{
Hash: tezos.MustParseBlockHash("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg"),
Header: rpc.BlockHeader{
Predecessor: tezos.MustParseBlockHash("BLc1BjmZ7WevMoMoj8jxh4k2wLoRqoMUxjrQuDmKzAsApfRRjFL"),
Level: 12345,
},
Operations: [][]*rpc.Operation{
{}, // consensus operations
{}, // voting operations
{}, // anonymous operations
{
{
Hash: tezos.MustParseOpHash("op13B8GtoK1UAx8p67L8y4huPqjay9yzRrpSFLTiY9kjJsrF5uV"),
},
}, // manager operations
},
}, nil).
Twice() // two cache misses and a hit

req := &ffcapi.BlockInfoByNumberRequest{
BlockNumber: fftypes.NewFFBigInt(12345),
}
res, reason, err := c.BlockInfoByNumber(ctx, req)
assert.NoError(t, err)
assert.Empty(t, reason)

assert.Equal(t, "BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg", res.BlockHash)
assert.Equal(t, "BLc1BjmZ7WevMoMoj8jxh4k2wLoRqoMUxjrQuDmKzAsApfRRjFL", res.ParentHash)
assert.Equal(t, int64(12345), res.BlockNumber.Int64())

res, reason, err = c.BlockInfoByNumber(ctx, req) // cached
assert.NoError(t, err)
assert.Equal(t, "BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg", res.BlockHash)

req.ExpectedParentHash = "BMWDjzorc6GFb2DnengeB2TRikAENukebRwubnu6ghfZceicmig"
res, reason, err = c.BlockInfoByNumber(ctx, req) // cache miss
assert.NoError(t, err)
assert.Equal(t, "BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg", res.BlockHash)
}

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

mRPC.On("GetBlock", mock.Anything, mock.Anything).
Return(nil, errors.New("status 404"))

req := &ffcapi.BlockInfoByNumberRequest{
BlockNumber: fftypes.NewFFBigInt(1),
}
res, reason, err := c.BlockInfoByNumber(ctx, req)
assert.Regexp(t, "FF23011", err)
assert.Equal(t, ffcapi.ErrorReasonNotFound, reason)
assert.Nil(t, res)
}

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

mRPC.On("GetBlock", mock.Anything, mock.Anything).
Return(nil, nil)

req := &ffcapi.BlockInfoByNumberRequest{
BlockNumber: fftypes.NewFFBigInt(12345),
}
res, reason, err := c.BlockInfoByNumber(ctx, req)
assert.Regexp(t, "FF23011", err)
assert.Equal(t, ffcapi.ErrorReasonNotFound, reason)
assert.Nil(t, res)

}

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

mRPC.On("GetBlock", mock.Anything, mock.Anything).
Return(nil, errors.New("err"))

req := &ffcapi.BlockInfoByNumberRequest{
BlockNumber: fftypes.NewFFBigInt(1),
}
res, reason, err := c.BlockInfoByNumber(ctx, req)
assert.Error(t, err)
assert.Empty(t, reason)
assert.Nil(t, res)
}

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

mRPC.On("GetBlock", mock.Anything, mock.Anything).
Return(&rpc.Block{
Hash: tezos.MustParseBlockHash("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg"),
Header: rpc.BlockHeader{
Predecessor: tezos.MustParseBlockHash("BLc1BjmZ7WevMoMoj8jxh4k2wLoRqoMUxjrQuDmKzAsApfRRjFL"),
Level: 12345,
},
Operations: [][]*rpc.Operation{
{}, // consensus operations
{}, // voting operations
{}, // anonymous operations
{
{
Hash: tezos.MustParseOpHash("op13B8GtoK1UAx8p67L8y4huPqjay9yzRrpSFLTiY9kjJsrF5uV"),
},
}, // manager operations
},
}, nil).
Once()

req := &ffcapi.BlockInfoByHashRequest{
BlockHash: "BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg",
}
res, reason, err := c.BlockInfoByHash(ctx, req)
assert.NoError(t, err)
assert.Empty(t, reason)

assert.Equal(t, "BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg", res.BlockHash)
assert.Equal(t, "BLc1BjmZ7WevMoMoj8jxh4k2wLoRqoMUxjrQuDmKzAsApfRRjFL", res.ParentHash)
assert.Equal(t, int64(12345), res.BlockNumber.Int64())

res, reason, err = c.BlockInfoByHash(ctx, req) // cached
assert.NoError(t, err)
assert.Empty(t, reason)
assert.Equal(t, "BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg", res.BlockHash)
}

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

req := &ffcapi.BlockInfoByHashRequest{
BlockHash: "wrong",
}
res, reason, err := c.BlockInfoByHash(ctx, req)
assert.Error(t, err)
assert.Empty(t, reason)
assert.Nil(t, res)
}

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

mRPC.On("GetBlock", mock.Anything, mock.Anything).
Return(nil, nil)

req := &ffcapi.BlockInfoByHashRequest{
BlockHash: "BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg",
}
res, reason, err := c.BlockInfoByHash(ctx, req)
assert.Regexp(t, "FF23011", err)
assert.Equal(t, ffcapi.ErrorReasonNotFound, reason)
assert.Nil(t, res)
}

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

mRPC.On("GetBlock", mock.Anything, mock.Anything).
Return(nil, errors.New("err"))

req := &ffcapi.BlockInfoByHashRequest{
BlockHash: "BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg",
}
res, reason, err := c.BlockInfoByHash(ctx, req)
assert.Error(t, err)
assert.Empty(t, reason)
assert.Nil(t, res)
}
45 changes: 45 additions & 0 deletions internal/tezos/get_next_nonce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package tezos

import (
"errors"
"testing"

"blockwatch.cc/tzgo/rpc"
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

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

mRPC.On("GetContractExt", mock.Anything, mock.Anything, mock.Anything).
Return(&rpc.ContractInfo{
Counter: 10,
}, nil)

req := &ffcapi.NextNonceForSignerRequest{
Signer: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
}
res, reason, err := c.NextNonceForSigner(ctx, req)
assert.NoError(t, err)
assert.Empty(t, reason)
assert.Equal(t, int64(11), res.Nonce.Int64())
}

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

mRPC.On("GetContractExt", mock.Anything, mock.Anything, mock.Anything).
Return(nil, errors.New("err"))

req := &ffcapi.NextNonceForSignerRequest{
Signer: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
}
res, reason, err := c.NextNonceForSigner(ctx, req)
assert.Error(t, err)
assert.Empty(t, reason)
assert.Nil(t, res)
}
5 changes: 3 additions & 2 deletions internal/tezos/get_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ type receiptExtraInfo struct {
// TransactionReceipt queries to see if a receipt is available for a given transaction hash
func (c *tezosConnector) TransactionReceipt(ctx context.Context, req *ffcapi.TransactionReceiptRequest) (*ffcapi.TransactionReceiptResponse, ffcapi.ErrorReason, error) {
// ensure block observer is running
mon := c.client.BlockObserver
mon.Listen(c.client)
rpcClient := c.client.(*rpc.Client)
mon := rpcClient.BlockObserver
mon.Listen(rpcClient)

// wait for confirmations
res := rpc.NewResult(tezos.MustParseOpHash(req.TransactionHash)) //.WithTTL(op.TTL).WithConfirmations(opts.Confirmations)
Expand Down
Loading