Avoid unneeded watch transactions

Store tx id for watch smart contract call while it is pending to avoid
sending unneeded transactions.
This commit is contained in:
Teemu Patja 2017-12-18 19:31:53 +02:00
parent 62b4394c14
commit a9e178a2f1
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
5 changed files with 49 additions and 6 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE "public"."issues"
ADD COLUMN "watch_hash" character varying(128);

View File

@ -339,6 +339,21 @@ UPDATE issues
SET winner_login = :winner_login
WHERE issue_id = :issue_id;
-- :name update-watch-hash :! :n
-- :doc updates issue with watch transaction hash
UPDATE issues
SET watch_hash = :watch_hash
WHERE issue_id = :issue_id;
-- :name pending-watch-calls :? :*
-- :doc issues with a pending watch transaction
SELECT
issue_id,
watch_hash
FROM issues
WHERE watch_hash IS NOT NULL;
-- :name update-payout-hash :! :n
-- :doc updates issue with payout transaction hash
UPDATE issues
@ -492,7 +507,8 @@ SELECT
i.issue_id AS issue_id,
i.balance_eth AS balance_eth,
i.tokens AS tokens,
i.value_usd AS value_usd
i.value_usd AS value_usd,
i.watch_hash AS watch_hash
FROM issues i, repositories r
WHERE r.repo_id = i.repo_id
AND contract_address IS NOT NULL

View File

@ -55,6 +55,16 @@
(jdbc/with-db-connection [con-db *db*]
(db/update-winner-login con-db {:issue_id issue-id :winner_login login})))
(defn update-watch-hash
[issue-id watch-hash]
(jdbc/with-db-connection [con-db *db*]
(db/update-watch-hash con-db {:issue_id issue-id :watch_hash watch-hash})))
(defn pending-watch-calls
[]
(jdbc/with-db-connection [con-db *db*]
(db/pending-watch-calls con-db)))
(defn update-payout-hash
[issue-id payout-hash]
(jdbc/with-db-connection [con-db *db*]

View File

@ -129,6 +129,16 @@
(db-bounties/update-confirm-hash issue-id confirm-hash)))))
(defn update-watch-hash
"Sets watch-hash to NULL for bounties where watch tx has been mined. Used to avoid unneeded watch transactions in update-bounty-token-balances"
[]
(doseq [{issue-id :issue_id
watch-hash :watch_hash} (db-bounties/pending-watch-calls)]
(log/info "pending watch call" watch-hash)
(when-let [receipt (eth/get-transaction-receipt watch-hash)]
(db-bounties/update-watch-hash issue-id nil))))
(defn older-than-3h?
[timestamp]
(let [now (t/now)
@ -198,23 +208,27 @@
(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."
[bounty-addr]
[issue-id bounty-addr watch-hash]
(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 (not= balance internal-balance)
(when (and (nil? watch-hash)
(not= balance internal-balance))
(log/info "balances not in sync, calling watch")
(multisig/watch-token bounty-addr tla))))))))
(let [hash (multisig/watch-token bounty-addr tla)]
(db-bounties/update-watch-hash issue-id hash)))))))))
(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."
[]
(doseq [{bounty-address :contract_address}
(doseq [{issue-id :issue_id
bounty-address :contract_address
watch-hash :watch_hash}
(db-bounties/open-bounty-contracts)]
(update-bounty-token-balances bounty-address)))
(update-bounty-token-balances issue-id bounty-address watch-hash)))
(defn get-bounty-funds
@ -313,6 +327,7 @@
update-issue-contract-address
update-confirm-hash
update-payout-receipt
update-watch-hash
self-sign-bounty
update-contract-internal-balances
update-balances])