Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
move all fork related logic to Forks module
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed Jul 7, 2018
1 parent 1fc740b commit a0be695
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 230 deletions.
6 changes: 3 additions & 3 deletions lib/ciri/evm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def transition(block, check_gas_limit: true, check_gas_used: true)
end

fork_config = Ciri::Forks.detect_fork(header: block.header, number: block.header.number)
rewards = fork_config.mining_rewards[block]
rewards = fork_config.mining_rewards_of_block(block)

# apply rewards
rewards.each do |address, value|
Expand All @@ -104,7 +104,7 @@ def execute_transaction(t, header: nil, block_info: nil, ignore_exception: false
state.add_balance(t.sender, -1 * t.gas_limit * t.gas_price)
fork_config = Ciri::Forks.detect_fork(header: header, number: block_info&.number)

gas_limit = t.gas_limit - fork_config.intrinsic_gas_of_transaction[t]
gas_limit = t.gas_limit - fork_config.intrinsic_gas_of_transaction(t)

instruction = Instruction.new(
origin: t.sender,
Expand Down Expand Up @@ -149,7 +149,7 @@ def execute_transaction(t, header: nil, block_info: nil, ignore_exception: false
raise exception if !ignore_exception && exception

# refund gas
refund_gas = fork_config.refund_gas[t, @vm]
refund_gas = fork_config.calculate_refund_gas(@vm)
gas_used = t.gas_limit - @vm.remain_gas
refund_gas = [refund_gas, gas_used / 2].min
state.add_balance(t.sender, (refund_gas + @vm.remain_gas) * t.gas_price)
Expand Down
183 changes: 0 additions & 183 deletions lib/ciri/evm/forks/frontier.rb

This file was deleted.

10 changes: 5 additions & 5 deletions lib/ciri/evm/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def create_contract(value:, init:)
call_instruction(create_contract_instruction) do
execute

deposit_code_gas = fork_config.deposit_code_fee[output]
deposit_code_gas = fork_config.calculate_deposit_code_gas(output)

if deposit_code_gas > remain_gas
# deposit_code_gas not enough
Expand Down Expand Up @@ -270,16 +270,16 @@ def operate

raise "can't find operation #{w}, pc #{ms.pc}" unless operation

op_cost = fork_config.cost_of_operation[self]
old_memory_cost = fork_config.cost_of_memory[ms.memory_item]
op_cost = fork_config.gas_of_operation(self)
old_memory_cost = fork_config.gas_of_memory(ms.memory_item)
ms.consume_gas op_cost

prev_sub_state = sub_state.dup

# call operation
operation.call(self)
# calculate gas_cost
new_memory_cost = fork_config.cost_of_memory[ms.memory_item]
new_memory_cost = fork_config.gas_of_memory(ms.memory_item)
memory_gas_cost = new_memory_cost - old_memory_cost

if ms.remain_gas >= memory_gas_cost
Expand Down Expand Up @@ -332,7 +332,7 @@ def check_exception(state, ms, instruction)
InvalidOpCodeError.new "can't find op code #{w}"
when ms.stack.size < (consume = OP.input_count(w))
StackError.new "stack not enough: stack:#{ms.stack.size} next consume: #{consume}"
when ms.remain_gas < (gas_cost = fork_config.cost_of_operation[self])
when ms.remain_gas < (gas_cost = fork_config.gas_of_operation(self))
GasNotEnoughError.new "gas not enough: gas remain:#{ms.remain_gas} gas cost: #{gas_cost}"
when w == OP::JUMP && instruction.destinations.include?(ms.get_stack(0, Integer))
InvalidJumpError.new "invalid jump dest #{ms.get_stack(0, Integer)}"
Expand Down
13 changes: 1 addition & 12 deletions lib/ciri/forks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,9 @@
module Ciri
module Forks

# Fork configure
ForkConfig = Struct.new(
:cost_of_operation,
:cost_of_memory,
:intrinsic_gas_of_transaction,
:deposit_code_fee,
:mining_rewards,
:refund_gas,
keyword_init: true
)

def self.detect_fork(header: nil, number: nil)
number ||= header.number
Frontier.fork_config
Frontier::Config.new
end

end
Expand Down
55 changes: 55 additions & 0 deletions lib/ciri/forks/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

# Copyright (c) 2018, by Jiang Jinyang. <https://justjjy.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


module Ciri
module Forks

class Base
# gas methods
def gas_of_operation(vm)
raise NotImplementedError
end

def gas_of_memory(word_count)
raise NotImplementedError
end

def intrinsic_gas_of_transaction(transaction)
raise NotImplementedError
end

def calculate_deposit_code_gas(code_bytes)
raise NotImplementedError
end

def mining_rewards_of_block(block)
raise NotImplementedError
end

def calculate_refund_gas(vm)
raise NotImplementedError
end
end

end
end
Loading

0 comments on commit a0be695

Please sign in to comment.