ciri 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +8 -3
  3. data/README.md +5 -0
  4. data/Rakefile +24 -2
  5. data/ciri-rlp/.gitignore +11 -0
  6. data/ciri-rlp/.rspec +3 -0
  7. data/ciri-rlp/.travis.yml +5 -0
  8. data/ciri-rlp/CODE_OF_CONDUCT.md +74 -0
  9. data/ciri-rlp/Gemfile +6 -0
  10. data/ciri-rlp/Gemfile.lock +39 -0
  11. data/ciri-rlp/LICENSE.txt +21 -0
  12. data/ciri-rlp/README.md +98 -0
  13. data/ciri-rlp/Rakefile +6 -0
  14. data/ciri-rlp/bin/console +14 -0
  15. data/ciri-rlp/bin/setup +8 -0
  16. data/ciri-rlp/ciri-rlp.gemspec +28 -0
  17. data/{lib → ciri-rlp/lib}/ciri/rlp.rb +2 -2
  18. data/ciri-rlp/lib/ciri/rlp/decode.rb +144 -0
  19. data/ciri-rlp/lib/ciri/rlp/encode.rb +140 -0
  20. data/ciri-rlp/lib/ciri/rlp/serializable.rb +241 -0
  21. data/ciri-rlp/lib/ciri/rlp/version.rb +5 -0
  22. data/ciri-rlp/spec/ciri/fixture_spec.rb +95 -0
  23. data/ciri-rlp/spec/ciri/rlp/decode_spec.rb +68 -0
  24. data/ciri-rlp/spec/ciri/rlp/encode_spec.rb +72 -0
  25. data/ciri-rlp/spec/ciri/rlp/serializable_spec.rb +147 -0
  26. data/ciri-rlp/spec/ciri/rlp_spec.rb +5 -0
  27. data/ciri-rlp/spec/spec_helper.rb +14 -0
  28. data/ciri-utils/.gitignore +11 -0
  29. data/ciri-utils/.rspec +3 -0
  30. data/ciri-utils/.travis.yml +5 -0
  31. data/ciri-utils/CODE_OF_CONDUCT.md +74 -0
  32. data/ciri-utils/Gemfile +6 -0
  33. data/ciri-utils/Gemfile.lock +37 -0
  34. data/ciri-utils/LICENSE.txt +21 -0
  35. data/ciri-utils/README.md +61 -0
  36. data/ciri-utils/Rakefile +6 -0
  37. data/ciri-utils/bin/console +14 -0
  38. data/ciri-utils/bin/setup +8 -0
  39. data/ciri-utils/ciri-utils.gemspec +28 -0
  40. data/{lib → ciri-utils/lib}/ciri/utils.rb +9 -33
  41. data/{lib → ciri-utils/lib}/ciri/utils/logger.rb +0 -0
  42. data/{lib → ciri-utils/lib}/ciri/utils/number.rb +15 -12
  43. data/ciri-utils/lib/ciri/utils/version.rb +5 -0
  44. data/ciri-utils/spec/ciri/utils_spec.rb +5 -0
  45. data/ciri-utils/spec/spec_helper.rb +14 -0
  46. data/ciri.gemspec +6 -3
  47. data/lib/ciri/bloom_filter.rb +83 -0
  48. data/lib/ciri/chain.rb +67 -130
  49. data/lib/ciri/chain/header.rb +2 -2
  50. data/lib/ciri/chain/header_chain.rb +136 -0
  51. data/lib/ciri/chain/transaction.rb +1 -1
  52. data/lib/ciri/crypto.rb +2 -2
  53. data/lib/ciri/db/account_db.rb +145 -0
  54. data/lib/ciri/db/backend/memory.rb +24 -1
  55. data/lib/ciri/devp2p/peer.rb +1 -1
  56. data/lib/ciri/devp2p/rlpx/connection.rb +5 -5
  57. data/lib/ciri/eth/peer.rb +3 -3
  58. data/lib/ciri/eth/protocol_manage.rb +3 -3
  59. data/lib/ciri/eth/protocol_messages.rb +27 -26
  60. data/lib/ciri/ethash.rb +18 -3
  61. data/lib/ciri/evm.rb +101 -56
  62. data/lib/ciri/{utils/lib_c.rb → evm/errors.rb} +28 -18
  63. data/lib/ciri/evm/instruction.rb +3 -1
  64. data/lib/ciri/evm/{serialize.rb → log_entry.rb} +9 -27
  65. data/lib/ciri/evm/machine_state.rb +21 -2
  66. data/lib/ciri/evm/op.rb +19 -16
  67. data/lib/ciri/evm/state.rb +61 -0
  68. data/lib/ciri/evm/vm.rb +78 -102
  69. data/lib/ciri/forks.rb +1 -4
  70. data/lib/ciri/forks/base.rb +55 -0
  71. data/lib/ciri/forks/frontier.rb +37 -10
  72. data/lib/ciri/forks/frontier/cost.rb +186 -0
  73. data/lib/ciri/key.rb +1 -1
  74. data/lib/ciri/pow.rb +1 -1
  75. data/lib/ciri/rlp/decode.rb +6 -3
  76. data/lib/ciri/rlp/encode.rb +10 -10
  77. data/lib/ciri/rlp/serializable.rb +12 -9
  78. data/lib/ciri/serialize.rb +58 -0
  79. data/lib/ciri/trie.rb +362 -0
  80. data/lib/ciri/trie/nibbles.rb +97 -0
  81. data/lib/ciri/trie/nodes.rb +187 -0
  82. data/lib/ciri/{evm → types}/account.rb +20 -13
  83. data/lib/ciri/types/address.rb +16 -11
  84. data/lib/ciri/types/number.rb +73 -0
  85. data/lib/ciri/types/receipt.rb +57 -0
  86. data/lib/ciri/version.rb +1 -1
  87. metadata +82 -19
  88. data/lib/ciri/evm/forks/frontier.rb +0 -183
