utxoracle 0.0.3 → 0.1.0

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
  SHA256:
3
- metadata.gz: 2ac6ef650a940eb64d180a28f6d556acca9310a42d199fa4bceb59e909cb6b2e
4
- data.tar.gz: '0792d9c0e1867d62fcc507a0574c9338577b8468399d26b34124092525dddd8d'
3
+ metadata.gz: 27c98fbe262713835893d7590648c0e566ac35cf837d5c1b6514961b54c99324
4
+ data.tar.gz: 8cf05e364850de7f330a4c833640d58c69b8a0c048aaec4fbaa8f4dbe54b529c
5
5
  SHA512:
6
- metadata.gz: 528c646c4201da999ca64ef7ea2ddd723a27e4663d7a253a0bc58472af5efc44d17ae28c4482b0bb271c7d87ed4269c165aa7355c6f0364b49cbdf1672b7e4e6
7
- data.tar.gz: a34764d738c7fc67d09d57a9bf142642c2087233fe0fbb9110327ad2153b8d5c737924fab4cb120386f798b4ad4f819102d19f809a41b59b213797f7396c414d
6
+ metadata.gz: 932541e0474fbdbe9515629cfa7f4f4b35a1a04b4f993fc21542b063f46f1273bf66b77cc91c457301af2f81fe95bd782ec0c31bd16d8060b4509f870e1806a6
7
+ data.tar.gz: dcabd03dcf97198240167c5ad9449345340c85f49b39f31e80a77cab3ba4a5c68f02736a8d09c988246af34ea2a35238795a3a7b4b3cd4bcdad38ef4a36310e8
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # This is a copy of bin/run but with timing code.
5
+ # Usage:
6
+ #
7
+ # Basic wall clock measurement:
8
+ # $ ./bin/benchmarks/run aUser aPassword 127.0.0.1 8332 2023-10-01
9
+ #
10
+ # With StackProf measurement:
11
+ # ./bin/benchmarks/run aUser aPassword 127.0.0.1 8332 2023-10-01 true
12
+
13
+ require 'bundler/setup'
14
+ require 'utxoracle'
15
+ require 'benchmark'
16
+
17
+ user = ARGV[0]
18
+ password = ARGV[1]
19
+ ip = ARGV[2]
20
+ port = ARGV[3]
21
+ date = ARGV[4]
22
+ profile = ARGV[5]
23
+
24
+ if profile
25
+ require 'stackprof'
26
+ # See https://github.com/tmm1/stackprof#sampling: for :cpu, :wall, :object
27
+ StackProf.run(mode: :wall, out: 'tmp/stackprof-wall.dump', raw: true) do
28
+ provider = Utxoracle::Node.new(user, password, ip, port)
29
+ oracle = Utxoracle::Oracle.new(provider, log = false)
30
+ puts oracle.price(date)
31
+ end
32
+ else
33
+ time = Benchmark.measure do
34
+ provider = Utxoracle::Node.new(user, password, ip, port)
35
+ oracle = Utxoracle::Oracle.new(provider, log = false)
36
+ puts oracle.price(date)
37
+ end
38
+ puts time
39
+ end
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # This is a copy of bin/run_range but with timing code.
5
+ # Usage:
6
+ #
7
+ # ./bin/benchmarks/run_range aUser aPassword 127.0.0.1 8332 2023-10-01 2023-10-05
8
+ #
9
+ # Note: Too long of a range will crash the program.
10
+ # Bitcoin Core has limits:
11
+ # https://github.com/bitcoin/bitcoin/blob/c0ddd32bf629bb48426b0651def497ca1a78e6b1/src/httpserver.h#L12-L14
12
+ # for threads, queue and timeout.
13
+ #
14
+ # With default settings, 20 days works fine.
15
+ #
16
+ # To run for a full month, I've found the following settings to work fine:
17
+ #
18
+ # rpcworkqueue=32
19
+ # rpcthreads=16
20
+ # rpctimeout=60
21
+ # Database cache 1024 MiB
22
+
23
+ require 'bundler/setup'
24
+ require 'utxoracle'
25
+ require 'benchmark'
26
+
27
+ user = ARGV[0]
28
+ password = ARGV[1]
29
+ ip = ARGV[2]
30
+ port = ARGV[3]
31
+ start_date = ARGV[4]
32
+ end_date = ARGV[5]
33
+
34
+ time = Benchmark.measure do
35
+ provider = Utxoracle::Node.new(user, password, ip, port)
36
+ batch_oracle = Utxoracle::BatchOracle.new(provider)
37
+ batch_oracle.prices(start_date, end_date)
38
+ end
39
+
40
+ puts time.real
data/bin/run CHANGED
@@ -4,24 +4,12 @@
4
4
  require 'bundler/setup'
