simple_blockchain 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d9e6c664e2ea96b3f3886fba30231527b3abd2eadb410b10e223e88c61809761
4
+ data.tar.gz: 64efbb1de1e233bc547747c9f26a8eed869698ed20804d5f3ee835795abccf8f
5
+ SHA512:
6
+ metadata.gz: ff167f4c210596331cdffd54e0b6b24c37fc91d47dbef4de1dd149f45d7e514d0d3c4da15b604b2d5d2c8ddee8b1784d9c8f15f808143847cf28717382d9b3a3
7
+ data.tar.gz: b57bb47a4db0965c3b449cb4132e66e46fe13c3a8675b6112aebd48eba2be615c99c43d083a31904cb33a6ddd0bc9145a0ded4b740270596e5fe9538e119dac3
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in simple_blockchain.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,44 @@
1
+ # SimpleBlockchain
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/simple_blockchain`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'simple_blockchain'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install simple_blockchain
22
+
23
+ ## Usage
24
+
25
+ require 'simple_blockchain'
26
+
27
+ blockchain = SimpleBlockchain::Blockchain.new
28
+ puts blockchain.addBlock({ amount: 4 })
29
+ puts blockchain.addBlock({ amount: 50 })
30
+ puts blockchain.isValid
31
+ ## exemple malicious attack
32
+ puts blockchain.blocks[1].data['amount'] = 30000
33
+ puts blockchain.isValid
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ 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).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simple_blockchain.
44
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_blockchain"
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__)
@@ -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
@@ -0,0 +1,72 @@
1
+ require "simple_blockchain/version"
2
+ require 'digest'
3
+ module SimpleBlockchain
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ class Block
7
+ attr_reader :index, :previousHash, :data, :timestamp, :hash
8
+ def initialize(index = 0, previousHash = nil, data = 'Genesis block', difficulty = 1)
9
+ @index = index
10
+ @previousHash = previousHash
11
+ @data = data
12
+ @timestamp = Time.now
13
+ @difficulty = difficulty
14
+ @nonce = 0
15
+ self.mine
16
+ end
17
+
18
+ def generateHash
19
+ return Digest::SHA2.new(256).hexdigest [@index, @previousHash, @data, @timestamp, @nonce].to_s
20
+ end
21
+
22
+ def mine
23
+ @hash = self.generateHash
24
+ str_zero = '0' * @difficulty
25
+ until @hash[0..@difficulty - 1].match?str_zero,0 do
26
+ @nonce +=1
27
+ @hash = self.generateHash
28
+ end
29
+ end
30
+ end
31
+
32
+ class Blockchain
33
+ attr_reader :blocks, :index
34
+ def initialize(difficulty = 1)
35
+ @blocks = [Block.new]
36
+ @index = 1
37
+ @difficulty = difficulty
38
+ end
39
+
40
+ def getLastBlock
41
+ return @blocks.last
42
+ end
43
+
44
+ def addBlock(data)
45
+ previousHash = self.getLastBlock.hash
46
+ block = Block.new(@index, previousHash, data, @difficulty)
47
+ @index +=1
48
+ @blocks.push(block)
49
+ return previousHash
50
+ end
51
+
52
+ def isValid
53
+ for i in 1..@blocks.length - 1
54
+ currentBlock = @blocks[i]
55
+ previousBlock = @blocks[i - 1]
56
+
57
+ if currentBlock.hash != currentBlock.generateHash
58
+ return false
59
+ end
60
+
61
+ if currentBlock.index != previousBlock.index.+(1)
62
+ return false
63
+ end
64
+
65
+ if currentBlock.previousHash != previousBlock.hash
66
+ return false
67
+ end
68
+ end
69
+ return true
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleBlockchain
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/simple_blockchain/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "simple_blockchain"
5
+ spec.version = SimpleBlockchain::VERSION
6
+ spec.authors = ["Davidson"]
7
+ spec.email = ["davidsonts@outlook.com"]
8
+
9
+ spec.summary = "Simple BlockChain"
10
+ spec.description = "Simple BlockChain"
11
+ spec.homepage = "Simple BlockChain"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage = 'https://github.com/Davidsonts/simple_blockchain'
17
+ spec.metadata["source_code_uri"] = "https://github.com/Davidsonts/simple_blockchain"
18
+ spec.metadata["changelog_uri"] = "https://github.com/Davidsonts/simple_blockchain"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_blockchain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Davidson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple BlockChain
14
+ email:
15
+ - davidsonts@outlook.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - README.md
23
+ - Rakefile
24
+ - bin/console
25
+ - bin/setup
26
+ - lib/simple_blockchain.rb
27
+ - lib/simple_blockchain/version.rb
28
+ - simple_blockchain.gemspec
29
+ homepage: https://github.com/Davidsonts/simple_blockchain
30
+ licenses: []
31
+ metadata:
32
+ allowed_push_host: https://rubygems.org
33
+ homepage_uri: https://github.com/Davidsonts/simple_blockchain
34
+ source_code_uri: https://github.com/Davidsonts/simple_blockchain
35
+ changelog_uri: https://github.com/Davidsonts/simple_blockchain
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.3.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.1.2
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Simple BlockChain
55
+ test_files: []