Explorer.Chain.Hash.Truncated -> Explorer.Chain.Hash.Address

Blocks nonce will also be a type of hash, but it is only 8-bytes,
instead of 20-bytes, so the general name "Truncated" will be confusing,
so name it "Address" to be specific.
pull/335/head
Luke Imhoff 7 years ago
parent 2701d1fd20
commit 904a63b504
  1. 29
      apps/explorer/lib/explorer/chain.ex
  2. 4
      apps/explorer/lib/explorer/chain/address.ex
  3. 4
      apps/explorer/lib/explorer/chain/block.ex
  4. 2
      apps/explorer/lib/explorer/chain/data.ex
  5. 20
      apps/explorer/lib/explorer/chain/hash/address.ex
  6. 10
      apps/explorer/lib/explorer/chain/internal_transaction.ex
  7. 4
      apps/explorer/lib/explorer/chain/log.ex
  8. 2
      apps/explorer/lib/explorer/chain/smart_contract.ex
  9. 10
      apps/explorer/lib/explorer/chain/transaction.ex
  10. 5
      apps/explorer/test/explorer/chain/hash/address_test.exs
  11. 5
      apps/explorer/test/explorer/chain/hash/truncated_test.exs
  12. 2
      apps/explorer/test/support/factory.ex
  13. 2
      apps/indexer/lib/indexer/address_balance_fetcher.ex

