From 8b6d02d76f0321b502d70c87952f8662202c54fd Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Tue, 13 Mar 2018 13:32:23 +0200 Subject: [PATCH] Add map-float= for proper token balances comparison --- src/clj/commiteth/scheduler.clj | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/clj/commiteth/scheduler.clj b/src/clj/commiteth/scheduler.clj index f9c25dd..90fc9cd 100644 --- a/src/clj/commiteth/scheduler.clj +++ b/src/clj/commiteth/scheduler.clj @@ -226,12 +226,6 @@ (neg? n) (- n) :else n)) -(defn float= - ([x y] (float= x y 0.0000001)) - ([x y epsilon] - (log/debug x y epsilon) - (let [scale (if (or (zero? x) (zero? y)) 1 (abs x))] - (<= (abs (- x y)) (* scale epsilon))))) (defn update-bounty-token-balances "Helper function for updating internal ERC20 token balances to token multisig contract. Will be called periodically for all open bounty contracts." @@ -291,6 +285,17 @@ (db-bounties/open-bounty-contracts)] (update-issue-usd-value bounty-addr))) +(defn float= + ([x y] (float= x y 0.0000001)) + ([x y epsilon] + (log/debug x y epsilon) + (let [scale (if (or (zero? x) (zero? y)) 1 (abs x))] + (<= (abs (- x y)) (* scale epsilon))))) + +(defn map-float= [m1 m2] + (and (= (set (keys m1)) (set (keys m2))) + (every? #(float= (get m1 %1) (get m2 %1)) (keys m1)))) + (defn update-balances [] (log/info "In update-balances") @@ -312,13 +317,13 @@ (when (or (not (float= db-balance-eth balance-eth)) - (not= db-tokens token-balances)) - (log/debug "balances differ") - (log/debug "ETH (db):" db-balance-eth (type db-balance-eth) ) - (log/debug "ETH (chain):" balance-eth (type balance-eth) ) - (log/debug "ETH cmp:" (float= db-balance-eth balance-eth)) - (log/debug "tokens (db):" db-tokens (type db-tokens) (type (:SNT db-tokens))) - (log/debug "tokens (chain):" token-balances (type token-balances) (type (:SNT token-balances))) + (not (map-float= db-tokens token-balances))) + (log/info "balances differ") + (log/info "ETH (db):" db-balance-eth (type db-balance-eth) ) + (log/info "ETH (chain):" balance-eth (type balance-eth) ) + (log/info "ETH cmp:" (float= db-balance-eth balance-eth)) + (log/info "tokens (db):" db-tokens (type db-tokens) (type (:SNT db-tokens))) + (log/info "tokens (chain):" token-balances (type token-balances) (type (:SNT token-balances))) (log/debug "tokens cmp:" (= db-tokens token-balances)) (issues/update-eth-balance contract-address balance-eth)