5
5
  require 'utxoracle'
6
6
 
7
- user = ARGV[0]
8
- password = ARGV[1]
9
- ip = ARGV[2]
10
- port = ARGV[3]
11
- date = ARGV[4]
12
- profile = ARGV[5]
13
-
14
-
15
- if profile
16
- require 'stackprof'
17
- # See https://github.com/tmm1/stackprof#sampling: for :cpu, :wall, :object
18
- StackProf.run(mode: :wall, out: 'tmp/stackprof-wall.dump', raw: true) do
19
- provider = Utxoracle::Node.new(user, password, ip, port)
20
- oracle = Utxoracle::Oracle.new(provider, log = true)
21
- oracle.price(date)
22
- end
23
- else
24
- provider = Utxoracle::Node.new(user, password, ip, port)
25
- oracle = Utxoracle::Oracle.new(provider, log = true)
26
- oracle.price(date)
27
- end
7
+ user = ARGV[0]
8
+ password = ARGV[1]
9
+ ip = ARGV[2]
10
+ port = ARGV[3]
11
+ date = ARGV[4]
12
+
13
+ provider = Utxoracle::Node.new(user, password, ip, port)
14
+ oracle = Utxoracle::Oracle.new(provider, log = true)
15
+ oracle.price(date)
data/bin/run_range ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'utxoracle'
6
+
7
+ user = ARGV[0]
8
+ password = ARGV[1]
9
+ ip = ARGV[2]
10
+ port = ARGV[3]
11
+ start_date = ARGV[4]
12
+ end_date = ARGV[5]
13
+
14
+ provider = Utxoracle::Node.new(user, password, ip, port)
15
+ batch_oracle = Utxoracle::BatchOracle.new(provider)
16
+ batch_oracle.prices(start_date, end_date)
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Utxoracle
4
+ class BatchOracle
5
+ def initialize(provider, log = false)
6
+ @provider = provider
7
+ @log = log
8
+ end
9
+
10
+ def prices(start_date, end_date)
11
+ formatted_start_date = Time.parse start_date.tr('\n', '')
12
+ formatted_end_date = Time.parse end_date.tr('\n', '')
13
+ ret = {}
14
+
15
+ date = formatted_start_date
16
+ threads = []
17
+
18
+ while date < formatted_end_date
19
+ # Bind `date` to thread scope: https://ruby-doc.org/core-2.0.0/Thread.html#method-c-new
20
+ threads << Thread.new(date) do |t_date|
21
+ oracle = Utxoracle::Oracle.new(@provider, log = @log)
22
+ date_str = t_date.to_s[0..9]
23
+ ret[date_str] = oracle.price(date_str)
24
+ end
25
+
26
+ date += Utxoracle::Oracle::SECONDS_PER_DAY
27
+ end
28
+ threads.each(&:join)
29
+
30
+ puts ret
31
+ ret
32
+ end
33
+ end
34
+ end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'time'
2
4
 
3
5
  module Utxoracle
4
6
  class Oracle
5
- SECONDS_IN_A_DAY = 60 * 60 * 24
7
+ SECONDS_PER_DAY = 86_400
6
8
  FOUR_HOURS = 14_400
7
9
  MAINNET_PORT = 8332
8
10
  TESTNET_PORT = 18_332
@@ -51,7 +53,7 @@ module Utxoracle
51
53
  latest_utc_midnight = Time.new(latest_year, latest_month, latest_day).utc
52
54
 
53
55
  latest_time_in_seconds = blockheader['time']
54
- yesterday_seconds = latest_time_in_seconds - SECONDS_IN_A_DAY
56
+ yesterday_seconds = latest_time_in_seconds - SECONDS_PER_DAY
55
57
  latest_price_day = Time.at(yesterday_seconds).utc
