Use random json-rpc request IDs

* use a random 0-4096 int for json-rpc request ID and ensure reponse
  has the same ID (may solve #32))
This commit is contained in:
Teemu Patja 2017-03-10 20:19:35 +02:00
parent 219d697fee
commit 2bd5685638
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
1 changed files with 10 additions and 5 deletions

View File

@ -14,18 +14,23 @@
(defn eth-rpc
[method params]
(let [body (json/write-str {:jsonrpc "2.0"
(let [request-id (rand-int 4096)
body (json/write-str {:jsonrpc "2.0"
:method method
:params params
:id 1})
:id request-id})
options {:headers {"content-type" "application/json"}
:body body}
response (:body @(post eth-rpc-url options))
result (json/read-str response :key-fn keyword)]
(log/debug body "\n" result)
(when-let [error (:error result)]
(log/error "Method: " method ", error: " error))
(:result result)))
(if (= (:id result) request-id)
(:result result)
(do
(log/error "Geth returned an invalid json-rpc request ID,"
"ignoring response")
(when-let [error (:error result)]
(log/error "Method: " method ", error: " error))))))
(defn estimate-gas
[from to value & [params]]