ciri 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -2
  3. data/Gemfile.lock +6 -6
  4. data/README.md +68 -48
  5. data/Rakefile +16 -11
  6. data/ciri-crypto/lib/ciri/crypto/signature.rb +7 -5
  7. data/ciri-rlp/ciri-rlp.gemspec +1 -1
  8. data/ciri-rlp/lib/ciri/rlp.rb +1 -1
  9. data/ciri-rlp/lib/ciri/rlp/decode.rb +23 -7
  10. data/ciri-rlp/lib/ciri/rlp/encode.rb +8 -2
  11. data/ciri-rlp/lib/ciri/rlp/serializable.rb +14 -4
  12. data/ciri-rlp/lib/ciri/rlp/version.rb +1 -1
  13. data/ciri-rlp/spec/ciri/fixture_spec.rb +2 -2
  14. data/ciri-utils/Gemfile.lock +1 -1
  15. data/ciri-utils/lib/ciri/utils/version.rb +1 -1
  16. data/ciri.gemspec +4 -4
  17. data/lib/ciri/chain/header.rb +13 -11
  18. data/lib/ciri/chain/header_chain.rb +1 -10
  19. data/lib/ciri/chain/transaction.rb +5 -23
  20. data/lib/ciri/db/account_db.rb +0 -4
  21. data/lib/ciri/db/backend/errors.rb +27 -0
  22. data/lib/ciri/db/backend/memory.rb +10 -12
  23. data/lib/ciri/db/backend/rocks.rb +13 -6
  24. data/lib/ciri/db/backend/rocks_db.rb +4 -0
  25. data/lib/ciri/evm.rb +6 -1
  26. data/lib/ciri/evm/execution_context.rb +6 -2
  27. data/lib/ciri/evm/instruction.rb +0 -1
  28. data/lib/ciri/evm/op.rb +11 -5
  29. data/lib/ciri/evm/op/errors.rb +37 -0
  30. data/lib/ciri/evm/state.rb +1 -1
  31. data/lib/ciri/evm/sub_state.rb +6 -6
  32. data/lib/ciri/evm/vm.rb +53 -33
  33. data/lib/ciri/forks.rb +4 -0
  34. data/lib/ciri/forks/base.rb +28 -0
  35. data/lib/ciri/forks/byzantium.rb +45 -11
  36. data/lib/ciri/forks/byzantium/opcodes.rb +37 -0
  37. data/lib/ciri/forks/constantinople.rb +29 -0
  38. data/lib/ciri/forks/frontier.rb +49 -29
  39. data/lib/ciri/forks/frontier/cost.rb +91 -95
  40. data/lib/ciri/forks/frontier/opcodes.rb +99 -0
  41. data/lib/ciri/forks/frontier/transaction.rb +62 -0
  42. data/lib/ciri/forks/homestead.rb +31 -7
  43. data/lib/ciri/forks/homestead/opcodes.rb +37 -0
  44. data/lib/ciri/forks/homestead/transaction.rb +43 -0
  45. data/lib/ciri/forks/spurious_dragon.rb +55 -0
  46. data/lib/ciri/forks/spurious_dragon/cost.rb +91 -0
  47. data/lib/ciri/forks/spurious_dragon/transaction.rb +44 -0
  48. data/lib/ciri/forks/tangerine_whistle.rb +37 -0
  49. data/lib/ciri/forks/tangerine_whistle/cost.rb +98 -0
  50. data/lib/ciri/rlp/decode.rb +4 -4
  51. data/lib/ciri/rlp/encode.rb +2 -2
  52. data/lib/ciri/version.rb +1 -1
  53. metadata +22 -10
  54. data/lib/ciri/forks/homestead/cost.rb +0 -195
