Skip to content

Commit

Permalink
Remove BlockChain::Block{HashMismatch, DifficultyError, BlockTimeErro…
Browse files Browse the repository at this point in the history
…r, NotValid}
  • Loading branch information
oguzbilgic committed Dec 16, 2018
1 parent 5907f7e commit 74cfd10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
37 changes: 17 additions & 20 deletions src/zincir/blockchain.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ require "./difficulty"
module Zincir
class Blockchain
class BlockNotAdded < ::Exception end
class BlockHashMismatch < BlockNotAdded end
class BlockIndexTooHigh < BlockNotAdded end
class BlockDifficultyError < BlockNotAdded end
class BlockNotPreferred < BlockNotAdded end
class BlockTimeError < BlockNotAdded end
class BlockNotValid < BlockNotAdded end
class BlockOnForkChain < BlockNotAdded end

# Desired time between blocks
Expand Down Expand Up @@ -67,7 +63,7 @@ module Zincir

private def add_block(block)
unless block.valid?
raise BlockNotValid.new "Invalid block at index #{block.index}"
raise BlockNotAdded.new "Invalid block at index #{block.index}"
end

if block.index > next_index
Expand All @@ -80,32 +76,33 @@ module Zincir
return if our_block.hash == block.hash

# if this is the first block of the fork
if our_block.previous_hash == block.previous_hash
# raise if the forked chain's first block isn't better than ours
if our_block.timestamp < block.timestamp
raise BlockNotPreferred.new "Blockchain contains a better block for index #{block.index}"
end

puts "Reseting chain with #{block}"
@blocks = @blocks[0..block.index]
@blocks << block
emit :block, block
return
if our_block.previous_hash != block.previous_hash
raise BlockOnForkChain.new
end

raise BlockOnForkChain.new
# raise if the forked chain's first block isn't better than ours
if our_block.timestamp < block.timestamp
raise BlockNotPreferred.new "Blockchain contains a better block for index #{block.index}"
end

puts "Reseting chain with #{block}"
@blocks = @blocks[0..block.index]
@blocks << block
emit :block, block
return
end

if block.previous_hash != last.hash
raise BlockHashMismatch.new "Hash mismatch for block at index #{block.index}"
# raise BlockHashMismatch.new "Hash mismatch for block at index #{block.index}"
raise BlockOnForkChain.new
end

if block.difficulty != next_difficulty
raise BlockDifficultyError.new "Wrong difficulty block: #{block.difficulty} our: #{next_difficulty}"
raise BlockNotAdded.new "Wrong difficulty block: #{block.difficulty} our: #{next_difficulty}"
end

if block.timestamp <= last.timestamp
raise BlockTimeError.new "Block time is wrong #{block.index}"
raise BlockNotAdded.new "Block time is wrong #{block.index}"
end

if last.difficulty != block.difficulty
Expand Down
2 changes: 1 addition & 1 deletion src/zincir/storage/network.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Zincir

blockchain.queue_block block
go_back_index = nil
rescue Blockchain::BlockHashMismatch | Blockchain::BlockOnForkChain
rescue Blockchain::BlockOnForkChain
puts "Downloaded block's previous hash doesn't match with ours, will check previous..."
go_back_index = go_back_index ? go_back_index -1 : index.not_nil! - 2
rescue Blockchain::BlockNotPreferred
Expand Down

0 comments on commit 74cfd10

Please sign in to comment.