fix: Handle delegatecall in state changes (#10906)

pull/10943/head
Maxim Filonov 1 month ago committed by GitHub
parent 8635b5ca86
commit 92e7ce50f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      apps/block_scout_web/lib/block_scout_web/models/transaction_state_helper.ex
  2. 36
      apps/block_scout_web/test/block_scout_web/controllers/api/v2/transaction_controller_test.exs
  3. 3
      apps/explorer/lib/explorer/chain/transaction/state_change.ex

@ -8,7 +8,7 @@ defmodule BlockScoutWeb.Models.TransactionStateHelper do
alias Explorer.Chain.Transaction.StateChange
alias Explorer.{Chain, PagingOptions, Repo}
alias Explorer.Chain.{BlockNumberHelper, Transaction, Wei}
alias Explorer.Chain.{BlockNumberHelper, InternalTransaction, Transaction, Wei}
alias Explorer.Chain.Cache.StateChanges
alias Indexer.Fetcher.OnDemand.CoinBalance, as: CoinBalanceOnDemand
alias Indexer.Fetcher.OnDemand.TokenBalance, as: TokenBalanceOnDemand
@ -114,6 +114,8 @@ defmodule BlockScoutWeb.Models.TransactionStateHelper do
)
end
defp internal_transaction_to_coin_balances(%InternalTransaction{call_type: :delegatecall}, _, _, acc), do: acc
defp internal_transaction_to_coin_balances(internal_transaction, previous_block_number, options, acc) do
if internal_transaction.value |> Wei.to(:wei) |> Decimal.positive?() do
acc

@ -1039,7 +1039,11 @@ defmodule BlockScoutWeb.API.V2.TransactionControllerTest do
internal_transaction_from = insert(:address)
internal_transaction_to = insert(:address)
internal_transaction_from_delegatecall = insert(:address)
internal_transaction_to_delegatecall = insert(:address)
insert(:internal_transaction,
call_type: :call,
transaction: transaction,
index: 0,
block_number: transaction.block_number,
@ -1053,7 +1057,9 @@ defmodule BlockScoutWeb.API.V2.TransactionControllerTest do
to_address: internal_transaction_to
)
# must be ignored, hence we expect only 5 state changes
insert(:internal_transaction,
call_type: :delegatecall,
transaction: transaction,
index: 1,
block_number: transaction.block_number,
@ -1061,6 +1067,21 @@ defmodule BlockScoutWeb.API.V2.TransactionControllerTest do
block_hash: transaction.block_hash,
block_index: 1,
value: %Wei{value: Decimal.new(7)},
from_address_hash: internal_transaction_from_delegatecall.hash,
from_address: internal_transaction_from_delegatecall,
to_address_hash: internal_transaction_to_delegatecall.hash,
to_address: internal_transaction_to_delegatecall
)
insert(:internal_transaction,
call_type: :call,
transaction: transaction,
index: 2,
block_number: transaction.block_number,
transaction_index: transaction.index,
block_hash: transaction.block_hash,
block_index: 2,
value: %Wei{value: Decimal.new(7)},
from_address_hash: internal_transaction_from.hash,
from_address: internal_transaction_from,
to_address_hash: internal_transaction_to.hash,
@ -1070,31 +1091,36 @@ defmodule BlockScoutWeb.API.V2.TransactionControllerTest do
insert(:address_coin_balance,
address: transaction.from_address,
address_hash: transaction.from_address_hash,
block_number: block_before.number
block_number: block_before.number,
value: %Wei{value: Decimal.new(1000)}
)
insert(:address_coin_balance,
address: transaction.to_address,
address_hash: transaction.to_address_hash,
block_number: block_before.number
block_number: block_before.number,
value: %Wei{value: Decimal.new(1000)}
)
insert(:address_coin_balance,
address: transaction.block.miner,
address_hash: transaction.block.miner_hash,
block_number: block_before.number
block_number: block_before.number,
value: %Wei{value: Decimal.new(1000)}
)
insert(:address_coin_balance,
address: internal_transaction_from,
address_hash: internal_transaction_from.hash,
block_number: block_before.number
block_number: block_before.number,
value: %Wei{value: Decimal.new(1000)}
)
insert(:address_coin_balance,
address: internal_transaction_to,
address_hash: internal_transaction_to.hash,
block_number: block_before.number
block_number: block_before.number,
value: %Wei{value: Decimal.new(1000)}
)
request = get(conn, "/api/v2/transactions/#{to_string(transaction.hash)}/state-changes")

@ -56,6 +56,9 @@ defmodule Explorer.Chain.Transaction.StateChange do
end
end
defp update_coin_balances_from_internal_tx(%InternalTransaction{call_type: :delegatecall}, coin_balances),
do: coin_balances
defp update_coin_balances_from_internal_tx(%InternalTransaction{index: 0}, coin_balances), do: coin_balances
defp update_coin_balances_from_internal_tx(internal_tx, coin_balances) do

Loading…
Cancel
Save