Module: Ciri::RLP::Serializable
- Included in:
- Chain::Block, Chain::Header, Chain::Transaction, DevP2P::RLPX::AuthMsgV4, DevP2P::RLPX::AuthRespV4, DevP2P::RLPX::Cap, DevP2P::RLPX::Message, DevP2P::RLPX::ProtocolHandshake, EVM::LogEntry, Eth::BlockBodies::Bodies, Eth::GetBlockHeaders, Eth::Status, Types::Account, Types::Receipt
- Defined in:
- lib/ciri/rlp/serializable.rb
Overview
Serializable module allow ruby objects serialize/deserialize to or from RLP encoding. See Ciri::RLP::Serializable::TYPES for supported type.
schema method define ordered data structure for class, and determine how to encoding objects.
schema follow ‘type` format, if attr is raw type(string or array of string), you can just use :attr_name to define it schema simple types include Integer, Bool, String, Array…
schema also support complex types: array and serializable.
array types represented as ‘[type]`, for example: `[Integer]` means value of bill attr is an array of integer serializable type represent value of attr is a RLP serializable object
Examples:
class AuthMsgV4
include Ciri::RLP::Serializable
# define schema
schema [
:signature, # raw type: string
{initiator_pubkey: MySerializableKey}, # this attr is a RLP serializable object
{nonce: [Integer]},
{version: Integer}
]
# default values
default_data(got_plain: false)
end
msg = AuthMsgV4.new(signature: "\x00", initiator_pubkey: my_pubkey, nonce: [1, 2, 3], version: 4)
encoded = msg.rlp_encode
msg2 = AuthMsgV4.rlp_decode(encoded)
msg == msg2 # true
Defined Under Namespace
Modules: ClassMethods Classes: Schema
Constant Summary collapse
- TYPES =
nil represent RLP raw value(string or array of string)
[Raw, Integer, Bool].map {|key| [key, true]}.to_h.freeze
Instance Attribute Summary collapse
-
#serializable_attributes ⇒ Object
readonly
Returns the value of attribute serializable_attributes.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #initialize(**data) ⇒ Object
- #initialize_copy(orig) ⇒ Object
-
#rlp_encode(skip_keys: nil, white_list_keys: nil) ⇒ Object
Encode object to rlp encoding string.
Instance Attribute Details
#serializable_attributes ⇒ Object (readonly)
Returns the value of attribute serializable_attributes.
212 213 214 |
# File 'lib/ciri/rlp/serializable.rb', line 212 def serializable_attributes @serializable_attributes end |
Class Method Details
.included(base) ⇒ Object
207 208 209 |
# File 'lib/ciri/rlp/serializable.rb', line 207 def included(base) base.send :extend, ClassMethods end |
Instance Method Details
#==(other) ⇒ Object
229 230 231 |
# File 'lib/ciri/rlp/serializable.rb', line 229 def ==(other) self.class == other.class && serializable_attributes == other.serializable_attributes end |
#initialize(**data) ⇒ Object
214 215 216 217 |
# File 'lib/ciri/rlp/serializable.rb', line 214 def initialize(**data) @serializable_attributes = (self.class.default_data || {}).merge(data) self.class.schema.validate!(@serializable_attributes) end |
#initialize_copy(orig) ⇒ Object
219 220 221 222 |
# File 'lib/ciri/rlp/serializable.rb', line 219 def initialize_copy(orig) super @serializable_attributes = orig.serializable_attributes.dup end |
#rlp_encode(skip_keys: nil, white_list_keys: nil) ⇒ Object
Encode object to rlp encoding string
225 226 227 |
# File 'lib/ciri/rlp/serializable.rb', line 225 def rlp_encode(skip_keys: nil, white_list_keys: nil) self.class.rlp_encode(self, skip_keys: skip_keys, white_list_keys: white_list_keys) end |