Luke Imhoff 6 years ago
parent b878590125
commit dcf5e3fe14
  1. 118
      apps/explorer/test/explorer/chain/import_test.exs

@ -1511,5 +1511,123 @@ defmodule Explorer.Chain.ImportTest do
}
})
end
# https://github.com/poanetwork/blockscout/pull/833#issuecomment-426102868 regression test
test "a non-consensus block being added after a block with same number does not change the consensus block to non-consensus" 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: false,
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 does not grab `consensus`
assert %Block{
consensus: false,
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
# nothing changes on the original consensus block
assert %Block{
consensus: true,
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

Loading…
Cancel
Save