@@ -837,6 +837,129 @@ All in order? Yes. The blockchain is (almost) unbreakable.
837
837
838
838
839
839
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
+
840
963
841
964
842
965
0 commit comments