Skip to content

Commit

Permalink
Merge pull request blockscout#5285 from blockscout/vb-fix-contract-by…
Browse files Browse the repository at this point in the history
…tecode-twins-feature

Fix verified smart-contract bytecode twins feature
  • Loading branch information
vbaranov authored and jagdeep sidhu committed Mar 9, 2022
1 parent 8c52212 commit 61590b5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [#5268](https://github.com/blockscout/blockscout/pull/5268) - Contract names display improvement

### Fixes
- [#5285](https://github.com/blockscout/blockscout/pull/5285) - Fix verified smart-contract bytecode twins feature
- [#5269](https://github.com/blockscout/blockscout/pull/5269) - Address Page: Fix implementation address align
- [#5264](https://github.com/blockscout/blockscout/pull/5264) - Fix bug with 500 response on `partial` sourcify status
- [#5263](https://github.com/blockscout/blockscout/pull/5263) - Fix bug with name absence for contract
Expand Down
5 changes: 1 addition & 4 deletions apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4187,10 +4187,7 @@ defmodule Explorer.Chain do
%{contract_code: %Chain.Data{bytes: contract_code_bytes}} <- target_address do
target_address_hash = target_address.hash

contract_code_md5 =
Base.encode16(:crypto.hash(:md5, "\\x" <> Base.encode16(contract_code_bytes, case: :lower)),
case: :lower
)
contract_code_md5 = Helper.contract_code_md5(contract_code_bytes)

verified_contract_twin_query =
from(
Expand Down
16 changes: 8 additions & 8 deletions apps/explorer/lib/explorer/smart_contract/helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ defmodule Explorer.SmartContract.Helper do
def add_contract_code_md5(%{address_hash: address_hash_string} = attrs) when is_binary(address_hash_string) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash) do
contract_code_md5 =
:md5
|> :crypto.hash(address.contract_code.bytes)
|> Base.encode16(case: :lower)
contract_code_md5 = contract_code_md5(address.contract_code.bytes)

attrs
|> Map.put_new(:contract_code_md5, contract_code_md5)
Expand All @@ -57,10 +54,7 @@ defmodule Explorer.SmartContract.Helper do
def add_contract_code_md5(%{address_hash: address_hash} = attrs) do
case Chain.hash_to_address(address_hash) do
{:ok, address} ->
contract_code_md5 =
:md5
|> :crypto.hash(address.contract_code.bytes)
|> Base.encode16(case: :lower)
contract_code_md5 = contract_code_md5(address.contract_code.bytes)

attrs
|> Map.put_new(:contract_code_md5, contract_code_md5)
Expand All @@ -71,4 +65,10 @@ defmodule Explorer.SmartContract.Helper do
end

def add_contract_code_md5(attrs), do: attrs

def contract_code_md5(bytes) do
:md5
|> :crypto.hash(bytes)
|> Base.encode16(case: :lower)
end
end
7 changes: 3 additions & 4 deletions apps/explorer/test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ defmodule Explorer.Factory do
StakingPoolsDelegator
}

alias Explorer.SmartContract.Helper

alias Explorer.Market.MarketHistory
alias Explorer.Repo

Expand Down Expand Up @@ -607,10 +609,7 @@ defmodule Explorer.Factory do
def smart_contract_factory do
contract_code_info = contract_code_info()

bytecode_md5 =
:md5
|> :crypto.hash(contract_code_info.bytecode)
|> Base.encode16(case: :lower)
bytecode_md5 = Helper.contract_code_md5(contract_code_info.bytecode)

%SmartContract{
address_hash: insert(:address, contract_code: contract_code_info.bytecode, verified: true).hash,
Expand Down

0 comments on commit 61590b5

Please sign in to comment.