-
Notifications
You must be signed in to change notification settings - Fork 1
/
miner.rb
41 lines (33 loc) · 790 Bytes
/
miner.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
require './dag.rb'
class Miner
attr_reader :id, :dag, :propagation_delay
attr_accessor :peers
def initialize(id:, genesis_block:)
@id = id
@dag = DAG.new(self, genesis_block)
@peers = []
puts "Miner ##{@id} started with genesis block #{genesis_block.id}"
end
def create_block
dag.create_block
end
def next_block
block = create_block
propogate_block_to_peers(block)
end
def propogate_block_to_peers(block)
peers.each do |peer|
Thread.new do
sleep(random_propagation_delay)
peer.add_block(block)
end
end
end
def random_propagation_delay
random_delay = @@propagation_delay * rand(-0.1...0.1)
@@propagation_delay + random_delay
end
def add_block(block)
dag.add_block(block)
end
end