From 74a2a8060a22421696a1fed5658ab50b2097399b Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Fri, 13 Apr 2018 16:47:24 +0300 Subject: [PATCH] Throw exception if attempting to create tx with the same nonce --- src/clj/commiteth/bounties.clj | 5 +++-- src/clj/commiteth/eth/core.clj | 23 ++++++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/clj/commiteth/bounties.clj b/src/clj/commiteth/bounties.clj index 2be8511..d1ee19d 100644 --- a/src/clj/commiteth/bounties.clj +++ b/src/clj/commiteth/bounties.clj @@ -23,7 +23,7 @@ (defn deploy-contract [owner owner-address repo issue-id issue-number] (if (empty? owner-address) (log/errorf "issue %s: Unable to deploy bounty contract because repo owner has no Ethereum addres" issue-id) - (do + (try (log/infof "issue %s: Deploying contract to %s" issue-id owner-address) (if-let [transaction-hash (multisig/deploy-multisig owner-address)] (do @@ -36,7 +36,8 @@ (log/infof "issue %s: post-deploying-comment response: %s" issue-id resp) (issues/update-comment-id issue-id comment-id)) (issues/update-transaction-hash issue-id transaction-hash)) - (log/errorf "issue %s Failed to deploy contract to %s" issue-id owner-address))))) + (log/errorf "issue %s Failed to deploy contract to %s" issue-id owner-address)) + (catch Exception ex (log/errorf ex "issue %s: deploy-contract exception" issue-id))))) (defn add-bounty-for-issue [repo repo-id issue] (let [{issue-id :id diff --git a/src/clj/commiteth/eth/core.clj b/src/clj/commiteth/eth/core.clj index f70374d..4250153 100644 --- a/src/clj/commiteth/eth/core.clj +++ b/src/clj/commiteth/eth/core.clj @@ -53,18 +53,27 @@ (throw (ex-info "Make sure you provided proper credentials in appropriate resources/config.edn" {:password password :file-path file-path})))))) +(defn get-nonce [] + (let [current-nonce (atom nil)] + (fn [] + (let [nonce (.. (.ethGetTransactionCount (create-web3j) + (env :eth-account) + DefaultBlockParameterName/LATEST) + sendAsync + get + getTransactionCount)] + (if (= nonce @current-nonce) + (throw (Exception. (str "Attempting to create transaction with the same nonce: " nonce))) + (swap! current-nonce (constantly nonce))) + (log/info "Current nonce:" nonce) + nonce)))) + (defn get-signed-tx [gas-price gas-limit to data] "Create a sign a raw transaction. 'From' argument is not needed as it's already encoded in credentials. See https://web3j.readthedocs.io/en/latest/transactions.html#offline-transaction-signing" - (let [web3j (create-web3j) - nonce (.. (.ethGetTransactionCount web3j - (env :eth-account) - DefaultBlockParameterName/LATEST) - sendAsync - get - getTransactionCount) + (let [nonce ((get-nonce)) tx (RawTransaction/createTransaction nonce gas-price