-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockchain.rb
executable file
·92 lines (78 loc) · 2.31 KB
/
blockchain.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'json'
require 'set'
require './validator'
require './crypto_helper'
class Blockchain
attr_reader :chain, :validator, :nodes
def initialize
@chain = []
@validator = Validator.new(chain)
@nodes = Set.new
# Create the genesis block
new_block(previous_hash: 1, transaction: {})
end
# Create a new block in the blockchain
def new_block(transaction:, previous_hash: nil)
block = {
index: chain.length + 1,
timestamp: Time.now.to_f,
transaction: transaction,
previous_hash: previous_hash || CryptoHelper.block_hash(last_block)
}
chain.push(block)
block
end
# Creates a new transaction
def new_transaction(vote_cipher:, ring_members:, ring_sig_hex:)
if validator.valid_transaction?(vote_cipher, ring_members, ring_sig_hex)
transaction = {
vote: vote_cipher,
ring_members: ring_members,
ring_signature: ring_sig_hex
}
ring_sig = CryptoHelper.ringsig_from(ring_sig_hex)
validator.add_voter(ring_sig.key_image)
new_block(transaction: transaction)
end
end
def last_block
chain[-1]
end
# This is our consensus algorithm, it resolves conflicts
# by replacing our chain with the longest one in the network.
# True if our chain was replaced, False if not
def resolve_conflicts
neighbors = nodes
new_chain = nil
new_validator = nil
max_length = chain.length
neighbors.each do |node|
response = RestClient.get("http://#{node}/chain")
next unless response.code == '200'
response_body = JSON.parse(response.body)
length = response_body['length']
chain = response_body['chain']
validator = Validator.new(chain)
# Check if the length is longer and the chain is valid
if length > max_length && local_validator.valid_chain?
max_length = length
new_chain = chain
new_validator = validator
end
end
# Replace our chain if we discovered a new, valid chain longer than ours
if new_chain
@chain = new_chain
@validator = new_validator
true
else
false
end
end
# Add a new node to the list of nodes
# address ex: http://192.168.0.5:5000
def register_node(address)
parsed_url = URI.parse(address)
nodes.add("#{parsed_url.host}:#{parsed_url.port}")
end
end