@@ -1,195 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2018 Jiang Jinyang <https://justjjy.com>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
-
18
- require 'ciri/evm/op'
19
-
20
- module Ciri
21
- module Forks
22
- module Homestead
23
-
24
- module Cost
25
-
26
- include Ciri::EVM::OP
27
-
28
- # fee schedule, start with G
29
- G_ZERO = 0
30
- G_BASE = 2
31
- G_VERYLOW = 3
32
- G_LOW = 5
33
- G_MID = 8
34
- G_HIGH = 10
35
- G_EXTCODE = 700
36
- G_BALANCE = 400
37
- G_SLOAD = 50
38
- G_JUMPDEST = 1
39
- G_SSET = 20000
40
- G_RESET = 5000
41
- R_SCLEAR = 15000
42
- R_SELFDESTRUCT = 24000
43
- G_SELFDESTRUCT = 0
44
- G_CREATE = 32000
45
- G_CODEDEPOSIT = 200
46
- G_CALL = 700
47
- G_CALLVALUE = 9000
48
- G_CALLSTIPEND = 2300
49
- G_NEWACCOUNT = 25000
50
- G_EXP = 10
51
- G_EXPBYTE = 10
52
- G_MEMORY = 3
53
- G_TXCREATE = 32000
54
- G_TXDATAZERO = 4
55
- G_TXDATANONZERO = 68
56
- G_TRANSACTION = 21000
57
- G_LOG = 375
58
- G_LOGDATA = 8
59
- G_TOPIC = 375
60
- G_SHA3 = 30
61
- G_SHA3WORD = 6
62
- G_COPY = 3
63
- G_BLOCKHASH = 20
64
- G_QUADDIVISOR = 100
65
-
66
- # operation code by group, for later calculation
67
- W_ZERO = [STOP, RETURN, REVERT]
68
- W_BASE = [ADDRESS, ORIGIN, CALLER, CALLVALUE, CALLDATASIZE, CODESIZE, GASPRICE,
69
- COINBASE, TIMESTAMP, NUMBER, DIFFICULTY, GASLIMIT, RETURNDATASIZE,
70
- POP, PC, MSIZE, GAS]
71
- W_VERYLOW = [ADD, SUB, NOT, LT, GT, SLT, SGT, EQ, ISZERO, AND, OR,
72
- XOR, BYTE, CALLDATALOAD, MLOAD, MSTORE, MSTORE8,
73
- *(1..32).map {|i| EVM::OP.get(PUSH1 + i - 1).code}, # push1 - push32
74
- *(1..16).map {|i| EVM::OP.get(DUP1 + i - 1).code}, # dup1 - dup16
75
- *(1..16).map {|i| EVM::OP.get(SWAP1 + i - 1).code}] # swap1 - swap16
76
- W_LOW = [MUL, DIV, SDIV, MOD, SMOD, SIGNEXTEND]
77
- W_MID = [ADDMOD, MULMOD, JUMP]
78
- W_HIGH = [JUMPI]
79
- W_EXTCODE = [EXTCODESIZE]
80
-
81
-
82
- class << self
83
- include Ciri::EVM::OP
84
-
85
- # C(σ,μ,I)
86
- # calculate cost of current operation
87
- def cost_of_operation(vm)
88
- ms = vm.machine_state
89
- instruction = vm.instruction
90
- w = instruction.get_op(vm.pc)
91
- if w == SSTORE
92
- cost_of_sstore(vm)
93
- elsif w == EXP && ms.get_stack(1, Integer) == 0
94
- G_EXP
95
- elsif w == EXP && (x = ms.get_stack(1, Integer)) > 0
96
- G_EXP + G_EXPBYTE * Utils.ceil_div(x.bit_length, 8)
97
- elsif w == CALLDATACOPY || w == CODECOPY || w == RETURNDATACOPY
98
- G_VERYLOW + G_COPY * Utils.ceil_div(ms.get_stack(2, Integer), 32)
99
- elsif w == EXTCODECOPY
100
- G_EXTCODE + G_COPY * Utils.ceil_div(ms.get_stack(3, Integer), 32)
101
- elsif (LOG0..LOG4).include? w
102
- G_LOG + G_LOGDATA * ms.get_stack(1, Integer) + (w - LOG0) * G_TOPIC
103
- elsif w == CALL || w == CALLCODE || w == DELEGATECALL
104
- G_CALL
105
- elsif w == SELFDESTRUCT
106
- cost_of_self_destruct(vm)
107
- elsif w == CREATE
108
- G_CREATE
109
- elsif w == SHA3
110
- G_SHA3 + G_SHA3WORD * Utils.ceil_div(ms.get_stack(1, Integer), 32)
111
- elsif w == JUMPDEST
112
- G_JUMPDEST
113
- elsif w == SLOAD
114
- G_SLOAD
115
- elsif W_ZERO.include? w
116
- G_ZERO
117
- elsif W_BASE.include? w
118
- G_BASE
119
- elsif W_VERYLOW.include? w
120
- G_VERYLOW
121
- elsif W_LOW.include? w
122
- G_LOW
123
- elsif W_MID.include? w
124
- G_MID
125
- elsif W_HIGH.include? w
126
- G_HIGH
127
- elsif W_EXTCODE.include? w
128
- G_EXTCODE
129
- elsif w == BALANCE
130
- G_BALANCE
131
- elsif w == BLOCKHASH
132
- G_BLOCKHASH
133
- else
134
- raise "can't compute cost for unknown op #{w}"
135
- end
136
- end
137
-
138
- def cost_of_memory(i)
139
- G_MEMORY * i + (i ** 2) / 512
140
- end
141
-
142
- def intrinsic_gas_of_transaction(t)
143
- gas = (t.data.each_byte || '').reduce(0) {|sum, i| sum + (i.zero? ? G_TXDATAZERO : G_TXDATANONZERO)}
144
- # gas + (t.to.empty? ? G_TXCREATE : 0) + G_TRANSACTION
145
- gas + G_TRANSACTION
146
- end
147
-
148
- def gas_of_call(vm:, gas:, to:, value:)
149
- # TODO handle gas calculation for all categories calls
150
- account_exists = vm.account_exist?(to)
151
- transfer_gas_fee = value > 0 ? G_CALLVALUE : 0
152
- create_gas_fee = !account_exists ? G_NEWACCOUNT : 0
153
- extra_gas = transfer_gas_fee + create_gas_fee
154
-
155
- total_fee = gas + extra_gas
156
- child_gas_limit = gas + (value > 0 ? G_CALLSTIPEND : 0)
157
- [child_gas_limit, total_fee]
158
- end
159
-
160
- private
161
-
162
- def cost_of_self_destruct(vm)
163
- G_SELFDESTRUCT
164
- end
165
-
166
- def cost_of_sstore(vm)
167
- ms = vm.machine_state
168
- instruction = vm.instruction
169
-
170
- key = ms.get_stack(0, Integer)
171
- value = ms.get_stack(1, Integer)
172
-
173
- current_is_empty = vm.fetch(instruction.address, key).zero?
174
- value_is_empty = value.nil? || value.zero?
175
-
176
- gas_cost = if current_is_empty && !value_is_empty
177
- G_SSET
178
- else
179
- G_RESET
180
- end
181
- gas_refund = if !current_is_empty && value_is_empty
182
- R_SCLEAR
183
- else
184
- 0
185
- end
186
-
187
- [gas_cost, gas_refund]
188
- end
189
-
190
- end
191
- end
192
-
193
- end
194
- end
195
- end