Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limit code size EIP170 #350

Merged
merged 2 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions apps/blockchain/lib/blockchain/contract/create_contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,29 @@ defmodule Blockchain.Contract.CreateContract do
contract_creation_cost = creation_cost(output)
insufficient_gas = remaining_gas < contract_creation_cost

if insufficient_gas && EVM.Configuration.fail_contract_creation_lack_of_gas?(params.config) do
{:error, {params.state, 0, SubState.empty()}}
else
resultant_gas =
if insufficient_gas do
remaining_gas
else
remaining_gas - contract_creation_cost
end

resultant_state =
if insufficient_gas do
state_after_init
else
Account.put_code(state_after_init, address, output)
end

{:ok, {resultant_state, resultant_gas, accrued_sub_state}}
cond do
insufficient_gas && EVM.Configuration.fail_contract_creation_lack_of_gas?(params.config) ->
{:error, {params.state, 0, SubState.empty()}}

EVM.Configuration.limit_contract_code_size?(params.config, byte_size(output)) ->
{:error, {params.state, 0, SubState.empty()}}

true ->
resultant_gas =
if insufficient_gas do
remaining_gas
else
remaining_gas - contract_creation_cost
end

resultant_state =
if insufficient_gas do
state_after_init
else
Account.put_code(state_after_init, address, output)
end

{:ok, {resultant_state, resultant_gas, accrued_sub_state}}
end
end

Expand Down
1 change: 0 additions & 1 deletion apps/blockchain/test/blockchain/state_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ defmodule Blockchain.StateTest do
"stCreateTest/CREATE_ContractSSTOREDuringInit",
"stCreateTest/CREATE_AcreateB_BSuicide_BStore",
"stCodeSizeLimit/codesizeValid",
"stCodeSizeLimit/codesizeOOGInvalidSize",
"stCodeSizeLimit/codesizeInit",
"stCallCreateCallCodeTest/createNameRegistratorPerTxs",
"stCallCodes/callcodeDynamicCode",
Expand Down
1 change: 0 additions & 1 deletion apps/blockchain/test/blockchain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ defmodule BlockchainTest do
"GeneralStateTests/stCallCodes/call_OOG_additionalGasCosts1_d0g0v0.json",
"GeneralStateTests/stCallCodes/callcodeDynamicCode_d0g0v0.json",
"GeneralStateTests/stCallCreateCallCodeTest/createNameRegistratorPerTxs_d0g0v0.json",
"GeneralStateTests/stCodeSizeLimit/codesizeOOGInvalidSize_d0g0v0.json",
"GeneralStateTests/stCodeSizeLimit/codesizeValid_d0g0v0.json",
"GeneralStateTests/stCreateTest/CREATE_AcreateB_BSuicide_BStore_d0g0v0.json",
"GeneralStateTests/stCreateTest/CREATE_ContractSSTOREDuringInit_d0g0v0.json",
Expand Down
4 changes: 4 additions & 0 deletions apps/evm/lib/evm/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ defprotocol EVM.Configuration do
# EIP160
@spec exp_byte_cost(t) :: integer()
def exp_byte_cost(t)

# EIP170
@spec limit_contract_code_size?(t, integer) :: boolean()
def limit_contract_code_size?(t, size \\ 0)
end
4 changes: 4 additions & 0 deletions apps/evm/lib/evm/configuration/eip150.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ defimpl EVM.Configuration, for: EVM.Configuration.EIP150 do

@spec exp_byte_cost(Configuration.t()) :: integer()
def exp_byte_cost(config), do: Configuration.exp_byte_cost(config.fallback_config)

@spec limit_contract_code_size?(Configuration.t(), integer()) :: boolean()
def limit_contract_code_size?(config, _),
do: Configuration.limit_contract_code_size?(config.fallback_config)
end
6 changes: 5 additions & 1 deletion apps/evm/lib/evm/configuration/eip158.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule EVM.Configuration.EIP158 do
defstruct fallback_config: EVM.Configuration.EIP150.new(),
exp_byte_cost: 50
exp_byte_cost: 50,
code_size_limit: 24_577

def new do
%__MODULE__{}
Expand Down Expand Up @@ -46,4 +47,7 @@ defimpl EVM.Configuration, for: EVM.Configuration.EIP158 do

@spec exp_byte_cost(Configuration.t()) :: integer()
def exp_byte_cost(config), do: config.exp_byte_cost

@spec limit_contract_code_size?(Configuration.t(), integer()) :: boolean()
def limit_contract_code_size?(config, size), do: size >= config.code_size_limit
end
6 changes: 5 additions & 1 deletion apps/evm/lib/evm/configuration/frontier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ defmodule EVM.Configuration.Frontier do
call_cost: 40,
selfdestruct_cost: 0,
fail_nested_operation: true,
exp_byte_cost: 10
exp_byte_cost: 10,
limit_contract_code_size: false

def new do
%__MODULE__{}
Expand Down Expand Up @@ -51,4 +52,7 @@ defimpl EVM.Configuration, for: EVM.Configuration.Frontier do

@spec exp_byte_cost(Configuration.t()) :: integer()
def exp_byte_cost(config), do: config.exp_byte_cost

@spec limit_contract_code_size?(Configuration.t(), integer()) :: boolean()
def limit_contract_code_size?(config, _), do: config.limit_contract_code_size
end
3 changes: 3 additions & 0 deletions apps/evm/lib/evm/configuration/homestead.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ defimpl EVM.Configuration, for: EVM.Configuration.Homestead do

@spec exp_byte_cost(Configuration.t()) :: integer()
def exp_byte_cost(config), do: config.fallback_config.exp_byte_cost

@spec limit_contract_code_size?(Configuration.t(), integer()) :: boolean()
def limit_contract_code_size?(config, _), do: config.fallback_config.limit_contract_code_size
end