blockchain-lite 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d943d2cc505e7e01dbaf6fb364ca317d55d9563
4
- data.tar.gz: 4ab4b023a57d9ef9794e6024035c6cf6f718afaa
3
+ metadata.gz: b722f05bc1278363432a4645667ea2f1f6beb135
4
+ data.tar.gz: ecb976219a2e0f7b9ff8ad90a8f7ebd60d9ba052
5
5
  SHA512:
6
- metadata.gz: 4d10c42c537839b325d0ab9ebc59bd86238e985f9c549f5119b1b055bd5bf9d70a949ed51dd704082f07974408d40eb887fad1302148819d4fc6f9d63621de76
7
- data.tar.gz: 612428a641e7e40ffab34c1c33f30a5b0e224e441479c68b551da1581c8e845f31d29624967a9c7915afce4ed6ce6f0aa421ae71f8a67924d37ea23349d55f9b
6
+ metadata.gz: d56f16148e7706e848183a94f4a6133f88b26591e34db1c3e024ab4dd0b8e7a5d94d8711101962bfc5108d52de8711ca9fe00dbf418a8bbb218dc01817d78ab9
7
+ data.tar.gz: 861642febb265b139bf25e4da9d305cbabf28be8797a918e9834ddf00204d68fa9cb70df5a302c88052ce550107d4680db44d41ecf0321dc6530ba7277775781
data/README.md CHANGED
@@ -48,21 +48,21 @@ will pretty print (pp) something like:
48
48
  @hash = "edbd4e11e69bc399a9ccd8faaea44fb27410fe8e3023bb9462450a0a9c4caa1b">,
49
49
  #<Block:0x1eec9a0
50
50
  @index = 1,
51
- @timestamp = 2017-09-15 20:52:38,
51
+ @timestamp = 2017-09-15 21:02:38,
52
52
  @transactions_count = 1,
53
53
  @transactions = ["Transaction Data..."],
54
54
  @hash = "eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743",
55
55
  @previous_hash = "edbd4e11e69bc399a9ccd8faaea44fb27410fe8e3023bb9462450a0a9c4caa1b">,
56
56
  #<Block:0x1eec838
57
57
  @index = 2,
58
- @timestamp = 2017-09-15 20:52:38,
58
+ @timestamp = 2017-09-15 21:12:38,
59
59
  @transactions_count = 1,
60
60
  @transactions = ["Transaction Data..."],
61
61
  @hash = "be50017ee4bbcb33844b3dc2b7c4e476d46569b5df5762d14ceba9355f0a85f4",
62
62
  @previous_hash = "eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743">,
63
63
  #<Block:0x1eec6d0
64
64
  @index = 3,
65
- @timestamp = 2017-09-15 20:52:38
65
+ @timestamp = 2017-09-15 21:22:38
66
66
  @transactions_count = 1,
67
67
  @transactions = ["Transaction Data..."],
68
68
  @hash = "5ee2981606328abfe0c3b1171440f0df746c1e1f8b3b56c351727f7da7ae5d8d",
@@ -100,10 +100,8 @@ class Block
100
100
 
101
101
  def calc_hash
102
102
  sha = Digest::SHA256.new