@@ -1,183 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright (c) 2018, by Jiang Jinyang. <https://justjjy.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
22
-
23
-
24
- require 'ciri/evm/op'
25
-
26
- module Ciri
27
- class EVM
28
- module Forks
29
- module Frontier
30
-
31
- module Cost
32
- # fee schedule, start with G
33
- G_ZERO = 0
34
- G_BASE = 2
35
- G_VERYLOW = 3
36
- G_LOW = 5
37
- G_MID = 8
38
- G_HIGH = 10
39
- G_EXTCODE = 700
40
- G_BALANCE = 400
41
- G_SLOAD = 50
42
- G_JUMPDEST = 1
43
- G_SSET = 20000
44
- G_RESET = 5000
45
- R_SCLEAR = 15000
46
- R_SELFDESTRUCT = 24000
47
- G_SELFDESTRUCT = 0
48
- G_CREATE = 32000
49
- G_CODEDEPOSIT = 200
50
- G_CALL = 700
51
- G_CALLVALUE = 9000
52
- G_CALLSTIPEND = 2300
53
- G_NEWACCOUNT = 25000
54
- G_EXP = 10
55
- G_EXPBYTE = 10
56
- G_MEMORY = 3
57
- G_TXCREATE = 32000
58
- G_TXDATAZERO = 4
59
- G_TXDATANONZERO = 68
60
- G_TRANSACTION = 21000
61
- G_LOG = 375
62
- G_LOGDATA = 8
63
- G_TOPIC = 375
64
- G_SHA3 = 30
65
- G_SHA3WORD = 6
66
- G_COPY = 3
67
- G_BLOCKHASH = 20
68
- G_QUADDIVISOR = 100
69
-
70
- # operation code by group, for later calculation
71
- W_ZERO = [OP::STOP, OP::RETURN, OP::REVERT]
72
- W_BASE = [OP::ADDRESS, OP::ORIGIN, OP::CALLER, OP::CALLVALUE, OP::CALLDATASIZE, OP::CODESIZE, OP::GASPRICE,
73
- OP::COINBASE, OP::TIMESTAMP, OP::NUMBER, OP::DIFFICULTY, OP::GASLIMIT, OP::RETURNDATASIZE,
74
- OP::POP, OP::PC, OP::MSIZE, OP::GAS]
75
- W_VERYLOW = [OP::ADD, OP::SUB, OP::NOT, OP::LT, OP::GT, OP::SLT, OP::SGT, OP::EQ, OP::ISZERO, OP::AND, OP::OR,
76
- OP::XOR, OP::BYTE, OP::CALLDATALOAD, OP::MLOAD, OP::MSTORE, OP::MSTORE8,
77
- *(1..32).map {|i| OP.get(OP::PUSH1 + i - 1).code}, # push1 - push32
78
- *(1..16).map {|i| OP.get(OP::DUP1 + i - 1).code}, # dup1 - dup16
79
- *(1..16).map {|i| OP.get(OP::SWAP1 + i - 1).code}] # swap1 - swap16
80
- W_LOW = [OP::MUL, OP::DIV, OP::SDIV, OP::MOD, OP::SMOD, OP::SIGNEXTEND]
81
- W_MID = [OP::ADDMOD, OP::MULMOD, OP::JUMP]
82
- W_HIGH = [OP::JUMPI]
83
- W_EXTCODE = [OP::EXTCODESIZE]
84
-
85
-
86
- class << self
87
- # C(σ,μ,I)
88
- # calculate cost of current operation
89
- def cost_of_operation(vm)
90
- ms = vm.machine_state
91
- instruction = vm.instruction
92
- w = instruction.get_op(ms.pc)
93
- if w == OP::SSTORE
94
- cost_of_sstore(vm)
95
- elsif w == OP::EXP && ms.get_stack(1, Integer) == 0
96
- G_EXP
97
- elsif w == OP::EXP && (x = ms.get_stack(1, Integer)) > 0
98
- G_EXP + G_EXPBYTE * Utils.ceil_div(x.bit_length, 8)
99
- elsif w == OP::CALLDATACOPY || w == OP::CODECOPY || w == OP::RETURNDATACOPY
100
- G_VERYLOW + G_COPY * Utils.ceil_div(ms.get_stack(2, Integer), 32)
101
- elsif w == OP::EXTCODECOPY
102
- G_EXTCODE + G_COPY * Utils.ceil_div(ms.get_stack(3, Integer), 32)
103
- elsif (OP::LOG0..OP::LOG4).include? w
104
- G_LOG + G_LOGDATA * ms.get_stack(1, Integer) + (w - OP::LOG0) * G_TOPIC
105
- elsif w == OP::CALL || w == OP::CALLCODE || w == OP::DELEGATECALL
106
- 1# cost_of_call(state, ms)
107
- elsif w == OP::SELFDESTRUCT
108
- cost_of_self_destruct(vm)
109
- elsif w == OP::CREATE
110
- G_CREATE
111
- elsif w == OP::SHA3
112
- G_SHA3 + G_SHA3WORD * Utils.ceil_div(ms.get_stack(1, Integer), 32)
113
- elsif w == OP::JUMPDEST
114
- G_JUMPDEST
115
- elsif w == OP::SLOAD
116
- G_SLOAD
117
- elsif W_ZERO.include? w
118
- G_ZERO
119
- elsif W_BASE.include? w
120
- G_BASE
121
- elsif W_VERYLOW.include? w
122
- G_VERYLOW
123
- elsif W_LOW.include? w
124
- G_LOW
125
- elsif W_MID.include? w
126
- G_MID
127
- elsif W_HIGH.include? w
128
- G_HIGH
129
- elsif W_EXTCODE.include? w
130
- G_EXTCODE
131
- elsif w == OP::BALANCE
132
- G_BALANCE
133
- elsif w == OP::BLOCKHASH
134
- G_BLOCKHASH
135
- else
136
- raise "can't compute cost for unknown op #{w}"
137
- end
138
- end
139
-
140
- def cost_of_memory(i)
141
- G_MEMORY * i + (i ** 2) / 512
142
- end
143
-
144
- def intrinsic_gas_of_transaction(t)
145
- gas = (t.data.each_byte || '').reduce(0) {|sum, i| sum + i.zero? ? G_TXDATAZERO : G_TXDATANONZERO}
146
- gas + (t.to.empty? ? G_TXCREATE : 0) + G_TRANSACTION
147
- end
148
-
149
- private
150
-
151
- def cost_of_self_destruct(vm)
152
- G_SELFDESTRUCT
153
- end
154
-
155
- def cost_of_call
156
-
157
- end
158
-
159
- def cost_of_sstore(vm)
160
- ms = vm.machine_state
161
- instruction = vm.instruction
162
-
163
- key = ms.stack[0]
164
- value = ms.stack[1]
165
- account = vm.find_account instruction.address
166
-
167
- value_exists = !Ciri::Utils.blank_binary?(value)
168
- has_key = account && account.storage[key]
169
-
170
- if value_exists && !has_key
171
- G_SSET
172
- else
173
- G_RESET
174
- end
175
- end
176
-
177
- end
178
- end
179
-
180
- end
181
- end
182
- end
183
- end