Skip to content

Instantly share code, notes, and snippets.

View yatender-oktalk's full-sized avatar
✌️
Focusing

Yatender Singh yatender-oktalk

✌️
Focusing
View GitHub Profile
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
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)
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{
defmodule ExChain.BlockchainTest do
@moduledoc """
This module contains test related to a blockchain
"""
use ExUnit.Case
alias ExChain.Blockchain
alias ExChain.Blockchain.Block
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
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(),
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(),
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
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(),
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