ciri 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +8 -3
- data/README.md +5 -0
- data/Rakefile +24 -2
- data/ciri-rlp/.gitignore +11 -0
- data/ciri-rlp/.rspec +3 -0
- data/ciri-rlp/.travis.yml +5 -0
- data/ciri-rlp/CODE_OF_CONDUCT.md +74 -0
- data/ciri-rlp/Gemfile +6 -0
- data/ciri-rlp/Gemfile.lock +39 -0
- data/ciri-rlp/LICENSE.txt +21 -0
- data/ciri-rlp/README.md +98 -0
- data/ciri-rlp/Rakefile +6 -0
- data/ciri-rlp/bin/console +14 -0
- data/ciri-rlp/bin/setup +8 -0
- data/ciri-rlp/ciri-rlp.gemspec +28 -0
- data/{lib → ciri-rlp/lib}/ciri/rlp.rb +2 -2
- data/ciri-rlp/lib/ciri/rlp/decode.rb +144 -0
- data/ciri-rlp/lib/ciri/rlp/encode.rb +140 -0
- data/ciri-rlp/lib/ciri/rlp/serializable.rb +241 -0
- data/ciri-rlp/lib/ciri/rlp/version.rb +5 -0
- data/ciri-rlp/spec/ciri/fixture_spec.rb +95 -0
- data/ciri-rlp/spec/ciri/rlp/decode_spec.rb +68 -0
- data/ciri-rlp/spec/ciri/rlp/encode_spec.rb +72 -0
- data/ciri-rlp/spec/ciri/rlp/serializable_spec.rb +147 -0
- data/ciri-rlp/spec/ciri/rlp_spec.rb +5 -0
- data/ciri-rlp/spec/spec_helper.rb +14 -0
- data/ciri-utils/.gitignore +11 -0
- data/ciri-utils/.rspec +3 -0
- data/ciri-utils/.travis.yml +5 -0
- data/ciri-utils/CODE_OF_CONDUCT.md +74 -0
- data/ciri-utils/Gemfile +6 -0
- data/ciri-utils/Gemfile.lock +37 -0
- data/ciri-utils/LICENSE.txt +21 -0
- data/ciri-utils/README.md +61 -0
- data/ciri-utils/Rakefile +6 -0
- data/ciri-utils/bin/console +14 -0
- data/ciri-utils/bin/setup +8 -0
- data/ciri-utils/ciri-utils.gemspec +28 -0
- data/{lib → ciri-utils/lib}/ciri/utils.rb +9 -33
- data/{lib → ciri-utils/lib}/ciri/utils/logger.rb +0 -0
- data/{lib → ciri-utils/lib}/ciri/utils/number.rb +15 -12
- data/ciri-utils/lib/ciri/utils/version.rb +5 -0
- data/ciri-utils/spec/ciri/utils_spec.rb +5 -0
- data/ciri-utils/spec/spec_helper.rb +14 -0
- data/ciri.gemspec +6 -3
- data/lib/ciri/bloom_filter.rb +83 -0
- data/lib/ciri/chain.rb +67 -130
- data/lib/ciri/chain/header.rb +2 -2
- data/lib/ciri/chain/header_chain.rb +136 -0
- data/lib/ciri/chain/transaction.rb +1 -1
- data/lib/ciri/crypto.rb +2 -2
- data/lib/ciri/db/account_db.rb +145 -0
- data/lib/ciri/db/backend/memory.rb +24 -1
- data/lib/ciri/devp2p/peer.rb +1 -1
- data/lib/ciri/devp2p/rlpx/connection.rb +5 -5
- data/lib/ciri/eth/peer.rb +3 -3
- data/lib/ciri/eth/protocol_manage.rb +3 -3
- data/lib/ciri/eth/protocol_messages.rb +27 -26
- data/lib/ciri/ethash.rb +18 -3
- data/lib/ciri/evm.rb +101 -56
- data/lib/ciri/{utils/lib_c.rb → evm/errors.rb} +28 -18
- data/lib/ciri/evm/instruction.rb +3 -1
- data/lib/ciri/evm/{serialize.rb → log_entry.rb} +9 -27
- data/lib/ciri/evm/machine_state.rb +21 -2
- data/lib/ciri/evm/op.rb +19 -16
- data/lib/ciri/evm/state.rb +61 -0
- data/lib/ciri/evm/vm.rb +78 -102
- data/lib/ciri/forks.rb +1 -4
- data/lib/ciri/forks/base.rb +55 -0
- data/lib/ciri/forks/frontier.rb +37 -10
- data/lib/ciri/forks/frontier/cost.rb +186 -0
- data/lib/ciri/key.rb +1 -1
- data/lib/ciri/pow.rb +1 -1
- data/lib/ciri/rlp/decode.rb +6 -3
- data/lib/ciri/rlp/encode.rb +10 -10
- data/lib/ciri/rlp/serializable.rb +12 -9
- data/lib/ciri/serialize.rb +58 -0
- data/lib/ciri/trie.rb +362 -0
- data/lib/ciri/trie/nibbles.rb +97 -0
- data/lib/ciri/trie/nodes.rb +187 -0
- data/lib/ciri/{evm → types}/account.rb +20 -13
- data/lib/ciri/types/address.rb +16 -11
- data/lib/ciri/types/number.rb +73 -0
- data/lib/ciri/types/receipt.rb +57 -0
- data/lib/ciri/version.rb +1 -1
- metadata +82 -19
- data/lib/ciri/evm/forks/frontier.rb +0 -183
@@ -20,7 +20,7 @@
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
|
-
|
23
|
+
require 'ciri/rlp/version'
|
24
24
|
require_relative 'rlp/decode'
|
25
25
|
require_relative 'rlp/encode'
|
26
26
|
require_relative 'rlp/serializable'
|
@@ -35,4 +35,4 @@ module Ciri
|
|
35
35
|
extend Decode
|
36
36
|
|
37
37
|
end
|
38
|
-
end
|
38
|
+
end
|
@@ -0,0 +1,144 @@
|
|
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 'stringio'
|
25
|
+
|
26
|
+
module Ciri
|
27
|
+
module RLP
|
28
|
+
module Decode
|
29
|
+
|
30
|
+
# Decode input from rlp encoding, only produce string or array
|
31
|
+
#
|
32
|
+
# Examples:
|
33
|
+
#
|
34
|
+
# Ciri::RLP.decode(input)
|
35
|
+
#
|
36
|
+
def decode(input, type = Raw)
|
37
|
+
decode_with_type(input, type)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Use this method after RLP.decode, decode values from string or array to specific types
|
41
|
+
# see Ciri::RLP::Serializable::TYPES for supported types
|
42
|
+
#
|
43
|
+
# Examples:
|
44
|
+
#
|
45
|
+
# item = Ciri::RLP.decode(encoded_text)
|
46
|
+
# decode_with_type(item, Integer)
|
47
|
+
#
|
48
|
+
def decode_with_type(s, type)
|
49
|
+
s = StringIO.new(s) if s.is_a?(String)
|
50
|
+
if type == Integer
|
51
|
+
item = s.read(1)
|
52
|
+
if item.nil?
|
53
|
+
raise InvalidValueError.new "invalid bool value nil"
|
54
|
+
elsif item == "\x80".b || item.empty?
|
55
|
+
0
|
56
|
+
elsif item.ord < 0x80
|
57
|
+
item.ord
|
58
|
+
else
|
59
|
+
size = item[0].ord - 0x80
|
60
|
+
Ciri::Utils.big_endian_decode(s.read(size))
|
61
|
+
end
|
62
|
+
elsif type == Bool
|
63
|
+
item = s.read(1)
|
64
|
+
if item == Bool::ENCODED_TRUE
|
65
|
+
true
|
66
|
+
elsif item == Bool::ENCODED_FALSE
|
67
|
+
false
|
68
|
+
else
|
69
|
+
raise InvalidValueError.new "invalid bool value #{item}"
|
70
|
+
end
|
71
|
+
elsif type.is_a?(Class) && type.respond_to?(:rlp_decode)
|
72
|
+
type.rlp_decode(s)
|
73
|
+
elsif type.is_a?(Array)
|
74
|
+
decode_list(s) do |list, s2|
|
75
|
+
i = 0
|
76
|
+
until s2.eof?
|
77
|
+
t = type.size > i ? type[i] : type[-1]
|
78
|
+
list << decode_with_type(s2, t)
|
79
|
+
i += 1
|
80
|
+
end
|
81
|
+
end
|
82
|
+
elsif type == Raw
|
83
|
+
decode_stream(s)
|
84
|
+
else
|
85
|
+
raise RLP::InvalidValueError.new "unknown type #{type}"
|
86
|
+
end
|
87
|
+
rescue
|
88
|
+
STDERR.puts "when decoding #{s} into #{type}"
|
89
|
+
raise
|
90
|
+
end
|
91
|
+
|
92
|
+
protected
|
93
|
+
|
94
|
+
def decode_list(s, first_char = nil, &decoder)
|
95
|
+
s = StringIO.new(s) if s.is_a?(String)
|
96
|
+
c = first_char || s.read(1)
|
97
|
+
list = []
|
98
|
+
|
99
|
+
sub_s = case c.ord
|
100
|
+
when 0xc0..0xf7
|
101
|
+
length = c.ord - 0xc0
|
102
|
+
s.read(length)
|
103
|
+
when 0xf8..0xff
|
104
|
+
length_binary = s.read(c.ord - 0xf7)
|
105
|
+
length = int_from_binary(length_binary)
|
106
|
+
s.read(length)
|
107
|
+
else
|
108
|
+
raise InvalidValueError.new("invalid char #{c}")
|
109
|
+
end
|
110
|
+
|
111
|
+
decoder.call(list, StringIO.new(sub_s))
|
112
|
+
list
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def decode_stream(s)
|
118
|
+
c = s.read(1)
|
119
|
+
case c.ord
|
120
|
+
when 0x00..0x7f
|
121
|
+
c
|
122
|
+
when 0x80..0xb7
|
123
|
+
length = c.ord - 0x80
|
124
|
+
s.read(length)
|
125
|
+
when 0xb8..0xbf
|
126
|
+
length_binary = s.read(c.ord - 0xb7)
|
127
|
+
length = int_from_binary(length_binary)
|
128
|
+
s.read(length)
|
129
|
+
else
|
130
|
+
decode_list(s, c) do |list, s2|
|
131
|
+
until s2.eof?
|
132
|
+
list << decode_stream(s2)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def int_from_binary(input)
|
139
|
+
Ciri::Utils.big_endian_decode(input)
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,140 @@
|
|
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
|
+
module Ciri
|
25
|
+
module RLP
|
26
|
+
module Encode
|
27
|
+
|
28
|
+
class InputOverflow < StandardError
|
29
|
+
end
|
30
|
+
|
31
|
+
# Encode input to rlp encoding
|
32
|
+
#
|
33
|
+
# Examples:
|
34
|
+
#
|
35
|
+
# Ciri::RLP.encode("hello world")
|
36
|
+
#
|
37
|
+
def encode(input, type = nil)
|
38
|
+
type ? encode_with_type(input, type) : encode_simple(input)
|
39
|
+
end
|
40
|
+
|
41
|
+
def encode_simple(input)
|
42
|
+
if input.is_a?(Array)
|
43
|
+
encode_list(input) {|i| encode_simple(i)}
|
44
|
+
elsif input.is_a?(Integer)
|
45
|
+
encode_with_type(input, Integer)
|
46
|
+
elsif input.class.respond_to?(:rlp_encode)
|
47
|
+
input.class.rlp_encode(input)
|
48
|
+
else
|
49
|
+
encode_with_type(input, Raw)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Use this method before RLP.encode, this method encode ruby objects to rlp friendly format, string or array.
|
54
|
+
# see Ciri::RLP::Serializable::TYPES for supported types
|
55
|
+
#
|
56
|
+
# Examples:
|
57
|
+
#
|
58
|
+
# item = Ciri::RLP.encode_with_type(number, :int, zero: "\x00".b)
|
59
|
+
# encoded_text = Ciri::RLP.encode(item)
|
60
|
+
#
|
61
|
+
def encode_with_type(item, type, zero: '')
|
62
|
+
if type == Integer
|
63
|
+
if item == 0
|
64
|
+
"\x80".b
|
65
|
+
elsif item < 0x80
|
66
|
+
Ciri::Utils.big_endian_encode(item, zero)
|
67
|
+
else
|
68
|
+
buf = Ciri::Utils.big_endian_encode(item, zero)
|
69
|
+
[0x80 + buf.size].pack("c*") + buf
|
70
|
+
end
|
71
|
+
elsif type == Bool
|
72
|
+
item ? Bool::ENCODED_TRUE : Bool::ENCODED_FALSE
|
73
|
+
elsif type.is_a?(Class) && type.respond_to?(:rlp_encode)
|
74
|
+
type.rlp_encode(item)
|
75
|
+
elsif type.is_a?(Array)
|
76
|
+
if type.size == 1 # array type
|
77
|
+
encode_list(item) {|i| encode_with_type(i, type[0])}
|
78
|
+
else # unknown
|
79
|
+
raise RLP::InvalidValueError.new "type size should be 1, got #{type}"
|
80
|
+
end
|
81
|
+
elsif type == Raw
|
82
|
+
encode_raw(item)
|
83
|
+
else
|
84
|
+
raise RLP::InvalidValueError.new "unknown type #{type}"
|
85
|
+
end
|
86
|
+
rescue
|
87
|
+
STDERR.puts "when encoding #{Utils.to_hex item.to_s} into #{type}"
|
88
|
+
raise
|
89
|
+
end
|
90
|
+
|
91
|
+
protected
|
92
|
+
|
93
|
+
def encode_raw(input)
|
94
|
+
result = if input.is_a?(String)
|
95
|
+
encode_string(input)
|
96
|
+
elsif input.is_a?(Array)
|
97
|
+
encode_list(input) {|item| encode(item)}
|
98
|
+
else
|
99
|
+
raise ArgumentError.new("input must be a String or Array, #{input.inspect}")
|
100
|
+
end
|
101
|
+
result.b
|
102
|
+
end
|
103
|
+
|
104
|
+
def encode_string(input)
|
105
|
+
length = input.length
|
106
|
+
if length == 1 && input.ord < 0x80
|
107
|
+
input
|
108
|
+
elsif length < 56
|
109
|
+
to_binary(0x80 + length) + input
|
110
|
+
elsif length < 256 ** 8
|
111
|
+
binary_length = to_binary(length)
|
112
|
+
to_binary(0xb7 + binary_length.size) + binary_length + input
|
113
|
+
else
|
114
|
+
raise InputOverflow.new("input length #{input.size} is too long")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def encode_list(input, &encoder)
|
119
|
+
input ||= [] # allow nil list
|
120
|
+
output = encoder ? input.map {|item| encoder.call(item)}.join : input.join
|
121
|
+
length = output.length
|
122
|
+
if length < 56
|
123
|
+
to_binary(0xc0 + length) + output
|
124
|
+
elsif length < 256 ** 8
|
125
|
+
binary_length = to_binary(length)
|
126
|
+
to_binary(0xf7 + binary_length.size) + binary_length + output
|
127
|
+
else
|
128
|
+
raise InputOverflow.new("input length #{input.size} is too long")
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
|
+
def to_binary(n)
|
135
|
+
Ciri::Utils.big_endian_encode(n)
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,241 @@
|
|
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/utils'
|
25
|
+
|
26
|
+
module Ciri
|
27
|
+
module RLP
|
28
|
+
|
29
|
+
# represent bool types: true | false
|
30
|
+
class Bool
|
31
|
+
ENCODED_TRUE = Ciri::Utils.big_endian_encode(0x01)
|
32
|
+
ENCODED_FALSE = Ciri::Utils.big_endian_encode(0x80)
|
33
|
+
end
|
34
|
+
|
35
|
+
# represent RLP raw types, binary or array
|
36
|
+
class Raw
|
37
|
+
end
|
38
|
+
|
39
|
+
# Serializable module allow ruby objects serialize/deserialize to or from RLP encoding.
|
40
|
+
# See Ciri::RLP::Serializable::TYPES for supported type.
|
41
|
+
#
|
42
|
+
# schema method define ordered data structure for class, and determine how to encoding objects.
|
43
|
+
#
|
44
|
+
# schema follow `{attr_name: type}` format,
|
45
|
+
# if attr is raw type(string or array of string), you can just use `:attr_name` to define it
|
46
|
+
# schema simple types include Integer, Bool, String, Array...
|
47
|
+
#
|
48
|
+
# schema also support complex types: array and serializable.
|
49
|
+
#
|
50
|
+
# array types represented as `{attr_name: [type]}`, for example: `{bills: [Integer]}` means value of bill attr is an array of integer
|
51
|
+
# serializable type represent value of attr is a RLP serializable object
|
52
|
+
#
|
53
|
+
#
|
54
|
+
# Examples:
|
55
|
+
#
|
56
|
+
# class AuthMsgV4
|
57
|
+
# include Ciri::RLP::Serializable
|
58
|
+
#
|
59
|
+
# # define schema
|
60
|
+
# schema [
|
61
|
+
# :signature, # raw type: string
|
62
|
+
# {initiator_pubkey: MySerializableKey}, # this attr is a RLP serializable object
|
63
|
+
# {nonce: [Integer]},
|
64
|
+
# {version: Integer}
|
65
|
+
# ]
|
66
|
+
#
|
67
|
+
# # default values
|
68
|
+
# default_data(got_plain: false)
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# msg = AuthMsgV4.new(signature: "\x00", initiator_pubkey: my_pubkey, nonce: [1, 2, 3], version: 4)
|
72
|
+
# encoded = msg.rlp_encode
|
73
|
+
# msg2 = AuthMsgV4.rlp_decode(encoded)
|
74
|
+
# msg == msg2 # true
|
75
|
+
#
|
76
|
+
module Serializable
|
77
|
+
# nil represent RLP raw value(string or array of string)
|
78
|
+
TYPES = [Raw, Integer, Bool].map {|key| [key, true]}.to_h.freeze
|
79
|
+
|
80
|
+
# Schema specific columns types of classes, normally you should not use Serializable::Schema directly
|
81
|
+
#
|
82
|
+
class Schema
|
83
|
+
include Encode
|
84
|
+
include Decode
|
85
|
+
|
86
|
+
class InvalidSchemaError < StandardError
|
87
|
+
end
|
88
|
+
|
89
|
+
# keys return data columns array
|
90
|
+
attr_reader :keys
|
91
|
+
|
92
|
+
KeySchema = Struct.new(:type, :options, keyword_init: true)
|
93
|
+
|
94
|
+
def initialize(schema)
|
95
|
+
keys = []
|
96
|
+
@_schema = {}
|
97
|
+
|
98
|
+
schema.each do |key|
|
99
|
+
if key.is_a?(Hash)
|
100
|
+
options = [:optional].map {|o| [o, key.delete(o)]}.to_h
|
101
|
+
raise InvalidSchemaError.new("include unknown options #{key}") unless key.size == 1
|
102
|
+
key, type = key.to_a[0]
|
103
|
+
else
|
104
|
+
options = {}
|
105
|
+
type = Raw
|
106
|
+
end
|
107
|
+
raise InvalidSchemaError.new("missing type #{type} for key #{key}") unless check_key_type(type)
|
108
|
+
keys << key
|
109
|
+
@_schema[key] = KeySchema.new(type: type, options: options)
|
110
|
+
end
|
111
|
+
|
112
|
+
@_schema.freeze
|
113
|
+
@keys = keys.freeze
|
114
|
+
end
|
115
|
+
|
116
|
+
# Get column type, see Serializable::TYPES for supported type
|
117
|
+
def [](key)
|
118
|
+
@_schema[key]
|
119
|
+
end
|
120
|
+
|
121
|
+
# Validate data, data is a Hash
|
122
|
+
def validate!(data)
|
123
|
+
keys.each do |key|
|
124
|
+
raise InvalidSchemaError.new("missing key #{key}") unless data.key?(key)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def rlp_encode(data, skip_keys: nil, white_list_keys: nil)
|
129
|
+
# pre-encode, encode data to rlp compatible format(only string or array)
|
130
|
+
used_keys = if white_list_keys
|
131
|
+
white_list_keys
|
132
|
+
elsif skip_keys
|
133
|
+
keys - skip_keys
|
134
|
+
else
|
135
|
+
keys
|
136
|
+
end
|
137
|
+
data_list = []
|
138
|
+
used_keys.each do |key|
|
139
|
+
value = data[key]
|
140
|
+
next if value.nil? && self[key].options[:optional]
|
141
|
+
data_list << encode_with_type(value, self[key].type)
|
142
|
+
end
|
143
|
+
encode_list(data_list)
|
144
|
+
end
|
145
|
+
|
146
|
+
def rlp_decode(input)
|
147
|
+
values = decode_list(input) do |list, stream|
|
148
|
+
keys.each do |key|
|
149
|
+
# decode data by type
|
150
|
+
next if stream.eof? && self[key].options[:optional]
|
151
|
+
list << decode_with_type(stream, self[key].type)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
# convert to key value hash
|
155
|
+
keys.zip(values).to_h
|
156
|
+
end
|
157
|
+
|
158
|
+
private
|
159
|
+
|
160
|
+
def check_key_type(type)
|
161
|
+
return true if TYPES.key?(type)
|
162
|
+
return true if type.is_a?(Class) && type.respond_to?(:rlp_decode) && type.respond_to?(:rlp_encode)
|
163
|
+
|
164
|
+
if type.is_a?(Array) && type.size == 1
|
165
|
+
check_key_type(type[0])
|
166
|
+
else
|
167
|
+
false
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
module ClassMethods
|
173
|
+
# Decode object from input
|
174
|
+
def rlp_decode(input)
|
175
|
+
data = schema.rlp_decode(input)
|
176
|
+
self.new(data)
|
177
|
+
end
|
178
|
+
|
179
|
+
# Encode object to rlp encoding string
|
180
|
+
def rlp_encode(item, skip_keys: nil, white_list_keys: nil)
|
181
|
+
schema.rlp_encode(item.serializable_attributes, skip_keys: skip_keys, white_list_keys: white_list_keys)
|
182
|
+
end
|
183
|
+
|
184
|
+
def schema(data_schema = nil)
|
185
|
+
@data_schema ||= Schema.new(data_schema).tap do |schema|
|
186
|
+
# define attributes methods
|
187
|
+
define_attributes(schema)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def default_data(data = nil)
|
192
|
+
@default_data ||= data
|
193
|
+
end
|
194
|
+
|
195
|
+
private
|
196
|
+
|
197
|
+
def define_attributes(schema)
|
198
|
+
schema.keys.each do |attribute|
|
199
|
+
module_eval <<-ATTR_METHODS
|
200
|
+
def #{attribute}
|
201
|
+
serializable_attributes[:"#{attribute}"]
|
202
|
+
end
|
203
|
+
|
204
|
+
def #{attribute}=(value)
|
205
|
+
serializable_attributes[:"#{attribute}"] = value
|
206
|
+
end
|
207
|
+
ATTR_METHODS
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
class << self
|
213
|
+
def included(base)
|
214
|
+
base.send :extend, ClassMethods
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
attr_reader :serializable_attributes
|
219
|
+
|
220
|
+
def initialize(**data)
|
221
|
+
@serializable_attributes = (self.class.default_data || {}).merge(data)
|
222
|
+
self.class.schema.validate!(@serializable_attributes)
|
223
|
+
end
|
224
|
+
|
225
|
+
def initialize_copy(orig)
|
226
|
+
super
|
227
|
+
@serializable_attributes = orig.serializable_attributes.dup
|
228
|
+
end
|
229
|
+
|
230
|
+
# Encode object to rlp encoding string
|
231
|
+
def rlp_encode(skip_keys: nil, white_list_keys: nil)
|
232
|
+
self.class.rlp_encode(self, skip_keys: skip_keys, white_list_keys: white_list_keys)
|
233
|
+
end
|
234
|
+
|
235
|
+
def ==(other)
|
236
|
+
self.class == other.class && serializable_attributes == other.serializable_attributes
|
237
|
+
end
|
238
|
+
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|