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 01f1bffd77
commit cb3feeeec3
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 (defn eth-rpc
[method params] [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 :method method
:params params :params params
:id 1}) :id request-id})
options {:headers {"content-type" "application/json"} options {:headers {"content-type" "application/json"}
:body body} :body body}
response (:body @(post eth-rpc-url options)) response (:body @(post eth-rpc-url options))
result (json/read-str response :key-fn keyword)] result (json/read-str response :key-fn keyword)]
(log/debug body "\n" result) (log/debug body "\n" result)
(when-let [error (:error result)] (if (= (:id result) request-id)
(log/error "Method: " method ", error: " error)) (:result result)
(: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 (defn estimate-gas
[from to value & [params]] [from to value & [params]]