Add exception handling inside doseq in scheduler tasks

This commit is contained in:
Vitaliy Vlasov 2018-02-26 17:14:08 +02:00 committed by Tetiana Churikova
parent cd44e15969
commit 6fac1eee9a

View File

@ -15,43 +15,50 @@
[clj-time.periodic :refer [periodic-seq]]
[chime :refer [chime-at]]))
(defn update-issue-contract-address
"For each pending deployment: gets transaction receipt, updates db
state (contract-address, comment-id) and posts github comment"
[]
(log/info "In update-issue-contract-address")
(doseq [{issue-id :issue_id
transaction-hash :transaction_hash} (issues/list-pending-deployments)]
(log/debug "pending deployment:" transaction-hash)
(when-let [receipt (eth/get-transaction-receipt transaction-hash)]
(log/info "update-issue-contract-address: transaction receipt for issue #"
issue-id ": " receipt)
(if-let [contract-address (multisig/find-created-multisig-address receipt)]
(let [issue (issues/update-contract-address issue-id contract-address)
{owner :owner
repo :repo
comment-id :comment_id
issue-number :issue_number} issue
balance-eth-str (eth/get-balance-eth contract-address 6)
balance-eth (read-string balance-eth-str)]
(log/info "Updating comment image")
(bounties/update-bounty-comment-image issue-id
owner
repo
issue-number
contract-address
balance-eth
balance-eth-str
{})
(log/info "Updating comment")
(github/update-comment owner
repo
comment-id
issue-number
contract-address
balance-eth
balance-eth-str
{}))
(log/error "Failed to find contract address in tx logs")))))
(log/info "pending deployment:" transaction-hash)
(try
(when-let [receipt (eth/get-transaction-receipt transaction-hash)]
(log/info "update-issue-contract-address: transaction receipt for issue #"
issue-id ": " receipt)
(if-let [contract-address (multisig/find-created-multisig-address receipt)]
(let [issue (issues/update-contract-address issue-id contract-address)
{owner :owner
repo :repo
comment-id :comment_id
issue-number :issue_number} issue
balance-eth-str (eth/get-balance-eth contract-address 6)
balance-eth (read-string balance-eth-str)]
(log/info "Updating comment image")
(bounties/update-bounty-comment-image issue-id
owner
repo
issue-number
contract-address
balance-eth
balance-eth-str
{})
(log/info "Updating comment")
(github/update-comment owner
repo
comment-id
issue-number
contract-address
balance-eth
balance-eth-str
{}))
(log/error "Failed to find contract address in tx logs")))
(catch Throwable ex
(do (log/error "update-issue-contract-address exception:" ex)
(clojure.stacktrace/print-stack-trace ex)))))
(log/info "Exit update-issue-contract-address"))
(defn deploy-contract [owner-address issue-id]
@ -85,6 +92,7 @@
(defn self-sign-bounty
"Walks through all issues eligible for bounty payout and signs corresponding transaction"
[]
(log/info "In self-sign-bounty")
(doseq [{contract-address :contract_address
issue-id :issue_id
payout-address :payout_address
@ -94,35 +102,42 @@
issue-number :issue_number
balance-eth :balance_eth
tokens :tokens
winner-login :winner_login} (db-bounties/pending-bounties)
:let [value (eth/get-balance-hex contract-address)]]
(if (empty? payout-address)
(do
(log/error "Cannot sign pending bounty - winner has no payout address")
(github/update-merged-issue-comment owner
repo
comment-id
contract-address
(eth-decimal->str balance-eth)
tokens
winner-login
true))
(let [execute-hash (multisig/send-all contract-address payout-address)]
(log/info "Payout self-signed, called sign-all(" contract-address payout-address ") tx:" execute-hash)
(db-bounties/update-execute-hash issue-id execute-hash)
(db-bounties/update-winner-login issue-id winner-login)
(github/update-merged-issue-comment owner
repo
comment-id
contract-address
(eth-decimal->str balance-eth)
tokens
winner-login
false)))))
winner-login :winner_login} (db-bounties/pending-bounties)]
(try
(let [value (eth/get-balance-hex contract-address)]
(if (empty? payout-address)
(do
(log/error "Cannot sign pending bounty - winner has no payout address")
(github/update-merged-issue-comment owner
repo
comment-id
contract-address
(eth-decimal->str balance-eth)
tokens
winner-login
true))
(let [execute-hash (multisig/send-all contract-address payout-address)]
(log/info "Payout self-signed, called sign-all(" contract-address payout-address ") tx:" execute-hash)
(db-bounties/update-execute-hash issue-id execute-hash)
(db-bounties/update-winner-login issue-id winner-login)
(github/update-merged-issue-comment owner
repo
comment-id
contract-address
(eth-decimal->str balance-eth)
tokens
winner-login
false))))
(catch Throwable ex
(do (log/error "self-sign-bounty exception:" ex)
(clojure.stacktrace/print-stack-trace ex)))))
(log/info "Exit self-sign-bounty")
)
(defn update-confirm-hash
"Gets transaction receipt for each pending payout and updates DB confirm_hash with tranaction ID of commiteth bot account's confirmation."
[]
(log/info "In update-confirm-hash")
(doseq [{issue-id :issue_id
execute-hash :execute_hash} (db-bounties/pending-payouts)]
(log/info "pending payout:" execute-hash)
@ -130,7 +145,8 @@
(log/info "execution receipt for issue #" issue-id ": " receipt)
(when-let [confirm-hash (multisig/find-confirmation-tx-id receipt)]
(log/info "confirm hash:" confirm-hash)
(db-bounties/update-confirm-hash issue-id confirm-hash)))))
(db-bounties/update-confirm-hash issue-id confirm-hash))))
(log/info "Exit update-confirm-hash"))
(defn update-watch-hash
@ -154,6 +170,7 @@
(defn update-payout-receipt
"Gets transaction receipt for each confirmed payout and updates payout_hash"
[]
(log/info "In update-payout-receipt")
(doseq [{issue-id :issue_id
payout-hash :payout_hash
contract-address :contract_address
@ -167,32 +184,38 @@
payee-login :payee_login
updated :updated} (db-bounties/confirmed-payouts)]
(log/debug "confirmed payout:" payout-hash)
(if-let [receipt (eth/get-transaction-receipt payout-hash)]
(let [contract-tokens (multisig/token-balances contract-address)
contract-eth-balance (eth/get-balance-wei contract-address)]
(if (or
(some #(> (second %) 0.0) contract-tokens)
(> contract-eth-balance 0))
(do
(log/info "Contract still has funds")
(when (multisig/is-confirmed? contract-address confirm-id)
(log/info "Detected bounty with funds and confirmed payout, calling executeTransaction")
(let [execute-tx-hash (multisig/execute-tx contract-address confirm-id)]
(log/info "execute tx:" execute-tx-hash))))
(try
(if-let [receipt (eth/get-transaction-receipt payout-hash)]
(let [contract-tokens (multisig/token-balances contract-address)
contract-eth-balance (eth/get-balance-wei contract-address)]
(if (or
(some #(> (second %) 0.0) contract-tokens)
(> contract-eth-balance 0))
(do
(log/info "Contract still has funds")
(when (multisig/is-confirmed? contract-address confirm-id)
(log/info "Detected bounty with funds and confirmed payout, calling executeTransaction")
(let [execute-tx-hash (multisig/execute-tx contract-address confirm-id)]
(log/info "execute tx:" execute-tx-hash))))
(do
(log/info "Payout has succeeded, saving payout receipt for issue #" issue-id ": " receipt)
(db-bounties/update-payout-receipt issue-id receipt)
(github/update-paid-issue-comment owner
repo
comment-id
contract-address
(eth-decimal->str balance-eth)
tokens
payee-login))))
(when (older-than-3h? updated)
(log/info "Resetting payout hash for issue" issue-id "as it has not been mined in 3h")
(db-bounties/reset-payout-hash issue-id)))))
(do
(log/info "Payout has succeeded, saving payout receipt for issue #" issue-id ": " receipt)
(db-bounties/update-payout-receipt issue-id receipt)
(github/update-paid-issue-comment owner
repo
comment-id
contract-address
(eth-decimal->str balance-eth)
tokens
payee-login))))
(when (older-than-3h? updated)
(log/info "Resetting payout hash for issue" issue-id "as it has not been mined in 3h")
(db-bounties/reset-payout-hash issue-id)))
(catch Throwable ex
(do (log/error "update-payout-receipt exception:" ex)
(clojure.stacktrace/print-stack-trace ex)))))
(log/info "Exit update-payout-receipt")
)
(defn abs
"(abs n) is the absolute value of n"
@ -213,17 +236,24 @@
(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."
[issue-id bounty-addr watch-hash]
(log/info "In update-bounty-token-balances for issue" issue-id)
(doseq [[tla token-data] (token-data/as-map)]
(let [balance (multisig/token-balance bounty-addr tla)]
(when (> balance 0)
(do
(log/debug "bounty at" bounty-addr "has" balance "of token" tla)
(let [internal-balance (multisig/token-balance-in-bounty bounty-addr tla)]
(when (and (nil? watch-hash)
(not= balance internal-balance))
(log/info "balances not in sync, calling watch")
(let [hash (multisig/watch-token bounty-addr tla)]
(db-bounties/update-watch-hash issue-id hash)))))))))
(try
(let [balance (multisig/token-balance bounty-addr tla)]
(when (> balance 0)
(do
(log/info "bounty at" bounty-addr "has" balance "of token" tla)
(let [internal-balance (multisig/token-balance-in-bounty bounty-addr tla)]
(when (and (nil? watch-hash)
(not= balance internal-balance))
(log/info "balances not in sync, calling watch")
(let [hash (multisig/watch-token bounty-addr tla)]
(db-bounties/update-watch-hash issue-id hash)))))))
(catch Throwable ex
(do (log/error "update-bounty-token-balances exception:" ex)
(clojure.stacktrace/print-stack-trace ex)))))
(log/info "Exit update-bounty-token-balances"))
(defn update-contract-internal-balances
"It is required in our current smart contract to manually update it's internal balance when some tokens have been added."
@ -263,6 +293,7 @@
(defn update-balances
[]
(log/info "In update-balances")
(doseq [{contract-address :contract_address
owner :owner
repo :repo
@ -271,43 +302,48 @@
db-balance-eth :balance_eth
db-tokens :tokens
issue-number :issue_number} (db-bounties/open-bounty-contracts)]
(when comment-id
(let [balance-eth-str (eth/get-balance-eth contract-address 6)
balance-eth (read-string balance-eth-str)
token-balances (multisig/token-balances contract-address)]
(log/debug "update-balances" balance-eth
balance-eth-str token-balances owner repo issue-number)
(try
(when comment-id
(let [balance-eth-str (eth/get-balance-eth contract-address 6)
balance-eth (read-string balance-eth-str)
token-balances (multisig/token-balances contract-address)]
(log/debug "update-balances" balance-eth
balance-eth-str token-balances owner repo issue-number)
(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)))
(log/debug "tokens cmp:" (= db-tokens token-balances))
(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)))
(log/debug "tokens cmp:" (= db-tokens token-balances))
(issues/update-eth-balance contract-address balance-eth)
(issues/update-token-balances contract-address token-balances)
(bounties/update-bounty-comment-image issue-id
owner
repo
issue-number
contract-address
balance-eth
balance-eth-str
token-balances)
(github/update-comment owner
repo
comment-id
issue-number
contract-address
balance-eth
balance-eth-str
token-balances)
(update-issue-usd-value contract-address))))))
(issues/update-eth-balance contract-address balance-eth)
(issues/update-token-balances contract-address token-balances)
(bounties/update-bounty-comment-image issue-id
owner
repo
issue-number
contract-address
balance-eth
balance-eth-str
token-balances)
(github/update-comment owner
repo
comment-id
issue-number
contract-address
balance-eth
balance-eth-str
token-balances)
(update-issue-usd-value contract-address))))
(catch Throwable ex
(do (log/error "update-balances exception:" ex)
(clojure.stacktrace/print-stack-trace ex)))))
(log/info "Exit update-balances"))
(defn wrap-in-try-catch [func]
@ -323,7 +359,7 @@
(defn run-1-min-interval-tasks [time]
(do
(log/debug "run-1-min-interval-tasks" time)
(log/info "run-1-min-interval-tasks" time)
;; TODO: disabled for now. looks like it may cause extraneus
;; contract deployments and costs
(run-tasks