From 834a3a221c8f72b178b40ef3ed87e77dfb941c22 Mon Sep 17 00:00:00 2001 From: Mariusz Kulerski Date: Wed, 21 Feb 2018 07:54:03 +0100 Subject: [PATCH 1/2] Include pending transactions in Client#get_nonce Reason: Submitting more than one transaction from the same account before the block is mined resulted with colliding transactions, because of the same nonce. This change takes the nonce from the pending transactions, so it's incrementes as the pool growths before block is mined. Relates to: https://github.com/EthWorks/ethereum.rb/issues/35 --- lib/ethereum/client.rb | 2 +- spec/ethereum/client_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ethereum/client.rb b/lib/ethereum/client.rb index 8a29eca..8606d5e 100644 --- a/lib/ethereum/client.rb +++ b/lib/ethereum/client.rb @@ -73,7 +73,7 @@ def get_chain end def get_nonce(address) - eth_get_transaction_count(address, "latest")["result"].to_i(16) + eth_get_transaction_count(address, "pending")["result"].to_i(16) end diff --git a/spec/ethereum/client_spec.rb b/spec/ethereum/client_spec.rb index ac6444e..fe39c92 100644 --- a/spec/ethereum/client_spec.rb +++ b/spec/ethereum/client_spec.rb @@ -67,7 +67,7 @@ end describe ".get_nonce" do - let (:request) { {"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"],"id":1} } + let (:request) { {"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "pending"],"id":1} } let (:response) { '{"jsonrpc":"2.0","result":"0xd30beea08891180","id":1}' } it "returns chain no" do expect(client).to receive(:send_single).once.with(request.to_json).and_return(response) From 13996ae867605e6c598c45d7ae44059012d80c68 Mon Sep 17 00:00:00 2001 From: Mariusz Kulerski Date: Thu, 22 Feb 2018 09:30:47 +0100 Subject: [PATCH 2/2] Allow to change nonce on contract before sending transaction Reason: It allows a developer to calculate transaction nonce on his own. May be needed when you want to control transactions order or calculate nonce using transaction pool rather than relying on current client algorithm. Relates to: - https://github.com/EthWorks/ethereum.rb/issues/35 - https://github.com/ethereum/go-ethereum/issues/2880 --- lib/ethereum/contract.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ethereum/contract.rb b/lib/ethereum/contract.rb index 35622a1..57783e5 100644 --- a/lib/ethereum/contract.rb +++ b/lib/ethereum/contract.rb @@ -5,7 +5,7 @@ class Contract attr_reader :address attr_accessor :key - attr_accessor :gas_limit, :gas_price + attr_accessor :gas_limit, :gas_price, :nonce attr_accessor :code, :name, :abi, :class_object, :sender, :deployment, :client attr_accessor :events, :functions, :constructor_inputs attr_accessor :call_raw_proxy, :call_proxy, :transact_proxy, :transact_and_wait_proxy @@ -247,7 +247,7 @@ def build extend Forwardable def_delegators :parent, :deploy_payload, :deploy_args, :call_payload, :call_args def_delegators :parent, :signed_deploy, :key, :key= - def_delegators :parent, :gas_limit, :gas_price, :gas_limit=, :gas_price= + def_delegators :parent, :gas_limit, :gas_price, :gas_limit=, :gas_price=, :nonce, :nonce= def_delegators :parent, :abi, :deployment, :events def_delegators :parent, :estimate, :deploy, :deploy_and_wait def_delegators :parent, :address, :address=, :sender, :sender=