Skip to content

Commit

Permalink
Add validation for negative fee amount (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
raghavapamula authored Apr 13, 2022
1 parent 2bcdd1d commit 30818f6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
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 {
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": {
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

0 comments on commit 30818f6

Please sign in to comment.