forked from rubycocos/blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.rb
64 lines (49 loc) · 1.66 KB
/
block.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# encoding: utf-8
module BlockchainLite
module ProofOfWork
class Block
attr_reader :index
attr_reader :timestamp
attr_reader :data
attr_reader :previous_hash
attr_reader :nonce ## proof of work if hash starts with leading zeros (00)
attr_reader :hash
def initialize(index, data, previous_hash)
@index = index
@timestamp = Time.now.utc ## note: use coordinated universal time (utc)
@data = data
@previous_hash = previous_hash
@nonce, @hash = compute_hash_with_proof_of_work
end
def calc_hash
sha = Digest::SHA256.new
sha.update( @nonce.to_s + @index.to_s + @timestamp.to_s + @data + @previous_hash )
sha.hexdigest
end
def self.first( data='Genesis' ) # create genesis (big bang! first) block
## uses index zero (0) and arbitrary previous_hash ('0')
Block.new( 0, data, '0' )
end
def self.next( previous, data='Transaction Data...' )
Block.new( previous.index+1, data, previous.hash )
end
private
def compute_hash_with_proof_of_work( difficulty='00' )
nonce = 0
loop do
hash = calc_hash_with_nonce( nonce )
if hash.start_with?( difficulty )
return [nonce,hash] ## bingo! proof of work if hash starts with leading zeros (00)
else
nonce += 1 ## keep trying (and trying and trying)
end
end
end
def calc_hash_with_nonce( nonce=0 )
sha = Digest::SHA256.new
sha.update( nonce.to_s + @index.to_s + @timestamp.to_s + @data + @previous_hash )
sha.hexdigest
end
end # class Block
end ## module ProofOfWork
end ## module BlockchainLite