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.BlockchainTest do | |
.... | |
test "validate a chain", %{blockchain: blockchain} do | |
# add block into blockchain | |
blockchain = Blockchain.add_block(blockchain, "some-block-data") | |
# assert if blockchain is valid | |
assert Blockchain.valid_chain?(blockchain) | |
end |
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 | |
""" | |
.... | |
@spec add_block(BlockChain.t(), any) :: BlockChain.t() | |
def add_block(blockchain = %__MODULE__{chain: chain}, data) do | |
{last_block, _} = List.pop_at(chain, -1) |
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 | |
defstruct ~w(chain)a | |
@type t :: %Blockchain{ |
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.BlockchainTest do | |
@moduledoc """ | |
This module contains test related to a blockchain | |
""" | |
use ExUnit.Case | |
alias ExChain.Blockchain | |
alias ExChain.Blockchain.Block |
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.BlockTest do | |
@moduledoc """ | |
This module contains test related to a block | |
""" | |
use ExUnit.Case | |
alias ExChain.Blockchain.Block | |
describe "block" do | |
test "genesis is valid" do |
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.Block do | |
@moduledoc """ | |
This module is the single block struct in a blockchain | |
""" | |
alias __MODULE__ | |
@type t :: %Block{ | |
timestamp: pos_integer(), | |
last_hash: String.t(), |
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.Block do | |
@moduledoc """ | |
This module is the single block struct in a blockchain | |
""" | |
alias __MODULE__ | |
@type t :: %Block{ | |
timestamp: pos_integer(), | |
last_hash: String.t(), |
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.BlockTest do | |
@moduledoc """ | |
This module contains test related to a block | |
""" | |
use ExUnit.Case | |
alias ExChain.BlockChain.Block | |
describe "block" do | |
test "genesis is valid" do |
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.Block do | |
@moduledoc """ | |
This module is the single block struct in a blockchain | |
""" | |
alias __MODULE__ | |
@type t :: %Block{ | |
timestamp: pos_integer(), | |
last_hash: String.t(), | |
hash: String.t(), |
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.Block do | |
@moduledoc """ | |
This module is the single block struct in a blockchain | |
""" | |
defstruct ~w(timestamp last_hash hash data)a | |
end |