shilling 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/HISTORY.md +3 -0
- data/LICENSE.md +116 -0
- data/Manifest.txt +26 -0
- data/README.md +115 -0
- data/Rakefile +32 -0
- data/bin/shilling +17 -0
- data/lib/shilling.rb +103 -0
- data/lib/shilling/bank.rb +109 -0
- data/lib/shilling/block.rb +44 -0
- data/lib/shilling/blockchain.rb +47 -0
- data/lib/shilling/cache.rb +22 -0
- data/lib/shilling/ledger.rb +30 -0
- data/lib/shilling/node.rb +82 -0
- data/lib/shilling/pool.rb +42 -0
- data/lib/shilling/service.rb +113 -0
- data/lib/shilling/tool.rb +66 -0
- data/lib/shilling/transaction.rb +30 -0
- data/lib/shilling/version.rb +11 -0
- data/lib/shilling/views/_blockchain.erb +37 -0
- data/lib/shilling/views/_ledger.erb +15 -0
- data/lib/shilling/views/_peers.erb +24 -0
- data/lib/shilling/views/_pending_transactions.erb +23 -0
- data/lib/shilling/views/_wallet.erb +16 -0
- data/lib/shilling/views/index.erb +30 -0
- data/lib/shilling/views/style.scss +172 -0
- data/lib/shilling/wallet.rb +15 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7c48c6763c71aa3e8a80d954e1369691d06d1434
|
4
|
+
data.tar.gz: 3ef89982be97773871b268c382282d64ea044b6e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5c0b2c9a620f33779d21027211ea77f46707ee365490a9870dc7f09da8c114fc470c47b2b95bc6b569d8fbb67b45bf28fea883128ff1c5b61e64259b79bf9439
|
7
|
+
data.tar.gz: 8959cd16c9f051b037c0a7a403cac66f1c617e1efe2a0d6542eee4b1d008a8bf2f5dbb83fb5b70e3ecb0dc90c4f687285f6f862f7792ee1241d55586c3336720
|
data/HISTORY.md
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
CC0 1.0 Universal
|
2
|
+
|
3
|
+
Statement of Purpose
|
4
|
+
|
5
|
+
The laws of most jurisdictions throughout the world automatically confer
|
6
|
+
exclusive Copyright and Related Rights (defined below) upon the creator and
|
7
|
+
subsequent owner(s) (each and all, an "owner") of an original work of
|
8
|
+
authorship and/or a database (each, a "Work").
|
9
|
+
|
10
|
+
Certain owners wish to permanently relinquish those rights to a Work for the
|
11
|
+
purpose of contributing to a commons of creative, cultural and scientific
|
12
|
+
works ("Commons") that the public can reliably and without fear of later
|
13
|
+
claims of infringement build upon, modify, incorporate in other works, reuse
|
14
|
+
and redistribute as freely as possible in any form whatsoever and for any
|
15
|
+
purposes, including without limitation commercial purposes. These owners may
|
16
|
+
contribute to the Commons to promote the ideal of a free culture and the
|
17
|
+
further production of creative, cultural and scientific works, or to gain
|
18
|
+
reputation or greater distribution for their Work in part through the use and
|
19
|
+
efforts of others.
|
20
|
+
|
21
|
+
For these and/or other purposes and motivations, and without any expectation
|
22
|
+
of additional consideration or compensation, the person associating CC0 with a
|
23
|
+
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
|
24
|
+
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
|
25
|
+
and publicly distribute the Work under its terms, with knowledge of his or her
|
26
|
+
Copyright and Related Rights in the Work and the meaning and intended legal
|
27
|
+
effect of CC0 on those rights.
|
28
|
+
|
29
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
30
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
31
|
+
Related Rights"). Copyright and Related Rights include, but are not limited
|
32
|
+
to, the following:
|
33
|
+
|
34
|
+
i. the right to reproduce, adapt, distribute, perform, display, communicate,
|
35
|
+
and translate a Work;
|
36
|
+
|
37
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
38
|
+
|
39
|
+
iii. publicity and privacy rights pertaining to a person's image or likeness
|
40
|
+
depicted in a Work;
|
41
|
+
|
42
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
43
|
+
subject to the limitations in paragraph 4(a), below;
|
44
|
+
|
45
|
+
v. rights protecting the extraction, dissemination, use and reuse of data in
|
46
|
+
a Work;
|
47
|
+
|
48
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
49
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
50
|
+
protection of databases, and under any national implementation thereof,
|
51
|
+
including any amended or successor version of such directive); and
|
52
|
+
|
53
|
+
vii. other similar, equivalent or corresponding rights throughout the world
|
54
|
+
based on applicable law or treaty, and any national implementations thereof.
|
55
|
+
|
56
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention of,
|
57
|
+
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
|
58
|
+
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
|
59
|
+
and Related Rights and associated claims and causes of action, whether now
|
60
|
+
known or unknown (including existing as well as future claims and causes of
|
61
|
+
action), in the Work (i) in all territories worldwide, (ii) for the maximum
|
62
|
+
duration provided by applicable law or treaty (including future time
|
63
|
+
extensions), (iii) in any current or future medium and for any number of
|
64
|
+
copies, and (iv) for any purpose whatsoever, including without limitation
|
65
|
+
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
|
66
|
+
the Waiver for the benefit of each member of the public at large and to the
|
67
|
+
detriment of Affirmer's heirs and successors, fully intending that such Waiver
|
68
|
+
shall not be subject to revocation, rescission, cancellation, termination, or
|
69
|
+
any other legal or equitable action to disrupt the quiet enjoyment of the Work
|
70
|
+
by the public as contemplated by Affirmer's express Statement of Purpose.
|
71
|
+
|
72
|
+
3. Public License Fallback. Should any part of the Waiver for any reason be
|
73
|
+
judged legally invalid or ineffective under applicable law, then the Waiver
|
74
|
+
shall be preserved to the maximum extent permitted taking into account
|
75
|
+
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
|
76
|
+
is so judged Affirmer hereby grants to each affected person a royalty-free,
|
77
|
+
non transferable, non sublicensable, non exclusive, irrevocable and
|
78
|
+
unconditional license to exercise Affirmer's Copyright and Related Rights in
|
79
|
+
the Work (i) in all territories worldwide, (ii) for the maximum duration
|
80
|
+
provided by applicable law or treaty (including future time extensions), (iii)
|
81
|
+
in any current or future medium and for any number of copies, and (iv) for any
|
82
|
+
purpose whatsoever, including without limitation commercial, advertising or
|
83
|
+
promotional purposes (the "License"). The License shall be deemed effective as
|
84
|
+
of the date CC0 was applied by Affirmer to the Work. Should any part of the
|
85
|
+
License for any reason be judged legally invalid or ineffective under
|
86
|
+
applicable law, such partial invalidity or ineffectiveness shall not
|
87
|
+
invalidate the remainder of the License, and in such case Affirmer hereby
|
88
|
+
affirms that he or she will not (i) exercise any of his or her remaining
|
89
|
+
Copyright and Related Rights in the Work or (ii) assert any associated claims
|
90
|
+
and causes of action with respect to the Work, in either case contrary to
|
91
|
+
Affirmer's express Statement of Purpose.
|
92
|
+
|
93
|
+
4. Limitations and Disclaimers.
|
94
|
+
|
95
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
96
|
+
surrendered, licensed or otherwise affected by this document.
|
97
|
+
|
98
|
+
b. Affirmer offers the Work as-is and makes no representations or warranties
|
99
|
+
of any kind concerning the Work, express, implied, statutory or otherwise,
|
100
|
+
including without limitation warranties of title, merchantability, fitness
|
101
|
+
for a particular purpose, non infringement, or the absence of latent or
|
102
|
+
other defects, accuracy, or the present or absence of errors, whether or not
|
103
|
+
discoverable, all to the greatest extent permissible under applicable law.
|
104
|
+
|
105
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
106
|
+
that may apply to the Work or any use thereof, including without limitation
|
107
|
+
any person's Copyright and Related Rights in the Work. Further, Affirmer
|
108
|
+
disclaims responsibility for obtaining any necessary consents, permissions
|
109
|
+
or other rights required for any use of the Work.
|
110
|
+
|
111
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
112
|
+
party to this document and has no duty or obligation with respect to this
|
113
|
+
CC0 or use of the Work.
|
114
|
+
|
115
|
+
For more information, please see
|
116
|
+
<http://creativecommons.org/publicdomain/zero/1.0/>
|
data/Manifest.txt
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
HISTORY.md
|
2
|
+
LICENSE.md
|
3
|
+
Manifest.txt
|
4
|
+
README.md
|
5
|
+
Rakefile
|
6
|
+
bin/shilling
|
7
|
+
lib/shilling.rb
|
8
|
+
lib/shilling/bank.rb
|
9
|
+
lib/shilling/block.rb
|
10
|
+
lib/shilling/blockchain.rb
|
11
|
+
lib/shilling/cache.rb
|
12
|
+
lib/shilling/ledger.rb
|
13
|
+
lib/shilling/node.rb
|
14
|
+
lib/shilling/pool.rb
|
15
|
+
lib/shilling/service.rb
|
16
|
+
lib/shilling/tool.rb
|
17
|
+
lib/shilling/transaction.rb
|
18
|
+
lib/shilling/version.rb
|
19
|
+
lib/shilling/views/_blockchain.erb
|
20
|
+
lib/shilling/views/_ledger.erb
|
21
|
+
lib/shilling/views/_peers.erb
|
22
|
+
lib/shilling/views/_pending_transactions.erb
|
23
|
+
lib/shilling/views/_wallet.erb
|
24
|
+
lib/shilling/views/index.erb
|
25
|
+
lib/shilling/views/style.scss
|
26
|
+
lib/shilling/wallet.rb
|
data/README.md
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
# shilling (or schilling) command line tool (and core library)
|
2
|
+
|
3
|
+
shilling (or schilling) on the blockchain! rock-solid alpine dollar from austria;
|
4
|
+
print (mine) your own shillings; run your own federated shilling central bank nodes
|
5
|
+
w/ public distributed (hyper) ledger book on the blockchain peer-to-peer over HTTP; revolutionize the world one block at a time with cryptos
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
* home :: [github.com/blockchainaustria/shilling.tools](https://github.com/blockchainaustria/shilling.tools)
|
10
|
+
* bugs :: [github.com/blockchainaustria/shilling.tools/issues](https://github.com/blockchainaustria/shilling.tools/issues)
|
11
|
+
* gem :: [rubygems.org/gems/shilling](https://rubygems.org/gems/shilling)
|
12
|
+
* rdoc :: [rubydoc.info/gems/shilling](http://rubydoc.info/gems/shilling)
|
13
|
+
|
14
|
+
|
15
|
+
## Command Line
|
16
|
+
|
17
|
+
Use the `shilling` command line tool. Try:
|
18
|
+
|
19
|
+
```
|
20
|
+
$ shilling -h
|
21
|
+
```
|
22
|
+
|
23
|
+
resulting in:
|
24
|
+
|
25
|
+
```
|
26
|
+
Usage: shilling [options]
|
27
|
+
|
28
|
+
Wallet options:
|
29
|
+
-n, --name=NAME Address name (default: Theresa)
|
30
|
+
|
31
|
+
Server (node) options:
|
32
|
+
-o, --host HOST listen on HOST (default: 0.0.0.0)
|
33
|
+
-p, --port PORT use PORT (default: 4567)
|
34
|
+
-h, --help Prints this help
|
35
|
+
```
|
36
|
+
|
37
|
+
To start a new (network) node using the default wallet
|
38
|
+
address (that is, Theresa) and the default server host and port settings
|
39
|
+
use:
|
40
|
+
|
41
|
+
```
|
42
|
+
$ shilling
|
43
|
+
```
|
44
|
+
|
45
|
+
Stand back ten feets :-) while starting up the machinery.
|
46
|
+
Ready to print (mine) shillings on the blockchain?
|
47
|
+
In your browser open up the page e.g. `http://localhost:4567`. Voila!
|
48
|
+
|
49
|
+
![](shilling.png)
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
Note: You can start a second node on your computer -
|
54
|
+
make sure to use a different port (use the `-p/--port` option)
|
55
|
+
and (recommended)
|
56
|
+
a different wallet address (use the `-n/--name` option).
|
57
|
+
Example:
|
58
|
+
|
59
|
+
```
|
60
|
+
$ shilling -p 5678 -n Franz
|
61
|
+
```
|
62
|
+
|
63
|
+
Happy mining!
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
## Local Development Setup
|
68
|
+
|
69
|
+
For local development - clone or download (and unzip) the shilling.tools code repo.
|
70
|
+
Next install all dependencies using bundler with a Gemfile e.g.:
|
71
|
+
|
72
|
+
``` ruby
|
73
|
+
# Gemfile
|
74
|
+
|
75
|
+
source "https://rubygems.org"
|
76
|
+
|
77
|
+
gem 'sinatra'
|
78
|
+
gem 'sass'
|
79
|
+
gem 'blockchain-lite'
|
80
|
+
```
|
81
|
+
|
82
|
+
run
|
83
|
+
|
84
|
+
```
|
85
|
+
$ bundle ## will use the Gemfile (see above)
|
86
|
+
```
|
87
|
+
|
88
|
+
and now you're ready to run your own shilling server node. Use the [`config.ru`](config.ru) script for rack:
|
89
|
+
|
90
|
+
``` ruby
|
91
|
+
# config.ru
|
92
|
+
|
93
|
+
$LOAD_PATH << './lib'
|
94
|
+
|
95
|
+
require 'shilling'
|
96
|
+
|
97
|
+
run Shilling::Service
|
98
|
+
```
|
99
|
+
|
100
|
+
and startup the money printing machine using rackup - the rack command line tool:
|
101
|
+
|
102
|
+
```
|
103
|
+
$ rackup ## will use the config.ru - rackup configuration script (see above).
|
104
|
+
```
|
105
|
+
|
106
|
+
In your browser open up the page e.g. `http://localhost:9292`. Voila! Happy mining!
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
## License
|
111
|
+
|
112
|
+
![](https://publicdomainworks.github.io/buttons/zero88x31.png)
|
113
|
+
|
114
|
+
The `shilling` scripts are dedicated to the public domain.
|
115
|
+
Use it as you please with no restrictions whatsoever.
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'hoe'
|
2
|
+
require './lib/shilling/version.rb'
|
3
|
+
|
4
|
+
Hoe.spec 'shilling' do
|
5
|
+
|
6
|
+
self.version = Shilling::VERSION
|
7
|
+
|
8
|
+
self.summary = '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 with cryptos'
|
9
|
+
self.description = summary
|
10
|
+
|
11
|
+
self.urls = ['https://github.com/blockchainaustria/shilling.tools']
|
12
|
+
|
13
|
+
self.author = 'Gerald Bauer'
|
14
|
+
self.email = 'ruby-talk@ruby-lang.org'
|
15
|
+
|
16
|
+
# switch extension to .markdown for gihub formatting
|
17
|
+
self.readme_file = 'README.md'
|
18
|
+
self.history_file = 'History.md'
|
19
|
+
|
20
|
+
self.extra_deps = [
|
21
|
+
['sinatra', '>=2.0'],
|
22
|
+
['sass'], ## used for css style preprocessing (scss)
|
23
|
+
['blockchain-lite', '>=1.3.1'],
|
24
|
+
]
|
25
|
+
|
26
|
+
self.licenses = ['Public Domain']
|
27
|
+
|
28
|
+
self.spec_extras = {
|
29
|
+
required_ruby_version: '>= 2.3'
|
30
|
+
}
|
31
|
+
|
32
|
+
end
|
data/bin/shilling
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
###################
|
4
|
+
# == DEV TIPS:
|
5
|
+
#
|
6
|
+
# For local testing run like:
|
7
|
+
#
|
8
|
+
# ruby -Ilib bin/shilling
|
9
|
+
#
|
10
|
+
# Set the executable bit in Linux. Example:
|
11
|
+
#
|
12
|
+
# % chmod a+x bin/shilling
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'shilling'
|
16
|
+
|
17
|
+
Shilling.main
|
data/lib/shilling.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# stdlibs
|
4
|
+
require 'json'
|
5
|
+
require 'digest'
|
6
|
+
require 'net/http'
|
7
|
+
require 'set'
|
8
|
+
require 'pp'
|
9
|
+
require 'optparse' ## note: used for command line tool (see Tool in tool.rb)
|
10
|
+
|
11
|
+
|
12
|
+
### 3rd party gems
|
13
|
+
require 'sinatra/base' # note: use "modular" sinatra app / service
|
14
|
+
|
15
|
+
require 'merkletree'
|
16
|
+
require 'blockchain-lite/proof_of_work/block' # note: use proof-of-work block only (for now)
|
17
|
+
|
18
|
+
|
19
|
+
### our own code
|
20
|
+
require 'shilling/version' ## let version always go first
|
21
|
+
require 'shilling/block'
|
22
|
+
require 'shilling/cache'
|
23
|
+
require 'shilling/transaction'
|
24
|
+
require 'shilling/blockchain'
|
25
|
+
require 'shilling/pool'
|
26
|
+
require 'shilling/bank'
|
27
|
+
require 'shilling/ledger'
|
28
|
+
require 'shilling/wallet'
|
29
|
+
|
30
|
+
require 'shilling/node'
|
31
|
+
require 'shilling/service'
|
32
|
+
|
33
|
+
require 'shilling/tool' ## add (optional) command line tool
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
module Shilling
|
40
|
+
|
41
|
+
|
42
|
+
class Configuration
|
43
|
+
## user/node settings
|
44
|
+
attr_accessor :address ## single wallet address (for now "clear" name e.g.Sepp, Franz, etc.)
|
45
|
+
|
46
|
+
WALLET_ADDRESSES = %w[Theresa Franz Antonia Maximilan Maria Ferdinand Elisabeth Adam Eva]
|
47
|
+
|
48
|
+
## system/blockchain settings
|
49
|
+
attr_accessor :coinbase
|
50
|
+
attr_accessor :mining_reward
|
51
|
+
|
52
|
+
## note: add a (†) coinbase marker
|
53
|
+
COINBASE = ['Großglockner†', 'Wildspitze†', 'Großvenediger†',
|
54
|
+
'Hochfeiler†', 'Zuckerhütl†', 'Hochalmspitze†',
|
55
|
+
'Gr. Muntanitz†', 'Hoher Riffler†',
|
56
|
+
'Parseierspitze†', 'Hoher Dachstein†'
|
57
|
+
]
|
58
|
+
|
59
|
+
|
60
|
+
def initialize
|
61
|
+
## try default setup via ENV variables
|
62
|
+
## pick "random" address if nil (none passed in)
|
63
|
+
@address = ENV[ 'SHILLING_NAME'] || rand_address()
|
64
|
+
|
65
|
+
@coinbase = COINBASE ## use a different name - why? why not?
|
66
|
+
## note: for now is an array (multiple coinbases)
|
67
|
+
@mining_reward = 43 ## use country code for austria (43)
|
68
|
+
end
|
69
|
+
|
70
|
+
def rand_address() WALLET_ADDRESSES[rand( WALLET_ADDRESSES.size )]; end
|
71
|
+
def rand_coinbase() @coinbase[rand( @coinbase.size )]; end
|
72
|
+
|
73
|
+
def coinbase?( address ) ## check/todo: use wallet - why? why not? (for now wallet==address)
|
74
|
+
@coinbase.include?( address )
|
75
|
+
end
|
76
|
+
|
77
|
+
end # class Configuration
|
78
|
+
|
79
|
+
|
80
|
+
## lets you use
|
81
|
+
## Shilling.configure do |config|
|
82
|
+
## config.address = 'Sepp'
|
83
|
+
## end
|
84
|
+
|
85
|
+
def self.configure
|
86
|
+
yield( config )
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.config
|
90
|
+
@config ||= Configuration.new
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
## add command line binary (tool) e.g. $ try shilling -h
|
95
|
+
def self.main
|
96
|
+
Tool.new.run(ARGV)
|
97
|
+
end
|
98
|
+
|
99
|
+
end # module Shilling
|
100
|
+
|
101
|
+
|
102
|
+
# say hello
|
103
|
+
puts Shilling::Service.banner
|
@@ -0,0 +1,109 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
class Bank
|
4
|
+
attr_reader :pending, :chain, :ledger
|
5
|
+
|
6
|
+
|
7
|
+
def initialize( address )
|
8
|
+
@address = address
|
9
|
+
|
10
|
+
## note: add address name for now to cache
|
11
|
+
## allows to start more nodes in same folder / directory
|
12
|
+
@cache = Cache.new( "data.#{address.downcase}.json" )
|
13
|
+
h = @cache.read
|
14
|
+
if h
|
15
|
+
## restore blockchain
|
16
|
+
@chain = Blockchain.from_json( h['chain'] )
|
17
|
+
## restore pending (unconfirmed) transactions pool too
|
18
|
+
@pending = Pool.from_json( h['transactions'] )
|
19
|
+
else
|
20
|
+
@chain = Blockchain.new
|
21
|
+
@chain << [Tx.new( Shilling.config.rand_coinbase,
|
22
|
+
@address,
|
23
|
+
Shilling.config.mining_reward )] # genesis (big bang!) starter block
|
24
|
+
@pending = Pool.new
|
25
|
+
end
|
26
|
+
|
27
|
+
## update ledger (balances) with confirmed transactions
|
28
|
+
@ledger = Ledger.new( @chain )
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
def mine_block!
|
34
|
+
add_transaction( Tx.new( Shilling.config.rand_coinbase,
|
35
|
+
@address,
|
36
|
+
Shilling.config.mining_reward ))
|
37
|
+
|
38
|
+
## add mined (w/ computed/calculated hash) block
|
39
|
+
@chain << @pending.transactions
|
40
|
+
@pending = Pool.new ## clear out/ empty pool (just create a new one for now)
|
41
|
+
|
42
|
+
## update ledger (balances) with new confirmed transactions
|
43
|
+
@ledger = Ledger.new( @chain )
|
44
|
+
|
45
|
+
@cache.write as_json
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def sufficient_funds?( wallet, amount )
|
50
|
+
## (convenience) delegate for ledger
|
51
|
+
## todo/check: use address instead of wallet - why? why not?
|
52
|
+
## for now single address wallet (that is, wallet==address)
|
53
|
+
@ledger.sufficient_funds?( wallet, amount )
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def add_transaction( tx )
|
58
|
+
if tx.valid? && transaction_is_new?( tx )
|
59
|
+
@pending << tx
|
60
|
+
@cache.write as_json
|
61
|
+
return true
|
62
|
+
else
|
63
|
+
return false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
##
|
69
|
+
# check - how to name incoming chain - chain_new, chain_candidate - why? why not?
|
70
|
+
# what's an intuitive name - what's gets used most often???
|
71
|
+
|
72
|
+
def resolve!( chain_new )
|
73
|
+
# TODO this does not protect against invalid block shapes (bogus COINBASE transactions for example)
|
74
|
+
|
75
|
+
if !chain_new.empty? && chain_new.last.valid? && chain_new.size > @chain.size
|
76
|
+
@chain = chain_new
|
77
|
+
## update ledger (balances) with new confirmed transactions
|
78
|
+
@ledger = Ledger.new( @chain )
|
79
|
+
|
80
|
+
## document - keep only pending transaction not yet (confirmed) in (new) blockchain ????
|
81
|
+
@pending.update!( @chain.transactions )
|
82
|
+
@cache.write as_json
|
83
|
+
return true
|
84
|
+
else
|
85
|
+
return false
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def as_json
|
92
|
+
{ chain: @chain.as_json,
|
93
|
+
transactions: @pending.as_json
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def transaction_is_new?( tx_new )
|
102
|
+
## check if tx exists already in blockchain or pending tx pool
|
103
|
+
|
104
|
+
## todo: use chain.include? to check for include
|
105
|
+
## avoid loop and create new array for check!!!
|
106
|
+
(@chain.transactions + @pending.transactions).none? { |tx| tx_new.id == tx.id }
|
107
|
+
end
|
108
|
+
|
109
|
+
end ## class Bank
|