Skip to content

Commit

Permalink
use hash as primary key for block
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienmo committed Jun 5, 2018
1 parent c6eb667 commit 54be521
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 43 deletions.
3 changes: 2 additions & 1 deletion apps/neoscan/lib/neoscan/blocks/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ defmodule Neoscan.Blocks.Block do
alias Neoscan.BlockGasGeneration
alias Neoscan.Blocks.Block

@primary_key {:hash, :string, []}
@foreign_key_type :hash
schema "blocks" do
field(:confirmations, :integer)
field(:hash, :string)
field(:index, :integer)
field(:merkleroot, :string)
field(:nextblockhash, :string)
Expand Down
10 changes: 5 additions & 5 deletions apps/neoscan/lib/neoscan/blocks/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Neoscan.Blocks do
"""
def count_blocks do
Repo.aggregate(Block, :count, :id)
Repo.aggregate(Block, :count, :hash)
end

@doc """
Expand All @@ -54,7 +54,7 @@ defmodule Neoscan.Blocks do
e in Block,
where: e.index > -1,
order_by: [
desc: e.id
desc: e.index
],
select: %{
:index => e.index,
Expand Down Expand Up @@ -84,7 +84,7 @@ defmodule Neoscan.Blocks do
e in Block,
where: e.index > -1,
order_by: [
desc: e.id
desc: e.index
],
select: %{
:index => e.index,
Expand Down Expand Up @@ -113,7 +113,7 @@ defmodule Neoscan.Blocks do
** (Ecto.NoResultsError)
"""
def get_block!(id), do: Repo.get!(Block, id)
def get_block!(hash), do: Repo.get!(Block, hash)

@doc """
Gets a single block by its hash value
Expand Down Expand Up @@ -199,7 +199,7 @@ defmodule Neoscan.Blocks do
Repo.all(query)
|> List.first()

transactions = Transactions.paginate_transactions_for_block(block.id, page)
transactions = Transactions.paginate_transactions_for_block(block.hash, page)

{block, transactions}
end
Expand Down
10 changes: 8 additions & 2 deletions apps/neoscan/lib/neoscan/transactions/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule Neoscan.Transactions.Transaction do
field(:version, :integer)
field(:vin, {:array, :map})
field(:time, :integer)
field(:block_hash, :string)
field(:block_height, :integer)
field(:nonce, :integer)
field(:claims, {:array, :map})
Expand All @@ -28,7 +27,14 @@ defmodule Neoscan.Transactions.Transaction do
field(:asset_moved, :string)

has_many(:vouts, Neoscan.Vouts.Vout)
belongs_to(:block, Neoscan.Blocks.Block)

belongs_to(
:block,
Neoscan.Blocks.Block,
foreign_key: :block_hash,
references: :hash,
type: :string
)

timestamps()
end
Expand Down
4 changes: 2 additions & 2 deletions apps/neoscan/lib/neoscan/transactions/transactions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ defmodule Neoscan.Transactions do
[%Transaction{}, ...]
"""
def paginate_transactions_for_block(id, pag) do
def paginate_transactions_for_block(hash, pag) do
transaction_query =
from(
e in Transaction,
order_by: [
desc: e.id
],
where: e.block_id == ^id,
where: e.block_hash == ^hash,
select: %{
:id => e.id,
:type => e.type,
Expand Down
8 changes: 7 additions & 1 deletion apps/neoscan/lib/neoscan/transfers/transfer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ defmodule Neoscan.Transfers.Transfer do
field(:time, :integer)
field(:check_hash, :string)

belongs_to(:block, Neoscan.Blocks.Block)
belongs_to(
:block,
Neoscan.Blocks.Block,
foreign_key: :block_hash,
references: :hash,
type: :string
)

timestamps()
end
Expand Down
4 changes: 2 additions & 2 deletions apps/neoscan/priv/repo/migrations/20170708162423_blocks.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ defmodule Neoscan.Repo.Migrations.Blocks do
use Ecto.Migration

def change do
create table(:blocks) do
create table(:blocks, primary_key: false) do
add(:confirmations, :integer)
add(:hash, :string)
add :hash, :string, primary_key: true
add(:index, :bigint)
add(:merkleroot, :string)
add(:nextblockhash, :string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule Neoscan.Repo.Migrations.Transactions do
add(:vin, {:array, :map})

add(:time, :integer)
add(:block_hash, :string)
add(:block_height, :integer)

add(:nonce, :bigint)
Expand All @@ -28,14 +27,15 @@ defmodule Neoscan.Repo.Migrations.Transactions do

add(:asset_moved, :string)

add(:block_id, references(:blocks, on_delete: :delete_all))
add(:block_hash, :string)
#add(:block_hash, references(:blocks, column: :hash, type: :string, on_delete: :delete_all))

timestamps()
end

create(index(:transactions, ["inserted_at DESC NULLS LAST"]))
create(unique_index(:transactions, [:txid]))
create(index(:transactions, [:type]))
create(index(:transactions, [:block_id]))
create(index(:transactions, [:block_hash]))
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ defmodule Neoscan.Repo.Migrations.Addresses do
end

create(unique_index(:addresses, [:address]))
create(index(:addresses, [:updated_at]))
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ defmodule Neoscan.Repo.Migrations.Transfers do
add(:time, :integer)
add(:check_hash, :string)

add(:block_id, references(:blocks, on_delete: :delete_all))
add(:block_hash, :string)
#add(:block_hash, references(:blocks, column: :hash, type: :string, on_delete: :delete_all))

timestamps()
end

create(index(:transfers, [:txid]))
create(index(:transfers, [:block_id]))
create(index(:transfers, [:block_hash]))
end
end

This file was deleted.

30 changes: 15 additions & 15 deletions apps/neoscan/test/blocks/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -588,29 +588,29 @@ defmodule Neoscan.BlocksTest do

test "get_block!/1 returns the block with given id" do
block = insert(:block)
assert Blocks.get_block!(block.id) == block
assert Blocks.get_block!(block.hash) == block
end

test "get_block_by_hash/1" do
block = insert(:block)
assert block.id == Blocks.get_block_by_hash(block.hash).id
assert block.hash == Blocks.get_block_by_hash(block.hash).hash
end

test "get_block_by_hash_for_view/1" do
block = insert(:block)
assert block.id == Blocks.get_block_by_hash_for_view(block.hash).id
assert block.hash == Blocks.get_block_by_hash_for_view(block.hash).hash
end

test "paginate_transactions/2" do
block = insert(:block)
insert(:transaction, %{block_id: block.id})
insert(:transaction, %{block_hash: block.hash})
{_, transactions} = Blocks.paginate_transactions(block.hash, 1)
assert 1 == Enum.count(transactions)
end

test "get_block_by_height/1" do
block = insert(:block)
assert block.id == Blocks.get_block_by_height(block.index).id
assert block.hash == Blocks.get_block_by_height(block.index).hash
assert is_nil(Blocks.get_block_by_height(12355))
end

Expand All @@ -635,23 +635,23 @@ defmodule Neoscan.BlocksTest do

test "get_higher_than/1" do
block1 = insert(:block)
%{id: block_id} = insert(:block)
assert [%{id: ^block_id}] = Blocks.get_higher_than(block1.index)
%{hash: block_hash} = insert(:block)
assert [%{hash: ^block_hash}] = Blocks.get_higher_than(block1.index)
end

test "delete_higher_than/1" do
block1 = insert(:block)
%{id: block_id} = insert(:block)
assert [] == Blocks.delete_higher_than(block_id)
assert [%{id: ^block_id}] = Blocks.get_higher_than(block1.index)
%{hash: block_hash, index: index} = insert(:block)
assert [] == Blocks.delete_higher_than(index)
assert [%{hash: ^block_hash}] = Blocks.get_higher_than(index - 1)

assert block_id ==
assert block_hash ==
block1.index
|> Blocks.delete_higher_than()
|> List.first()
|> Map.get(:id)
|> Map.get(:hash)

assert [] = Blocks.get_higher_than(block1.index)
assert [] = Blocks.get_higher_than(index)
end

test "update_block/2 with valid data updates the block" do
Expand All @@ -676,13 +676,13 @@ defmodule Neoscan.BlocksTest do
test "update_block/2 with invalid data returns error changeset" do
block = insert(:block)
assert {:error, %Ecto.Changeset{}} = Blocks.update_block(block, %{"confirmations" => nil})
assert block == Blocks.get_block!(block.id)
assert block == Blocks.get_block!(block.hash)
end

test "delete_block/1 deletes the block" do
block = insert(:block)
assert %Block{} = Blocks.delete_block(block)
assert_raise Ecto.NoResultsError, fn -> Blocks.get_block!(block.id) end
assert_raise Ecto.NoResultsError, fn -> Blocks.get_block!(block.hash) end
end

test "add_block/1" do
Expand Down
1 change: 0 additions & 1 deletion apps/neoscan/test/transactions/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ defmodule Neoscan.Transactions.TransactionTest do
assert %{version: ["can't be blank"]} = errors_on(changeset)
assert %{vin: ["can't be blank"]} = errors_on(changeset)
assert %{time: ["can't be blank"]} = errors_on(changeset)
assert %{block_hash: ["can't be blank"]} = errors_on(changeset)
assert %{block_height: ["can't be blank"]} = errors_on(changeset)
end

Expand Down
9 changes: 7 additions & 2 deletions apps/neoscan/test/transactions/transactions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ defmodule Neoscan.TransactionsTest do

test "paginate_transactions_for_block/2" do
block = insert(:block)
transaction1 = insert(:transaction, %{type: "InvocationTransaction", block_id: block.id})

transaction1 =
insert(:transaction, %{type: "InvocationTransaction", block_hash: block.hash})

insert(:vout, %{transaction_id: transaction1.id})

assert 1 ==
Enum.count(Transactions.paginate_transactions_for_block(transaction1.block_id, 1))
Enum.count(
Transactions.paginate_transactions_for_block(transaction1.block_hash, 1)
)
end

test "get_transaction_vouts/1" do
Expand Down

0 comments on commit 54be521

Please sign in to comment.