gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] branch master updated: Amount comparison.


From: gnunet
Subject: [libeufin] branch master updated: Amount comparison.
Date: Tue, 04 Apr 2023 17:26:26 +0200

This is an automated email from the git hooks/post-receive script.

ms pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new 9c7079e5 Amount comparison.
9c7079e5 is described below

commit 9c7079e5323eed4d16e24c1c4245d6586cecac53
Author: MS <ms@taler.net>
AuthorDate: Tue Apr 4 17:23:20 2023 +0200

    Amount comparison.
    
    Checking zero amounts as strings, rather than
    via brittle BigDecimal operations.
---
 sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt |  4 ++--
 util/src/main/kotlin/amounts.kt                             |  9 +++++++++
 util/src/test/kotlin/AmountTest.kt                          | 11 +++++++++++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt
index d66aa83e..b04af23d 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/CircuitApi.kt
@@ -804,8 +804,8 @@ fun circuitApi(circuitRoute: Route) {
             resourceName,
             withBankFault = true // See comment "CUSTOMER AND BANK ACCOUNT 
INVARIANT".
         )
-        val balance = getBalance(bankAccount)
-        if (balance != BigDecimal.ZERO) {
+        val balance: BigDecimal = getBalance(bankAccount)
+        if (!isAmountZero(balance)) {
             logger.error("Account $resourceName has $balance balance.  Won't 
delete it")
             throw SandboxError(
                 HttpStatusCode.PreconditionFailed,
diff --git a/util/src/main/kotlin/amounts.kt b/util/src/main/kotlin/amounts.kt
index a1cfe47d..00eb7b6d 100644
--- a/util/src/main/kotlin/amounts.kt
+++ b/util/src/main/kotlin/amounts.kt
@@ -2,6 +2,7 @@ package tech.libeufin.util
 
 import UtilError
 import io.ktor.http.*
+import java.math.BigDecimal
 
 /*
  * This file is part of LibEuFin.
@@ -36,4 +37,12 @@ fun parseAmount(amount: String): AmountWithCurrency {
         throw UtilError(HttpStatusCode.BadRequest, "invalid amount: $amount")
     val (currency, number) = match.destructured
     return AmountWithCurrency(currency = currency, amount = number)
+}
+
+fun isAmountZero(a: BigDecimal): Boolean {
+    a.abs().toPlainString().forEach {
+        if (it != '0' && it != '.')
+            return false
+    }
+    return true
 }
\ No newline at end of file
diff --git a/util/src/test/kotlin/AmountTest.kt 
b/util/src/test/kotlin/AmountTest.kt
index 1dac0be0..5ae1e05f 100644
--- a/util/src/test/kotlin/AmountTest.kt
+++ b/util/src/test/kotlin/AmountTest.kt
@@ -1,6 +1,11 @@
+import io.ktor.util.reflect.*
 import org.junit.Test
+import tech.libeufin.sandbox.roundToTwoDigits
+import tech.libeufin.util.isAmountZero
 import tech.libeufin.util.parseAmount
 import tech.libeufin.util.validatePlainAmount
+import java.math.BigDecimal
+import kotlin.reflect.typeOf
 
 inline fun <reified ExceptionType> assertException(block: () -> Unit) {
     try {
@@ -12,6 +17,12 @@ inline fun <reified ExceptionType> assertException(block: () 
-> Unit) {
     return assert(false)
 }
 class AmountTest {
+    @Test
+    fun equalTest() {
+        val z = BigDecimal("-0000000000.0000000000")
+        assert(isAmountZero(z))
+    }
+
     @Test
     fun parse() {
         var res = parseAmount("KUDOS:5.5")

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]