etherlite 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc29fa8a2381a001b79627f9b851b9df230fb6d6
4
- data.tar.gz: 99c02011cd1453f7277bdbb3efe0a4c1aff648b9
3
+ metadata.gz: ed20095933d11d8e8d7b9cda7dce06cfb6acd1bf
4
+ data.tar.gz: aebb4e88d292b3c021cebb1b87318fd8b51abd2d
5
5
  SHA512:
6
- metadata.gz: f385968df3f4005adfb1a0b8605c0995848c71934ee2c4e12e9920d8bc60f1d040a568f15fafdc4dc670d199b6b698a8f8ab5f9772d58918b5fd25d0fbcb67c9
7
- data.tar.gz: 74527462ecd791fcf1da3a35115cbda0e0c54b42a2405dd8ca4d5ee2ee92b19407d61a88414f62deb7a6b2a9447b036652065c2b2181252eb5b956612c614056
6
+ metadata.gz: dff58138afca0dd99d83692a218a4e410880c6f07cb6bdf0727ff8603f8ce107a5ec9c5ab7886462e831ef4bbd0dcb16bb9b2f254ea5dd7e9a328f3a345ff79d
7
+ data.tar.gz: d7c84736ea549eb7b3f59c9bf41e5fc493ebb3c37aaad2b25caff2a7c1d6db5ce196c7809eccf5fe1252ef1b1cd8705f6400819b6345f1af9ae54c851d880ce9
data/lib/etherlite.rb CHANGED
@@ -28,6 +28,8 @@ require "etherlite/configuration"
28
28
  require "etherlite/abi"
29
29
  require "etherlite/utils"
30
30
  require "etherlite/connection"
31
+ require "etherlite/transaction"
32
+ require "etherlite/nonce_manager"
31
33
  require "etherlite/pk_account"
32
34
  require "etherlite/account"
33
35
  require "etherlite/address"
@@ -73,11 +73,13 @@ module Etherlite
73
73
  _params[:gasPrice] = Utils.encode_quantity_param(_opt[:gas_price]) if _opt.key? :gas_price
74
74
 
75
75
  passphrase = _opt.fetch(:passphrase, @passphrase)
76
- if passphrase.nil?
77
- @connection.ipc_call(:eth_sendTransaction, _params)
78
- else
79
- @connection.ipc_call(:personal_sendTransaction, _params, passphrase)
80
- end
76
+ tx_hash = if passphrase.nil?
77
+ @connection.ipc_call(:eth_sendTransaction, _params)
78
+ else
79
+ @connection.ipc_call(:personal_sendTransaction, _params, passphrase)
80
+ end
81
+
82
+ Transaction.new @connection, tx_hash
81
83
  end
82
84
  end
83
85
  end
@@ -9,7 +9,7 @@ module Etherlite
9
9
  payload = { jsonrpc: "2.0", method: _method, params: _params, id: id }
10
10
 
11
11
  # TODO: support ipc
