Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yatender-oktalk/bce4230ddbf9449ca923b974f5e9c86b to your computer and use it in GitHub Desktop.
Save yatender-oktalk/bce4230ddbf9449ca923b974f5e9c86b to your computer and use it in GitHub Desktop.
defmodule ExChain.Blockchain do
@moduledoc """
This module contains the blockchain related functions
"""
alias __MODULE__
alias ExChain.Blockchain.Block
...
@spec valid_chain?(Blockchain.t()) :: boolean()
def valid_chain?(%__MODULE__{chain: chain}) do
chain
|> Enum.chunk_every(2, 1, :discard)
|> Enum.all?(fn [prev_block, block] ->
valid_last_hash?(prev_block, block) && valid_block_hash?(prev_block)
end)
end
# Private functions
defp valid_last_hash?(
%Block{hash: hash} = _last_block,
%Block{last_hash: last_hash} = _current_block
) do
hash == last_hash
end
defp valid_block_hash?(current_block) do
current_block.hash == Block.block_hash(current_block)
end
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment