Update address extraction testcases

pull/1140/head
svenski123 6 years ago committed by Amanda Sposito
parent 29199eb54e
commit 3b45474e36
  1. 81
      apps/indexer/lib/indexer/address_extraction.ex
  2. 16
      apps/indexer/test/indexer/address_extraction_test.exs

@ -285,11 +285,13 @@ defmodule Indexer.AddressExtraction do
...> transactions: [
...> %{
...> block_number: 3,
...> to_address_hash: "0x0000000000000000000000000000000000000001"
...> to_address_hash: "0x0000000000000000000000000000000000000001",
...> nonce: 5
...> },
...> %{
...> block_number: 2,
...> from_address_hash: "0x0000000000000000000000000000000000000001"
...> from_address_hash: "0x0000000000000000000000000000000000000001",
...> nonce: 4
...> }
...> ],
...> logs: [
@ -303,7 +305,8 @@ defmodule Indexer.AddressExtraction do
[
%{
fetched_coin_balance_block_number: 7,
hash: "0x0000000000000000000000000000000000000001"
hash: "0x0000000000000000000000000000000000000001",
nonce: 4
}
]
@ -322,11 +325,13 @@ defmodule Indexer.AddressExtraction do
...> transactions: [
...> %{
...> block_number: 2,
...> from_address_hash: "0x0000000000000000000000000000000000000001"
...> from_address_hash: "0x0000000000000000000000000000000000000001",
...> nonce: 4
...> },
...> %{
...> block_number: 3,
...> to_address_hash: "0x0000000000000000000000000000000000000001"
...> to_address_hash: "0x0000000000000000000000000000000000000001",
...> nonce: 5
...> }
...> ]
...> }
@ -335,7 +340,8 @@ defmodule Indexer.AddressExtraction do
%{
contract_code: "0x",
fetched_coin_balance_block_number: 3,
hash: "0x0000000000000000000000000000000000000001"
hash: "0x0000000000000000000000000000000000000001",
nonce: 4
}
]
@ -452,38 +458,41 @@ defmodule Indexer.AddressExtraction do
# Ensure that when `:addresses` or `:address_coin_balances` are present, their :fetched_coin_balance will win
defp merge_addresses(%{hash: hash} = first, %{hash: hash} = second) do
case {first[:fetched_coin_balance], second[:fetched_coin_balance]} do
{nil, nil} ->
first
|> Map.merge(second)
|> Map.put(
:fetched_coin_balance_block_number,
max_nil_last(
Map.get(first, :fetched_coin_balance_block_number),
Map.get(second, :fetched_coin_balance_block_number)
merged_addresses =
case {first[:fetched_coin_balance], second[:fetched_coin_balance]} do
{nil, nil} ->
first
|> Map.merge(second)
|> Map.put(
:fetched_coin_balance_block_number,
max_nil_last(
Map.get(first, :fetched_coin_balance_block_number),
Map.get(second, :fetched_coin_balance_block_number)
)
)
)
{nil, _} ->
# merge in `second` so its balance and block_number wins
Map.merge(first, second)
{_, nil} ->
# merge in `first` so its balance and block_number wins
Map.merge(second, first)
{_, _} ->
if greater_than_nil_last(
Map.get(first, :fetched_coin_balance_block_number),
Map.get(second, :fetched_coin_balance_block_number)
) do
# merge in `first` so its block number wins
Map.merge(second, first)
else
# merge in `second` so its block number wins
{nil, _} ->
# merge in `second` so its balance and block_number wins
Map.merge(first, second)
end
end
{_, nil} ->
# merge in `first` so its balance and block_number wins
Map.merge(second, first)
{_, _} ->
if greater_than_nil_last(
Map.get(first, :fetched_coin_balance_block_number),
Map.get(second, :fetched_coin_balance_block_number)
) do
# merge in `first` so its block number wins
Map.merge(second, first)
else
# merge in `second` so its block number wins
Map.merge(first, second)
end
end
merged_addresses
|> Map.put(:nonce, max_nil_last(first[:nonce], second[:nonce]))
end

@ -76,7 +76,8 @@ defmodule Indexer.AddressExtractionTest do
%{
fetched_coin_balance_block_number: 2,
contract_code: "0x1",
hash: "0x0000000000000000000000000000000000000001"
hash: "0x0000000000000000000000000000000000000001",
nonce: nil
}
]
end
@ -95,7 +96,8 @@ defmodule Indexer.AddressExtractionTest do
transaction = %{
block_number: 3,
from_address_hash: gen_hash(),
to_address_hash: gen_hash()
to_address_hash: gen_hash(),
nonce: 7
}
log = %{address_hash: gen_hash(), block_number: 4}
@ -136,7 +138,11 @@ defmodule Indexer.AddressExtractionTest do
contract_code: internal_transaction.created_contract_code,
fetched_coin_balance_block_number: internal_transaction.block_number
},
%{hash: transaction.from_address_hash, fetched_coin_balance_block_number: transaction.block_number},
%{
hash: transaction.from_address_hash,
fetched_coin_balance_block_number: transaction.block_number,
nonce: 7
},
%{hash: transaction.to_address_hash, fetched_coin_balance_block_number: transaction.block_number},
%{hash: log.address_hash, fetched_coin_balance_block_number: log.block_number},
%{
@ -176,7 +182,7 @@ defmodule Indexer.AddressExtractionTest do
blockchain_data = %{
blocks: [%{miner_hash: hash, number: 34}],
transactions: [%{block_number: 34, from_address_hash: hash}],
transactions: [%{block_number: 34, from_address_hash: hash, nonce: 12}],
internal_transactions: [
%{
block_number: 34,
@ -188,7 +194,7 @@ defmodule Indexer.AddressExtractionTest do
assert AddressExtraction.extract_addresses(blockchain_data) ==
[
%{hash: hash, fetched_coin_balance_block_number: 34, contract_code: "code"}
%{hash: hash, fetched_coin_balance_block_number: 34, contract_code: "code", nonce: 12}
]
end

Loading…
Cancel
Save