Skip to content

Commit

Permalink
Fix nil issue
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzbilgic committed Nov 30, 2018
1 parent 1839854 commit 51db2e1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
5 changes: 4 additions & 1 deletion blockchain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ def block_at index

def << block
@blocks << block

puts block
end

def work!
loop do
next_block = Block.next $blockchain.last, "Transaction Data..."

puts next_block
@blocks << next_block

puts next_block
end
end
end
17 changes: 10 additions & 7 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
class Web < Sinatra::Base
configure do
set :port, 4000+rand(1000)
# set :quiet, true
# set :logging, false
set :quiet, true
set :logging, false
end

get '/blocks' do
json $blockchain.last.to_hash
end

get '/blocks/:index' do
index = params['index']
json $blockchain.block_at(index.to_i).to_hash
index = params['index'].to_i
if index > $blockchain.last.index
status 404
return
end
json $blockchain.block_at(index).to_hash
end
end

Expand All @@ -32,16 +36,15 @@ class Web < Sinatra::Base

# Download blocks from the seed node
if ARGV[0]
puts "Seed node: #{seed_node}"
puts "Seed node: #{ARGV[0]}"

loop do
index = $blockchain.last.index.to_i + 1
response = HTTParty.get("#{ARGV[0]}/blocks/#{index.to_s}")

break if response.code != 200

$blockchain << Block.from_json_str response.body
puts block
$blockchain << Block.from_json_str(response.body)
end

puts 'Finished downloading the chain'
Expand Down

0 comments on commit 51db2e1

Please sign in to comment.