103
- sha.update( @index.to_s +
104
- @timestamp.to_s +
103
+ sha.update( @timestamp.to_s +
105
104
  @transactions.to_s +
106
- @transactions_count.to_s +
107
105
  @previous_hash )
108
106
  sha.hexdigest
109
107
  end
@@ -123,6 +121,7 @@ class Block
123
121
  attr_reader :timestamp
124
122
  attr_reader :transactions_count
125
123
  attr_reader :transactions
124
+ attr_reader :transactions_hash ## merkle_root
126
125
  attr_reader :previous_hash
127
126
  attr_reader :nonce ## proof of work if hash starts with leading zeros (00)
128
127
  attr_reader :hash
@@ -132,6 +131,7 @@ class Block
132
131
  @timestamp = Time.now.utc ## note: use coordinated universal time (utc)
133
132
  @transactions = transactions
134
133
  @transactions_count = transactions.size
134
+ @transactions_hash = MerkleTree.compute_root_for( transactions )
135
135
  @previous_hash = previous_hash
136
136
  @nonce, @hash = compute_hash_with_proof_of_work
137
137
  end
@@ -139,10 +139,8 @@ class Block
139
139
  def calc_hash
140
140
  sha = Digest::SHA256.new
141
141
  sha.update( @nonce.to_s +
142
- @index.to_s +
143
142
  @timestamp.to_s +
144
- @transactions.to_s +
145
- @transactions_count.to_s +
143
+ @transactions_hash +
146
144
  @previous_hash )
147
145
  sha.hexdigest
148
146
  end
data/Rakefile CHANGED
@@ -18,6 +18,7 @@ Hoe.spec 'blockchain-lite' do
18
18
  self.history_file = 'HISTORY.md'
19
19
 
20
20
  self.extra_deps = [
21
+ ['merkletree'],
21
22
  ]
22
23
 
23
24
  self.licenses = ['Public Domain']
@@ -9,6 +9,9 @@ require 'json'
9
9
  require 'uri'
10
10
 
11
11
 
12
+ ## 3rd party libs
13
+ require 'merkletree'
14
+
12
15
 
13
16
  ## our own code
14
17
  require 'blockchain-lite/version' # note: let version always go first
@@ -30,10 +30,8 @@ class Block
30
30
 
31
31
  def calc_hash
32
32
  sha = Digest::SHA256.new
33
- sha.update( @index.to_s +
34
- @timestamp.to_s +
33
+ sha.update( @timestamp.to_s +
35
34
  @transactions.to_s +
36
- @transactions_count.to_s +
37
35
  @previous_hash )
38
36
  sha.hexdigest
39
37
  end
@@ -10,6 +10,7 @@ class Block
10
10
  attr_reader :timestamp
11
11
  attr_reader :transactions_count # use alias - txn_count - why? why not?
12
12
  attr_reader :transactions # use alias - txn - why? why not?
13
+ attr_reader :transactions_hash # use alias - merkle_root - why? why not?
13
14
  attr_reader :previous_hash
14
15
  attr_reader :nonce # ("lucky" number used once) - proof of work if hash starts with leading zeros (00)
15
16
  attr_reader :hash
@@ -21,6 +22,9 @@ class Block
21
22
  @transactions = transactions
22
23
  @transactions_count = transactions.size
23
24
 
25
+ ## todo: add empty array check to merkletree.compute why? why not?
26
+ @transactions_hash = transactions.empty? ? '0' : MerkleTree.compute_root_for( transactions )
27
+
24
28
  @previous_hash = previous_hash
25
29
 
26
30
  ## note: use coordinated universal time (utc)
@@ -68,10 +72,8 @@ private
68
72
  def calc_hash_with_nonce( nonce=0 )
69
73
  sha = Digest::SHA256.new
70
74
  sha.update( nonce.to_s +
71
- @index.to_s +
72
75
  @timestamp.to_s +
73
- @transactions.to_s +
74
- @transactions_count.to_s +
76
+ @transactions_hash +
75
77
  @previous_hash )
76
78
  sha.hexdigest
77
79
  end
@@ -4,7 +4,7 @@
4
4
  module BlockchainLite
5
5
 
6
6
  MAJOR = 1
7
- MINOR = 2
7
+ MINOR = 3
8
8
  PATCH = 0
9
9
  VERSION = [MAJOR,MINOR,PATCH].join('.')
10
10
 
@@ -61,7 +61,10 @@ def test_wrap
61
61
  assert_equal true, b.valid?
62
62
 
63
63
  ## corrupt data in block in chain
64
- b2.instance_eval %{ @transactions=['XXXXXXX'] }
64
+ b2.instance_eval <<RUBY
65
+ @transactions = ['xxxxxx']
66
+ @transactions_hash = MerkleTree.compute_root_for( @transactions )
67
+ RUBY
65
68
 
66
69
  assert_equal true, b.broken?
67
70
  assert_equal false, b.valid?
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blockchain-lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-17 00:00:00.000000000 Z
11
+ date: 2017-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: merkletree
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rdoc
15
29
  requirement: !ruby/object:Gem::Requirement