ledger-lite 0.0.1 → 1.0.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 +3 -1
- data/README.md +197 -0
- data/lib/ledger-lite.rb +113 -34
- data/lib/ledger-lite/version.rb +2 -2
- data/test/test_blocks.rb +107 -0
- data/test/test_transactions.rb +156 -0
- data/test/test_version.rb +22 -0
- metadata +5 -3
- data/test/test_transfer.rb +0 -118
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27bbb8efe382ef8c1c4fa635037363e667ec46c9
|
4
|
+
data.tar.gz: 13f8ddac47b8b2f8b44074fe8d3b0a6da256e32e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fafcfdd2f34532ec5885e6db2c35bb594e19027bfdc55fca920dfb69c7231e795ca1b29e7e3c135e703d3745c9274997196291895055b696673fd8cb8e94363
|
7
|
+
data.tar.gz: cc347bb5d02ba4cb6ca76aa348bcdbcc59ff8abf3f413268c3705599395607b69783f2d5c1d216758a9fafb308573d4e7985c8f23ae0b6d4846717da9329f703
|
data/Manifest.txt
CHANGED
data/README.md
CHANGED
@@ -9,6 +9,203 @@ ledger-lite library / gem - hyper ledger book for the distributed blockchain int
|
|
9
9
|
* rdoc :: [rubydoc.info/gems/ledger-lite](http://rubydoc.info/gems/ledger-lite)
|
10
10
|
|
11
11
|
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
Let's add some transactions to the (hyper) ledger book:
|
15
|
+
|
16
|
+
| From | To | $ |
|
17
|
+
|---------------------|--------------|----:|
|
18
|
+
| Keukenhof (†) | Vincent | 11 |
|
19
|
+
| Vincent | Anne | 3 |
|
20
|
+
| Anne | Julia | 2 |
|
21
|
+
| Julia | Luuk | 1 |
|
22
|
+
| | | |
|
23
|
+
| Dutchgrown (†) | Ruben | 11 |
|
24
|
+
| Vincent | Max | 3 |
|
25
|
+
| Ruben | Julia | 2 |
|
26
|
+
| Anne | Martijn | 1 |
|
27
|
+
|
28
|
+
(†): Mining Transaction - New Dutch Gulden ($) on the Market!
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
### Use `send` (send payment/transfer money)
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
|
36
|
+
ledger = Ledger.new
|
37
|
+
|
38
|
+
ledger.send( "Keukenhof†", "Vincent", 11 )
|
39
|
+
ledger.send( "Vincent", "Anne", 3 )
|
40
|
+
ledger.send( "Anne", "Julia", 2 )
|
41
|
+
ledger.send( "Julia", "Luuk", 1 )
|
42
|
+
|
43
|
+
ledger.send( "Dutchgrown†", "Ruben", 11 )
|
44
|
+
ledger.send( "Vincent", "Max", 3 )
|
45
|
+
ledger.send( "Ruben", "Julia", 2 )
|
46
|
+
ledger.send( "Anne", "Martijn", 1 )
|
47
|
+
|
48
|
+
pp ledger ## pp = pretty print
|
49
|
+
|
50
|
+
```
|
51
|
+
|
52
|
+
resulting in
|
53
|
+
|
54
|
+
```
|
55
|
+
#<LedgerLite::Ledger
|
56
|
+
@addr={
|
57
|
+
"Vincent" => 5,
|
58
|
+
"Anne" => 0,
|
59
|
+
"Julia" => 3,
|
60
|
+
"Luuk" => 1,
|
61
|
+
"Ruben" => 9,
|
62
|
+
"Max" => 3,
|
63
|
+
"Martijn" => 1}>
|
64
|
+
```
|
65
|
+
|
66
|
+
that is, the addr hash holds all addresses (addr) with the account balances
|
67
|
+
telling you who owns how much:
|
68
|
+
|
69
|
+
| Addr(ess) | Balance $ |
|
70
|
+
|---------------------|----------:|
|
71
|
+
| Vincent | 5 |
|
72
|
+
| Anne | 0 |
|
73
|
+
| Julia | 3 |
|
74
|
+
| Luuk | 1 |
|
75
|
+
| Ruben | 9 |
|
76
|
+
| Max | 3 |
|
77
|
+
| Martijn | 1 |
|
78
|
+
|
79
|
+
|
80
|
+
### Use `write` to write / add transactions
|
81
|
+
|
82
|
+
Or use transaction (tx) classes/structs:
|
83
|
+
|
84
|
+
``` ruby
|
85
|
+
ledger = Ledger.new
|
86
|
+
|
87
|
+
ledger.write( Tx.new( "Keukenhof†", "Vincent", 11 ))
|
88
|
+
ledger.write( Tx.new( "Vincent", "Anne", 3 ))
|
89
|
+
ledger.write( Tx.new( "Anne", "Julia", 2 ))
|
90
|
+
ledger.write( Tx.new( "Julia", "Luuk", 1 ))
|
91
|
+
|
92
|
+
ledger.write( Tx.new( "Dutchgrown†", "Ruben", 11 ))
|
93
|
+
ledger.write( Tx.new( "Vincent", "Max", 3 ))
|
94
|
+
ledger.write( Tx.new( "Ruben", "Julia", 2 ))
|
95
|
+
ledger.write( Tx.new( "Anne", "Martijn", 1 ))
|
96
|
+
|
97
|
+
pp ledger
|
98
|
+
```
|
99
|
+
|
100
|
+
|
101
|
+
Or pass in many transaction (Tx) classes/structs:
|
102
|
+
|
103
|
+
``` ruby
|
104
|
+
ledger = Ledger.new
|
105
|
+
|
106
|
+
ledger.write( Tx.new( "Keukenhof†", "Vincent", 11 ),
|
107
|
+
Tx.new( "Vincent", "Anne", 3 ),
|
108
|
+
Tx.new( "Anne", "Julia", 2 ),
|
109
|
+
Tx.new( "Julia", "Luuk", 1 ))
|
110
|
+
|
111
|
+
ledger.write( Tx.new( "Dutchgrown†", "Ruben", 11 ),
|
112
|
+
Tx.new( "Vincent", "Max", 3 ),
|
113
|
+
Tx.new( "Ruben", "Julia", 2 ),
|
114
|
+
Tx.new( "Anne", "Martijn", 1 ))
|
115
|
+
|
116
|
+
pp ledger
|
117
|
+
```
|
118
|
+
|
119
|
+
Or use the operator `<<` alias for `write`:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
ledger = Ledger.new
|
123
|
+
|
124
|
+
ledger << Tx.new( "Keukenhof†", "Vincent", 11 )
|
125
|
+
ledger << Tx.new( "Vincent", "Anne", 3 )
|
126
|
+
ledger << Tx.new( "Anne", "Julia", 2 )
|
127
|
+
ledger << Tx.new( "Julia", "Luuk", 1 )
|
128
|
+
|
129
|
+
ledger << [Tx.new( "Dutchgrown†", "Ruben", 11 ),
|
130
|
+
Tx.new( "Vincent", "Max", 3 ),
|
131
|
+
Tx.new( "Ruben", "Julia", 2 ),
|
132
|
+
Tx.new( "Anne", "Martijn", 1 )]
|
133
|
+
|
134
|
+
pp ledger
|
135
|
+
```
|
136
|
+
|
137
|
+
Or use transaction hashes:
|
138
|
+
|
139
|
+
``` ruby
|
140
|
+
ledger = Ledger.new
|
141
|
+
|
142
|
+
ledger.write( { from: "Keukenhof†", to: "Vincent", amount: 11 },
|
143
|
+
{ from: "Vincent", to: "Anne", amount: 3 },
|
144
|
+
{ from: "Anne", to: "Julia", amount: 2 },
|
145
|
+
{ from: "Julia", to: "Luuk", amount: 1 })
|
146
|
+
|
147
|
+
ledger.write( { from: "Dutchgrown†", to: "Ruben", amount: 11 },
|
148
|
+
{ from: "Vincent", to: "Max", amount: 3 },
|
149
|
+
{ from: "Ruben", to: "Julia", amount: 2 },
|
150
|
+
{ from: "Anne", to: "Martijn", amount: 1 })
|
151
|
+
|
152
|
+
pp ledger
|
153
|
+
```
|
154
|
+
|
155
|
+
|
156
|
+
Or use blocks of transaction hashes:
|
157
|
+
|
158
|
+
``` ruby
|
159
|
+
|
160
|
+
ledger = Ledger.new
|
161
|
+
|
162
|
+
ledger.write( Block.new( { from: "Keukenhof†", to: "Vincent", amount: 11 },
|
163
|
+
{ from: "Vincent", to: "Anne", amount: 3 },
|
164
|
+
{ from: "Anne", to: "Julia", amount: 2 },
|
165
|
+
{ from: "Julia", to: "Luuk", amount: 1 } ),
|
166
|
+
Block.new( { from: "Dutchgrown†", to: "Ruben", amount: 11 },
|
167
|
+
{ from: "Vincent", to: "Max", amount: 3 },
|
168
|
+
{ from: "Ruben", to: "Julia", amount: 2 },
|
169
|
+
{ from: "Anne", to: "Martijn", amount: 1 } ))
|
170
|
+
|
171
|
+
pp ledger
|
172
|
+
```
|
173
|
+
|
174
|
+
|
175
|
+
Or use blocks of transaction classes/structs:
|
176
|
+
|
177
|
+
``` ruby
|
178
|
+
ledger = Ledger.new
|
179
|
+
|
180
|
+
ledger.write( Block.new( Tx.new( "Keukenhof†", "Vincent", 11 ),
|
181
|
+
Tx.new( "Vincent", "Anne", 3 ),
|
182
|
+
Tx.new( "Anne", "Julia", 2 ),
|
183
|
+
Tx.new( "Julia", "Luuk", 1 )),
|
184
|
+
Block.new( Tx.new( "Dutchgrown†", "Ruben", 11 ),
|
185
|
+
Tx.new( "Vincent", "Max", 3 ),
|
186
|
+
Tx.new( "Ruben", "Julia", 2 ),
|
187
|
+
Tx.new( "Anne", "Martijn", 1 )))
|
188
|
+
|
189
|
+
pp ledger
|
190
|
+
```
|
191
|
+
|
192
|
+
Or use blocks of transaction classes/structs (with keyword arguments):
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
ledger = Ledger.new
|
196
|
+
|
197
|
+
ledger.write( Block.new( Tx.new( from: "Keukenhof†", to: "Vincent", amount: 11 ),
|
198
|
+
Tx.new( from: "Vincent", to: "Anne", amount: 3 ),
|
199
|
+
Tx.new( from: "Anne", to: "Julia", amount: 2 ),
|
200
|
+
Tx.new( from: "Julia", to: "Luuk", amount: 1 )),
|
201
|
+
Block.new( Tx.new( from: "Dutchgrown†", to: "Ruben", amount: 11 ),
|
202
|
+
Tx.new( from: "Vincent", to: "Max", amount: 3 ),
|
203
|
+
Tx.new( from: "Ruben", to: "Julia", amount: 2 ),
|
204
|
+
Tx.new( from: "Anne", to: "Martijn", amount: 1 )))
|
205
|
+
|
206
|
+
pp ledger
|
207
|
+
```
|
208
|
+
|
12
209
|
|
13
210
|
|
14
211
|
## License
|
data/lib/ledger-lite.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require 'pp'
|
4
|
-
|
3
|
+
require 'pp' # for pp => pretty printer
|
4
|
+
require 'forwardable'
|
5
5
|
require 'date'
|
6
6
|
require 'time'
|
7
7
|
require 'json'
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
|
11
12
|
## our own code
|
12
13
|
require 'ledger-lite/version' # note: let version always go first
|
13
14
|
|
@@ -15,52 +16,114 @@ require 'ledger-lite/version' # note: let version always go first
|
|
15
16
|
|
16
17
|
module LedgerLite
|
17
18
|
|
19
|
+
|
20
|
+
|
21
|
+
class Configuration
|
22
|
+
|
23
|
+
## system settings
|
24
|
+
|
25
|
+
## use a different name e.g.
|
26
|
+
## - mint (like royal mint or federal coin mint?) or
|
27
|
+
## - base
|
28
|
+
## - magic (for creating coins out-of-nothing?) or
|
29
|
+
## - network or ??) - why? why not?
|
30
|
+
##
|
31
|
+
attr_accessor :coinbase
|
32
|
+
|
33
|
+
## note: for now is an array (allow/ support multiple coinbases)
|
34
|
+
## note: add a (†) coinbase marker
|
35
|
+
COINBASE = ['Coinbase†']
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
@coinbase = COINBASE
|
39
|
+
end
|
40
|
+
|
41
|
+
def coinbase?( addr )
|
42
|
+
@coinbase.include?( addr )
|
43
|
+
end
|
44
|
+
end # class Configuration
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
18
49
|
class Ledger
|
50
|
+
extend Forwardable
|
51
|
+
|
52
|
+
|
53
|
+
## lets you use
|
54
|
+
## Ledger.configure do |config|
|
55
|
+
## config.coinbase = ['Keukenhof†']
|
56
|
+
## end
|
57
|
+
|
58
|
+
def self.configure
|
59
|
+
yield( config )
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.config
|
63
|
+
@config ||= Configuration.new
|
64
|
+
end
|
65
|
+
|
66
|
+
|
19
67
|
|
20
68
|
attr_reader :addr ## make addr private e.g. remove - why? e.g. use hash forwards/delegates - why not?
|
21
69
|
|
22
|
-
def initialize( args=[] )
|
23
|
-
## todo/fix: use *args - same interface as merkletree/blochchain-lite
|
24
70
|
|
71
|
+
## delegate some methods (and operators) to addr hash (for easier/shortcut access)
|
72
|
+
def_delegators :@addr, :[], :size, :each, :empty?, :any?
|
73
|
+
|
74
|
+
|
75
|
+
def initialize( *args )
|
25
76
|
@addr = {}
|
26
77
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
78
|
+
## add all transactions passed in on startup; if no args - do nothing
|
79
|
+
unless args.empty?
|
80
|
+
## note: MUST unsplat (*) args
|
81
|
+
## otherwise args get "double" wrapped in array
|
82
|
+
write( *args )
|
83
|
+
end
|
34
84
|
end # method initialize
|
35
85
|
|
36
86
|
|
37
87
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
##
|
43
|
-
|
88
|
+
def write( *args )
|
89
|
+
puts "write:"
|
90
|
+
pp args
|
91
|
+
|
92
|
+
## note: allow/support splat-* for now for convenience (auto-wraps args into array)
|
93
|
+
if args.size == 1 && args[0].is_a?( Array )
|
94
|
+
puts " unwrap array in array"
|
95
|
+
blks_or_txns = args[0] ## "unwrap" array in array
|
96
|
+
else
|
97
|
+
blks_or_txns = args ## use "auto-wrapped" splat array
|
98
|
+
end
|
99
|
+
|
100
|
+
## "unpack" transactions from possible (optional) blocks
|
101
|
+
## and return "flattend" **single** array of transactions
|
102
|
+
transactions = unpack_transactions( blks_or_txns )
|
103
|
+
|
104
|
+
## unpack & unsplat array (to pass in args to send) => from, to, amount
|
105
|
+
transactions.each { |tx| send( *unpack(tx) ) }
|
44
106
|
end
|
45
107
|
|
108
|
+
## note: add and the << operator is an alias for write
|
109
|
+
alias :add :write
|
110
|
+
alias :<< :write
|
46
111
|
|
47
|
-
def transfer( arg )
|
48
|
-
## todo/fix: use *args - same interface as merkletree/blochchain-lite
|
49
112
|
|
50
|
-
|
51
|
-
|
113
|
+
def unpack_transactions( blocks )
|
114
|
+
## "unpack" transactions from possible (optional) blocks
|
115
|
+
## and return "flattend" **single** array of transactions
|
52
116
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
117
|
+
blocks.reduce( [] ) do |acc, block|
|
118
|
+
if block.respond_to?( :transactions ) ## bingo! assume it's block if it has transactions method
|
119
|
+
acc + block.transactions
|
120
|
+
else ## note: otherwise assumes it's just a "plain" **single** transaction
|
121
|
+
tx = block
|
122
|
+
acc + [tx] ## wrap in array (use acc << tx - with side effects/mutate in place - why? why not?)
|
123
|
+
end
|
60
124
|
end
|
61
125
|
end
|
62
126
|
|
63
|
-
|
64
127
|
def unpack( tx )
|
65
128
|
## "unpack" from, to, amount values
|
66
129
|
|
@@ -80,22 +143,38 @@ class Ledger
|
|
80
143
|
end
|
81
144
|
|
82
145
|
|
83
|
-
##
|
146
|
+
##
|
147
|
+
# find a better name - why? why not?
|
148
|
+
## e.g. use can? funds? sufficient? has_funds?
|
149
|
+
def sufficient_funds?( addr, amount )
|
150
|
+
return true if self.class.config.coinbase?( addr ) ## note: coinbase has unlimited funds!!!
|
151
|
+
@addr.has_key?( addr ) && @addr[addr] - amount >= 0
|
152
|
+
end
|
153
|
+
|
154
|
+
|
155
|
+
## apply/do single transaction - send payment - do transfer
|
84
156
|
## - find a different name - why? why not?
|
85
|
-
def
|
157
|
+
def send( from, to, amount )
|
86
158
|
|
87
159
|
if sufficient_funds?( from, amount )
|
88
|
-
|
89
|
-
|
160
|
+
if self.class.config.coinbase?( from )
|
161
|
+
# note: coinbase has unlimited funds!! ("virtual" built-in money printing address)
|
162
|
+
else
|
163
|
+
@addr[ from ] ||= 0
|
164
|
+
@addr[ from ] -= amount
|
165
|
+
end
|
90
166
|
@addr[ to ] ||= 0
|
91
167
|
@addr[ to ] += amount
|
92
168
|
end
|
169
|
+
end # method transfer
|
93
170
|
|
94
|
-
|
171
|
+
## note: transfer is an alias for send (payment)
|
172
|
+
alias :transfer :send
|
95
173
|
|
96
174
|
|
97
175
|
end # class Ledger
|
98
176
|
|
177
|
+
|
99
178
|
end # module LedgerLite
|
100
179
|
|
101
180
|
|
data/lib/ledger-lite/version.rb
CHANGED
data/test/test_blocks.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_blocks.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestBlocks < MiniTest::Test
|
12
|
+
|
13
|
+
def setup
|
14
|
+
Ledger.configure do |config|
|
15
|
+
config.coinbase = ["Keukenhof†", "Dutchgrown†"]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_blocks_with_hash
|
20
|
+
|
21
|
+
b0 = Block.new( { from: "Keukenhof†", to: "Vincent", amount: 11 },
|
22
|
+
{ from: "Vincent", to: "Anne", amount: 3 },
|
23
|
+
{ from: "Anne", to: "Julia", amount: 2 },
|
24
|
+
{ from: "Julia", to: "Luuk", amount: 1 } )
|
25
|
+
|
26
|
+
b1 = Block.new( { from: "Dutchgrown†", to: "Ruben", amount: 11 },
|
27
|
+
{ from: "Vincent", to: "Max", amount: 3 },
|
28
|
+
{ from: "Ruben", to: "Julia", amount: 2 },
|
29
|
+
{ from: "Anne", to: "Martijn", amount: 1 } )
|
30
|
+
|
31
|
+
blockchain = [b0,b1]
|
32
|
+
|
33
|
+
ledger = Ledger.new( blockchain )
|
34
|
+
|
35
|
+
pp ledger
|
36
|
+
|
37
|
+
balances = {"Vincent"=>5,
|
38
|
+
"Anne"=>0,
|
39
|
+
"Julia"=>3,
|
40
|
+
"Luuk"=>1,
|
41
|
+
"Ruben"=>9,
|
42
|
+
"Max"=>3,
|
43
|
+
"Martijn"=>1}
|
44
|
+
assert_equal balances, ledger.addr
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
def test_blocks_with_tx_v1
|
49
|
+
|
50
|
+
b0 = Block.new( Tx.new( from: "Keukenhof†", to: "Vincent", amount: 11 ),
|
51
|
+
Tx.new( from: "Vincent", to: "Anne", amount: 3 ),
|
52
|
+
Tx.new( from: "Anne", to: "Julia", amount: 2 ),
|
53
|
+
Tx.new( from: "Julia", to: "Luuk", amount: 1 ))
|
54
|
+
|
55
|
+
b1 = Block.new( Tx.new( from: "Dutchgrown†", to: "Ruben", amount: 11 ),
|
56
|
+
Tx.new( from: "Vincent", to: "Max", amount: 3 ),
|
57
|
+
Tx.new( from: "Ruben", to: "Julia", amount: 2 ),
|
58
|
+
Tx.new( from: "Anne", to: "Martijn", amount: 1 ))
|
59
|
+
|
60
|
+
blockchain = [b0,b1]
|
61
|
+
|
62
|
+
ledger = Ledger.new( blockchain )
|
63
|
+
|
64
|
+
pp ledger
|
65
|
+
|
66
|
+
balances = {"Vincent"=>5,
|
67
|
+
"Anne"=>0,
|
68
|
+
"Julia"=>3,
|
69
|
+
"Luuk"=>1,
|
70
|
+
"Ruben"=>9,
|
71
|
+
"Max"=>3,
|
72
|
+
"Martijn"=>1}
|
73
|
+
assert_equal balances, ledger.addr
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
def test_blocks_with_tx_v2
|
79
|
+
|
80
|
+
b0 = Block.new( Tx.new( "Keukenhof†", "Vincent", 11 ),
|
81
|
+
Tx.new( "Vincent", "Anne", 3 ),
|
82
|
+
Tx.new( "Anne", "Julia", 2 ),
|
83
|
+
Tx.new( "Julia", "Luuk", 1 ))
|
84
|
+
|
85
|
+
b1 = Block.new( Tx.new( "Dutchgrown†", "Ruben", 11 ),
|
86
|
+
Tx.new( "Vincent", "Max", 3 ),
|
87
|
+
Tx.new( "Ruben", "Julia", 2 ),
|
88
|
+
Tx.new( "Anne", "Martijn", 1 ))
|
89
|
+
|
90
|
+
blockchain = [b0,b1]
|
91
|
+
|
92
|
+
ledger = Ledger.new( blockchain )
|
93
|
+
|
94
|
+
pp ledger
|
95
|
+
|
96
|
+
balances = {"Vincent"=>5,
|
97
|
+
"Anne"=>0,
|
98
|
+
"Julia"=>3,
|
99
|
+
"Luuk"=>1,
|
100
|
+
"Ruben"=>9,
|
101
|
+
"Max"=>3,
|
102
|
+
"Martijn"=>1}
|
103
|
+
assert_equal balances, ledger.addr
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
end # class TestBlocks
|
@@ -0,0 +1,156 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_transactions.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestTransactions < MiniTest::Test
|
12
|
+
|
13
|
+
def setup
|
14
|
+
Ledger.configure do |config|
|
15
|
+
config.coinbase = ["Keukenhof†", "Dutchgrown†"]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def test_send
|
21
|
+
|
22
|
+
ledger = Ledger.new
|
23
|
+
|
24
|
+
ledger.send( "Keukenhof†", "Vincent", 11 )
|
25
|
+
ledger.send( "Vincent", "Anne", 3 )
|
26
|
+
ledger.send( "Anne", "Julia", 2 )
|
27
|
+
ledger.send( "Julia", "Luuk", 1 )
|
28
|
+
|
29
|
+
ledger.send( "Dutchgrown†", "Ruben", 11 )
|
30
|
+
ledger.send( "Vincent", "Max", 3 )
|
31
|
+
ledger.send( "Ruben", "Julia", 2 )
|
32
|
+
ledger.send( "Anne", "Martijn", 1 )
|
33
|
+
|
34
|
+
pp ledger
|
35
|
+
|
36
|
+
balances = {"Vincent"=>5,
|
37
|
+
"Anne"=>0,
|
38
|
+
"Julia"=>3,
|
39
|
+
"Luuk"=>1,
|
40
|
+
"Ruben"=>9,
|
41
|
+
"Max"=>3,
|
42
|
+
"Martijn"=>1}
|
43
|
+
assert_equal balances, ledger.addr
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def test_with_tx_v1
|
48
|
+
|
49
|
+
ledger = Ledger.new
|
50
|
+
|
51
|
+
ledger.write( Tx.new( "Keukenhof†", "Vincent", 11 ))
|
52
|
+
ledger.write( Tx.new( "Vincent", "Anne", 3 ))
|
53
|
+
ledger.write( Tx.new( "Anne", "Julia", 2 ))
|
54
|
+
ledger.write( Tx.new( "Julia", "Luuk", 1 ))
|
55
|
+
|
56
|
+
ledger.write( Tx.new( "Dutchgrown†", "Ruben", 11 ))
|
57
|
+
ledger.write( Tx.new( "Vincent", "Max", 3 ))
|
58
|
+
ledger.write( Tx.new( "Ruben", "Julia", 2 ))
|
59
|
+
ledger.write( Tx.new( "Anne", "Martijn", 1 ))
|
60
|
+
|
61
|
+
pp ledger
|
62
|
+
|
63
|
+
balances = {"Vincent"=>5,
|
64
|
+
"Anne"=>0,
|
65
|
+
"Julia"=>3,
|
66
|
+
"Luuk"=>1,
|
67
|
+
"Ruben"=>9,
|
68
|
+
"Max"=>3,
|
69
|
+
"Martijn"=>1}
|
70
|
+
assert_equal balances, ledger.addr
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def test_with_tx_v2
|
75
|
+
|
76
|
+
ledger = Ledger.new
|
77
|
+
|
78
|
+
ledger.write( Tx.new( "Keukenhof†", "Vincent", 11 ),
|
79
|
+
Tx.new( "Vincent", "Anne", 3 ),
|
80
|
+
Tx.new( "Anne", "Julia", 2 ),
|
81
|
+
Tx.new( "Julia", "Luuk", 1 ))
|
82
|
+
|
83
|
+
ledger.write( Tx.new( "Dutchgrown†", "Ruben", 11 ),
|
84
|
+
Tx.new( "Vincent", "Max", 3 ),
|
85
|
+
Tx.new( "Ruben", "Julia", 2 ),
|
86
|
+
Tx.new( "Anne", "Martijn", 1 ))
|
87
|
+
|
88
|
+
pp ledger
|
89
|
+
|
90
|
+
balances = {"Vincent"=>5,
|
91
|
+
"Anne"=>0,
|
92
|
+
"Julia"=>3,
|
93
|
+
"Luuk"=>1,
|
94
|
+
"Ruben"=>9,
|
95
|
+
"Max"=>3,
|
96
|
+
"Martijn"=>1}
|
97
|
+
assert_equal balances, ledger.addr
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
def test_with_tx_v3
|
102
|
+
|
103
|
+
ledger = Ledger.new
|
104
|
+
|
105
|
+
ledger << Tx.new( "Keukenhof†", "Vincent", 11 )
|
106
|
+
ledger << Tx.new( "Vincent", "Anne", 3 )
|
107
|
+
ledger << Tx.new( "Anne", "Julia", 2 )
|
108
|
+
ledger << Tx.new( "Julia", "Luuk", 1 )
|
109
|
+
|
110
|
+
ledger << [Tx.new( "Dutchgrown†", "Ruben", 11 ),
|
111
|
+
Tx.new( "Vincent", "Max", 3 ),
|
112
|
+
Tx.new( "Ruben", "Julia", 2 ),
|
113
|
+
Tx.new( "Anne", "Martijn", 1 )]
|
114
|
+
|
115
|
+
pp ledger
|
116
|
+
|
117
|
+
balances = {"Vincent"=>5,
|
118
|
+
"Anne"=>0,
|
119
|
+
"Julia"=>3,
|
120
|
+
"Luuk"=>1,
|
121
|
+
"Ruben"=>9,
|
122
|
+
"Max"=>3,
|
123
|
+
"Martijn"=>1}
|
124
|
+
assert_equal balances, ledger.addr
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def test_with_hash
|
130
|
+
|
131
|
+
ledger = Ledger.new
|
132
|
+
|
133
|
+
ledger.write( { from: "Keukenhof†", to: "Vincent", amount: 11 },
|
134
|
+
{ from: "Vincent", to: "Anne", amount: 3 },
|
135
|
+
{ from: "Anne", to: "Julia", amount: 2 },
|
136
|
+
{ from: "Julia", to: "Luuk", amount: 1 })
|
137
|
+
|
138
|
+
ledger.write( { from: "Dutchgrown†", to: "Ruben", amount: 11 },
|
139
|
+
{ from: "Vincent", to: "Max", amount: 3 },
|
140
|
+
{ from: "Ruben", to: "Julia", amount: 2 },
|
141
|
+
{ from: "Anne", to: "Martijn", amount: 1 })
|
142
|
+
|
143
|
+
pp ledger
|
144
|
+
|
145
|
+
balances = {"Vincent"=>5,
|
146
|
+
"Anne"=>0,
|
147
|
+
"Julia"=>3,
|
148
|
+
"Luuk"=>1,
|
149
|
+
"Ruben"=>9,
|
150
|
+
"Max"=>3,
|
151
|
+
"Martijn"=>1}
|
152
|
+
assert_equal balances, ledger.addr
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
end # class TestTransactions
|
@@ -0,0 +1,22 @@
|
|
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 LedgerLite.version
|
15
|
+
pp LedgerLite.banner
|
16
|
+
pp LedgerLite.root
|
17
|
+
|
18
|
+
assert true ## (for now) everything ok if we get here
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
end # class TestVersion
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ledger-lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.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: 2018-01-
|
11
|
+
date: 2018-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|
@@ -58,7 +58,9 @@ files:
|
|
58
58
|
- lib/ledger-lite.rb
|
59
59
|
- lib/ledger-lite/version.rb
|
60
60
|
- test/helper.rb
|
61
|
-
- test/
|
61
|
+
- test/test_blocks.rb
|
62
|
+
- test/test_transactions.rb
|
63
|
+
- test/test_version.rb
|
62
64
|
homepage: https://github.com/openblockchains/ledger.lite.rb
|
63
65
|
licenses:
|
64
66
|
- Public Domain
|
data/test/test_transfer.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
###
|
4
|
-
# to run use
|
5
|
-
# ruby -I ./lib -I ./test test/test_transfer.rb
|
6
|
-
|
7
|
-
|
8
|
-
require 'helper'
|
9
|
-
|
10
|
-
|
11
|
-
class TestTransfer < MiniTest::Test
|
12
|
-
|
13
|
-
|
14
|
-
def test_do_transfer
|
15
|
-
|
16
|
-
ledger = Ledger.new
|
17
|
-
|
18
|
-
ledger.do_transfer( "Vicent", "Anne", 3 )
|
19
|
-
ledger.do_transfer( "Anne", "Julia", 2 )
|
20
|
-
ledger.do_transfer( "Julia", "Luuk", 1 )
|
21
|
-
|
22
|
-
ledger.do_transfer( "Vincent", "Max", 3 )
|
23
|
-
ledger.do_transfer( "Anne", "Martijn", 2 )
|
24
|
-
ledger.do_transfer( "Ruben", "Julia", 1 )
|
25
|
-
|
26
|
-
pp ledger
|
27
|
-
|
28
|
-
balances = {"Vicent"=>-3,
|
29
|
-
"Anne"=>-1,
|
30
|
-
"Julia"=>2,
|
31
|
-
"Luuk"=>1,
|
32
|
-
"Vincent"=>-3,
|
33
|
-
"Max"=>3,
|
34
|
-
"Martijn"=>2,
|
35
|
-
"Ruben"=>-1}
|
36
|
-
assert_equal balances, ledger.addr
|
37
|
-
end
|
38
|
-
|
39
|
-
|
40
|
-
def test_transfer_with_tx_v1
|
41
|
-
|
42
|
-
ledger = Ledger.new
|
43
|
-
|
44
|
-
ledger.transfer( Tx.new( "Vicent", "Anne", 3 ))
|
45
|
-
ledger.transfer( Tx.new( "Anne", "Julia", 2 ))
|
46
|
-
ledger.transfer( Tx.new( "Julia", "Luuk", 1 ))
|
47
|
-
|
48
|
-
ledger.transfer( Tx.new( "Vincent", "Max", 3 ))
|
49
|
-
ledger.transfer( Tx.new( "Anne", "Martijn", 2 ))
|
50
|
-
ledger.transfer( Tx.new( "Ruben", "Julia", 1 ))
|
51
|
-
|
52
|
-
pp ledger
|
53
|
-
|
54
|
-
balances = {"Vicent"=>-3,
|
55
|
-
"Anne"=>-1,
|
56
|
-
"Julia"=>2,
|
57
|
-
"Luuk"=>1,
|
58
|
-
"Vincent"=>-3,
|
59
|
-
"Max"=>3,
|
60
|
-
"Martijn"=>2,
|
61
|
-
"Ruben"=>-1}
|
62
|
-
assert_equal balances, ledger.addr
|
63
|
-
end
|
64
|
-
|
65
|
-
|
66
|
-
def test_transfer_with_tx_v2
|
67
|
-
|
68
|
-
ledger = Ledger.new
|
69
|
-
|
70
|
-
ledger.transfer( [Tx.new( "Vicent", "Anne", 3 ),
|
71
|
-
Tx.new( "Anne", "Julia", 2 ),
|
72
|
-
Tx.new( "Julia", "Luuk", 1 )])
|
73
|
-
|
74
|
-
ledger.transfer( [Tx.new( "Vincent", "Max", 3 ),
|
75
|
-
Tx.new( "Anne", "Martijn", 2 ),
|
76
|
-
Tx.new( "Ruben", "Julia", 1 )])
|
77
|
-
|
78
|
-
pp ledger
|
79
|
-
|
80
|
-
balances = {"Vicent"=>-3,
|
81
|
-
"Anne"=>-1,
|
82
|
-
"Julia"=>2,
|
83
|
-
"Luuk"=>1,
|
84
|
-
"Vincent"=>-3,
|
85
|
-
"Max"=>3,
|
86
|
-
"Martijn"=>2,
|
87
|
-
"Ruben"=>-1}
|
88
|
-
assert_equal balances, ledger.addr
|
89
|
-
end
|
90
|
-
|
91
|
-
|
92
|
-
def test_transfer_with_hash
|
93
|
-
|
94
|
-
ledger = Ledger.new
|
95
|
-
|
96
|
-
ledger.transfer( [{ from: "Vicent", to: "Anne", amount: 3 },
|
97
|
-
{ from: "Anne", to: "Julia", amount: 2 },
|
98
|
-
{ from: "Julia", to: "Luuk", amount: 1 }])
|
99
|
-
|
100
|
-
ledger.transfer( [{ from: "Vincent", to: "Max", amount: 3 },
|
101
|
-
{ from: "Anne", to: "Martijn", amount: 2 },
|
102
|
-
{ from: "Ruben", to: "Julia", amount: 1 }])
|
103
|
-
|
104
|
-
pp ledger
|
105
|
-
|
106
|
-
balances = {"Vicent"=>-3,
|
107
|
-
"Anne"=>-1,
|
108
|
-
"Julia"=>2,
|
109
|
-
"Luuk"=>1,
|
110
|
-
"Vincent"=>-3,
|
111
|
-
"Max"=>3,
|
112
|
-
"Martijn"=>2,
|
113
|
-
"Ruben"=>-1}
|
114
|
-
assert_equal balances, ledger.addr
|
115
|
-
end
|
116
|
-
|
117
|
-
|
118
|
-
end # class TestTransfer
|