@ -178,7 +178,7 @@ defmodule Explorer.Chain do
"""
@spec address_to_transactions(Address.t(), [paging_options | necessity_by_association_option]) :: [Transaction.t()]
def address_to_transactions(
%Address{hash: %Hash{byte_count: unquote(Hash.Truncated.byte_count())} = address_hash},
%Address{hash: %Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash},
options \\ []
)
when is_list(options) do
@ -356,7 +356,7 @@ defmodule Explorer.Chain do
[
[{:addresses, [timeout_option]}] | timeout_option
]
) :: {:ok, [Hash.Truncated.t()]} | {:error, [Changeset.t()]}
) :: {:ok, [Hash.Address.t()]} | {:error, [Changeset.t()]}
def update_balances(addresses_params, options \\ []) when is_list(options) do
with {:ok, changes_list} <- changes_list(addresses_params, for: Address, with: :balance_changeset) do
timestamps = timestamps()
@ -504,12 +504,12 @@ defmodule Explorer.Chain do
...> %{hash: "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0"}
...> )
...> errors
[hash: {"is invalid", [type: Explorer.Chain.Hash.Truncated, validation: :cast]}]
[hash: {"is invalid", [type: Explorer.Chain.Hash.Address, validation: :cast]}]
iex> {:error, %Ecto.Changeset{errors: errors}} = Explorer.Chain.create_address(
...> %{hash: "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0ba"}
...> )
...> errors
[hash: {"is invalid", [type: Explorer.Chain.Hash.Truncated, validation: :cast]}]
[hash: {"is invalid", [type: Explorer.Chain.Hash.Address, validation: :cast]}]
"""
@spec create_address(map()) :: {:ok, Address.t()} | {:error, Ecto.Changeset.t()}
@ -618,8 +618,8 @@ defmodule Explorer.Chain do
{:error, :not_found}
"""
@spec hash_to_address(Hash.Truncated.t()) :: {:ok, Address.t()} | {:error, :not_found}
def hash_to_address(%Hash{byte_count: unquote(Hash.Truncated.byte_count())} = hash) do
@spec hash_to_address(Hash.Address.t()) :: {:ok, Address.t()} | {:error, :not_found}
def hash_to_address(%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash) do
query =
from(
address in Address,
@ -635,7 +635,7 @@ defmodule Explorer.Chain do
end
end
def find_contract_address(%Hash{byte_count: unquote(Hash.Truncated.byte_count())} = hash) do
def find_contract_address(%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash) do
query =
from(
address in Address,
@ -1182,7 +1182,7 @@ defmodule Explorer.Chain do
]) ::
{:ok,
%{
optional(:addresses) => [Hash.Truncated.t()],
optional(:addresses) => [Hash.Address.t()],
optional(:blocks) => [Hash.Full.t()],
optional(:internal_transactions) => [
%{required(:index) => non_neg_integer(), required(:transaction_hash) => Hash.Full.t()}
@ -1233,7 +1233,7 @@ defmodule Explorer.Chain do
]) ::
{:ok,
%{
optional(:addresses) => [Hash.Truncated.t()],
optional(:addresses) => [Hash.Address.t()],
optional(:internal_transactions) => [
%{required(:index) => non_neg_integer(), required(:transaction_hash) => Hash.Full.t()}
]
@ -1634,7 +1634,7 @@ defmodule Explorer.Chain do
@spec stream_unfetched_addresses(
initial :: accumulator,
reducer ::
(entry :: %{block_number: Block.block_number(), hash: Hash.Truncated.t()}, accumulator -> accumulator)
(entry :: %{block_number: Block.block_number(), hash: Hash.Address.t()}, accumulator -> accumulator)
) :: {:ok, accumulator}
when accumulator: term()
def stream_unfetched_addresses(initial, reducer) when is_function(reducer, 2) do
@ -1991,7 +1991,7 @@ defmodule Explorer.Chain do
end
@doc """
The `string` must start with `0x`, then is converted to an integer and then to `t:Explorer.Chain.Hash.Truncated.t/0`.
The `string` must start with `0x`, then is converted to an integer and then to `t:Explorer.Chain.Hash.Address.t/0`.
iex> Explorer.Chain.string_to_address_hash("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
{
@ -2008,9 +2008,9 @@ defmodule Explorer.Chain do
:error
"""
@spec string_to_address_hash(String.t()) :: {:ok, Hash.Truncated.t()} | :error
@spec string_to_address_hash(String.t()) :: {:ok, Hash.Address.t()} | :error
def string_to_address_hash(string) when is_binary(string) do
Hash.Truncated.cast(string)
Hash.Address.cast(string)
end
@doc """
@ -2274,8 +2274,7 @@ defmodule Explorer.Chain do
)
end
@spec insert_addresses([%{hash: Hash.Truncated.t()}], [timeout_option | timestamps_option]) ::
{:ok, [Hash.Truncated.t()]}
@spec insert_addresses([%{hash: Hash.Address.t()}], [timeout_option | timestamps_option]) :: {:ok, [Hash.Address.t()]}
defp insert_addresses(changes_list, named_arguments)
when is_list(changes_list) and is_list(named_arguments) do
timestamps = Keyword.fetch!(named_arguments, :timestamps)

@ -29,13 +29,13 @@ defmodule Explorer.Chain.Address do
@type t :: %__MODULE__{
fetched_balance: Wei.t(),
fetched_balance_block_number: Block.block_number(),
hash: Hash.Truncated.t(),
hash: Hash.Address.t(),
contract_code: Data.t() | nil,
inserted_at: DateTime.t(),
updated_at: DateTime.t()
}
@primary_key {:hash, Hash.Truncated, autogenerate: false}
@primary_key {:hash, Hash.Address, autogenerate: false}
schema "addresses" do
field(:fetched_balance, Wei)
field(:fetched_balance_block_number, :integer)

@ -48,7 +48,7 @@ defmodule Explorer.Chain.Block do
gas_used: Gas.t(),
hash: Hash.t(),
miner: %Ecto.Association.NotLoaded{} | Address.t(),
miner_hash: Hash.Truncated.t(),
miner_hash: Hash.Address.t(),
nonce: Hash.t(),
number: block_number(),
parent_hash: Hash.t(),
@ -71,7 +71,7 @@ defmodule Explorer.Chain.Block do
timestamps()
belongs_to(:miner, Address, foreign_key: :miner_hash, references: :hash, type: Hash.Truncated)
belongs_to(:miner, Address, foreign_key: :miner_hash, references: :hash, type: Hash.Address)
belongs_to(:parent, __MODULE__, foreign_key: :parent_hash, references: :hash, type: Hash.Full)
has_many(:transactions, Transaction)
end

@ -81,7 +81,7 @@ defmodule Explorer.Chain.Data do
{:ok, %Explorer.Chain.Data{bytes: <<>>}}
Hashes can be represented as `Explorer.Chain.Data`, but it is better to use `Explorer.Chain.Hash.Full` or
`Explorer.Chain.Hash.Truncated`.
`Explorer.Chain.Hash.Address`.
iex> Explorer.Chain.Data.cast("0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b")
{

@ -1,4 +1,4 @@
defmodule Explorer.Chain.Hash.Truncated do
defmodule Explorer.Chain.Hash.Address do
@moduledoc """
The address (40 (hex) characters / 160 bits / 20 bytes) is derived from the public key (128 (hex) characters /
512 bits / 64 bytes) which is derived from the private key (64 (hex) characters / 256 bits / 32 bytes).
@ -24,7 +24,7 @@ defmodule Explorer.Chain.Hash.Truncated do
If the `term` is already in `t:t/0`, then it is returned
iex> Explorer.Chain.Hash.Truncated.cast(
iex> Explorer.Chain.Hash.Address.cast(
...> %Explorer.Chain.Hash{
...> byte_count: 20,
...> bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
@ -40,7 +40,7 @@ defmodule Explorer.Chain.Hash.Truncated do
If the `term` is an `non_neg_integer`, then it is converted to `t:t/0`
iex> Explorer.Chain.Hash.Truncated.cast(0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed)
iex> Explorer.Chain.Hash.Address.cast(0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed)
{
:ok,
%Explorer.Chain.Hash{
@ -51,12 +51,12 @@ defmodule Explorer.Chain.Hash.Truncated do
If the `non_neg_integer` is too large, then `:error` is returned.
iex> Explorer.Chain.Hash.Truncated.cast(0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b)
iex> Explorer.Chain.Hash.Address.cast(0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b)
:error
If the `term` is a `String.t` that starts with `0x`, then is converted to an integer and then to `t:t/0`.
iex> Explorer.Chain.Hash.Truncated.cast("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
iex> Explorer.Chain.Hash.Address.cast("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
{
:ok,
%Explorer.Chain.Hash{
@ -68,7 +68,7 @@ defmodule Explorer.Chain.Hash.Truncated do
While `non_neg_integers` don't have to be the correct width (because zero padding it difficult with numbers),
`String.t` format must always have #{@hexadecimal_digit_count} digits after the `0x` base prefix.
iex> Explorer.Chain.Hash.Truncated.cast("0x0")
iex> Explorer.Chain.Hash.Address.cast("0x0")
:error
"""
@ -83,7 +83,7 @@ defmodule Explorer.Chain.Hash.Truncated do
If the field from the struct is `t:t/0`, then it succeeds
iex> Explorer.Chain.Hash.Truncated.dump(
iex> Explorer.Chain.Hash.Address.dump(
...> %Explorer.Chain.Hash{
...> byte_count: 20,
...> bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
@ -93,7 +93,7 @@ defmodule Explorer.Chain.Hash.Truncated do
If the field from the struct is an incorrect format such as `t:Explorer.Chain.Hash.t/0`, `:error` is returned
iex> Explorer.Chain.Hash.Truncated.dump(
iex> Explorer.Chain.Hash.Address.dump(
...> %Explorer.Chain.Hash{
...> byte_count: 32,
...> bytes: <<0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b ::
@ -114,7 +114,7 @@ defmodule Explorer.Chain.Hash.Truncated do
If the binary hash is the correct format, it is returned.
iex> Explorer.Chain.Hash.Truncated.load(
iex> Explorer.Chain.Hash.Address.load(
...> <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
...> )
{
@ -127,7 +127,7 @@ defmodule Explorer.Chain.Hash.Truncated do
If the binary hash is an incorrect format, such as if an `Explorer.Chain.Hash` field is loaded, `:error` is returned.
iex> Explorer.Chain.Hash.Truncated.load(
iex> Explorer.Chain.Hash.Address.load(
...> <<0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b :: big-integer-size(32)-unit(8)>>
...> )
:error

@ -33,7 +33,7 @@ defmodule Explorer.Chain.InternalTransaction do
created_contract_code: Data.t() | nil,
error: String.t(),
from_address: %Ecto.Association.NotLoaded{} | Address.t(),
from_address_hash: Hash.Truncated.t(),
from_address_hash: Hash.Address.t(),
gas: Gas.t(),
gas_used: Gas.t() | nil,
index: non_neg_integer(),
@ -41,7 +41,7 @@ defmodule Explorer.Chain.InternalTransaction do
input: Data.t(),
output: Data.t() | nil,
to_address: %Ecto.Association.NotLoaded{} | Address.t(),
to_address_hash: Hash.Truncated.t(),
to_address_hash: Hash.Address.t(),
trace_address: [non_neg_integer()],
transaction: %Ecto.Association.NotLoaded{} | Transaction.t(),
transaction_hash: Explorer.Chain.Hash.t(),
@ -70,7 +70,7 @@ defmodule Explorer.Chain.InternalTransaction do
Address,
foreign_key: :created_contract_address_hash,
references: :hash,
type: Hash.Truncated
type: Hash.Address
)
belongs_to(
@ -78,7 +78,7 @@ defmodule Explorer.Chain.InternalTransaction do
Address,
foreign_key: :from_address_hash,
references: :hash,
type: Hash.Truncated
type: Hash.Address
)
belongs_to(
@ -86,7 +86,7 @@ defmodule Explorer.Chain.InternalTransaction do
Address,
foreign_key: :to_address_hash,
references: :hash,
type: Hash.Truncated
type: Hash.Address
)
belongs_to(:transaction, Transaction, foreign_key: :transaction_hash, references: :hash, type: Hash.Full)

@ -23,7 +23,7 @@ defmodule Explorer.Chain.Log do
"""
@type t :: %__MODULE__{
address: %Ecto.Association.NotLoaded{} | Address.t(),
address_hash: Hash.Truncated.t(),
address_hash: Hash.Address.t(),
data: Data.t(),
first_topic: String.t(),
fourth_topic: String.t(),
@ -46,7 +46,7 @@ defmodule Explorer.Chain.Log do
timestamps()
belongs_to(:address, Address, foreign_key: :address_hash, references: :hash, type: Hash.Truncated)
belongs_to(:address, Address, foreign_key: :address_hash, references: :hash, type: Hash.Address)
belongs_to(:transaction, Transaction, foreign_key: :transaction_hash, references: :hash, type: Hash.Full)
end

@ -32,7 +32,7 @@ defmodule Explorer.Chain.SmartContract do
Address,
foreign_key: :address_hash,
references: :hash,
type: Hash.Truncated
type: Hash.Address
)
timestamps()

@ -114,7 +114,7 @@ defmodule Explorer.Chain.Transaction do
block_number: Block.block_number() | nil,
cumulative_gas_used: Gas.t() | nil,
from_address: %Ecto.Association.NotLoaded{} | Address.t(),
from_address_hash: Hash.Truncated.t(),
from_address_hash: Hash.Address.t(),
gas: Gas.t(),
gas_price: wei_per_gas,
gas_used: Gas.t() | nil,
@ -131,7 +131,7 @@ defmodule Explorer.Chain.Transaction do
standard_v: standard_v(),
status: Status.t() | nil,
to_address: %Ecto.Association.NotLoaded{} | Address.t(),
to_address_hash: Hash.Truncated.t(),
to_address_hash: Hash.Address.t(),
v: v(),
value: Wei.t()
}
@ -154,7 +154,7 @@ defmodule Explorer.Chain.Transaction do
field(:status, Status)
field(:v, :integer)
field(:value, Wei)
field(:created_contract_address_hash, Hash.Truncated, virtual: true)
field(:created_contract_address_hash, Hash.Address, virtual: true)
timestamps()
@ -165,7 +165,7 @@ defmodule Explorer.Chain.Transaction do
Address,
foreign_key: :from_address_hash,
references: :hash,
type: Hash.Truncated
type: Hash.Address
)
has_many(:internal_transactions, InternalTransaction, foreign_key: :transaction_hash)
@ -176,7 +176,7 @@ defmodule Explorer.Chain.Transaction do
Address,
foreign_key: :to_address_hash,
references: :hash,
type: Hash.Truncated
type: Hash.Address
)
end

@ -0,0 +1,5 @@
defmodule Explorer.Chain.Hash.AddressTest do
use ExUnit.Case, async: true
doctest Explorer.Chain.Hash.Address
end

@ -1,5 +0,0 @@
defmodule Explorer.Chain.Hash.TruncatedTest do
use ExUnit.Case, async: true
doctest Explorer.Chain.Hash.Truncated
end

@ -65,7 +65,7 @@ defmodule Explorer.Factory do
{:ok, address_hash} =
"address_hash"
|> sequence(& &1)
|> Hash.Truncated.cast()
|> Hash.Address.cast()
address_hash
end

@ -22,7 +22,7 @@ defmodule Indexer.AddressBalanceFetcher do
@doc """
Asynchronously fetches balances for each address `hash` at the `block_number`.
"""
@spec async_fetch_balances([%{required(:block_number) => Block.block_number(), required(:hash) => Hash.Truncated.t()}]) ::
@spec async_fetch_balances([%{required(:block_number) => Block.block_number(), required(:hash) => Hash.Address.t()}]) ::
:ok
def async_fetch_balances(address_fields) when is_list(address_fields) do
params_list = Enum.map(address_fields, &address_fields_to_params/1)

Loading…
Cancel
Save