Skip to content

Commit 8360bbf

Browse files
committed
add hashrate
1 parent f34316b commit 8360bbf

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

README.md

+123
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,129 @@ All in order? Yes. The blockchain is (almost) unbreakable.
837837

838838

839839

840+
## Mining, Mining, Mining
841+
842+
What's your hash rate? Let's find out.
843+
Let's use a "stand-alone" version of the by now "classic" `compute_hash_with_proof_of_work`
844+
function:
845+
846+
``` ruby
847+
def compute_hash_with_proof_of_work( data, difficulty='00' )
848+
nonce = 0
849+
loop do
850+
hash = Digest::SHA256.hexdigest( "#{nonce}#{data}" )
851+
if hash.start_with?( difficulty )
852+
return [nonce,hash] ## bingo! proof of work if hash starts with leading zeros (00)
853+
else
854+
nonce += 1 ## keep trying (and trying and trying)
855+
end
856+
end # loop
857+
end # method compute_hash_with_proof_of_work
858+
```
859+
860+
Let's try (run) benchmarks for the difficulty from `0` (4 bits)
861+
to `0000000` (28 bits).
862+
Remember: `0` in hex (base16, 2^4 bits) equals `0000` in binary (base2),
863+
thus, `0000000` in hex (base16) equals `0` x 4 x 7 = 28 zero bits
864+
in binary (base2). Example:
865+
866+
``` ruby
867+
(1..7).each do |factor|
868+
difficulty = '0' * factor
869+
puts "difficulty: #{difficulty} (#{difficulty.length*4} bits)"
870+
end
871+
872+
# => difficulty: 0 (4 bits)
873+
# difficulty: 00 (8 bits)
874+
# difficulty: 000 (12 bits)
875+
# difficulty: 0000 (16 bits)
876+
# difficulty: 00000 (20 bits)
877+
# difficulty: 000000 (24 bits)
878+
# difficulty: 0000000 (28 bits)
879+
```
880+
881+
Let's add the hash proof-of-work hash computing
882+
machinery and re(run):
883+
884+
``` ruby
885+
(1..7).each do |factor|
886+
difficulty = '0' * factor
887+
puts "Difficulty: #{difficulty} (#{difficulty.length*4} bits)"
888+
889+
puts "Starting search..."
890+
t1 = Time.now
891+
nonce, hash = compute_hash_with_proof_of_work( 'Hello, Cryptos!', difficulty )
892+
t2 = Time.now
893+
894+
delta = t2 - t1
895+
puts "Elapsed Time: %.4f seconds, Hashes Calculated: %d" % [delta,nonce]
896+
897+
if delta > 0
898+
hashrate = Float( nonce / delta )
899+
puts "Hash Rate: %d hashes per second" % hashrate
900+
end
901+
puts
902+
end
903+
```
904+
905+
Resulting on a "low-end" home computer:
906+
907+
```
908+
Difficulty: 0 (4 bits)
909+
Starting search...
910+
Elapsed Time: 0.0156 seconds, Hashes Calculated: 56
911+
Hash Rate: 3 588 hashes per second
912+
913+
Difficulty: 00 (8 bits)
914+
Starting search...
915+
Elapsed Time: 0.0000 seconds, Hashes Calculated: 143
916+
Hash Rate: Infinity ;-)
917+
918+
Difficulty: 000 (12 bits)
919+
Starting search...
920+
Elapsed Time: 0.0313 seconds, Hashes Calculated: 3 834
921+
Hash Rate: 122 684 hashes per second
922+
923+
Difficulty: 0000 (16 bits)
924+
Starting search...
925+
Elapsed Time: 0.2656 seconds, Hashes Calculated: 26 762
926+
Hash Rate: 100 753 hashes per second
927+
928+
Difficulty: 00000 (20 bits)
929+
Starting search...
930+
Elapsed Time: 1.2031 seconds, Hashes Calculated: 118 592
931+
Hash Rate: 98 569 hashes per second
932+
933+
Difficulty: 000000 (24 bits)
934+
Starting search...
935+
Elapsed Time: 220.5767 seconds, Hashes Calculated: 21 554 046
936+
Hash Rate: 97 716 hashes per second
937+
938+
Difficulty: 0000000 (28 bits)
939+
Starting search...
940+
```
941+
942+
To sum up the hash rate is about 100 000 hashes per second
943+
on a "low-end" home computer. What's your hash rate?
944+
Run the benchmark on your machinery!
945+
946+
The search for the 28 bits difficulty proof-of-work hash
947+
is still running... expected to find the lucky number in the next hours...
948+
949+
950+
Trivia Quiz: What's the Hash Rate of the Bitcoin Classic Network?
951+
952+
A: About 25 000 trillions of hashes per second (in March 2018)
953+
954+
Estimated number of tera hashes per second (trillions of hashes per second)
955+
the Bitcoin network is performing.
956+
957+
![](i/bitcoin-hashrate.png)
958+
959+
(Source: [blockchain.info](https://blockchain.info/charts/hash-rate))
960+
961+
962+
840963

841964

842965

hashrate.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'digest'
2+
require 'pp'
3+
4+
5+
def compute_hash_with_proof_of_work( data, difficulty='00' )
6+
nonce = 0
7+
loop do
8+
hash = Digest::SHA256.hexdigest( "#{nonce}#{data}" )
9+
if hash.start_with?( difficulty )
10+
return [nonce,hash] ## bingo! proof of work if hash starts with leading zeros (00)
11+
else
12+
nonce += 1 ## keep trying (and trying and trying)
13+
end
14+
end # loop
15+
end # method compute_hash_with_proof_of_work
16+
17+
18+
(1..7).each do |factor|
19+
difficulty = '0' * factor
20+
puts "Difficulty: #{difficulty} (#{difficulty.length*4} bits)"
21+
end
22+
23+
24+
(1..6).each do |factor|
25+
difficulty = '0' * factor
26+
puts "Difficulty: #{difficulty} (#{difficulty.length*4} bits)"
27+
28+
puts "Starting search..."
29+
t1 = Time.now
30+
nonce, hash = compute_hash_with_proof_of_work( 'Hello, Cryptos!', difficulty )
31+
t2 = Time.now
32+
33+
delta = t2 - t1
34+
puts "Elapsed Time: %.4f seconds, Hashes Calculated: %d" % [delta,nonce]
35+
36+
if delta > 0
37+
hashrate = Float( nonce / delta )
38+
puts "Hash Rate: %d hashes per second" % hashrate
39+
end
40+
puts
41+
end

i/bitcoin-hashrate.png

42.2 KB
Loading

0 commit comments

Comments
 (0)