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

Add validation for negative fee amount #397

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
15 changes: 14 additions & 1 deletion asserter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,20 @@ func (a *Asserter) Operations( // nolint:gocognit
)
}

val, _ := new(big.Int).SetString(op.Amount.Value, 10)
val, err := types.BigInt(op.Amount.Value)
if err != nil {
return err
}

// Validate that fee operation amount is negative
if val.Sign() != -1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add test coverage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return fmt.Errorf(
"%w: operation index %d",
ErrFeeAmountNotNegative,
op.OperationIdentifier.Index,
)
}

feeTotal.Add(feeTotal, val)
feeCount++
}
Expand Down
90 changes: 90 additions & 0 deletions asserter/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ func TestOperationsValidations(t *testing.T) {
},
}

invalidFeeAmount = &types.Amount{
Value: "100",
Currency: &types.Currency{
Symbol: "BTC",
Decimals: 8,
},
}

validAccount = &types.AccountIdentifier{
Address: "test",
}
Expand Down Expand Up @@ -513,6 +521,88 @@ func TestOperationsValidations(t *testing.T) {
construction: false,
err: ErrRelatedOperationInFeeNotAllowed,
},

"fee amount is non-negative": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the happy path, we want error case test as well

operations: []*types.Operation{
{
OperationIdentifier: &types.OperationIdentifier{
Index: int64(0),
},
Type: "PAYMENT",
Status: types.String("SUCCESS"),
Account: validAccount,
Amount: validDepositAmount,
},
{
OperationIdentifier: &types.OperationIdentifier{
Index: int64(1),
},
Type: "PAYMENT",
Status: types.String("SUCCESS"),
Account: validAccount,
Amount: &types.Amount{
Value: "-2000",
Currency: &types.Currency{
Symbol: "BTC",
Decimals: 8,
},
},
},
{
OperationIdentifier: &types.OperationIdentifier{
Index: int64(2),
},
Type: "FEE",
Status: types.String("SUCCESS"),
Account: validAccount,
Amount: invalidFeeAmount,
},
},
validationFilePath: "data/validation_fee_and_payment_unbalanced.json",
construction: false,
err: ErrFeeAmountNotNegative,
},

"fee amount is negative as expected": {
operations: []*types.Operation{
{
OperationIdentifier: &types.OperationIdentifier{
Index: int64(0),
},
Type: "PAYMENT",
Status: types.String("SUCCESS"),
Account: validAccount,
Amount: validDepositAmount,
},
{
OperationIdentifier: &types.OperationIdentifier{
Index: int64(1),
},
Type: "PAYMENT",
Status: types.String("SUCCESS"),
Account: validAccount,
Amount: &types.Amount{
Value: "-2000",
Currency: &types.Currency{
Symbol: "BTC",
Decimals: 8,
},
},
},
{
OperationIdentifier: &types.OperationIdentifier{
Index: int64(2),
},
Type: "FEE",
Status: types.String("SUCCESS"),
Account: validAccount,
Amount: validFeeAmount,
},
},
validationFilePath: "data/validation_fee_and_payment_unbalanced.json",
construction: false,
err: nil,
},
}

for name, test := range tests {
Expand Down
2 changes: 2 additions & 0 deletions asserter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ var (
ErrFeeAmountNotBalancing = errors.New("fee amount doesn't balance")
ErrPaymentCountMismatch = errors.New("payment count doesn't match")
ErrFeeCountMismatch = errors.New("fee count doesn't match")
ErrFeeAmountNotNegative = errors.New("fee amount is not negative")

BlockErrs = []error{
ErrAmountValueMissing,
Expand Down Expand Up @@ -142,6 +143,7 @@ var (
ErrDuplicateRelatedTransaction,
ErrPaymentAmountNotBalancing,
ErrFeeAmountNotBalancing,
ErrFeeAmountNotNegative,
}
)

Expand Down