eth-native 0.1.0
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 +7 -0
- data/LICENSE +202 -0
- data/README.md +84 -0
- data/docs/ARCHITECTURE.md +162 -0
- data/docs/PLAN.md +90 -0
- data/docs/RELEASE.md +46 -0
- data/docs/benchmarks/abi.md +41 -0
- data/docs/benchmarks/e2e.md +46 -0
- data/docs/benchmarks/rlp.md +44 -0
- data/ext/eth_native/Cargo.lock +281 -0
- data/ext/eth_native/Cargo.toml +22 -0
- data/ext/eth_native/README.md +15 -0
- data/ext/eth_native/extconf.rb +6 -0
- data/ext/eth_native/src/abi.rs +2169 -0
- data/ext/eth_native/src/lib.rs +35 -0
- data/ext/eth_native/src/rlp.rs +694 -0
- data/lib/eth/native/abi.rb +267 -0
- data/lib/eth/native/rlp.rb +147 -0
- data/lib/eth/native/version.rb +7 -0
- data/lib/eth/native.rb +82 -0
- metadata +123 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Eth
|
|
4
|
+
module Native
|
|
5
|
+
# Compiled-schema ABI encode/decode backed by the Rust extension.
|
|
6
|
+
#
|
|
7
|
+
# Ruby owns the authoritative `Eth::Abi::Type` parsing; {compile} turns
|
|
8
|
+
# parsed types into an immutable native schema that encodes and decodes
|
|
9
|
+
# whole payloads with a single boundary crossing. Byte-for-byte and
|
|
10
|
+
# error parity with eth.rb 0.5.18 is established by differential spec.
|
|
11
|
+
#
|
|
12
|
+
# Unsupported base types (real/fixed family for now) compile to a
|
|
13
|
+
# {RubySchema} that explicitly delegates to the reference; no silent
|
|
14
|
+
# approximation happens anywhere.
|
|
15
|
+
module Abi
|
|
16
|
+
# Base class for all eth-native ABI errors.
|
|
17
|
+
class Error < ::StandardError; end
|
|
18
|
+
|
|
19
|
+
# Raised at compile time for base types the native encoder does not
|
|
20
|
+
# support yet; the schema then delegates to the pure-Ruby reference.
|
|
21
|
+
class UnsupportedType < Error; end
|
|
22
|
+
|
|
23
|
+
# Raised for value shapes reserved for the pure-Ruby implementation
|
|
24
|
+
# (BigDecimal coercions); the whole call falls back explicitly.
|
|
25
|
+
class UnsupportedInput < Error; end
|
|
26
|
+
|
|
27
|
+
# Raised when encoding fails. Mirrors `Eth::Abi::EncodingError` with
|
|
28
|
+
# an identical message; remapped when eth.rb is loaded.
|
|
29
|
+
class EncodingError < Error; end
|
|
30
|
+
|
|
31
|
+
# Raised when decoding fails. Mirrors `Eth::Abi::DecodingError` with
|
|
32
|
+
# an identical message; remapped when eth.rb is loaded.
|
|
33
|
+
class DecodingError < Error; end
|
|
34
|
+
|
|
35
|
+
# Raised when a value is out of bounds for its type. Mirrors
|
|
36
|
+
# `Eth::Abi::ValueOutOfBounds`; remapped when eth.rb is loaded.
|
|
37
|
+
class ValueOutOfBounds < Error; end
|
|
38
|
+
|
|
39
|
+
# Raised when eth-native is disabled via ETH_NATIVE=0.
|
|
40
|
+
class DisabledError < Error; end
|
|
41
|
+
|
|
42
|
+
module_function
|
|
43
|
+
|
|
44
|
+
# Compiles parsed `Eth::Abi::Type` objects (or Solidity type strings)
|
|
45
|
+
# into a reusable schema. Compile once — e.g. at contract function
|
|
46
|
+
# initialization — and reuse the handle for every call.
|
|
47
|
+
#
|
|
48
|
+
# @param parsed_types [Array<Eth::Abi::Type, String>] the ABI types.
|
|
49
|
+
# @return [NativeSchema, RubySchema] a schema with `encode`, `decode`,
|
|
50
|
+
# `decode_many` and `native?`.
|
|
51
|
+
def compile(parsed_types)
|
|
52
|
+
ensure_usable!
|
|
53
|
+
types = parsed_types.map do |t|
|
|
54
|
+
if defined?(::Eth::Abi::Type) && t.is_a?(::Eth::Abi::Type)
|
|
55
|
+
t
|
|
56
|
+
elsif defined?(::Eth::Abi)
|
|
57
|
+
::Eth::Abi::Type.parse(t)
|
|
58
|
+
else
|
|
59
|
+
raise UnsupportedType, "compiling type strings requires gem 'eth'"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
descriptors = types.map { |t| type_descriptor(t) }
|
|
63
|
+
native = ::EthNativeExt.__abi_compile__(descriptors)
|
|
64
|
+
NativeSchema.new(native, types)
|
|
65
|
+
rescue UnsupportedType
|
|
66
|
+
RubySchema.new(types)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Builds the plain-hash descriptor tree the extension expects.
|
|
70
|
+
def type_descriptor(type)
|
|
71
|
+
descriptor = { "base_type" => type.base_type, "dims" => type.dimensions }
|
|
72
|
+
case type.base_type
|
|
73
|
+
when "uint", "int", "hash"
|
|
74
|
+
descriptor["bits"] = type.sub_type.to_i
|
|
75
|
+
when "bytes"
|
|
76
|
+
descriptor["bits"] = type.sub_type.empty? ? 0 : type.sub_type.to_i
|
|
77
|
+
when "ufixed", "ureal", "fixed", "real"
|
|
78
|
+
# Packed::Encoder maps byte size from the high part and scaling
|
|
79
|
+
# from the low part.
|
|
80
|
+
high, low = type.sub_type.split("x").map(&:to_i)
|
|
81
|
+
descriptor["bits"] = high
|
|
82
|
+
descriptor["decimals"] = low
|
|
83
|
+
when "tuple"
|
|
84
|
+
descriptor["comps"] = type.components.map { |c| type_descriptor(c) }
|
|
85
|
+
descriptor["names"] = type.components.map(&:name)
|
|
86
|
+
end
|
|
87
|
+
descriptor
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def ensure_usable!
|
|
91
|
+
raise DisabledError, "eth-native is disabled (ETH_NATIVE=0)" if ::Eth::Native.disabled?
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Encodes values in non-standard packed mode (`Eth::Abi.solidity_packed`
|
|
95
|
+
# parity). Type strings are parsed once per call like the reference;
|
|
96
|
+
# shapes the reference handles through its own string matching (inline
|
|
97
|
+
# tuples, Type objects, Float/BigDecimal values) fall back explicitly.
|
|
98
|
+
#
|
|
99
|
+
# @param type_strings [Array<String>] Solidity type strings.
|
|
100
|
+
# @param args [Array] the values, one per type.
|
|
101
|
+
# @return [String] the packed encoding in ASCII-8BIT.
|
|
102
|
+
def packed_encode(type_strings, args)
|
|
103
|
+
ensure_usable!
|
|
104
|
+
unless defined?(::Eth::Abi)
|
|
105
|
+
raise UnsupportedType, "packed encoding requires gem 'eth' for Type.parse"
|
|
106
|
+
end
|
|
107
|
+
unless type_strings.is_a?(Array) && type_strings.all? { |t| t.is_a?(String) } &&
|
|
108
|
+
type_strings.all? { |t| !t.include?("(") || t.start_with?("tuple(") }
|
|
109
|
+
return ::Eth::Abi.solidity_packed(type_strings, args)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
descriptors = type_strings.map { |t| type_descriptor(::Eth::Abi::Type.parse(t)) }
|
|
113
|
+
::EthNativeExt.__abi_packed_encode__(
|
|
114
|
+
::EthNativeExt.__abi_packed_compile__(descriptors), args
|
|
115
|
+
)
|
|
116
|
+
rescue Abi::UnsupportedInput
|
|
117
|
+
if defined?(::Eth::Abi)
|
|
118
|
+
::Eth::Abi.solidity_packed(type_strings, args)
|
|
119
|
+
else
|
|
120
|
+
raise
|
|
121
|
+
end
|
|
122
|
+
rescue Abi::EncodingError => e
|
|
123
|
+
Abi.remap(e, defined?(::Eth::Abi) ? ::Eth::Abi::EncodingError : nil)
|
|
124
|
+
rescue Abi::ValueOutOfBounds => e
|
|
125
|
+
Abi.remap(e, defined?(::Eth::Abi) ? ::Eth::Abi::ValueOutOfBounds : nil)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Re-raises native errors as the eth.rb reference classes when eth.rb
|
|
129
|
+
# is loaded, preserving the stable message.
|
|
130
|
+
def remap(error, target)
|
|
131
|
+
target ? raise(target, error.message) : raise(error)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Replicates `Abi.decode`'s input handling: `Util#hex?`-shaped input is
|
|
135
|
+
# unpacked (with encoding validation like the RLP path); non-String
|
|
136
|
+
# data raises the raw NoMethodError the reference produces when it
|
|
137
|
+
# dereferences `data[pos, 32]`.
|
|
138
|
+
def normalize_decode_data!(data)
|
|
139
|
+
unless data.is_a?(String)
|
|
140
|
+
receiver = data.nil? || data.equal?(true) || data.equal?(false) ? data.inspect : "an instance of #{data.class}"
|
|
141
|
+
raise NoMethodError, "undefined method '[]' for #{receiver}"
|
|
142
|
+
end
|
|
143
|
+
if data.encoding != Encoding::BINARY && !data.valid_encoding?
|
|
144
|
+
raise ArgumentError, "invalid byte sequence in #{data.encoding.name}"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
hex = data.start_with?("0x", "0X") ? data.byteslice(2..) : data
|
|
148
|
+
if hex.match?(/\A[0-9a-fA-F]*\z/)
|
|
149
|
+
raise TypeError, "Value must be an instance of String" unless data.instance_of?(String)
|
|
150
|
+
|
|
151
|
+
hex = "0#{hex}" if hex.size.odd?
|
|
152
|
+
[hex].pack("H*")
|
|
153
|
+
else
|
|
154
|
+
data
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Schema handle whose encode/decode cross the Rust boundary.
|
|
159
|
+
class NativeSchema
|
|
160
|
+
def initialize(native, types)
|
|
161
|
+
@native = native
|
|
162
|
+
@types = types
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# @return [Boolean] true; the schema runs natively.
|
|
166
|
+
def native?
|
|
167
|
+
true
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Encodes values (wrapped like `Eth::Abi.encode` wraps its args).
|
|
171
|
+
#
|
|
172
|
+
# @param args [Array] the values, one per schema type.
|
|
173
|
+
# @return [String] the ABI-encoded payload in ASCII-8BIT.
|
|
174
|
+
def encode(args)
|
|
175
|
+
args = [args] unless args.instance_of?(Array)
|
|
176
|
+
::EthNativeExt.__abi_encode__(@native, args)
|
|
177
|
+
rescue Abi::UnsupportedInput => e
|
|
178
|
+
fallback(:encode, args, e)
|
|
179
|
+
rescue Abi::EncodingError => e
|
|
180
|
+
Abi.remap(e, defined?(::Eth::Abi) ? ::Eth::Abi::EncodingError : nil)
|
|
181
|
+
rescue Abi::ValueOutOfBounds => e
|
|
182
|
+
Abi.remap(e, defined?(::Eth::Abi) ? ::Eth::Abi::ValueOutOfBounds : nil)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Decodes a payload (hex or binary, per the reference heuristic).
|
|
186
|
+
#
|
|
187
|
+
# @param data [String] the ABI-encoded payload.
|
|
188
|
+
# @return [Array] the decoded values.
|
|
189
|
+
def decode(data)
|
|
190
|
+
data = Abi.normalize_decode_data!(data)
|
|
191
|
+
::EthNativeExt.__abi_decode__(@native, data)
|
|
192
|
+
rescue Abi::UnsupportedInput => e
|
|
193
|
+
fallback(:decode, data, e)
|
|
194
|
+
rescue Abi::DecodingError => e
|
|
195
|
+
Abi.remap(e, defined?(::Eth::Abi) ? ::Eth::Abi::DecodingError : nil)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Decodes many payloads with the same compiled schema.
|
|
199
|
+
#
|
|
200
|
+
# @param payloads [Array<String>] ABI-encoded payloads.
|
|
201
|
+
# @return [Array<Array>] one result array per payload.
|
|
202
|
+
def decode_many(payloads)
|
|
203
|
+
normalized = payloads.map { |p| Abi.normalize_decode_data!(p) }
|
|
204
|
+
::EthNativeExt.__abi_decode_many__(@native, normalized)
|
|
205
|
+
rescue Abi::UnsupportedInput => e
|
|
206
|
+
fallback(:decode_many, payloads, e)
|
|
207
|
+
rescue Abi::DecodingError => e
|
|
208
|
+
Abi.remap(e, defined?(::Eth::Abi) ? ::Eth::Abi::DecodingError : nil)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
private
|
|
212
|
+
|
|
213
|
+
# Explicit fallback for unsupported value shapes; native-internal
|
|
214
|
+
# failures never reach this path. Calls the reference directly so a
|
|
215
|
+
# routing adapter in eth.rb cannot re-enter this module.
|
|
216
|
+
def fallback(method, arg, unsupported)
|
|
217
|
+
if defined?(::Eth::Abi)
|
|
218
|
+
case method
|
|
219
|
+
when :encode then ::Eth::Abi.encode(@types, arg)
|
|
220
|
+
when :decode then ::Eth::Abi.decode(@types, arg)
|
|
221
|
+
else
|
|
222
|
+
payloads = arg.map { |p| Abi.normalize_decode_data!(p) }
|
|
223
|
+
payloads.map { |p| ::Eth::Abi.decode(@types, p) }
|
|
224
|
+
end
|
|
225
|
+
else
|
|
226
|
+
raise Abi::UnsupportedInput,
|
|
227
|
+
"#{unsupported.message}; the pure-Ruby fallback requires gem 'eth'"
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# Schema for base types the native encoder does not support yet.
|
|
233
|
+
# Everything is delegated to the pure-Ruby reference.
|
|
234
|
+
class RubySchema
|
|
235
|
+
def initialize(types)
|
|
236
|
+
@types = types
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# @return [Boolean] false; the schema runs in Ruby.
|
|
240
|
+
def native?
|
|
241
|
+
false
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def encode(args)
|
|
245
|
+
require_reference!
|
|
246
|
+
::Eth::Abi.encode(@types, args)
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def decode(data)
|
|
250
|
+
require_reference!
|
|
251
|
+
::Eth::Abi.decode(@types, data)
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def decode_many(payloads)
|
|
255
|
+
payloads.map { |p| decode(p) }
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
private
|
|
259
|
+
|
|
260
|
+
def require_reference!
|
|
261
|
+
raise UnsupportedType,
|
|
262
|
+
"unsupported ABI types require gem 'eth' for the pure-Ruby path" unless defined?(::Eth::Abi)
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
end
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Eth
|
|
4
|
+
module Native
|
|
5
|
+
# Whole-payload RLP encode/decode backed by the Rust extension.
|
|
6
|
+
#
|
|
7
|
+
# Byte-for-byte parity with the eth.rb 0.5.18 reference implementation
|
|
8
|
+
# is established by spec against the official ethereum/tests fixtures
|
|
9
|
+
# and differential testing. Inputs the reference implementation routes
|
|
10
|
+
# through custom sedes, non-Array enumerables or `Rlp::Data` are rejected
|
|
11
|
+
# by the native encoder with {UnsupportedInput} and delegated to the
|
|
12
|
+
# pure-Ruby reference; no other failure falls back.
|
|
13
|
+
module Rlp
|
|
14
|
+
# Maximum tree depth handled natively; deeper trees fall back to Ruby.
|
|
15
|
+
MAX_NATIVE_DEPTH = 256
|
|
16
|
+
|
|
17
|
+
# Base class for all eth-native RLP errors.
|
|
18
|
+
class Error < ::StandardError; end
|
|
19
|
+
|
|
20
|
+
# Raised for inputs reserved for the pure-Ruby implementation.
|
|
21
|
+
class UnsupportedInput < Error; end
|
|
22
|
+
|
|
23
|
+
# Raised when encoding fails. Mirrors `Eth::Rlp::EncodingError` with
|
|
24
|
+
# an identical message; remapped when eth.rb is loaded.
|
|
25
|
+
class EncodingError < Error; end
|
|
26
|
+
|
|
27
|
+
# Raised when decoding fails. Mirrors `Eth::Rlp::DecodingError` with
|
|
28
|
+
# an identical message; remapped when eth.rb is loaded.
|
|
29
|
+
class DecodingError < Error; end
|
|
30
|
+
|
|
31
|
+
# Raised when eth-native is disabled via ETH_NATIVE=0.
|
|
32
|
+
class DisabledError < Error; end
|
|
33
|
+
|
|
34
|
+
module_function
|
|
35
|
+
|
|
36
|
+
# Encodes a Ruby object tree of Integer, exact String and exact Array
|
|
37
|
+
# nodes in RLP format with a single boundary crossing.
|
|
38
|
+
#
|
|
39
|
+
# @param obj [Object] a tree of Integer, String and Array nodes.
|
|
40
|
+
# @return [String] the RLP-encoded item in ASCII-8BIT.
|
|
41
|
+
# @raise [DisabledError] when eth-native is disabled via ETH_NATIVE=0.
|
|
42
|
+
# @raise [Eth::Rlp::EncodingError, EncodingError] when encoding fails.
|
|
43
|
+
# @raise [TypeError] for inputs the reference implementation rejects,
|
|
44
|
+
# when the pure-Ruby fallback is loaded.
|
|
45
|
+
def encode(obj)
|
|
46
|
+
ensure_usable!
|
|
47
|
+
::EthNativeExt.__rlp_encode__(obj)
|
|
48
|
+
rescue UnsupportedInput => e
|
|
49
|
+
fallback(:encode, obj, e)
|
|
50
|
+
rescue EncodingError => e
|
|
51
|
+
remap(e, ::Eth::Rlp::EncodingError)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Decodes an RLP-encoded item, applying the reference string semantics
|
|
55
|
+
# before crossing the boundary: the hex-vs-binary heuristic, its
|
|
56
|
+
# encoding validation, and the exact-type checks of the reference
|
|
57
|
+
# `Util` helpers. Returns binary strings, like the reference decoder.
|
|
58
|
+
#
|
|
59
|
+
# @param data [String] an RLP-encoded item; hex strings are unpacked.
|
|
60
|
+
# @return [Object] the decoded String/Array tree in ASCII-8BIT.
|
|
61
|
+
# @raise [DisabledError] when eth-native is disabled via ETH_NATIVE=0.
|
|
62
|
+
# @raise [NoMethodError] for non-String (or String-subclass, non-hex)
|
|
63
|
+
# input, like `Util#str_to_bytes` calling `.b` in the reference.
|
|
64
|
+
# @raise [ArgumentError] for non-binary strings with invalid byte
|
|
65
|
+
# sequences, like the reference's `Util#prefixed?`/`hex?` regexes.
|
|
66
|
+
# @raise [TypeError] for hex strings that are not exact String
|
|
67
|
+
# instances, like the reference's `Util#hex_to_bin`.
|
|
68
|
+
# @raise [Eth::Rlp::DecodingError, DecodingError] when decoding fails.
|
|
69
|
+
def decode(data)
|
|
70
|
+
ensure_usable!
|
|
71
|
+
::EthNativeExt.__rlp_decode__(normalize_decode_input!(data))
|
|
72
|
+
rescue UnsupportedInput => e
|
|
73
|
+
fallback(:decode, data, e)
|
|
74
|
+
rescue DecodingError => e
|
|
75
|
+
remap(e, ::Eth::Rlp::DecodingError)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Delegates an input the native encoder marked as unsupported to the
|
|
79
|
+
# pure-Ruby reference implementation, so its exact semantics (custom
|
|
80
|
+
# sedes, other enumerables, `Rlp::Data`, negative integers, ...) are
|
|
81
|
+
# preserved. Native-internal failures never reach this path.
|
|
82
|
+
#
|
|
83
|
+
# Calls the reference encoder/decoder directly instead of
|
|
84
|
+
# `Eth::Rlp.encode/decode`: when eth.rb itself has opted in to the
|
|
85
|
+
# native backend, the routing methods would re-enter this module and
|
|
86
|
+
# recurse.
|
|
87
|
+
def fallback(method, arg, unsupported)
|
|
88
|
+
if defined?(::Eth::Rlp)
|
|
89
|
+
if method == :encode
|
|
90
|
+
::Eth::Rlp::Encoder.perform(arg)
|
|
91
|
+
else
|
|
92
|
+
::Eth::Rlp::Decoder.perform(arg)
|
|
93
|
+
end
|
|
94
|
+
else
|
|
95
|
+
raise UnsupportedInput,
|
|
96
|
+
"#{unsupported.message}; the pure-Ruby fallback requires gem 'eth'"
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Re-raises native errors as the eth.rb reference classes when eth.rb
|
|
101
|
+
# is loaded, preserving the stable message.
|
|
102
|
+
def remap(error, target)
|
|
103
|
+
target ? raise(target, error.message) : raise(error)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def ensure_usable!
|
|
107
|
+
raise DisabledError, "eth-native is disabled (ETH_NATIVE=0)" if ::Eth::Native.disabled?
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Replicates `Eth::Rlp::Decoder.perform`'s input handling:
|
|
111
|
+
#
|
|
112
|
+
# 1. Non-String input: `Util#str_to_bytes` calls `.b`, raising
|
|
113
|
+
# `NoMethodError` outside the decoder's rescue block. String
|
|
114
|
+
# subclasses have `.b`, so only non-Strings raise.
|
|
115
|
+
# 2. The `hex?` heuristic's regexes raise `ArgumentError` for invalid
|
|
116
|
+
# byte sequences in non-binary encodings.
|
|
117
|
+
# 3. Hex-shaped input goes through `Util#hex_to_bin`, which requires an
|
|
118
|
+
# exact `String` (`TypeError` for subclasses) and unpacks odd-length
|
|
119
|
+
# hex with a leading zero digit.
|
|
120
|
+
# 4. Non-hex input goes back through `Util#str_to_bytes`: `.b` keeps
|
|
121
|
+
# the bytes, so any String (exact or subclass) passes unchanged
|
|
122
|
+
# byte-wise.
|
|
123
|
+
#
|
|
124
|
+
# Keeping this in Ruby preserves string semantics exactly and avoids
|
|
125
|
+
# re-running the heuristic over already-converted binary bytes.
|
|
126
|
+
def normalize_decode_input!(data)
|
|
127
|
+
unless data.is_a?(String)
|
|
128
|
+
receiver = data.nil? || data.equal?(true) || data.equal?(false) ? data.inspect : "an instance of #{data.class}"
|
|
129
|
+
raise NoMethodError, "undefined method 'b' for #{receiver}"
|
|
130
|
+
end
|
|
131
|
+
if data.encoding != Encoding::BINARY && !data.valid_encoding?
|
|
132
|
+
raise ArgumentError, "invalid byte sequence in #{data.encoding.name}"
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
hex = data.start_with?("0x", "0X") ? data.byteslice(2..) : data
|
|
136
|
+
if hex.match?(/\A[0-9a-fA-F]*\z/)
|
|
137
|
+
raise TypeError, "Value must be an instance of String" unless data.instance_of?(String)
|
|
138
|
+
|
|
139
|
+
hex = "0#{hex}" if hex.size.odd?
|
|
140
|
+
[hex].pack("H*")
|
|
141
|
+
else
|
|
142
|
+
data
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
data/lib/eth/native.rb
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "native/version"
|
|
4
|
+
require_relative "native/rlp"
|
|
5
|
+
require_relative "native/abi"
|
|
6
|
+
|
|
7
|
+
module Eth
|
|
8
|
+
# Optional Rust native acceleration for eth.rb.
|
|
9
|
+
#
|
|
10
|
+
# Currently accelerates whole-payload RLP encode/decode; ABI acceleration
|
|
11
|
+
# is not implemented yet.
|
|
12
|
+
module Native
|
|
13
|
+
# Native ABI version implemented by the compiled extension.
|
|
14
|
+
NATIVE_ABI_VERSION = 1
|
|
15
|
+
|
|
16
|
+
# Minimum and maximum CRuby versions supported by this gem.
|
|
17
|
+
MINIMUM_RUBY_VERSION = "4.0.0"
|
|
18
|
+
MAXIMUM_RUBY_VERSION = "5.0.0"
|
|
19
|
+
|
|
20
|
+
# Raised when the compiled extension cannot be loaded or fails self-check.
|
|
21
|
+
class LoadError < StandardError; end
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
# Checks whether the current Ruby engine and version are supported.
|
|
25
|
+
#
|
|
26
|
+
# @return [Boolean] true when the runtime satisfies the gem requirements.
|
|
27
|
+
def ruby_version_supported?
|
|
28
|
+
return false unless RUBY_ENGINE == "ruby"
|
|
29
|
+
|
|
30
|
+
Gem::Requirement.new(">= #{MINIMUM_RUBY_VERSION}", "< #{MAXIMUM_RUBY_VERSION}").satisfied_by?(Gem::Version.new(RUBY_VERSION))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Reports whether the native extension is loaded in this process.
|
|
34
|
+
#
|
|
35
|
+
# @return [Boolean] true when the compiled extension passed self-check.
|
|
36
|
+
def available?
|
|
37
|
+
!!@available
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Reports whether native acceleration is disabled by ETH_NATIVE=0.
|
|
41
|
+
# ETH_NATIVE=1 only expresses intent and never bypasses self-checks.
|
|
42
|
+
#
|
|
43
|
+
# @return [Boolean] true when the adapter is explicitly disabled.
|
|
44
|
+
def disabled?
|
|
45
|
+
ENV["ETH_NATIVE"] == "0"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Returns the native ABI version reported by the compiled extension.
|
|
49
|
+
#
|
|
50
|
+
# @return [Integer] the native ABI version.
|
|
51
|
+
# @raise [LoadError] when the native extension is not loaded.
|
|
52
|
+
def native_abi_version
|
|
53
|
+
raise LoadError, "eth-native extension is not loaded" unless available?
|
|
54
|
+
|
|
55
|
+
@native_abi_version
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Loads and verifies the compiled extension exactly once.
|
|
59
|
+
#
|
|
60
|
+
# @return [Boolean] true when the extension passed self-check.
|
|
61
|
+
# @raise [LoadError] when the runtime or native ABI is unsupported.
|
|
62
|
+
def load_extension!
|
|
63
|
+
return @available if defined?(@available) && !@available.nil?
|
|
64
|
+
|
|
65
|
+
unless ruby_version_supported?
|
|
66
|
+
raise LoadError, "eth-native requires CRuby >= #{MINIMUM_RUBY_VERSION}, < #{MAXIMUM_RUBY_VERSION}; got #{RUBY_ENGINE} #{RUBY_VERSION}"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
require_relative "native/eth_native"
|
|
70
|
+
|
|
71
|
+
@native_abi_version = ::EthNativeExt.__native_abi_version__
|
|
72
|
+
unless @native_abi_version == NATIVE_ABI_VERSION
|
|
73
|
+
raise LoadError, "eth-native native ABI mismatch: expected #{NATIVE_ABI_VERSION}, got #{@native_abi_version}"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
@available = true
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
Eth::Native.load_extension!
|
metadata
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: eth-native
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- ShiningRay
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rb_sys
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.9.130
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 0.9.130
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rake
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '13.2'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '13.2'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rake-compiler
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '1.2'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '1.2'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: rspec
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '3.13'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '3.13'
|
|
68
|
+
description: Optional Rust native extension that accelerates whole-payload RLP and
|
|
69
|
+
ABI encoding and decoding for eth.rb on CRuby 4.0.
|
|
70
|
+
email:
|
|
71
|
+
- tsowly@hotmail.com
|
|
72
|
+
executables: []
|
|
73
|
+
extensions:
|
|
74
|
+
- ext/eth_native/extconf.rb
|
|
75
|
+
extra_rdoc_files: []
|
|
76
|
+
files:
|
|
77
|
+
- LICENSE
|
|
78
|
+
- README.md
|
|
79
|
+
- docs/ARCHITECTURE.md
|
|
80
|
+
- docs/PLAN.md
|
|
81
|
+
- docs/RELEASE.md
|
|
82
|
+
- docs/benchmarks/abi.md
|
|
83
|
+
- docs/benchmarks/e2e.md
|
|
84
|
+
- docs/benchmarks/rlp.md
|
|
85
|
+
- ext/eth_native/Cargo.lock
|
|
86
|
+
- ext/eth_native/Cargo.toml
|
|
87
|
+
- ext/eth_native/README.md
|
|
88
|
+
- ext/eth_native/extconf.rb
|
|
89
|
+
- ext/eth_native/src/abi.rs
|
|
90
|
+
- ext/eth_native/src/lib.rs
|
|
91
|
+
- ext/eth_native/src/rlp.rs
|
|
92
|
+
- lib/eth/native.rb
|
|
93
|
+
- lib/eth/native/abi.rb
|
|
94
|
+
- lib/eth/native/rlp.rb
|
|
95
|
+
- lib/eth/native/version.rb
|
|
96
|
+
homepage: https://github.com/ShiningRay/eth-native
|
|
97
|
+
licenses:
|
|
98
|
+
- Apache-2.0
|
|
99
|
+
metadata:
|
|
100
|
+
bug_tracker_uri: https://github.com/ShiningRay/eth-native/issues
|
|
101
|
+
documentation_uri: https://github.com/ShiningRay/eth-native/tree/main/docs
|
|
102
|
+
source_code_uri: https://github.com/ShiningRay/eth-native
|
|
103
|
+
rdoc_options: []
|
|
104
|
+
require_paths:
|
|
105
|
+
- lib
|
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '4.0'
|
|
111
|
+
- - "<"
|
|
112
|
+
- !ruby/object:Gem::Version
|
|
113
|
+
version: '5.0'
|
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
|
+
requirements:
|
|
116
|
+
- - ">="
|
|
117
|
+
- !ruby/object:Gem::Version
|
|
118
|
+
version: '0'
|
|
119
|
+
requirements: []
|
|
120
|
+
rubygems_version: 4.0.20
|
|
121
|
+
specification_version: 4
|
|
122
|
+
summary: Optional Rust native acceleration for eth.rb.
|
|
123
|
+
test_files: []
|