Merge branch 'develop' into fix/nonce-increment

This commit is contained in:
Vitaliy Vlasov 2018-04-19 16:05:30 +03:00 committed by GitHub
commit 8c6b15d4c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 78 deletions

View File

@ -25,7 +25,8 @@
(log/errorf "issue %s: Unable to deploy bounty contract because repo owner has no Ethereum addres" issue-id) (log/errorf "issue %s: Unable to deploy bounty contract because repo owner has no Ethereum addres" issue-id)
(try (try
(log/infof "issue %s: Deploying contract to %s" issue-id owner-address) (log/infof "issue %s: Deploying contract to %s" issue-id owner-address)
(if-let [transaction-hash (multisig/deploy-multisig owner-address)] (if-let [transaction-hash (multisig/deploy-multisig {:owner owner-address
:internal-tx-id (str "contract-github-issue-" issue-id)})]
(do (do
(log/infof "issue %s: Contract deployed, transaction-hash: %s" issue-id transaction-hash) (log/infof "issue %s: Contract deployed, transaction-hash: %s" issue-id transaction-hash)
(let [resp (github/post-deploying-comment owner (let [resp (github/post-deploying-comment owner

View File

@ -28,7 +28,9 @@
(defn auto-gas-price? [] (env :auto-gas-price false)) (defn auto-gas-price? [] (env :auto-gas-price false))
(defn offline-signing? [] (env :offline-signing true)) (defn offline-signing? [] (env :offline-signing true))
(def web3j-obj (atom nil)) (def web3j-obj
(delay (Web3j/build (HttpService. (eth-rpc-url)))))
(def creds-obj (atom nil)) (def creds-obj (atom nil))
(defn wallet-file-path [] (defn wallet-file-path []
@ -37,10 +39,6 @@
(defn wallet-password [] (defn wallet-password []
(env :eth-password)) (env :eth-password))
(defn create-web3j []
(or @web3j-obj
(swap! web3j-obj (constantly (Web3j/build (HttpService. (eth-rpc-url)))))))
(defn creds [] (defn creds []
(or @creds-obj (or @creds-obj
(let [password (wallet-password) (let [password (wallet-password)
@ -53,6 +51,7 @@
(throw (ex-info "Make sure you provided proper credentials in appropriate resources/config.edn" (throw (ex-info "Make sure you provided proper credentials in appropriate resources/config.edn"
{:password password :file-path file-path})))))) {:password password :file-path file-path}))))))
(def highest-nonce (atom nil)) (def highest-nonce (atom nil))
(def nonces-dropped (atom (clojure.lang.PersistentQueue/EMPTY))) (def nonces-dropped (atom (clojure.lang.PersistentQueue/EMPTY)))
@ -90,6 +89,7 @@
nonce gas-price gas-limit data) nonce gas-price gas-limit data)
hex-string)) hex-string))
(defn eth-gasstation-gas-price (defn eth-gasstation-gas-price
"Get max of average and average_calc from gas station and use that "Get max of average and average_calc from gas station and use that
as gas price. average_calc is computed from a larger time period than average, as gas price. average_calc is computed from a larger time period than average,
@ -130,12 +130,7 @@
(try (try
(json/read-str s :key-fn keyword) (json/read-str s :key-fn keyword)
(catch Exception ex (catch Exception ex
(do (log/error "Exception when parsing json string:" (log/error ex "Exception when parsing json string:" s)))))
s
"message:"
ex)
nil)))))
(def req-id-tracker (def req-id-tracker
;; HACK to ensure previous random-number approach doesn't lead to ;; HACK to ensure previous random-number approach doesn't lead to
@ -143,7 +138,8 @@
(atom 0)) (atom 0))
(defn eth-rpc (defn eth-rpc
[method params] [{:keys [method params internal-tx-id]}]
{:pre [(string? method) (some? params)]}
(let [request-id (swap! req-id-tracker inc) (let [request-id (swap! req-id-tracker inc)
body {:jsonrpc "2.0" body {:jsonrpc "2.0"
:method method :method method
@ -153,9 +149,12 @@
:body (json/write-str body)} :body (json/write-str body)}
response @(post (eth-rpc-url) options) response @(post (eth-rpc-url) options)
result (safe-read-str (:body response))] result (safe-read-str (:body response))]
(log/infof "eth-rpc req(%s) body: %s\neth-rpc req(%s) result: %s" (when internal-tx-id
request-id body request-id result) (log/infof "%s: eth-rpc %s" internal-tx-id method))
(log/debugf "%s: eth-rpc req(%s) body: %s" internal-tx-id request-id body)
(if internal-tx-id
(log/infof "%s: eth-rpc req(%s) result: %s" internal-tx-id request-id result)
(log/debugf "no-tx-id: eth-rpc req(%s) result: %s" request-id result))
(cond (cond
;; Ignore any responses that have mismatching request ID ;; Ignore any responses that have mismatching request ID
(not= (:id result) request-id) (not= (:id result) request-id)
@ -163,7 +162,10 @@
;; If request ID matches but contains error, throw ;; If request ID matches but contains error, throw
(:error result) (:error result)
(throw (ex-info "Error submitting transaction via eth-rpc" (:error result))) (throw
(ex-info (format "%s: Error submitting transaction via eth-rpc %s"
(or internal-tx-id "(no-tx-id)") (:error result))
(:error result)))
:else :else
(:result result)))) (:result result))))
@ -200,9 +202,10 @@
(defn estimate-gas (defn estimate-gas
[from to value & [params]] [from to value & [params]]
(let [geth-estimate (eth-rpc (let [geth-estimate (eth-rpc
"eth_estimateGas" [(merge params {:from from {:method "eth_estimateGas"
:to to :params [(merge params {:from from
:value value})]) :to to
:value value})]})
adjusted-gas (adjust-gas-estimate geth-estimate)] adjusted-gas (adjust-gas-estimate geth-estimate)]
(log/debug "estimated gas (geth):" geth-estimate) (log/debug "estimated gas (geth):" geth-estimate)
@ -225,7 +228,8 @@
(defn get-balance-hex (defn get-balance-hex
[account] [account]
(eth-rpc "eth_getBalance" [account "latest"])) (eth-rpc {:method "eth_getBalance"
:params [account "latest"]}))
(defn get-balance-wei (defn get-balance-wei
[account] [account]
@ -244,7 +248,8 @@
(defn get-transaction-receipt (defn get-transaction-receipt
[hash] [hash]
(eth-rpc "eth_getTransactionReceipt" [hash])) (eth-rpc {:method "eth_getTransactionReceipt"
:params [hash]}))
(defn format-call-params (defn format-call-params
[method-id & params] [method-id & params]
@ -254,10 +259,12 @@
(defn call (defn call
[contract method-id & params] [contract method-id & params]
(let [data (apply format-call-params method-id params)] (let [data (apply format-call-params method-id params)]
(eth-rpc "eth_call" [{:to contract :data data} "latest"]))) (eth-rpc {:method "eth_call"
:params [{:to contract :data data} "latest"]})))
(defn execute (defn execute
[from contract method-id gas-limit & params] [{:keys [from contract method-id gas-limit params internal-tx-id]}]
{:pre [(string? method-id)]}
(let [data (apply format-call-params method-id params) (let [data (apply format-call-params method-id params)
gas-price (gas-price) gas-price (gas-price)
value (format "0x%x" 0) value (format "0x%x" 0)
@ -268,6 +275,7 @@
(merge {:gasPrice (integer->hex gas-price)}) (merge {:gasPrice (integer->hex gas-price)})
contract contract
(merge {:to contract})) (merge {:to contract}))
gas (if gas-limit gas-limit gas (if gas-limit gas-limit
(estimate-gas from contract value params)) (estimate-gas from contract value params))
nonce (when (offline-signing?) (get-nonce)) nonce (when (offline-signing?) (get-nonce))
@ -281,14 +289,16 @@
(if (offline-signing?) (if (offline-signing?)
(try (try
(eth-rpc (eth-rpc
"eth_sendRawTransaction" {:method "eth_sendRawTransaction"
[params]) :params [params]
:internal-tx-id internal-tx-id})
(catch Throwable ex (catch Throwable ex
(swap! nonces-dropped conj nonce) (swap! nonces-dropped conj nonce)
(throw ex))) (throw ex)))
(eth-rpc (eth-rpc
"personal_sendTransaction" {:method "personal_sendTransaction"
[params (eth-password)])))) :params [params (eth-password)]
:internal-tx-id internal-tx-id}))))
(defn hex-ch->num (defn hex-ch->num
[ch] [ch]
@ -349,7 +359,6 @@
eth-core eth-core
:start :start
(do (do
(swap! web3j-obj (constantly nil))
(swap! creds-obj (constantly nil)) (swap! creds-obj (constantly nil))
(log/info "eth/core started")) (log/info "eth/core started"))
:stop :stop

View File

@ -1,6 +1,5 @@
(ns commiteth.eth.multisig-wallet (ns commiteth.eth.multisig-wallet
(:require [commiteth.eth.core :as eth (:require [commiteth.eth.core :as eth]
:refer [create-web3j creds]]
[commiteth.config :refer [env]] [commiteth.config :refer [env]]
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[taoensso.tufte :as tufte :refer (defnp p profiled profile)] [taoensso.tufte :as tufte :refer (defnp p profiled profile)]
@ -36,24 +35,19 @@
[] []
(env :tokenreg-base-format :status)) (env :tokenreg-base-format :status))
(defn create-new
[owner1 owner2 required]
(eth/execute (eth/eth-account)
(factory-contract-addr)
(:create method-ids)
(eth/integer->hex 865000) ;; gas-limit
0x40
0x2
required
owner1
owner2))
(defn deploy-multisig (defn deploy-multisig
"Deploy a new multisig contract to the blockchain with commiteth bot "Deploy a new multisig contract to the blockchain with commiteth bot
and given owner as owners." and given owner as owners.
[owner] `internal-tx-id` is used to identify what issue this multisig is deployed
(create-new (eth/eth-account) owner 2)) for and manage nonces at a later point in time."
[{:keys [owner internal-tx-id]}]
{:pre [(string? owner) (string? internal-tx-id)]}
(eth/execute {:internal-tx-id internal-tx-id
:from (eth/eth-account)
:contract (factory-contract-addr)
:method-id (:create method-ids)
:gas-limit (eth/integer->hex 865000)
:params [0x40 0x2 2 (eth/eth-account) owner]}))
(defn find-event-in-tx-receipt [tx-receipt topic-id] (defn find-event-in-tx-receipt [tx-receipt topic-id]
(let [logs (:logs tx-receipt) (let [logs (:logs tx-receipt)
@ -92,20 +86,18 @@
(defn send-all (defn send-all
[contract to] [{:keys [contract payout-address internal-tx-id]}]
(log/debug "multisig/send-all " contract to) {:pre [(string? contract) (string? payout-address) (string? internal-tx-id)]}
(log/debug "multisig/send-all " contract payout-address internal-tx-id)
(let [params (eth/format-call-params (let [params (eth/format-call-params
(:withdraw-everything method-ids) (:withdraw-everything method-ids)
to)] payout-address)]
(eth/execute (eth/eth-account) (eth/execute {:internal-tx-id internal-tx-id
contract :from (eth/eth-account)
(:submit-transaction method-ids) :contract contract
nil :method-id (:submit-transaction method-ids)
contract :gas-limit nil
0 :params [contract 0 "0x60" "0x24" params]})))
"0x60" ;; TODO: document these
"0x24" ;; or refactor out
params)))
(defn get-token-address [token] (defn get-token-address [token]
@ -118,17 +110,17 @@
(log/debug "multisig/watch-token" bounty-addr token) (log/debug "multisig/watch-token" bounty-addr token)
(let [token-address (get-token-address token)] (let [token-address (get-token-address token)]
(assert token-address) (assert token-address)
(eth/execute (eth/eth-account) (eth/execute {:internal-tx-id (str "watch-token-" (System/currentTimeMillis) "-" bounty-addr)
bounty-addr :from (eth/eth-account)
(:watch method-ids) :contract bounty-addr
nil :method-id (:watch method-ids)
token-address))) :gas-limit nil
:params [token-address]})))
(defn load-bounty-contract [addr] (defn load-bounty-contract [addr]
(MultiSigTokenWallet/load addr (MultiSigTokenWallet/load addr
(create-web3j) @eth/web3j-obj
(creds) (eth/creds)
(eth/gas-price) (eth/gas-price)
(BigInteger/valueOf 500000))) (BigInteger/valueOf 500000)))

View File

@ -1,6 +1,5 @@
(ns commiteth.eth.token-registry (ns commiteth.eth.token-registry
(:require [commiteth.eth.core :as eth (:require [commiteth.eth.core :as eth]
:refer [create-web3j creds]]
[commiteth.config :refer [env]] [commiteth.config :refer [env]]
[clojure.tools.logging :as log]) [clojure.tools.logging :as log])
(:import [org.web3j (:import [org.web3j
@ -23,8 +22,8 @@
(defn- load-tokenreg-contract [addr] (defn- load-tokenreg-contract [addr]
(TokenReg/load addr (TokenReg/load addr
(create-web3j) @eth/web3j-obj
(creds) (eth/creds)
(eth/gas-price) (eth/gas-price)
(BigInteger/valueOf 21000))) (BigInteger/valueOf 21000)))
@ -59,9 +58,8 @@
(defn deploy-parity-tokenreg (defn deploy-parity-tokenreg
"Deploy an instance of parity token-registry to current network" "Deploy an instance of parity token-registry to current network"
[] []
(let [web3j (create-web3j)] (TokenReg/deploy @eth/web3j-obj
(TokenReg/deploy web3j (eth/creds)
(creds) (eth/gas-price)
(eth/gas-price) (BigInteger/valueOf 4000000) ;; gas limit
(BigInteger/valueOf 4000000) ;; gas limit BigInteger/ZERO))
BigInteger/ZERO)))

View File

@ -126,7 +126,9 @@
tokens tokens
winner-login winner-login
true)) true))
(let [execute-hash (multisig/send-all contract-address payout-address)] (let [execute-hash (multisig/send-all {:contract contract-address
:payout-address payout-address
:internal-tx-id (str "payout-github-issue-" issue-id)})]
(log/infof "issue %s: Payout self-signed, called sign-all(%s) tx: %s" issue-id contract-address payout-address execute-hash) (log/infof "issue %s: Payout self-signed, called sign-all(%s) tx: %s" issue-id contract-address payout-address execute-hash)
(db-bounties/update-execute-hash issue-id execute-hash) (db-bounties/update-execute-hash issue-id execute-hash)
(db-bounties/update-winner-login issue-id winner-login) (db-bounties/update-winner-login issue-id winner-login)