lapidar 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d024f24e254ae420266eb80b779cdca010785af1
4
+ data.tar.gz: 8012ac6e40bfcd0be7644d9b4b002466a63cd841
5
+ SHA512:
6
+ metadata.gz: c6ee55f3749e40f80d361be1d8ddbde43bdb9c1abdbbd572c4fa6845320006e3ac800a4a80f2ac35a3ab04b6378829f4ec5005633ef2e368b45bd666ffe8cfb0
7
+ data.tar.gz: 7cc94309a33288b6d48d89ac6dd1a45ff5cd83fe1d21e39d69de32940b4e72dbc8c600e0a990fcd167f1855f9855877f823b59da71f37741eb96d418f4f46744
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ Gemfile.lock
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.8
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3
5
+ - 2.4
6
+ - 2.5
7
+ - 2.6
8
+ cache: bundler
9
+ script: bin/check
data/CHANGELOG.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lapidar.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Josua Schmid
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Lapidar
2
+
3
+ Carve it in stone. Only that these stones are easy to move across the internet.
4
+ This is a multi purpose blockchain on which you can build some custom business logic.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'lapidar'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install lapidar
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lapidar.
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/check ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+
3
+ set -e
4
+
5
+ bundle exec standardrb --fix
6
+ bundle exec rspec
7
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lapidar"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/run ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler"
4
+ Bundler.setup(:default)
5
+
6
+ require_relative '../lib/lapidar'
7
+
8
+ puts 'Starting experiment…'
9
+
10
+ [
11
+ Thread.new { Lapidar.start_mining(port: 9999, neighbors: [{ host: 'localhost', port: 9995 }]) },
12
+ Thread.new { Lapidar.start_mining(port: 9998, neighbors: [{ host: 'localhost', port: 9996 }]) },
13
+ Thread.new { Lapidar.start_mining(port: 9997, neighbors: [{ host: 'localhost', port: 9997 }]) },
14
+ Thread.new { Lapidar.start_mining(port: 9996, neighbors: [{ host: 'localhost', port: 9998 }]) },
15
+ Thread.new { Lapidar.start_mining(port: 9995, neighbors: [{ host: 'localhost', port: 9999 }]) }
16
+ ].each(&:join)
17
+
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lapidar.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "lapidar/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lapidar"
7
+ spec.version = Lapidar::VERSION
8
+ spec.authors = ["Josua Schmid"]
9
+ spec.email = ["josua.schmid@renuo.ch"]
10
+
11
+ spec.summary = 'Carve it in stone. Only that these stones are easy to move across the internet.
12
+ This is a multi purpose blockchain on which you can build some custom business logic.'
13
+ spec.description = 'This is a custom blockchain with a working network layer. It just mines and receives blocks
14
+ and evaluates block order and correctness. Build any distributed business logic on top of it.'
15
+ spec.homepage = "https://github.com/renuo/lapidar"
16
+ spec.license = "MIT"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/renuo/lapidar"
20
+ spec.metadata["changelog_uri"] = "https://github.com/renuo/lapidar/blob/master/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.required_ruby_version = ">= 2.3.0"
32
+
33
+ spec.add_dependency "buschtelefon", "~> 0.2"
34
+ spec.add_development_dependency "bundler", ">= 1.17"
35
+ spec.add_development_dependency "factory_bot", "~> 5.0"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.8"
38
+ spec.add_development_dependency "simplecov", "~> 0.17"
39
+ spec.add_development_dependency "standard", "> 0"
40
+ end
@@ -0,0 +1,23 @@
1
+ module Lapidar
2
+ class Assessment
3
+ def self.valid_link?(previous_block, block)
4
+ hash(previous_block&.hash, block.nonce, block.data) == block.hash
5
+ end
6
+
7
+ def self.meets_difficulty?(block)
8
+ block.hash.start_with?("0000")
9
+ end
10
+
11
+ def self.genesis?(block)
12
+ first?(block) && valid_link?(nil, block)
13
+ end
14
+
15
+ def self.first?(block)
16
+ block.number.zero?
17
+ end
18
+
19
+ def self.hash(previous_hash, nonce, data)
20
+ Digest::SHA256.hexdigest("#{previous_hash}-#{nonce}-#{data}")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Lapidar
2
+ class Block
3
+ attr_reader :number, :hash, :data, :nonce, :created_at
4
+
5
+ def initialize(number:, hash:, nonce:, data: nil, created_at: Time.now)
6
+ @number = number
7
+ @hash = hash
8
+ @nonce = nonce
9
+ @data = data
10
+ @created_at = created_at
11
+ end
12
+
13
+ def to_h
14
+ {
15
+ number: @number,
16
+ hash: @hash,
17
+ nonce: @nonce,
18
+ data: @data,
19
+ created_at: @created_at,
20
+ }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,59 @@
1
+ module Lapidar
2
+ class Chain
3
+ def initialize
4
+ @blocks = []
5
+ end
6
+
7
+ # TODO: Queue up future blocks for later use
8
+ # TODO: Check for duplicates and dont add them to the chains
9
+ def add(block)
10
+ raise "future block?" if block.number > @blocks.count
11
+ raise "invalid block" unless valid?(block)
12
+
13
+ @blocks[block.number] ||= []
14
+ @blocks[block.number].push(block)
15
+
16
+ # Rebalance if second to last block has more than one candidate
17
+ rebalance if !contested?(block.number) && contested?(block.number - 1)
18
+ end
19
+
20
+ # For each positition in the chain the candidate positioned first is considered the valid one
21
+ def blocks
22
+ @blocks.map { |candidates| candidates&.first }
23
+ end
24
+
25
+ private
26
+
27
+ def valid?(block)
28
+ return true if Assessment.genesis?(block) # early valid if genesis
29
+ return false if Assessment.first?(block) # early invalid if fake genesis
30
+ return false unless Assessment.meets_difficulty?(block) # early invalid if difficulty not met
31
+
32
+ # Check if there's an existing parent
33
+ @blocks[block.number - 1].any? do |previous_block|
34
+ Assessment.valid_link?(previous_block, block)
35
+ end
36
+ end
37
+
38
+ # If a new last block comes in, we realign the first blocks to build the longest chain
39
+ def rebalance
40
+ winning_block = @blocks.last.first
41
+ parent_position = @blocks.count - 2
42
+
43
+ while contested?(parent_position)
44
+ # TODO: Is there's a smarter way to persistently select a winner than sorting the competition
45
+ @blocks[parent_position].sort_by! do |previous_block|
46
+ Assessment.valid_link?(previous_block, winning_block) ? 0 : 1
47
+ end
48
+
49
+ winning_block = @blocks[parent_position].first
50
+ parent_position -= 1
51
+ end
52
+ end
53
+
54
+ # Contested evaluates to true if there blocks are competing for the same position in the blockchain
55
+ def contested?(block_number)
56
+ @blocks[block_number].count > 1
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,31 @@
1
+ require "digest"
2
+
3
+ module Lapidar
4
+ class Miner
5
+ def initialize
6
+ @stomach = Digest::SHA2.new(256)
7
+ end
8
+
9
+ def mine(base_block, data = "")
10
+ base_block ||= god
11
+ nonce = 0
12
+ nonce += 1 until meets_difficulty?(digest(base_block, nonce, data))
13
+ Block.new(number: base_block.number + 1, hash: digest(base_block, nonce, data), nonce: nonce, data: data)
14
+ end
15
+
16
+ private
17
+
18
+ def digest(base_block, nonce, data)
19
+ @stomach.hexdigest("#{base_block.hash}-#{nonce}-#{data}")
20
+ end
21
+
22
+ def meets_difficulty?(digest)
23
+ digest.start_with?("0000")
24
+ end
25
+
26
+ def god
27
+ # Virtual block descending from Heaven to create the genesis block
28
+ Block.new(number: -1, hash: nil, nonce: nil)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Lapidar
2
+ VERSION = "0.1.0"
3
+ end
data/lib/lapidar.rb ADDED
@@ -0,0 +1,81 @@
1
+ require_relative "lapidar/assessment"
2
+ require_relative "lapidar/block"
3
+ require_relative "lapidar/chain"
4
+ require_relative "lapidar/miner"
5
+ require_relative "lapidar/version"
6
+
7
+ require "json"
8
+ require "buschtelefon"
9
+
10
+ module Lapidar
11
+ def self.start_mining(port:, neighbors:)
12
+ chain = Chain.new
13
+ miner = Miner.new
14
+ incoming_blocks = Queue.new
15
+
16
+ me = Buschtelefon::NetTattler.new(port: port)
17
+ neighbors.map! { |neighbor_location| Buschtelefon::RemoteTattler.new(neighbor_location) }
18
+ neighbors.each { |neighbor| me.connect(neighbor) }
19
+
20
+ Thread.abort_on_exception = true
21
+
22
+ consumer = Thread.new {
23
+ until_shutdown do
24
+ begin
25
+ chain.add(incoming_blocks.pop)
26
+ print "+"
27
+ rescue
28
+ puts "Consumer error"
29
+ end
30
+ end
31
+ }
32
+
33
+ network_producer = Thread.new {
34
+ until_shutdown do
35
+ me.listen do |message|
36
+ begin
37
+ incoming_json = JSON.parse(message, symbolize_names: true)
38
+
39
+ incoming_blocks << Block.new(
40
+ number: incoming_json[:number].to_i,
41
+ hash: incoming_json[:hash].to_s,
42
+ nonce: incoming_json[:none].to_i,
43
+ data: incoming_json[:data].to_s,
44
+ created_at: Time.parse(incoming_json[:created_at])
45
+ )
46
+ rescue JSON::ParserError, ArgumentError => e
47
+ puts "Incoming block isn't valid: #{e.message}"
48
+ end
49
+ end
50
+ end
51
+ }
52
+
53
+ local_producer = Thread.new {
54
+ until_shutdown do
55
+ new_block = miner.mine(chain.blocks.last)
56
+
57
+ me.feed(Buschtelefon::Gossip.new(new_block.to_h.to_json))
58
+ incoming_blocks << new_block
59
+
60
+ print "⚒ "
61
+ end
62
+ }
63
+
64
+ local_producer.join
65
+ network_producer.join
66
+ consumer.join
67
+
68
+ puts "\nShutting down…"
69
+ end
70
+
71
+ def self.until_shutdown
72
+ trap "SIGINT" do
73
+ puts "\nshutting down"
74
+ exit
75
+ end
76
+
77
+ loop do
78
+ yield
79
+ end
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lapidar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josua Schmid
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: buschtelefon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_bot
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.17'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.17'
97
+ - !ruby/object:Gem::Dependency
98
+ name: standard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: |-
112
+ This is a custom blockchain with a working network layer. It just mines and receives blocks
113
+ and evaluates block order and correctness. Build any distributed business logic on top of it.
114
+ email:
115
+ - josua.schmid@renuo.ch
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".ruby-version"
123
+ - ".travis.yml"
124
+ - CHANGELOG.md
125
+ - Gemfile
126
+ - LICENSE.txt
127
+ - README.md
128
+ - Rakefile
129
+ - bin/check
130
+ - bin/console
131
+ - bin/run
132
+ - bin/setup
133
+ - lapidar.gemspec
134
+ - lib/lapidar.rb
135
+ - lib/lapidar/assessment.rb
136
+ - lib/lapidar/block.rb
137
+ - lib/lapidar/chain.rb
138
+ - lib/lapidar/miner.rb
139
+ - lib/lapidar/version.rb
140
+ homepage: https://github.com/renuo/lapidar
141
+ licenses:
142
+ - MIT
143
+ metadata:
144
+ homepage_uri: https://github.com/renuo/lapidar
145
+ source_code_uri: https://github.com/renuo/lapidar
146
+ changelog_uri: https://github.com/renuo/lapidar/blob/master/CHANGELOG.md
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: 2.3.0
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.5.2.3
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Carve it in stone. Only that these stones are easy to move across the internet.
167
+ This is a multi purpose blockchain on which you can build some custom business logic.
168
+ test_files: []