btcruby 1.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64ee5200a38763d90afb6404bd5b5a5f527f144e
4
- data.tar.gz: ebcc849873a50338a4d226bdd111bd6988928689
3
+ metadata.gz: b2ed39f95cdd06b5950c9164180c4086592519ef
4
+ data.tar.gz: 826463f6e12c74f02e122f696251d9c212cf9077
5
5
  SHA512:
6
- metadata.gz: 04c8322fc7e1ef83c2f8b4a03d54ec0741776955e95fe92210abcdc80c81fbf999899713d15389fd0875ec5983f770c4e30dcd5372ecbbeafebd5668b2ad8f50
7
- data.tar.gz: 730d0a4c98735df953eaa01835055df65a1b24296bb17d06848ecbfd3aad2d3baa7b54f4379fcc47d7ec669fe6a896525eec1794e7d0e3d9610603c9613c2fb4
6
+ metadata.gz: 26f331d4b44fc40c0e211693baca89866176b101600b6dd70860eef7a05bf4db2047c54f7e2a676cb4154683da2d17764b9bfd1c652de805af259f5ab33e9623
7
+ data.tar.gz: 96817a00e44da817f3a83d91890c24b6ebde7d5079499e1e759303a99df4002067439761b12f34203f76b26d1afc361d3cb2ff873abde04937fa7ff4a7706858
data/README.md CHANGED
@@ -62,7 +62,7 @@ $ rake
62
62
  ```
63
63
  $ git push origin --tags
64
64
  $ gem build btcruby.gemspec
65
- $ gem push btcruby-<version>.gem
65
+ $ gem push btcruby-VER.gem
66
66
  ```
67
67
 
68
68
  ## Authors
data/RELEASE_NOTES.md CHANGED
@@ -2,6 +2,12 @@
2
2
  BTCRuby Release Notes
3
3
  =====================
4
4
 
5
+ 1.1.1 (July 30, 2015)
6
+ ---------------------
7
+
8
+ * Added work computation from bigint and 256-bit hash.
9
+
10
+
5
11
  1.1 (July 29, 2015)
6
12
  --------------------
7
13
 
@@ -49,6 +49,7 @@ module BTC
49
49
  end
50
50
 
51
51
  # Converts 32-bit compact representation to a 256-bit integer.
52
+ # int32 -> bigint
52
53
  def target_from_bits(bits)
53
54
  exponent = ((bits >> 24) & 0xff)
54
55
  mantissa = bits & 0x7fffff
@@ -59,31 +60,36 @@ module BTC
59
60
  # Computes bits from difficulty.
60
61
  # Could be inaccurate since difficulty is a limited-precision floating-point number.
61
62
  # Default max_target is for Bitcoin mainnet.
63
+ # float -> int32
62
64
  def bits_from_difficulty(difficulty, max_target: MAX_TARGET_MAINNET)
63
65
  bits_from_target(target_from_difficulty(difficulty, max_target: max_target))
64
66
  end
65
67
 
66
- # Computes difficulty from bits.
68
+ # Computes difficulty from bits.
67
69
  # Default max_target is for Bitcoin mainnet.
70
+ # int32 -> float
68
71
  def difficulty_from_bits(bits, max_target: MAX_TARGET_MAINNET)
69
72
  difficulty_from_target(target_from_bits(bits), max_target: max_target)
70
73
  end
71
74
 
72
- # Computes target from difficulty.
75
+ # Computes target from difficulty.
73
76
  # Could be inaccurate since difficulty is a limited-precision floating-point number.
74
77
  # Default max_target is for Bitcoin mainnet.
78
+ # float -> bigint
75
79
  def target_from_difficulty(difficulty, max_target: MAX_TARGET_MAINNET)
76
80
  (max_target / difficulty).round.to_i
77
81
  end
78
-
79
- # Compute relative difficulty from a given target.
82
+
83
+ # Compute relative difficulty from a given target.
80
84
  # E.g. returns 2.5 if target is 2.5 times harder to reach than the max_target.
81
85
  # Default max_target is for Bitcoin mainnet.
86
+ # bigint -> float
82
87
  def difficulty_from_target(target, max_target: MAX_TARGET_MAINNET)
83
88
  (max_target / target.to_f)
84
89
  end
85
90
 
86
91
  # Converts target integer to a binary 32-byte hash.
92
+ # bigint -> hash256
87
93
  def hash_from_target(target)
88
94
  bytes = []
89
95
  while target > 0
@@ -93,7 +99,8 @@ module BTC
93
99
  BTC::Data.data_from_bytes(bytes).ljust(32, "\x00".b)
94
100
  end
95
101
 
96
- # Converts 32-byte hash to target integer (hash is treated as little-endian integer)
102
+ # Converts 32-byte hash to target big integer (hash is treated as little-endian integer)
103
+ # hash256 -> bigint
97
104
  def target_from_hash(hash)
98
105
  target = 0
99
106
  i = 0
@@ -106,5 +113,22 @@ module BTC
106
113
 
107
114
  # TODO: add retargeting calculation routines
108
115
 
116
+ # Compute amount of work expressed as a target
117
+ # Based on `arith_uint256 GetBlockProof(const CBlockIndex& block)` from Bitcoin Core
118
+ # bigint -> bigint
119
+ def work_from_target(target)
120
+ # We need to compute 2**256 / (target+1), but we can't represent 2**256
121
+ # as it's too large for a arith_uint256. However, as 2**256 is at least as large
122
+ # as target+1, it is equal to ((2**256 - target - 1) / (target+1)) + 1,
123
+ # or ~target / (target+1) + 1.
124
+ # In Ruby bigint is signed, so we can't use '~', but we can use 2**256
125
+ return ((2**256 - target - 1) / (target + 1)) + 1
126
+ end
127
+
128
+ # hash256 -> bigint
129
+ def work_from_hash(hash)
130
+ work_from_target(target_from_hash(hash))
131
+ end
132
+
109
133
  end # ProofOfWork
110
134
  end # BTC
@@ -1,3 +1,3 @@
1
1
  module BTC
2
- VERSION = "1.1".freeze
2
+ VERSION = "1.1.1".freeze
3
3
  end
@@ -45,9 +45,22 @@ describe BTC::ProofOfWork do
45
45
  POW.difficulty_from_target(0x0000000007fff800000000000000000000000000000000000000000000000000,
46
46
  max_target: POW::MAX_TARGET_TESTNET).to_i.must_equal 256
47
47
  end
48
-
48
+
49
49
  it "should convert difficulty to target or bits" do
50
50
  POW.target_from_difficulty(1.0).must_equal 0x00000000ffff0000000000000000000000000000000000000000000000000000
51
51
  POW.bits_from_difficulty(39603666252.41841).must_equal 0x181bc330
52
52
  end
53
+
54
+ it "should convert hash to work" do
55
+ POW.work_from_target(0).must_equal 2**256
56
+ POW.work_from_target(1).must_equal 2**255
57
+ POW.work_from_target(2**255).must_equal 1
58
+ POW.work_from_target(2**256).must_equal 0
59
+
60
+ POW.work_from_hash("\x00".b*32).must_equal 2**256
61
+ POW.work_from_hash("\x01".b + "\x00".b*31).must_equal 2**255
62
+ POW.work_from_hash("8000000000000000000000000000000000000000000000000000000000000000".from_hex.reverse).must_equal 1
63
+ POW.work_from_hash("\xff".b*32).must_equal 1
64
+ end
65
+
53
66
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: btcruby
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Andreev
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-29 00:00:00.000000000 Z
12
+ date: 2015-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi