Skip to content

Commit

Permalink
fix(ARCO-212): Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boecklim committed Nov 13, 2024
1 parent 19b2fd5 commit c152b54
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 185 deletions.
1 change: 0 additions & 1 deletion internal/validator/default/default_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func cumulativeCheckFees(ctx context.Context, txFinder validator.TxFinderI, tx *
cumulativePaidFee := uint64(0)

for _, txFromSet := range txSet {

cumulativeSize += txFromSet.Size()
totalInput := txFromSet.TotalInputSatoshis()
totalOutput := txFromSet.TotalOutputSatoshis()
Expand Down
96 changes: 45 additions & 51 deletions internal/validator/default/default_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ func TestValidator(t *testing.T) {
t.Run("valid Raw Format tx - expect success", func(t *testing.T) {
// given
txFinder := mocks.TxFinderIMock{
GetRawTxsFunc: func(_ context.Context, _ validation.FindSourceFlag, _ []string) ([]validation.RawTx, error) {
res := []validation.RawTx{fixture.ParentTx1, fixture.ParentTx2}
GetRawTxsFunc: func(_ context.Context, _ validation.FindSourceFlag, _ []string) ([]*sdkTx.Transaction, error) {
res := []*sdkTx.Transaction{fixture.ParentTx1}
return res, nil
},
}

rawTx, _ := sdkTx.NewTransactionFromHex(fixture.ValidTxRawHex)
rawTx := fixture.ValidTx

sut := New(getPolicy(5), &txFinder)

Expand Down Expand Up @@ -352,85 +352,60 @@ func TestNeedExtension(t *testing.T) {
}

func TestCumulativeCheckFees(t *testing.T) {
txMap := map[string]*sdkTx.Transaction{
fixture.ParentTxID1: fixture.ParentTx1,
fixture.AncestorTxID1: fixture.AncestorTx1,
fixture.AncestorOfAncestorTx1ID1: fixture.AncestorOfAncestor1Tx1,
}

tcs := []struct {
name string
hex string
feeModel *fees.SatoshisPerKilobyte
getTxFinderFn func(t *testing.T) mocks.TxFinderIMock
name string
feeModel *fees.SatoshisPerKilobyte
mempoolAncestors []string
getMempoolAncestorsErr error
getRawTxsErr error

expectedErr *validation.Error
}{
{
name: "no unmined ancestors - valid fee",
hex: fixture.ValidTxRawHex,
feeModel: func() *fees.SatoshisPerKilobyte {
return &fees.SatoshisPerKilobyte{Satoshis: 1}
}(),
getTxFinderFn: func(_ *testing.T) mocks.TxFinderIMock {
return mocks.TxFinderIMock{
GetMempoolAncestorsFunc: func(_ context.Context, _ []string) ([]validation.RawTx, error) {
return []validation.RawTx{fixture.ParentTx1, fixture.ParentTx2}, nil
},
}
},
mempoolAncestors: []string{},
},
{
name: "no unmined ancestors - to low fee",
hex: fixture.ValidTxRawHex,
name: "no unmined ancestors - too low fee",
feeModel: func() *fees.SatoshisPerKilobyte {
return &fees.SatoshisPerKilobyte{Satoshis: 50}
}(),
getTxFinderFn: func(_ *testing.T) mocks.TxFinderIMock {
return mocks.TxFinderIMock{
GetMempoolAncestorsFunc: func(_ context.Context, _ []string) ([]validation.RawTx, error) {
return []validation.RawTx{fixture.ParentTx1, fixture.ParentTx2}, nil
},
}
},
mempoolAncestors: []string{},

expectedErr: validation.NewError(ErrTxFeeTooLow, api.ErrStatusCumulativeFees),
},
{
name: "cumulative fees too low",
hex: fixture.ValidTxRawHex,
feeModel: func() *fees.SatoshisPerKilobyte {
return &fees.SatoshisPerKilobyte{Satoshis: 50}
}(),
getTxFinderFn: func(_ *testing.T) mocks.TxFinderIMock {
return mocks.TxFinderIMock{
GetMempoolAncestorsFunc: func(_ context.Context, _ []string) ([]validation.RawTx, error) {
return []validation.RawTx{fixture.ParentTx1, fixture.ParentTx2}, nil
},
}
},
mempoolAncestors: []string{fixture.AncestorTxID1},

expectedErr: validation.NewError(ErrTxFeeTooLow, api.ErrStatusCumulativeFees),
},
{
name: "cumulative fees sufficient",
hex: fixture.ValidTxRawHex,
feeModel: func() *fees.SatoshisPerKilobyte {
return &fees.SatoshisPerKilobyte{Satoshis: 1}
}(),
getTxFinderFn: func(_ *testing.T) mocks.TxFinderIMock {
return mocks.TxFinderIMock{
GetMempoolAncestorsFunc: func(_ context.Context, _ []string) ([]validation.RawTx, error) {
return []validation.RawTx{fixture.ParentTx1, fixture.ParentTx2}, nil
},
}
},
mempoolAncestors: []string{fixture.AncestorTxID1},
},
{
name: "failed to get mempool ancestors",
hex: fixture.ValidTxRawHex,
feeModel: func() *fees.SatoshisPerKilobyte {
return &fees.SatoshisPerKilobyte{Satoshis: 5}
}(),
getTxFinderFn: func(_ *testing.T) mocks.TxFinderIMock {
return mocks.TxFinderIMock{
GetMempoolAncestorsFunc: func(_ context.Context, _ []string) ([]validation.RawTx, error) {
return nil, errors.New("some error")
},
}
},
getMempoolAncestorsErr: errors.New("some error"),

expectedErr: validation.NewError(
ErrFailedToGetMempoolAncestors,
api.ErrStatusCumulativeFees),
Expand All @@ -440,11 +415,30 @@ func TestCumulativeCheckFees(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
// given
txFinder := tc.getTxFinderFn(t)
tx, _ := sdkTx.NewTransactionFromHex(tc.hex)
txFinder := &mocks.TxFinderIMock{
GetMempoolAncestorsFunc: func(_ context.Context, _ []string) ([]string, error) {
return tc.mempoolAncestors, tc.getMempoolAncestorsErr
},
GetRawTxsFunc: func(_ context.Context, _ validation.FindSourceFlag, ids []string) ([]*sdkTx.Transaction, error) {
rawTxs := make([]*sdkTx.Transaction, len(ids))
for i, id := range ids {
rawTx, ok := txMap[id]
if !ok {
t.Fatalf("tx id %s not found", id)
}
rawTxs[i] = rawTx
}

return rawTxs, tc.getRawTxsErr
},
}
tx := fixture.ValidTx

err := extendTx(context.TODO(), txFinder, tx, false)
require.NoError(t, err)

// when
actualError := cumulativeCheckFees(context.TODO(), &txFinder, tx, tc.feeModel, false)
actualError := cumulativeCheckFees(context.TODO(), txFinder, tx, tc.feeModel, false)

// then
if tc.expectedErr == nil {
Expand Down
58 changes: 35 additions & 23 deletions internal/validator/default/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,33 @@ import (
"github.com/stretchr/testify/require"

"github.com/bitcoin-sv/arc/internal/validator"
"github.com/bitcoin-sv/arc/internal/validator/default/testdata"
fixture "github.com/bitcoin-sv/arc/internal/validator/default/testdata"
"github.com/bitcoin-sv/arc/internal/validator/mocks"
)

func TestDefaultValidator_helpers_extendTx(t *testing.T) {
tcs := []struct {
name string
txHex string
foundTransactions []validator.RawTx
foundTransactions []*sdkTx.Transaction
expectedErr error
}{
{
name: "cannot find parents",
txHex: testdata.ValidTxRawHex,
txHex: fixture.ValidTxRawHex,
foundTransactions: nil,
expectedErr: ErrParentNotFound,
},
{
name: "cannot find all parents",
txHex: testdata.ValidTxRawHex,
foundTransactions: []validator.RawTx{testdata.ParentTx1},
expectedErr: ErrParentNotFound,
},
{
name: "tx finder returns rubbish",
txHex: testdata.ValidTxRawHex,
foundTransactions: []validator.RawTx{testdata.ParentTx1, testdata.RandomTx1},
txHex: fixture.ValidTxRawHex,
foundTransactions: []*sdkTx.Transaction{fixture.ParentTx1, fixture.RandomTx1},
expectedErr: ErrParentNotFound,
},
{
name: "success",
txHex: testdata.ValidTxRawHex,
foundTransactions: []validator.RawTx{testdata.ParentTx1, testdata.ParentTx2},
txHex: fixture.ValidTxRawHex,
foundTransactions: []*sdkTx.Transaction{fixture.ParentTx1},
expectedErr: nil,
},
}
Expand All @@ -50,7 +44,7 @@ func TestDefaultValidator_helpers_extendTx(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// given
txFinder := mocks.TxFinderIMock{
GetRawTxsFunc: func(_ context.Context, _ validator.FindSourceFlag, _ []string) ([]validator.RawTx, error) {
GetRawTxsFunc: func(_ context.Context, _ validator.FindSourceFlag, _ []string) ([]*sdkTx.Transaction, error) {
return tc.foundTransactions, nil
},
}
Expand Down Expand Up @@ -78,29 +72,36 @@ func TestDefaultValidator_helpers_extendTx(t *testing.T) {
}

func TestDefaultValidator_helpers_getUnminedAncestors(t *testing.T) {
txMap := map[string]*sdkTx.Transaction{
fixture.ParentTxID1: fixture.ParentTx1,
fixture.AncestorTxID1: fixture.AncestorTx1,
fixture.AncestorOfAncestorTx1ID1: fixture.AncestorOfAncestor1Tx1,
fixture.RandomTxID1: fixture.RandomTx1,
}

tcs := []struct {
name string
txHex string
mempoolAncestors []validator.RawTx
mempoolAncestors []string
getMempoolAncestorsErr error

expectedError error
}{
{
name: "tx finder returns rubbish",
txHex: testdata.ValidTxRawHex,
mempoolAncestors: []validator.RawTx{testdata.ParentTx1, testdata.RandomTx1},
txHex: fixture.ValidTxRawHex,
mempoolAncestors: []string{fixture.ParentTx1.TxID(), fixture.RandomTx1.TxID()},

expectedError: ErrParentNotFound,
},
{
name: "with mined parents only",
txHex: testdata.ValidTxRawHex,
mempoolAncestors: []validator.RawTx{testdata.ParentTx1, testdata.ParentTx2},
txHex: fixture.ValidTxRawHex,
mempoolAncestors: []string{fixture.ParentTx1.TxID()},
},
{
name: "with mined parents only",
txHex: testdata.ValidTxRawHex,
txHex: fixture.ValidTxRawHex,
mempoolAncestors: nil,
getMempoolAncestorsErr: errors.New("some error"),

Expand All @@ -113,9 +114,21 @@ func TestDefaultValidator_helpers_getUnminedAncestors(t *testing.T) {
// given

txFinder := mocks.TxFinderIMock{
GetMempoolAncestorsFunc: func(_ context.Context, _ []string) ([]validator.RawTx, error) {
GetMempoolAncestorsFunc: func(_ context.Context, _ []string) ([]string, error) {
return tc.mempoolAncestors, tc.getMempoolAncestorsErr
},
GetRawTxsFunc: func(_ context.Context, _ validator.FindSourceFlag, ids []string) ([]*sdkTx.Transaction, error) {
var rawTxs []*sdkTx.Transaction
for _, id := range ids {
rawTx, ok := txMap[id]
if !ok {
continue
}
rawTxs = append(rawTxs, rawTx)
}

return rawTxs, nil
},
}

tx, _ := sdkTx.NewTransactionFromHex(tc.txHex)
Expand All @@ -130,8 +143,7 @@ func TestDefaultValidator_helpers_getUnminedAncestors(t *testing.T) {
}

require.NoError(t, actualError)
expectedUnminedAncestors := make([]validator.RawTx, 0)
require.Len(t, actual, len(expectedUnminedAncestors))
require.Len(t, actual, 1)
})
}
}
Loading

0 comments on commit c152b54

Please sign in to comment.