Add missing constraints for internal transactions

Calls didn't enforce that `call_type` and `input` was not `NULL`.
Creates didn't enforce that `init` was not `NULL`.
pull/1094/head
Luke Imhoff 6 years ago
parent 838a312ff6
commit 0c4c80c880
  1. 3
      apps/explorer/lib/explorer/chain/internal_transaction.ex
  2. 15
      apps/explorer/priv/repo/migrations/20181108205650_additional_internal_transaction_constraints.exs
  3. 3
      apps/explorer/test/support/factory.ex

@ -376,6 +376,8 @@ defmodule Explorer.Chain.InternalTransaction do
|> cast(attrs, @call_allowed_fields)
|> validate_required(@call_required_fields)
|> validate_call_error_or_result()
|> check_constraint(:call_type, message: ~S|can't be blank when type is 'call'|, name: :call_has_call_type)
|> check_constraint(:input, message: ~S|can't be blank when type is 'call'|, name: :call_has_call_type)
|> foreign_key_constraint(:from_address_hash)
|> foreign_key_constraint(:to_address_hash)
|> foreign_key_constraint(:transaction_hash)
@ -391,6 +393,7 @@ defmodule Explorer.Chain.InternalTransaction do
|> cast(attrs, @create_allowed_fields)
|> validate_required(@create_required_fields)
|> validate_create_error_or_result()
|> check_constraint(:init, message: ~S|can't be blank when type is 'create'|, name: :create_has_init)
|> foreign_key_constraint(:created_contract_address_hash)
|> foreign_key_constraint(:from_address_hash)
|> foreign_key_constraint(:transaction_hash)

@ -0,0 +1,15 @@
defmodule Explorer.Repo.Migrations.AdditionalInternalTransactionConstraints do
use Ecto.Migration
def up do
create(constraint(:internal_transactions, :call_has_call_type, check: "type != 'call' OR call_type IS NOT NULL"))
create(constraint(:internal_transactions, :call_has_input, check: "type != 'call' OR input IS NOT NULL"))
create(constraint(:internal_transactions, :create_has_init, check: "type != 'create' OR init IS NOT NULL"))
end
def down do
drop(constraint(:internal_transactions, :call_has_call_type))
drop(constraint(:internal_transactions, :call_has_input))
drop(constraint(:internal_transactions, :create_has_init))
end
end

@ -269,7 +269,8 @@ defmodule Explorer.Factory do
call_type: :delegatecall,
gas: gas,
gas_used: gas_used,
output: %Data{bytes: <<1>>},
input: %Data{bytes: <<1>>},
output: %Data{bytes: <<2>>},
# caller MUST supply `index`
trace_address: [],
# caller MUST supply `transaction` because it can't be built lazily to allow overrides without creating an extra

Loading…
Cancel
Save