From a9e178a2f16950ace1b146dd18b1bed128947f04 Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Mon, 18 Dec 2017 19:31:53 +0200 Subject: [PATCH] Avoid unneeded watch transactions Store tx id for watch smart contract call while it is pending to avoid sending unneeded transactions. --- .../20171218190701-watch-tx-id.down.sql | 0 .../20171218190701-watch-tx-id.up.sql | 2 ++ resources/sql/queries.sql | 18 ++++++++++++- src/clj/commiteth/db/bounties.clj | 10 ++++++++ src/clj/commiteth/scheduler.clj | 25 +++++++++++++++---- 5 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 resources/migrations/20171218190701-watch-tx-id.down.sql create mode 100644 resources/migrations/20171218190701-watch-tx-id.up.sql diff --git a/resources/migrations/20171218190701-watch-tx-id.down.sql b/resources/migrations/20171218190701-watch-tx-id.down.sql new file mode 100644 index 0000000..e69de29 diff --git a/resources/migrations/20171218190701-watch-tx-id.up.sql b/resources/migrations/20171218190701-watch-tx-id.up.sql new file mode 100644 index 0000000..84b7f32 --- /dev/null +++ b/resources/migrations/20171218190701-watch-tx-id.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE "public"."issues" + ADD COLUMN "watch_hash" character varying(128); diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index 228e416..074fd57 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -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 diff --git a/src/clj/commiteth/db/bounties.clj b/src/clj/commiteth/db/bounties.clj index 0032df2..ff1d14e 100644 --- a/src/clj/commiteth/db/bounties.clj +++ b/src/clj/commiteth/db/bounties.clj @@ -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*] diff --git a/src/clj/commiteth/scheduler.clj b/src/clj/commiteth/scheduler.clj index 4f1e26b..cef1792 100644 --- a/src/clj/commiteth/scheduler.clj +++ b/src/clj/commiteth/scheduler.clj @@ -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])