Sha256: 70f3be167fcc76a468c07bb5e5d852aeb675cfc7cfa3966b85f9bde6ab9c12aa

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

module Dcha
  class Peer
    # :nodoc:
    module HasBlockchain
      def chain
        @chain ||= Chain.new
      end

      def blocks(_address)
        transmit action: :mine, params: [chain.blocks]
      end

      def mine(blocks)
        blocks.sort! { |x, y| x.index <=> y.index }
        return unless blocks.last.index > chain.blocks.last.index
        append_blocks(blocks)
      end

      def create_block(root_hash)
        return unless chain.create_and_add_block(root_hash)
        transmit action: :mine, params: [chain.blocks.last(1)]
      end

      private

      def append_blocks(blocks)
        if block_linked?(blocks)
          add_block(blocks)
        elsif blocks.length == 1
          ask_blocks
        elsif blocks.length > 1
          replace_blocks(blocks)
        end
      end

      def add_block(blocks)
        return unless blocks.last.valid_proof?
        chain.add_block(blocks.last)
        reset(blocks.last.root_hash)
        transmit action: :mine, params: [blocks.last(1)]
      end

      def ask_blocks
        transmit action: :blocks, params: [ipaddr.ip_address]
      end

      def replace_blocks(blocks)
        blocks.shift if blocks.first.index.zero?
        chain.replace_with(blocks)
        reset(blocks.last.root_hash)
        transmit action: :mine, params: [blocks.last(1)]
      end

      def block_linked?(blocks)
        blocks.last.parent_hash == chain.blocks.last.hash
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dcha-0.1.4 lib/dcha/peer/has_blockchain.rb
dcha-0.1.3 lib/dcha/peer/has_blockchain.rb
dcha-0.1.2 lib/dcha/peer/has_blockchain.rb
dcha-0.1.1 lib/dcha/peer/has_blockchain.rb
dcha-0.1.0 lib/dcha/peer/has_blockchain.rb