blockchain.rb
def resolve_conflicts
    neighbors = nodes
    new_chain = nil
    max_length = chain.length

    neighbors.each do |node|
      response = get_request(URI.parse("http://#{node}/chain"))
      if response.code == '200'
        response_body = JSON.parse(response.body)
        length = response_body['length']
        chain = response_body['chain']

        # Check if the length is longer and the chain is valid
        if length > max_length && valid_chain?(chain)
          max_length = length
          new_chain = chain
        end
      end
    end

    # Replace our chain if we discovered a new, valid chain longer than ours
    if new_chain
      @chain = new_chain
      true
    else
      false
    end
  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
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
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
    max_length = chain.length
    neighbors.each do |node|
      response = get_request(URI.parse("http://#{node}/chain"))
      if response.code == '200'
        response_body = JSON.parse(response.body)
        length = response_body['length']
        chain = response_body['chain']
        if length > max_length && valid_chain?(chain)
          max_length = length
          new_chain = chain
        end
      end
    end
    if new_chain
      @chain = new_chain
      true
    else
      false
    end
  end
def resolve_conflicts neighbors = nodes new_chain = nil max_length = chain . length neighbors . each do | node | response = get_request ( URI . parse ( "http://#{node}/chain" ) ) if response . code == '200' response_body = JSON . parse ( response . body ) length = response_body [ 'length' ] chain = response_body [ 'chain' ] if length > max_length && valid_chain? ( chain ) max_length = length new_chain = chain end end end if new_chain @chain = new_chain true else false end end
This is our consensus algorithm, it resolves conflicts
by replacing our chain with the longest one in the network.
This is our consensus algorithm it resolves conflicts by replacing our chain with the longest one in the network .
# Check if the length is longer and the chain is valid # Replace our chain if we discovered a new, valid chain longer than ours