Fault tolerance for payout confirmal

* if a payout transaction sent by repo admin from cljs does not get
  mined within 3 hours, consider it as failed and re-enable confirm
  button

Fixes: #94
This commit is contained in:
Teemu Patja 2017-09-15 10:24:55 +03:00
parent 6d6de254fd
commit 0476b8c1ea
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
3 changed files with 46 additions and 18 deletions

View File

@ -295,7 +295,8 @@ SELECT
u.address AS payout_address,
u.login AS payee_login,
i.confirm_hash AS confirm_hash,
i.payout_hash AS payout_hash
i.payout_hash AS payout_hash,
i.updated AS updated
FROM issues i, pull_requests p, users u, repositories r
WHERE
p.issue_id = i.issue_id
@ -326,6 +327,13 @@ SET payout_hash = :payout_hash,
updated = timezone('utc'::text, now())
WHERE issue_id = :issue_id;
-- :name reset-payout-hash :! :n
-- :doc sets issue's payout transaction hash to NULL
UPDATE issues
SET payout_hash = NULL
WHERE issue_id = :issue_id;
-- :name update-payout-receipt :! :n
-- :doc updates issue with payout transaction receipt
UPDATE issues

View File

@ -55,6 +55,11 @@
(jdbc/with-db-connection [con-db *db*]
(db/update-payout-hash con-db {:issue_id issue-id :payout_hash payout-hash})))
(defn reset-payout-hash
[issue-id]
(jdbc/with-db-connection [con-db *db*]
(db/reset-payout-hash con-db {:issue_id issue-id})))
(defn update-payout-receipt
[issue-id payout-receipt]
(jdbc/with-db-connection [con-db *db*]

View File

@ -11,6 +11,7 @@
[clojure.tools.logging :as log]
[mount.core :as mount]
[clj-time.core :as t]
[clj-time.coerce :as time-coerce]
[clj-time.periodic :refer [periodic-seq]]
[chime :refer [chime-at]]))
@ -116,6 +117,15 @@
(log/info "confirm hash:" confirm-hash)
(db-bounties/update-confirm-hash issue-id confirm-hash)))))
(defn older-than-3h?
[timestamp]
(let [now (t/now)
ts (time-coerce/from-date timestamp)
diff (t/in-hours (t/interval ts now))]
(println "hour diff:" diff)
(> diff 3)))
(defn update-payout-receipt
"Gets transaction receipt for each confirmed payout and updates payout_hash"
[]
@ -129,30 +139,35 @@
balance-eth :balance_eth
tokens :tokens
confirm-id :confirm-hash
payee-login :payee_login} (db-bounties/confirmed-payouts)]
payee-login :payee_login
updated :updated} (db-bounties/confirmed-payouts)]
(log/debug "confirmed payout:" payout-hash)
(when-let [receipt (eth/get-transaction-receipt payout-hash)]
(if-let [receipt (eth/get-transaction-receipt payout-hash)]
(let [tokens (multisig/token-balances contract-address)
eth-balance (eth/get-balance-wei contract-address)]
(if (or
(some #(> (second %) 0.0) tokens)
(> eth-balance 0))
(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 "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))))))
(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)))))
(defn abs
"(abs n) is the absolute value of n"