From 078c0bf8acb506ad2c6d59e6503870f17618fb85 Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Fri, 28 Sep 2018 16:17:07 -0500 Subject: [PATCH] Failing regression test for #408 --- .../test/explorer/chain/import_test.exs | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/apps/explorer/test/explorer/chain/import_test.exs b/apps/explorer/test/explorer/chain/import_test.exs index 1e39d49983..cf33067f77 100644 --- a/apps/explorer/test/explorer/chain/import_test.exs +++ b/apps/explorer/test/explorer/chain/import_test.exs @@ -16,6 +16,8 @@ defmodule Explorer.Chain.ImportTest do Transaction } + @moduletag :capturelog + doctest Import describe "all/1" do @@ -1119,5 +1121,121 @@ defmodule Explorer.Chain.ImportTest do assert Repo.aggregate(Transaction.Fork, :count, :hash) == 1 end + + test "reorganizations can switch blocks to non-consensus with new block taking the consensus spot for the number" do + block_number = 0 + + miner_hash_before = address_hash() + from_address_hash_before = address_hash() + block_hash_before = block_hash() + difficulty_before = Decimal.new(0) + gas_limit_before = Decimal.new(0) + gas_used_before = Decimal.new(0) + {:ok, nonce_before} = Hash.Nonce.cast(0) + parent_hash_before = block_hash() + size_before = 0 + timestamp_before = Timex.parse!("2019-01-01T01:00:00Z", "{ISO:Extended:Z}") + total_difficulty_before = Decimal.new(0) + + assert {:ok, _} = + Import.all(%{ + addresses: %{ + params: [ + %{hash: miner_hash_before}, + %{hash: from_address_hash_before} + ] + }, + blocks: %{ + params: [ + %{ + consensus: true, + difficulty: difficulty_before, + gas_limit: gas_limit_before, + gas_used: gas_used_before, + hash: block_hash_before, + miner_hash: miner_hash_before, + nonce: nonce_before, + number: block_number, + parent_hash: parent_hash_before, + size: size_before, + timestamp: timestamp_before, + total_difficulty: total_difficulty_before + } + ] + } + }) + + %Block{consensus: true, number: ^block_number} = Repo.get(Block, block_hash_before) + + miner_hash_after = address_hash() + from_address_hash_after = address_hash() + block_hash_after = block_hash() + + assert {:ok, _} = + Import.all(%{ + addresses: %{ + params: [ + %{hash: miner_hash_after}, + %{hash: from_address_hash_after} + ] + }, + blocks: %{ + params: [ + %{ + consensus: true, + difficulty: 1, + gas_limit: 1, + gas_used: 1, + hash: block_hash_after, + miner_hash: miner_hash_after, + nonce: 1, + number: block_number, + parent_hash: block_hash(), + size: 1, + timestamp: Timex.parse!("2019-01-01T02:00:00Z", "{ISO:Extended:Z}"), + total_difficulty: 1 + } + ] + } + }) + + # new block grabs `consensus` + assert %Block{ + consensus: true, + difficulty: difficulty_after, + gas_limit: gas_limit_after, + gas_used: gas_used_after, + nonce: nonce_after, + number: ^block_number, + parent_hash: parent_hash_after, + size: size_after, + timestamp: timestamp_after, + total_difficulty: total_difficulty_after + } = Repo.get(Block, block_hash_after) + + refute difficulty_after == difficulty_before + refute gas_limit_after == gas_limit_before + refute gas_used_after == gas_used_before + refute nonce_after == nonce_before + refute parent_hash_after == parent_hash_before + refute size_after == size_before + refute timestamp_after == timestamp_before + refute total_difficulty_after == total_difficulty_before + + # only `consensus` changes in original block + assert %Block{ + consensus: false, + difficulty: ^difficulty_before, + gas_limit: ^gas_limit_before, + gas_used: ^gas_used_before, + nonce: ^nonce_before, + number: ^block_number, + parent_hash: ^parent_hash_before, + size: ^size_before, + timestamp: timestamp, + total_difficulty: ^total_difficulty_before + } = Repo.get(Block, block_hash_before) + assert DateTime.compare(timestamp, timestamp_before) == :eq + end end end