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 +4 -4
- data/README.md +1 -1
- data/RELEASE_NOTES.md +6 -0
- data/lib/btcruby/proof_of_work.rb +29 -5
- data/lib/btcruby/version.rb +1 -1
- data/spec/proof_of_work_spec.rb +14 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2ed39f95cdd06b5950c9164180c4086592519ef
|
4
|
+
data.tar.gz: 826463f6e12c74f02e122f696251d9c212cf9077
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26f331d4b44fc40c0e211693baca89866176b101600b6dd70860eef7a05bf4db2047c54f7e2a676cb4154683da2d17764b9bfd1c652de805af259f5ab33e9623
|
7
|
+
data.tar.gz: 96817a00e44da817f3a83d91890c24b6ebde7d5079499e1e759303a99df4002067439761b12f34203f76b26d1afc361d3cb2ff873abde04937fa7ff4a7706858
|
data/README.md
CHANGED
data/RELEASE_NOTES.md
CHANGED
@@ -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
|
data/lib/btcruby/version.rb
CHANGED
data/spec/proof_of_work_spec.rb
CHANGED
@@ -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:
|
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-
|
12
|
+
date: 2015-07-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ffi
|