ciri 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.rspec +0 -1
- data/Gemfile.lock +11 -7
- data/LICENSE.txt +201 -21
- data/README.md +78 -25
- data/Rakefile +45 -17
- data/bin/bundle +105 -0
- data/bin/htmldiff +29 -0
- data/bin/ldiff +29 -0
- data/bin/rake +29 -0
- data/bin/rspec +29 -0
- data/ciri-crypto/.gitignore +11 -0
- data/ciri-crypto/.rspec +3 -0
- data/ciri-crypto/.travis.yml +5 -0
- data/ciri-crypto/CODE_OF_CONDUCT.md +74 -0
- data/ciri-crypto/Gemfile +6 -0
- data/ciri-crypto/Gemfile.lock +43 -0
- data/ciri-crypto/LICENSE.txt +201 -0
- data/ciri-crypto/README.md +58 -0
- data/ciri-crypto/Rakefile +6 -0
- data/ciri-crypto/bin/console +14 -0
- data/ciri-crypto/bin/setup +8 -0
- data/ciri-crypto/ciri-crypto.gemspec +40 -0
- data/ciri-crypto/lib/ciri/crypto/errors.rb +29 -0
- data/ciri-crypto/lib/ciri/crypto/signature.rb +71 -0
- data/ciri-crypto/lib/ciri/crypto/version.rb +5 -0
- data/{lib → ciri-crypto/lib}/ciri/crypto.rb +16 -69
- data/ciri-crypto/spec/ciri/crypto_spec.rb +56 -0
- data/ciri-crypto/spec/spec_helper.rb +14 -0
- data/ciri-rlp/Gemfile.lock +5 -5
- data/ciri-rlp/LICENSE.txt +201 -21
- data/ciri-rlp/README.md +1 -1
- data/ciri-rlp/ciri-rlp.gemspec +3 -3
- data/ciri-rlp/lib/ciri/rlp/decode.rb +10 -16
- data/ciri-rlp/lib/ciri/rlp/encode.rb +10 -16
- data/ciri-rlp/lib/ciri/rlp/serializable.rb +10 -16
- data/ciri-rlp/lib/ciri/rlp.rb +10 -16
- data/ciri-rlp/spec/ciri/fixture_spec.rb +10 -16
- data/ciri-rlp/spec/ciri/rlp/serializable_spec.rb +10 -16
- data/ciri-utils/Gemfile.lock +4 -4
- data/ciri-utils/LICENSE.txt +201 -21
- data/ciri-utils/README.md +1 -1
- data/ciri-utils/ciri-utils.gemspec +2 -2
- data/ciri-utils/lib/ciri/utils/logger.rb +10 -16
- data/ciri-utils/lib/ciri/utils/number.rb +10 -16
- data/ciri-utils/lib/ciri/utils/version.rb +1 -1
- data/ciri-utils/lib/ciri/utils.rb +3 -3
- data/ciri.gemspec +4 -3
- data/docker/{Base → Dockerfile} +9 -3
- data/lib/ciri/actor.rb +10 -16
- data/lib/ciri/bloom_filter.rb +11 -17
- data/lib/ciri/chain/block.rb +10 -16
- data/lib/ciri/chain/header.rb +12 -18
- data/lib/ciri/chain/header_chain.rb +8 -22
- data/lib/ciri/chain/transaction.rb +38 -33
- data/lib/ciri/chain.rb +27 -26
- data/lib/ciri/core_ext.rb +61 -0
- data/lib/ciri/db/account_db.rb +25 -21
- data/lib/ciri/db/backend/memory.rb +10 -16
- data/lib/ciri/db/backend/rocks.rb +10 -16
- data/lib/ciri/db/backend/rocks_db.rb +10 -16
- data/lib/ciri/devp2p/peer.rb +10 -16
- data/lib/ciri/devp2p/protocol.rb +10 -16
- data/lib/ciri/devp2p/protocol_io.rb +10 -16
- data/lib/ciri/devp2p/rlpx/connection.rb +10 -16
- data/lib/ciri/devp2p/rlpx/encryption_handshake.rb +13 -19
- data/lib/ciri/devp2p/rlpx/error.rb +10 -16
- data/lib/ciri/devp2p/rlpx/frame_io.rb +10 -16
- data/lib/ciri/devp2p/rlpx/message.rb +10 -16
- data/lib/ciri/devp2p/rlpx/node.rb +10 -16
- data/lib/ciri/devp2p/rlpx/protocol_handshake.rb +10 -16
- data/lib/ciri/devp2p/rlpx/protocol_messages.rb +10 -16
- data/lib/ciri/devp2p/rlpx/secrets.rb +10 -16
- data/lib/ciri/devp2p/rlpx.rb +10 -16
- data/lib/ciri/devp2p/server.rb +10 -16
- data/lib/ciri/eth/peer.rb +10 -16
- data/lib/ciri/eth/protocol_manage.rb +10 -16
- data/lib/ciri/eth/protocol_messages.rb +10 -16
- data/lib/ciri/eth/synchronizer.rb +10 -16
- data/lib/ciri/eth.rb +10 -16
- data/lib/ciri/ethash.rb +10 -16
- data/lib/ciri/evm/block_info.rb +25 -17
- data/lib/ciri/evm/errors.rb +13 -16
- data/lib/ciri/evm/execution_context.rb +136 -0
- data/lib/ciri/evm/instruction.rb +17 -24
- data/lib/ciri/evm/log_entry.rb +12 -18
- data/lib/ciri/evm/machine_state.rb +28 -33
- data/lib/ciri/evm/op.rb +52 -89
- data/lib/ciri/evm/op_call.rb +114 -0
- data/lib/ciri/evm/precompile_contract.rb +102 -0
- data/lib/ciri/evm/state.rb +28 -21
- data/lib/ciri/evm/sub_state.rb +18 -24
- data/lib/ciri/evm/vm.rb +128 -190
- data/lib/ciri/evm.rb +77 -85
- data/lib/ciri/forks/base.rb +28 -16
- data/lib/ciri/forks/byzantium.rb +43 -0
- data/lib/ciri/forks/config.rb +36 -0
- data/lib/ciri/forks/frontier/cost.rb +42 -33
- data/lib/ciri/forks/frontier.rb +47 -18
- data/lib/ciri/forks/homestead/cost.rb +195 -0
- data/lib/ciri/forks/homestead.rb +46 -0
- data/lib/ciri/forks.rb +12 -22
- data/lib/ciri/key.rb +14 -3
- data/lib/ciri/pow.rb +11 -17
- data/lib/ciri/rlp/decode.rb +10 -16
- data/lib/ciri/rlp/encode.rb +10 -16
- data/lib/ciri/rlp/serializable.rb +10 -16
- data/lib/ciri/serialize.rb +14 -17
- data/lib/ciri/trie/nibbles.rb +10 -16
- data/lib/ciri/trie/nodes.rb +12 -17
- data/lib/ciri/trie.rb +12 -18
- data/lib/ciri/types/account.rb +15 -17
- data/lib/ciri/types/address.rb +11 -17
- data/lib/ciri/types/errors.rb +10 -16
- data/lib/ciri/types/receipt.rb +12 -18
- data/lib/ciri/types/uint.rb +79 -0
- data/lib/ciri/version.rb +1 -1
- data/lib/ciri.rb +10 -16
- metadata +54 -10
- data/lib/ciri/types/number.rb +0 -73
data/lib/ciri/chain/header.rb
CHANGED
@@ -1,24 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2018 Jiang Jinyang <https://justjjy.com>
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
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:
|
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
|
11
8
|
#
|
12
|
-
#
|
13
|
-
# all copies or substantial portions of the Software.
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
10
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
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.
|
22
16
|
|
23
17
|
|
24
18
|
module Ciri
|
@@ -48,12 +42,12 @@ module Ciri
|
|
48
42
|
|
49
43
|
# header hash
|
50
44
|
def get_hash
|
51
|
-
Utils.
|
45
|
+
Utils.keccak(rlp_encode)
|
52
46
|
end
|
53
47
|
|
54
48
|
# mining_hash, used for mining
|
55
49
|
def mining_hash
|
56
|
-
Utils.
|
50
|
+
Utils.keccak(rlp_encode skip_keys: [:mix_hash, :nonce])
|
57
51
|
end
|
58
52
|
|
59
53
|
end
|
@@ -10,12 +10,11 @@ module Ciri
|
|
10
10
|
TD_SUFFIX = 't'
|
11
11
|
NUM_SUFFIX = 'n'
|
12
12
|
|
13
|
-
attr_reader :store
|
13
|
+
attr_reader :store
|
14
14
|
|
15
|
-
def initialize(store,
|
15
|
+
def initialize(store, fork_config: nil)
|
16
16
|
@store = store
|
17
|
-
@
|
18
|
-
@homestead_block = homestead_block
|
17
|
+
@fork_config = fork_config
|
19
18
|
end
|
20
19
|
|
21
20
|
def head
|
@@ -71,29 +70,16 @@ module Ciri
|
|
71
70
|
def calculate_difficulty(header, parent_header)
|
72
71
|
return header.difficulty if header.number == 0
|
73
72
|
|
73
|
+
fork_schema = @fork_config.choose_fork(header.number)
|
74
|
+
difficulty_time_factor = fork_schema.difficulty_time_factor(header, parent_header)
|
75
|
+
|
74
76
|
x = parent_header.difficulty / 2048
|
75
|
-
y = header.ommers_hash == Utils::BLANK_SHA3 ? 1 : 2
|
76
|
-
|
77
|
-
# handle byzantium fork
|
78
|
-
# https://github.com/ethereum/EIPs/blob/181867ae830df5419eb9982d2a24797b2dcad28f/EIPS/eip-609.md
|
79
|
-
# https://github.com/ethereum/EIPs/blob/984cf5de90bbf5fbe7e49be227b0c2f9567e661e/EIPS/eip-100.md
|
80
|
-
byzantium_fork = byzantium_block && header.number > byzantium_block
|
81
|
-
# https://github.com/ethereum/EIPs/blob/984cf5de90bbf5fbe7e49be227b0c2f9567e661e/EIPS/eip-2.md
|
82
|
-
homestead_fork = homestead_block && header.number > homestead_block
|
83
|
-
|
84
|
-
time_factor = if byzantium_fork
|
85
|
-
[y - (header.timestamp - parent_header.timestamp) / 9, -99].max
|
86
|
-
elsif homestead_fork
|
87
|
-
[1 - (header.timestamp - parent_header.timestamp) / 10, -99].max
|
88
|
-
else
|
89
|
-
(header.timestamp - parent_header.timestamp) < 13 ? 1 : -1
|
90
|
-
end
|
91
77
|
|
92
78
|
# difficulty bomb
|
93
|
-
height =
|
79
|
+
height = fork_schema.difficulty_virtual_height(header.number)
|
94
80
|
height_factor = 2 ** (height / 100000 - 2)
|
95
81
|
|
96
|
-
difficulty = (parent_header.difficulty + x *
|
82
|
+
difficulty = (parent_header.difficulty + x * difficulty_time_factor + height_factor).to_i
|
97
83
|
[header.difficulty, difficulty].max
|
98
84
|
end
|
99
85
|
|
@@ -1,35 +1,33 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2018 Jiang Jinyang <https://justjjy.com>
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
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:
|
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
|
11
8
|
#
|
12
|
-
#
|
13
|
-
# all copies or substantial portions of the Software.
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
10
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
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.
|
22
16
|
|
23
17
|
|
24
18
|
require 'ciri/rlp'
|
25
19
|
require 'ciri/crypto'
|
20
|
+
require 'ciri/key'
|
26
21
|
require 'ciri/types/address'
|
22
|
+
require 'ciri/types/uint'
|
27
23
|
|
28
24
|
module Ciri
|
29
25
|
class Chain
|
30
26
|
|
31
27
|
class Transaction
|
32
28
|
|
29
|
+
include Types
|
30
|
+
|
33
31
|
class InvalidError < StandardError
|
34
32
|
end
|
35
33
|
|
@@ -39,15 +37,15 @@ module Ciri
|
|
39
37
|
include RLP::Serializable
|
40
38
|
|
41
39
|
schema [
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
40
|
+
{nonce: Integer},
|
41
|
+
{gas_price: Integer},
|
42
|
+
{gas_limit: Integer},
|
43
|
+
{to: Address},
|
44
|
+
{value: Integer},
|
45
|
+
:data,
|
46
|
+
{v: Integer},
|
47
|
+
{r: Integer},
|
48
|
+
{s: Integer}
|
51
49
|
]
|
52
50
|
|
53
51
|
default_data v: 0, r: 0, s: 0, data: "\x00".b
|
@@ -56,7 +54,7 @@ module Ciri
|
|
56
54
|
# @return address String
|
57
55
|
def sender
|
58
56
|
@sender ||= begin
|
59
|
-
address =
|
57
|
+
address = Key.ecdsa_recover(sign_hash(chain_id), signature).to_address
|
60
58
|
address.validate
|
61
59
|
address
|
62
60
|
end
|
@@ -93,16 +91,15 @@ module Ciri
|
|
93
91
|
if chain_id
|
94
92
|
list += [chain_id, ''.b, ''.b]
|
95
93
|
end
|
96
|
-
Utils.
|
94
|
+
Utils.keccak(RLP.encode_simple list)
|
97
95
|
end
|
98
96
|
|
99
97
|
def get_hash
|
100
|
-
Utils.
|
98
|
+
Utils.keccak rlp_encode
|
101
99
|
end
|
102
100
|
|
103
101
|
# validate transaction
|
104
|
-
|
105
|
-
def validate!(intrinsic_gas_of_transaction: nil)
|
102
|
+
def validate!(fork_schema: nil)
|
106
103
|
begin
|
107
104
|
sender
|
108
105
|
rescue Ciri::Crypto::ECDSASignatureError => e
|
@@ -112,11 +109,19 @@ module Ciri
|
|
112
109
|
end
|
113
110
|
|
114
111
|
raise InvalidError.new('signature rvs error') unless signature.valid?
|
115
|
-
raise InvalidError.new('signature s is low') unless signature.low_s?
|
112
|
+
# raise InvalidError.new('signature s is low') unless signature.low_s?
|
113
|
+
raise InvalidError.new('gas_price overflow') unless UInt256.valid?(gas_price)
|
114
|
+
raise InvalidError.new('nonce overflow') unless UInt256.valid?(nonce)
|
115
|
+
raise InvalidError.new('gas_limit overflow') unless UInt256.valid?(gas_limit)
|
116
|
+
raise InvalidError.new('value overflow') unless UInt256.valid?(value)
|
117
|
+
|
118
|
+
if fork_schema
|
119
|
+
if (reason = fork_schema.validate_transaction(self))
|
120
|
+
raise InvalidError.new(reason)
|
121
|
+
end
|
116
122
|
|
117
|
-
if intrinsic_gas_of_transaction
|
118
123
|
begin
|
119
|
-
intrinsic_gas = intrinsic_gas_of_transaction
|
124
|
+
intrinsic_gas = fork_schema.intrinsic_gas_of_transaction(self)
|
120
125
|
rescue StandardError
|
121
126
|
raise InvalidError.new 'intrinsic gas calculation error'
|
122
127
|
end
|
data/lib/ciri/chain.rb
CHANGED
@@ -1,29 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2018 Jiang Jinyang <https://justjjy.com>
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
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:
|
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
|
11
8
|
#
|
12
|
-
#
|
13
|
-
# all copies or substantial portions of the Software.
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
10
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
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.
|
22
16
|
|
23
17
|
|
24
18
|
require 'forwardable'
|
25
19
|
require 'ciri/evm'
|
20
|
+
require 'ciri/evm/state'
|
26
21
|
require 'ciri/utils/logger'
|
22
|
+
require 'ciri/forks'
|
27
23
|
require_relative 'chain/header_chain'
|
28
24
|
require_relative 'chain/block'
|
29
25
|
require_relative 'chain/header'
|
@@ -55,18 +51,19 @@ module Ciri
|
|
55
51
|
|
56
52
|
attr_reader :genesis, :network_id, :store, :header_chain
|
57
53
|
|
58
|
-
def initialize(store, genesis:, network_id:,
|
54
|
+
def initialize(store, genesis:, network_id:, fork_config:)
|
59
55
|
@store = store
|
60
|
-
@header_chain = HeaderChain.new(store,
|
56
|
+
@header_chain = HeaderChain.new(store, fork_config: fork_config)
|
61
57
|
@genesis = genesis
|
62
58
|
@network_id = network_id
|
63
|
-
@
|
59
|
+
@fork_config = fork_config
|
64
60
|
load_or_init_store
|
65
61
|
end
|
66
62
|
|
67
63
|
# run block
|
68
|
-
def import_block(block)
|
69
|
-
|
64
|
+
def import_block(block, validate: true)
|
65
|
+
debug("import block #{block.header.number}")
|
66
|
+
validate_block(block) if validate
|
70
67
|
write_block(block)
|
71
68
|
|
72
69
|
# update state
|
@@ -84,16 +81,19 @@ module Ciri
|
|
84
81
|
end
|
85
82
|
end
|
86
83
|
|
84
|
+
parent_header = @header_chain.get_header(block.header.parent_hash)
|
85
|
+
state = EVM::State.new(store, state_root: parent_header.state_root, chain: self)
|
86
|
+
evm = EVM.new(state: state, fork_schema: @fork_config.choose_fork(block.header.number))
|
87
87
|
# valid transactions and gas
|
88
88
|
begin
|
89
|
-
receipts =
|
89
|
+
receipts = evm.transition(block)
|
90
90
|
rescue EVM::InvalidTransition => e
|
91
91
|
raise InvalidBlockError.new(e.message)
|
92
92
|
end
|
93
93
|
|
94
94
|
# verify state root
|
95
|
-
if
|
96
|
-
error("incorrect state_root, evm: #{Utils.to_hex
|
95
|
+
if evm.state_root != block.header.state_root
|
96
|
+
error("incorrect state_root, evm: #{Utils.to_hex evm.state_root}, header: #{Utils.to_hex block.header.state_root} height: #{block.header.number}")
|
97
97
|
raise InvalidBlockError.new("incorrect state_root")
|
98
98
|
end
|
99
99
|
|
@@ -132,7 +132,7 @@ module Ciri
|
|
132
132
|
|
133
133
|
# insert blocks in order
|
134
134
|
# blocks must be ordered from lower height to higher height
|
135
|
-
def insert_blocks(blocks)
|
135
|
+
def insert_blocks(blocks, validate: true)
|
136
136
|
prev_block = blocks[0]
|
137
137
|
blocks[1..-1].each do |block|
|
138
138
|
unless block.number == prev_block.number + 1 && block.parent_hash == prev_block.get_hash
|
@@ -141,7 +141,7 @@ module Ciri
|
|
141
141
|
end
|
142
142
|
|
143
143
|
blocks.each do |block|
|
144
|
-
|
144
|
+
import_block(block, validate: validate)
|
145
145
|
end
|
146
146
|
end
|
147
147
|
|
@@ -181,6 +181,7 @@ module Ciri
|
|
181
181
|
private
|
182
182
|
|
183
183
|
def is_kin?(ommer, parent, n)
|
184
|
+
return false if parent.nil?
|
184
185
|
return false if n == 0
|
185
186
|
return true if get_header(ommer.parent_hash) == get_header(parent.header.parent_hash) &&
|
186
187
|
ommer != parent.header &&
|
@@ -0,0 +1,61 @@
|
|
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/utils'
|
19
|
+
|
20
|
+
module Ciri
|
21
|
+
# Core extension module for convenient
|
22
|
+
module CoreExt
|
23
|
+
|
24
|
+
refine(String) do
|
25
|
+
def to_hex
|
26
|
+
Utils.to_hex(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
alias encode_hex to_hex
|
30
|
+
|
31
|
+
def decode_hex
|
32
|
+
Utils.to_bytes(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
def keccak
|
36
|
+
Utils.keccak(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
def decode_big_endian
|
40
|
+
Utils.big_endian_decode(self)
|
41
|
+
end
|
42
|
+
|
43
|
+
alias decode_number decode_big_endian
|
44
|
+
|
45
|
+
def pad_zero(size)
|
46
|
+
self.rjust(size, "\x00".b)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
refine(Integer) do
|
51
|
+
def ceil_div(size)
|
52
|
+
Utils.ceil_div(self, size)
|
53
|
+
end
|
54
|
+
|
55
|
+
def encode_big_endian
|
56
|
+
Utils.big_endian_encode(self)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
data/lib/ciri/db/account_db.rb
CHANGED
@@ -1,31 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2018 Jiang Jinyang <https://justjjy.com>
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
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:
|
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
|
11
8
|
#
|
12
|
-
#
|
13
|
-
# all copies or substantial portions of the Software.
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
10
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
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.
|
22
16
|
|
23
17
|
|
18
|
+
require 'ciri/core_ext'
|
24
19
|
require 'ciri/trie'
|
25
20
|
require 'ciri/types/account'
|
26
21
|
require 'ciri/serialize'
|
27
22
|
require 'ciri/utils/logger'
|
28
23
|
|
24
|
+
using Ciri::CoreExt
|
25
|
+
|
29
26
|
module Ciri
|
30
27
|
module DB
|
31
28
|
class AccountDB
|
@@ -38,7 +35,7 @@ module Ciri
|
|
38
35
|
def initialize(db, root_hash: nil)
|
39
36
|
@db = db
|
40
37
|
root_hash ||= Trie::BLANK_NODE_HASH
|
41
|
-
@trie = Trie.new(db: @db, root_hash: root_hash, prune:
|
38
|
+
@trie = Trie.new(db: @db, root_hash: root_hash, prune: false)
|
42
39
|
end
|
43
40
|
|
44
41
|
def root_hash
|
@@ -48,7 +45,7 @@ module Ciri
|
|
48
45
|
def store(address, key, value)
|
49
46
|
account = find_account address
|
50
47
|
trie = Trie.new(db: @db, root_hash: account.storage_root)
|
51
|
-
|
48
|
+
debug("#{address.to_hex}[#{key}] -> #{value}")
|
52
49
|
converted_key = convert_key Utils.big_endian_encode(key, size: 32)
|
53
50
|
|
54
51
|
if value && value != 0
|
@@ -97,7 +94,7 @@ module Ciri
|
|
97
94
|
def set_account_code(address, code)
|
98
95
|
code ||= ''.b
|
99
96
|
account = find_account(address)
|
100
|
-
account.code_hash = Utils.
|
97
|
+
account.code_hash = Utils.keccak(code)
|
101
98
|
update_account(address, account)
|
102
99
|
db[account.code_hash] = code
|
103
100
|
end
|
@@ -119,7 +116,14 @@ module Ciri
|
|
119
116
|
end
|
120
117
|
end
|
121
118
|
|
119
|
+
def account_exist?(address)
|
120
|
+
rlp_encoded_account = @trie[convert_key address]
|
121
|
+
non_exists = rlp_encoded_account.nil? || rlp_encoded_account.size == 0
|
122
|
+
!non_exists
|
123
|
+
end
|
124
|
+
|
122
125
|
def delete_account(address)
|
126
|
+
debug "delete #{address.to_s.to_hex}"
|
123
127
|
@trie.delete(convert_key address)
|
124
128
|
end
|
125
129
|
|
@@ -131,13 +135,13 @@ module Ciri
|
|
131
135
|
|
132
136
|
def update_account(address, account)
|
133
137
|
debug 'update account'
|
134
|
-
debug
|
138
|
+
debug address.to_hex
|
135
139
|
debug account.serializable_attributes
|
136
140
|
@trie[convert_key address] = Types::Account.rlp_encode account
|
137
141
|
end
|
138
142
|
|
139
143
|
def convert_key(key)
|
140
|
-
Utils.
|
144
|
+
Utils.keccak key.to_s
|
141
145
|
end
|
142
146
|
|
143
147
|
end
|
@@ -1,24 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2018 Jiang Jinyang <https://justjjy.com>
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
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:
|
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
|
11
8
|
#
|
12
|
-
#
|
13
|
-
# all copies or substantial portions of the Software.
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
10
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
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.
|
22
16
|
|
23
17
|
|
24
18
|
require_relative 'rocks_db'
|
@@ -1,24 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2018 Jiang Jinyang <https://justjjy.com>
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
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:
|
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
|
11
8
|
#
|
12
|
-
#
|
13
|
-
# all copies or substantial portions of the Software.
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
10
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
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.
|
22
16
|
|
23
17
|
|
24
18
|
require_relative 'rocks_db'
|
@@ -1,24 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2018 Jiang Jinyang <https://justjjy.com>
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
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:
|
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
|
11
8
|
#
|
12
|
-
#
|
13
|
-
# all copies or substantial portions of the Software.
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
10
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
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.
|
22
16
|
|
23
17
|
|
24
18
|
require 'ffi'
|