56
58
  latest_price_date = latest_price_day.utc.strftime('%Y-%m-%d')
57
59
 
@@ -59,7 +61,7 @@ module Utxoracle
59
61
  price_day_date_utc = @requested_date
60
62
 
61
63
  seconds_since_price_day = latest_time_in_seconds - price_day_seconds
62
- blocks_ago_estimate = (144 * seconds_since_price_day.to_f / SECONDS_IN_A_DAY.to_f).round
64
+ blocks_ago_estimate = (144 * seconds_since_price_day.to_f / SECONDS_PER_DAY.to_f).round
63
65
  price_day_block_estimate = (block_count - blocks_ago_estimate).to_i
64
66
 
65
67
  block_hash_b = @provider.getblockhash(price_day_block_estimate)
@@ -67,7 +69,7 @@ module Utxoracle
67
69
  time_in_seconds = block_header['time']
68
70
 
69
71
  seconds_difference = time_in_seconds - price_day_seconds
70
- block_jump_estimate = (144.0 * seconds_difference / SECONDS_IN_A_DAY).round
72
+ block_jump_estimate = (144.0 * seconds_difference / SECONDS_PER_DAY).round
71
73
 
72
74
  last_estimate = 0
73
75
  last_last_estimate = 0
@@ -81,7 +83,7 @@ module Utxoracle
81
83
 
82
84
  time_in_seconds = block_header['time']
83
85
  seconds_difference = time_in_seconds - price_day_seconds
84
- block_jump_estimate = (144.0 * seconds_difference / SECONDS_IN_A_DAY).round
86
+ block_jump_estimate = (144.0 * seconds_difference / SECONDS_PER_DAY).round
85
87
  end
86
88
 
87
89
  if time_in_seconds > price_day_seconds
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Utxoracle
2
4
  class Provider
3
5
  def init
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Utxoracle
4
- VERSION = '0.0.3'
4
+ VERSION = '0.1.0'
5
5
  end
data/lib/utxoracle.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'utxoracle/version'
4
+ require_relative 'utxoracle/batch_oracle'
4
5
  require_relative 'utxoracle/oracle'
5
6
  require_relative 'utxoracle/providers/mempool'
6
7
  require_relative 'utxoracle/providers/node'
data/utxoracle.gemspec CHANGED
@@ -21,8 +21,8 @@ Gem::Specification.new do |spec|
21
21
  spec.bindir = 'bin'
22
22
  spec.require_path = 'lib'
23
23
 
24
- spec.add_dependency 'typhoeus', '~> 1.4.0'
25
24
  spec.add_dependency 'cleanrpc', '~> 0.0.2'
25
+ spec.add_dependency 'typhoeus', '~> 1.4.0'
26
26
 
27
27
  spec.add_development_dependency 'pry'
28
28
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: utxoracle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Gardner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-06 00:00:00.000000000 Z
11
+ date: 2023-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: typhoeus
14
+ name: cleanrpc
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.4.0
19
+ version: 0.0.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.4.0
26
+ version: 0.0.2
27
27
  - !ruby/object:Gem::Dependency
28
- name: cleanrpc
28
+ name: typhoeus
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.0.2
33
+ version: 1.4.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.0.2
40
+ version: 1.4.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -164,9 +164,13 @@ files:
164
164
  - LICENSE
165
165
  - README.md
166
166
  - Rakefile
167
+ - bin/benchmarks/run
168
+ - bin/benchmarks/run_range
167
169
  - bin/console
168
170
  - bin/run
171
+ - bin/run_range
169
172
  - lib/utxoracle.rb
173
+ - lib/utxoracle/batch_oracle.rb
170
174
  - lib/utxoracle/oracle.rb
171
175
  - lib/utxoracle/provider.rb
172
176
  - lib/utxoracle/providers/mempool.rb
@@ -196,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
200
  - !ruby/object:Gem::Version
197
201
  version: '0'
198
202
  requirements: []
199
- rubygems_version: 3.4.21
203
+ rubygems_version: 3.4.10
200
204
  signing_key:
201
205
  specification_version: 4
202
206
  summary: Offline price oracle for Bitcoin.