Last active
October 2, 2020 12:26
-
-
Save yatender-oktalk/bce4230ddbf9449ca923b974f5e9c86b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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