blockchain-lite 1.3.1 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Manifest.txt +2 -1
- data/README.md +10 -5
- data/lib/blockchain-lite.rb +11 -29
- data/lib/blockchain-lite/base.rb +28 -0
- data/lib/blockchain-lite/blockchain.rb +28 -41
- data/lib/blockchain-lite/version.rb +2 -2
- data/test/helper.rb +19 -1
- data/test/test_block.rb +21 -27
- data/test/test_block_basic.rb +8 -7
- data/test/test_block_proof_of_work.rb +8 -7
- data/test/test_blockchain.rb +8 -14
- data/test/test_version.rb +21 -0
- metadata +4 -3
- data/lib/blockchain-lite/block.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fb07dad5bc09c884d56005cca1811efa4b97885
|
4
|
+
data.tar.gz: 3ec43658943f4e9890d627effdbd1944abeea133
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52a6008f77adf15d70f0f8a71b41c880fc1015eb5144cf849b829337f6ab4dc67afeacd2b0ecbdf3b4e47e4adacbb257c2af857c831052dbe13cf9e7b00e74cf
|
7
|
+
data.tar.gz: 8ac88194f2f5791d83326f2c3fd4bdf8d71af6f9452fcffea13861cc954ef0db777c8f9cf902543facc6acbad56a28e3b537f99630fd521f8be30824abcb6e39
|
data/Manifest.txt
CHANGED
@@ -4,9 +4,9 @@ Manifest.txt
|
|
4
4
|
README.md
|
5
5
|
Rakefile
|
6
6
|
lib/blockchain-lite.rb
|
7
|
+
lib/blockchain-lite/base.rb
|
7
8
|
lib/blockchain-lite/basic/block.rb
|
8
9
|
lib/blockchain-lite/bitcoin/block.rb
|
9
|
-
lib/blockchain-lite/block.rb
|
10
10
|
lib/blockchain-lite/blockchain.rb
|
11
11
|
lib/blockchain-lite/proof_of_work/block.rb
|
12
12
|
lib/blockchain-lite/version.rb
|
@@ -15,3 +15,4 @@ test/test_block.rb
|
|
15
15
|
test/test_block_basic.rb
|
16
16
|
test/test_block_proof_of_work.rb
|
17
17
|
test/test_blockchain.rb
|
18
|
+
test/test_version.rb
|
data/README.md
CHANGED
@@ -281,13 +281,18 @@ resulting in:
|
|
281
281
|
```
|
282
282
|
|
283
283
|
|
284
|
-
##
|
284
|
+
## Blockchain Lite in the Real World
|
285
285
|
|
286
|
-
|
286
|
+
- [**centralbank**](https://github.com/openblockchains/centralbank) - command line tool (and core library) - print your own money / cryptocurrency; run your own federated central bank nodes on the blockchain peer-to-peer over HTTP; revolutionize the world one block at a time
|
287
|
+
- [**tulipmania**](https://github.com/openblockchains/tulipmania) - command line tool (and core library) - tulips on the blockchain; learn by example from the real world (anno 1637) - buy! sell! hodl! enjoy the beauty of admiral of admirals, semper augustus, and more; run your own hyper ledger tulip exchange nodes on the blockchain peer-to-peer over HTTP; revolutionize the world one block at a time
|
288
|
+
- [**shilling**](https://github.com/bitshilling/bitshilling.tools) - command line tool (and core library) - shilling (or schilling) on the blockchain! rock-solid alpine dollar from austria; print (mine) your own shillings; run your own federated shilling central bank nodes w/ public distributed (hyper) ledger book on the blockchain peer-to-peer over HTTP; revolutionize the world one block at a time
|
289
|
+
- You? Add your tool / service
|
290
|
+
|
291
|
+
|
292
|
+
## References
|
293
|
+
|
294
|
+
[**Programming Cryptocurrencies and Blockchains (in Ruby)**](http://yukimotopress.github.io/blockchains) by Gerald Bauer et al, 2018, Yuki & Moto Press
|
287
295
|
|
288
|
-
```
|
289
|
-
$ gem install blockchain-lite
|
290
|
-
```
|
291
296
|
|
292
297
|
|
293
298
|
## License
|
data/lib/blockchain-lite.rb
CHANGED
@@ -1,29 +1,11 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
## 3rd party libs
|
13
|
-
require 'merkletree'
|
14
|
-
|
15
|
-
|
16
|
-
## our own code
|
17
|
-
require 'blockchain-lite/version' # note: let version always go first
|
18
|
-
|
19
|
-
require 'blockchain-lite/basic/block'
|
20
|
-
require 'blockchain-lite/proof_of_work/block'
|
21
|
-
|
22
|
-
require 'blockchain-lite/blockchain'
|
23
|
-
|
24
|
-
require 'blockchain-lite/block' ## configure "standard" default block (e.g. basic, proof-of-work, etc.)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
# say hello
|
29
|
-
puts BlockchainLite.banner if defined?($RUBYLIBS_DEBUG) && $RUBYLIBS_DEBUG
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
|
4
|
+
## our own code (without "top-level" shortcuts e.g. "modular version")
|
5
|
+
require 'blockchain-lite/base'
|
6
|
+
|
7
|
+
###
|
8
|
+
# add convenience top-level shortcut / alias
|
9
|
+
# "standard" default block for now block with proof of work
|
10
|
+
Block = BlockchainLite::ProofOfWork::Block
|
11
|
+
Blockchain = BlockchainLite::Blockchain
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'digest' # for hash checksum digest function SHA256
|
4
|
+
require 'pp' # for pp => pretty printer
|
5
|
+
|
6
|
+
require 'forwardable'
|
7
|
+
require 'date'
|
8
|
+
require 'time'
|
9
|
+
require 'json'
|
10
|
+
require 'uri'
|
11
|
+
|
12
|
+
|
13
|
+
## 3rd party libs
|
14
|
+
require 'merkletree'
|
15
|
+
|
16
|
+
|
17
|
+
## our own code
|
18
|
+
require 'blockchain-lite/version' # note: let version always go first
|
19
|
+
|
20
|
+
require 'blockchain-lite/basic/block'
|
21
|
+
require 'blockchain-lite/proof_of_work/block'
|
22
|
+
|
23
|
+
require 'blockchain-lite/blockchain'
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
# say hello
|
28
|
+
puts BlockchainLite.banner if defined?($RUBYLIBS_DEBUG) && $RUBYLIBS_DEBUG
|
@@ -4,64 +4,49 @@
|
|
4
4
|
##
|
5
5
|
# convenience wrapper for array holding blocks (that is, a blockchain)
|
6
6
|
|
7
|
+
module BlockchainLite
|
7
8
|
|
8
|
-
class Blockchain
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
@block_class = if block_class
|
13
|
-
block_class
|
14
|
-
else
|
15
|
-
## check if Block is defined
|
16
|
-
## if yes, use it othwerwise fallback for ProofOfWork::Block
|
17
|
-
defined?( Block ) ? Block : BlockchainLite::ProofOfWork::Block
|
18
|
-
end
|
10
|
+
class Blockchain
|
11
|
+
extend Forwardable
|
19
12
|
|
20
|
-
|
21
|
-
|
22
|
-
else
|
23
|
-
@chain = chain # "wrap" passed in blockchain (in array)
|
24
|
-
@block_class = if block_class
|
25
|
-
block_class
|
26
|
-
else
|
27
|
-
### no block class configured; use class of first block
|
28
|
-
if @chain.first
|
29
|
-
@chain.first.class
|
30
|
-
else
|
31
|
-
## todo/fix: throw except if chain is empty (no class configured) - why? why not??
|
32
|
-
## throw exception on add block if not a block - why? why not??
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
13
|
+
def initialize( chain=[] )
|
14
|
+
@chain = chain # "wrap" passed in blockchain (in array)
|
36
15
|
end
|
37
16
|
|
17
|
+
##
|
18
|
+
# todo/check: can we make it work without "virtual" block_class method
|
19
|
+
## e.g. use constant lookup with singleton class or something - possible?
|
20
|
+
def block_class() Block; end # if not configured; fallback to "top level" Block
|
38
21
|
|
39
22
|
|
40
|
-
def last() @chain.last; end ## return last block in chain
|
41
23
|
|
24
|
+
## delegate some methods (and operators) to chain array (for easier/shortcut access)
|
25
|
+
def_delegators :@chain, :[], :size, :each, :last, :empty?, :any?
|
42
26
|
|
43
|
-
###
|
44
|
-
## make method-<< abstract/virtual - why? why not?
|
45
|
-
## must be added by to make sure proper block_class is always used - why? why not??
|
46
27
|
|
47
28
|
def <<( arg )
|
48
|
-
if arg.is_a?(
|
49
|
-
data = arg
|
50
|
-
bl = @chain.last
|
51
|
-
b = @block_class.next( bl, data )
|
52
|
-
elsif arg.class.respond_to?( :first ) && ## check if respond_to? Block.first? and Block.next? - assume it's a block
|
53
|
-
arg.class.respond_to?( :next ) ## check/todo: use is_a? @block_class why? why not?
|
29
|
+
if arg.is_a?( block_class )
|
54
30
|
b = arg
|
55
|
-
else
|
56
|
-
|
57
|
-
|
58
|
-
|
31
|
+
else
|
32
|
+
if arg.is_a?( Array ) ## assume its (just) data
|
33
|
+
data = arg
|
34
|
+
else ## fallback; assume single transaction record; wrap in array - allow fallback - why? why not??
|
35
|
+
data = [arg]
|
36
|
+
end
|
37
|
+
|
38
|
+
if @chain.empty?
|
39
|
+
b = block_class.first( data )
|
40
|
+
else
|
41
|
+
bl = @chain.last
|
42
|
+
b = block_class.next( bl, data )
|
43
|
+
end
|
59
44
|
end
|
45
|
+
|
60
46
|
@chain << b ## add/append (new) block to chain
|
61
47
|
end
|
62
48
|
|
63
49
|
|
64
|
-
|
65
50
|
def broken?
|
66
51
|
## check for validation conventions
|
67
52
|
## - start with first block?
|
@@ -110,3 +95,5 @@ class Blockchain
|
|
110
95
|
def valid?() !broken?; end
|
111
96
|
|
112
97
|
end ## class Blockchain
|
98
|
+
|
99
|
+
end ## module BlockchainLite
|
data/test/helper.rb
CHANGED
@@ -7,4 +7,22 @@ require 'minitest/autorun'
|
|
7
7
|
|
8
8
|
## our own code
|
9
9
|
|
10
|
-
require 'blockchain-lite'
|
10
|
+
require 'blockchain-lite/base' ## note: use "modular" version without "top-level" Block constant
|
11
|
+
|
12
|
+
|
13
|
+
module Basic
|
14
|
+
Block = BlockchainLite::Basic::Block ## convenience shortcut
|
15
|
+
|
16
|
+
class Blockchain < BlockchainLite::Blockchain
|
17
|
+
def block_class() Block; end
|
18
|
+
end
|
19
|
+
end # module Basic
|
20
|
+
|
21
|
+
|
22
|
+
module ProofOfWork
|
23
|
+
Block = BlockchainLite::ProofOfWork::Block ## convenience shortcut
|
24
|
+
|
25
|
+
class Blockchain < BlockchainLite::Blockchain
|
26
|
+
def block_class() Block; end
|
27
|
+
end
|
28
|
+
end # module ProofOfWork
|
data/test/test_block.rb
CHANGED
@@ -10,13 +10,7 @@ require 'helper'
|
|
10
10
|
|
11
11
|
class TestBlock < MiniTest::Test
|
12
12
|
|
13
|
-
|
14
|
-
pp BlockchainLite.version
|
15
|
-
pp BlockchainLite.banner
|
16
|
-
pp BlockchainLite.root
|
17
|
-
|
18
|
-
assert true ## (for now) everything ok if we get here
|
19
|
-
end
|
13
|
+
include ProofOfWork ## adds Block = BlockchainLite::ProofOfWork::Block etc.
|
20
14
|
|
21
15
|
|
22
16
|
def test_example
|
@@ -36,20 +30,20 @@ end
|
|
36
30
|
|
37
31
|
def test_tulips_example
|
38
32
|
b0 = Block.first(
|
39
|
-
{ from: "Dutchgrown", to: "Vincent",
|
40
|
-
{ from: "Keukenhof", to: "Anne",
|
33
|
+
{ from: "Dutchgrown", to: "Vincent", name: "Tulip Bloemendaal Sunset", qty: 10 },
|
34
|
+
{ from: "Keukenhof", to: "Anne", name: "Tulip Semper Augustus", qty: 7 } )
|
41
35
|
|
42
36
|
b1 = Block.next( b0,
|
43
|
-
{ from: "Flowers", to: "Ruben",
|
44
|
-
{ from: "Vicent", to: "Anne",
|
45
|
-
{ from: "Anne", to: "Julia",
|
46
|
-
{ from: "Julia", to: "Luuk",
|
37
|
+
{ from: "Flowers", to: "Ruben", name: "Tulip Admiral van Eijck", qty: 5 },
|
38
|
+
{ from: "Vicent", to: "Anne", name: "Tulip Bloemendaal Sunset", qty: 3 },
|
39
|
+
{ from: "Anne", to: "Julia", name: "Tulip Semper Augustus", qty: 1 },
|
40
|
+
{ from: "Julia", to: "Luuk", name: "Tulip Semper Augustus", qty: 1 } )
|
47
41
|
|
48
42
|
b2 = Block.next( b1,
|
49
|
-
{ from: "Bloom & Blossom", to: "Daisy",
|
50
|
-
{ from: "Vincent", to: "Max",
|
51
|
-
{ from: "Anne", to: "Martijn",
|
52
|
-
{ from: "Ruben", to: "Julia",
|
43
|
+
{ from: "Bloom & Blossom", to: "Daisy", name: "Tulip Admiral of Admirals", qty: 8 },
|
44
|
+
{ from: "Vincent", to: "Max", name: "Tulip Bloemendaal Sunset", qty: 2 },
|
45
|
+
{ from: "Anne", to: "Martijn", name: "Tulip Semper Augustus", qty: 2 },
|
46
|
+
{ from: "Ruben", to: "Julia", name: "Tulip Admiral van Eijck", qty: 2 } )
|
53
47
|
|
54
48
|
blockchain = [b0, b1, b2]
|
55
49
|
|
@@ -71,22 +65,22 @@ def timestamp1637
|
|
71
65
|
def test_tulips_1637_example
|
72
66
|
|
73
67
|
b0 = Block.first(
|
74
|
-
{ from: "Dutchgrown", to: "Vincent",
|
75
|
-
{ from: "Keukenhof", to: "Anne",
|
68
|
+
{ from: "Dutchgrown", to: "Vincent", name: "Tulip Bloemendaal Sunset", qty: 10 },
|
69
|
+
{ from: "Keukenhof", to: "Anne", name: "Tulip Semper Augustus", qty: 7 },
|
76
70
|
timestamp: timestamp1637 )
|
77
71
|
|
78
72
|
b1 = Block.next( b0,
|
79
|
-
{ from: "Flowers", to: "Ruben",
|
80
|
-
{ from: "Vicent", to: "Anne",
|
81
|
-
{ from: "Anne", to: "Julia",
|
82
|
-
{ from: "Julia", to: "Luuk",
|
73
|
+
{ from: "Flowers", to: "Ruben", name: "Tulip Admiral van Eijck", qty: 5 },
|
74
|
+
{ from: "Vicent", to: "Anne", name: "Tulip Bloemendaal Sunset", qty: 3 },
|
75
|
+
{ from: "Anne", to: "Julia", name: "Tulip Semper Augustus", qty: 1 },
|
76
|
+
{ from: "Julia", to: "Luuk", name: "Tulip Semper Augustus", qty: 1 },
|
83
77
|
timestamp: timestamp1637 )
|
84
78
|
|
85
79
|
b2 = Block.next( b1,
|
86
|
-
{ from: "Bloom & Blossom", to: "Daisy",
|
87
|
-
{ from: "Vincent", to: "Max",
|
88
|
-
{ from: "Anne", to: "Martijn",
|
89
|
-
{ from: "Ruben", to: "Julia",
|
80
|
+
{ from: "Bloom & Blossom", to: "Daisy", name: "Tulip Admiral of Admirals", qty: 8 },
|
81
|
+
{ from: "Vincent", to: "Max", name: "Tulip Bloemendaal Sunset", qty: 2 },
|
82
|
+
{ from: "Anne", to: "Martijn", name: "Tulip Semper Augustus", qty: 2 },
|
83
|
+
{ from: "Ruben", to: "Julia", name: "Tulip Admiral van Eijck", qty: 2 },
|
90
84
|
timestamp: timestamp1637 )
|
91
85
|
|
92
86
|
blockchain = [b0, b1, b2]
|
data/test/test_block_basic.rb
CHANGED
@@ -10,15 +10,16 @@ require 'helper'
|
|
10
10
|
|
11
11
|
class TestBlockBasic < MiniTest::Test
|
12
12
|
|
13
|
-
|
13
|
+
include Basic ## adds Block = BlockchainLite::Basic::Block etc.
|
14
|
+
|
14
15
|
|
15
|
-
|
16
|
+
def test_example
|
16
17
|
|
17
|
-
b0 =
|
18
|
-
b1 =
|
19
|
-
b2 =
|
20
|
-
b3 =
|
21
|
-
b4 =
|
18
|
+
b0 = Block.first( 'Genesis' )
|
19
|
+
b1 = Block.next( b0, 'Transaction Data...' )
|
20
|
+
b2 = Block.next( b1, 'Transaction Data...', 'Transaction Data...' )
|
21
|
+
b3 = Block.next( b2 ) ## no transaction data
|
22
|
+
b4 = Block.next( b3, ['Transaction Data...', 'Transaction Data...'] )
|
22
23
|
|
23
24
|
blockchain = [b0, b1, b2, b3, b4]
|
24
25
|
|
@@ -10,15 +10,16 @@ require 'helper'
|
|
10
10
|
|
11
11
|
class TestBlockProofOfWork < MiniTest::Test
|
12
12
|
|
13
|
-
|
13
|
+
include ProofOfWork ## adds Block = BlockchainLite::ProofOfWork::Block etc.
|
14
|
+
|
14
15
|
|
15
|
-
|
16
|
+
def test_example
|
16
17
|
|
17
|
-
b0 =
|
18
|
-
b1 =
|
19
|
-
b2 =
|
20
|
-
b3 =
|
21
|
-
b4 =
|
18
|
+
b0 = Block.first( 'Genesis' )
|
19
|
+
b1 = Block.next( b0, 'Transaction Data...' )
|
20
|
+
b2 = Block.next( b1, 'Transaction Data...', 'Transaction Data...' )
|
21
|
+
b3 = Block.next( b2 ) ## no transaction data
|
22
|
+
b4 = Block.next( b3, ['Transaction Data...', 'Transaction Data...'] )
|
22
23
|
|
23
24
|
blockchain = [b0, b1, b2, b3, b4]
|
24
25
|
|
data/test/test_blockchain.rb
CHANGED
@@ -9,10 +9,18 @@ require 'helper'
|
|
9
9
|
|
10
10
|
class TestBlockchain < MiniTest::Test
|
11
11
|
|
12
|
+
include ProofOfWork ## adds:
|
13
|
+
## Block = BlockchainLite::ProofOfWork::Block etc.
|
14
|
+
## class Blockchain < BlockchainLite::Blockchain
|
15
|
+
## def block_class() Block; end
|
16
|
+
## end
|
17
|
+
|
18
|
+
|
12
19
|
def test_new
|
13
20
|
|
14
21
|
b = Blockchain.new
|
15
22
|
|
23
|
+
b << 'Genesis'
|
16
24
|
b << 'Transaction Data...'
|
17
25
|
b << ['Transaction Data...']
|
18
26
|
b << ['Transaction Data...', 'Transaction Data...']
|
@@ -29,20 +37,6 @@ def test_new
|
|
29
37
|
end
|
30
38
|
|
31
39
|
|
32
|
-
def test_with_block_class
|
33
|
-
|
34
|
-
b = Blockchain.new( block_class: BlockchainLite::Basic::Block )
|
35
|
-
|
36
|
-
b << 'Transaction Data...'
|
37
|
-
b << 'Transaction Data...'
|
38
|
-
b << 'Transaction Data...'
|
39
|
-
|
40
|
-
pp b
|
41
|
-
|
42
|
-
assert true ## (for now) everything ok if we get here
|
43
|
-
end
|
44
|
-
|
45
|
-
|
46
40
|
def test_wrap
|
47
41
|
|
48
42
|
b0 = Block.first( 'Genesis' )
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_version.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestVersion < MiniTest::Test
|
12
|
+
|
13
|
+
def test_version
|
14
|
+
pp BlockchainLite.version
|
15
|
+
pp BlockchainLite.banner
|
16
|
+
pp BlockchainLite.root
|
17
|
+
|
18
|
+
assert true ## (for now) everything ok if we get here
|
19
|
+
end
|
20
|
+
|
21
|
+
end # class TestVersion
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blockchain-lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.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:
|
11
|
+
date: 2018-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: merkletree
|
@@ -69,9 +69,9 @@ files:
|
|
69
69
|
- README.md
|
70
70
|
- Rakefile
|
71
71
|
- lib/blockchain-lite.rb
|
72
|
+
- lib/blockchain-lite/base.rb
|
72
73
|
- lib/blockchain-lite/basic/block.rb
|
73
74
|
- lib/blockchain-lite/bitcoin/block.rb
|
74
|
-
- lib/blockchain-lite/block.rb
|
75
75
|
- lib/blockchain-lite/blockchain.rb
|
76
76
|
- lib/blockchain-lite/proof_of_work/block.rb
|
77
77
|
- lib/blockchain-lite/version.rb
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- test/test_block_basic.rb
|
81
81
|
- test/test_block_proof_of_work.rb
|
82
82
|
- test/test_blockchain.rb
|
83
|
+
- test/test_version.rb
|
83
84
|
homepage: https://github.com/openblockchains/blockchain.lite.rb
|
84
85
|
licenses:
|
85
86
|
- Public Domain
|