12
- Net::HTTP.start(@uri.hostname, @uri.port) do |http|
12
+ Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: use_ssl?) do |http|
13
13
  return handle_response http.post(
14
14
  @uri.path.empty? ? '/' : @uri.path,
15
15
  payload.to_json,
@@ -24,6 +24,10 @@ module Etherlite
24
24
  (Time.now.to_f * 1000.0).to_i
25
25
  end
26
26
 
27
+ def use_ssl?
28
+ @uri.scheme == 'https'
29
+ end
30
+
27
31
  def handle_response(_response, _id)
28
32
  case _response
29
33
  when Net::HTTPSuccess
@@ -0,0 +1,45 @@
1
+ module Etherlite
2
+ class NonceManager
3
+ @@nonce_cache = {}
4
+ @@nonce_mutex = Mutex.new
5
+
6
+ def initialize(_connection)
7
+ @connection = _connection
8
+ end
9
+
10
+ def last_nonce_for(_address)
11
+ _address = Utils.encode_address_param _address
12
+ last_nonce = @@nonce_cache[_address]
13
+ last_nonce = last_observed_nonce_for(_address) if last_nonce.nil?
14
+ last_nonce
15
+ end
16
+
17
+ def with_next_nonce_for(_address)
18
+ _address = Utils.encode_address_param _address
19
+
20
+ @@nonce_mutex.synchronize do
21
+ next_nonce = last_nonce_for(_address) + 1
22
+
23
+ begin
24
+ result = yield next_nonce
25
+ @@nonce_cache[_address] = next_nonce
26
+ return result
27
+ rescue
28
+ # if yield fails, cant be sure about transaction status so must rely again on observing.
29
+ @@nonce_cache.delete _address
30
+ raise
31
+ end
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def last_observed_nonce_for(_address)
38
+ # TODO: support using tx_pool API to improve this:
39
+ # http://qnimate.com/calculating-nonce-for-raw-transactions-in-geth/
40
+ Etherlite::Utils.hex_to_uint(
41
+ @connection.ipc_call(:eth_getTransactionCount, _address, 'pending')
42
+ ) - 1
43
+ end
44
+ end
45
+ end
@@ -52,23 +52,31 @@ module Etherlite
52
52
  end
53
53
 
54
54
  def send_transaction(_value, _hex_data, _hex_address, _opt)
55
- tx = Eth::Tx.new(
56
- value: _value,
57
- data: _hex_data,
58
- gas_limit: _opt.fetch(:gas, 90_000),
59
- gas_price: _opt.fetch(:gas_price, gas_price),
60
- to: _hex_address,
61
- nonce: 0
62
- )
55
+ tx_hash = nonce_manager.with_next_nonce_for(@key.address) do |nonce|
56
+ tx = Eth::Tx.new(
57
+ value: _value,
58
+ data: _hex_data,
59
+ gas_limit: _opt.fetch(:gas, 90_000),
60
+ gas_price: _opt.fetch(:gas_price, gas_price),
61
+ to: _hex_address,
62
+ nonce: nonce
63
+ )
64
+
65
+ tx.sign @key
63
66
 
64
- tx.sign @key
67
+ @connection.ipc_call(:eth_sendRawTransaction, tx.hex)
68
+ end
65
69
 
66
- @connection.ipc_call(:eth_sendRawTransaction, tx.hex)
70
+ Transaction.new @connection, tx_hash
67
71
  end
68
72
 
69
73
  def gas_price
70
74
  # TODO: improve on this
71
75
  @gas_price ||= Etherlite::Utils.hex_to_uint @connection.ipc_call(:eth_gasPrice)
72
76
  end
77
+
78
+ def nonce_manager
79
+ NonceManager.new @connection
80
+ end
73
81
  end
74
82
  end
@@ -0,0 +1,10 @@
1
+ module Etherlite
2
+ class Transaction
3
+ attr_reader :tx_hash
4
+
5
+ def initialize(_connection, _tx_hash)
6
+ @connection = _connection
7
+ @tx_hash = _tx_hash
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Etherlite
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etherlite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ignacio Baixas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-28 00:00:00.000000000 Z
11
+ date: 2017-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: digest-sha3
@@ -175,10 +175,12 @@ files:
175
175
  - lib/etherlite/contract/event_base.rb
176
176
  - lib/etherlite/contract/event_input.rb
177
177
  - lib/etherlite/contract/function.rb
178
+ - lib/etherlite/nonce_manager.rb
178
179
  - lib/etherlite/pk_account.rb
179
180
  - lib/etherlite/railtie.rb
180
181
  - lib/etherlite/railties/configuration_extensions.rb
181
182
  - lib/etherlite/railties/utils.rb
183
+ - lib/etherlite/transaction.rb
182
184
  - lib/etherlite/types/address.rb
183
185
  - lib/etherlite/types/array_base.rb
184
186
  - lib/etherlite/types/array_dynamic.rb