Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
rename Eth::DevP2P::Serializable to Eth::RLP::Serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed May 1, 2018
1 parent 8dab70e commit 55ea189
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 43 deletions.
4 changes: 2 additions & 2 deletions lib/ethruby/devp2p/rlpx/frame_io.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

require 'stringio'
require 'ethruby/devp2p/serializable'
require 'ethruby/rlp/serializable'

module Eth
module DevP2P
module RLPX

# RLPX message
class Message
include Eth::DevP2P::Serializable
include Eth::RLP::Serializable

schema [
{code: :int},
Expand Down
6 changes: 3 additions & 3 deletions lib/ethruby/devp2p/rlpx/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# RLPX
require 'ethruby/key'
require 'ethruby/devp2p/serializable'
require 'ethruby/rlp/serializable'

module Eth
module DevP2P
Expand All @@ -11,7 +11,7 @@ module RLPX
### messages

class AuthMsgV4
include Eth::DevP2P::Serializable
include Eth::RLP::Serializable

schema [
{got_plain: :bool},
Expand All @@ -33,7 +33,7 @@ def seal_eip8(remote_key)
end

class AuthRespV4
include Eth::DevP2P::Serializable
include Eth::RLP::Serializable

schema [
:random_pubkey,
Expand Down
63 changes: 33 additions & 30 deletions lib/ethruby/rlp.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'ethruby/rlp/decode'
require 'ethruby/rlp/encode'
require_relative 'rlp/decode'
require_relative 'rlp/encode'
require_relative 'rlp/serializable'

module Eth
module RLP
Expand All @@ -8,46 +9,48 @@ class InvalidValueError < StandardError

class << self

# Decode input from rlp encoding, only produce string or array
#
# Examples:
#
# Eth::RLP.decode(input)
#
def decode(input)
Decode.decode(input)
end

# Encode input to rlp encoding, only allow string or array
#
# Examples:
#
# Eth::RLP.encode("hello world")
#
def encode(input)
Encode.encode(input)
end

# use this method before RLP.encode
# encode item to string or array
# Use this method before RLP.encode, this method encode ruby objects to rlp friendly format, string or array.
# see Eth::RLP::Serializable::TYPES for supported types
#
# Examples:
#
# item = Eth::RLP.encode_with_type(number, :int, zero: "\x00".b)
# encoded_text = Eth::RLP.encode(item)
#
def encode_with_type(item, type, zero: '')
if type == :int
Eth::Utils.big_endian_encode(item, zero)
elsif type == :bool
Eth::Utils.big_endian_encode(item ? 0x01 : 0x80)
elsif type.is_a?(Array)
item.map {|i| encode_with_type(i, type[0])}
else
item
end
Serializable.encode_with_type(item, type, zero: zero)
end

# use this method after RLP.decode
# decode values from string or array to specific types
# Use this method after RLP.decode, decode values from string or array to specific types
# see Eth::RLP::Serializable::TYPES for supported types
#
# Examples:
#
# item = Eth::RLP.decode(encoded_text)
# number = Eth::RLP.decode_with_type(item, :int)
#
def decode_with_type(item, type)
if type == :int
Eth::Utils.big_endian_decode(item)
elsif type == :bool
if item == Eth::Utils.big_endian_encode(0x01)
true
elsif item == Eth::Utils.big_endian_encode(0x80)
false
else
raise InvalidValueError.new "invalid bool value #{item}"
end
elsif type.is_a?(Array)
item.map {|i| decode_with_type(i, type[0])}
else
item
end
Serializable.decode_with_type(item, type)
end

end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Usage:
#
# class AuthMsgV4
# include Eth::DevP2P::Serializable
# include Eth::RLP::Serializable
#
# schema [
# {got_plain: :bool},
Expand All @@ -23,7 +23,7 @@
#

module Eth
module DevP2P
module RLP
module Serializable
TYPES = %i{raw int bool}.map {|key| [key, true]}.to_h.freeze

Expand Down Expand Up @@ -111,8 +111,50 @@ def default_data(data = nil)
end
end

def self.included(base)
base.send :extend, ClassMethods
class << self
def included(base)
base.send :extend, ClassMethods
end

# use this method before RLP.encode
# encode item to string or array
def encode_with_type(item, type, zero: '')
if type == :int
Eth::Utils.big_endian_encode(item, zero)
elsif type == :bool
Eth::Utils.big_endian_encode(item ? 0x01 : 0x80)
elsif type.is_a?(Array)
item.map {|i| encode_with_type(i, type[0])}
else
item
end
end

# Use this method after RLP.decode, decode values from string or array to specific types
# see Eth::RLP::Serializable::TYPES for supported types
#
# Examples:
#
# item = Eth::RLP.decode(encoded_text)
# decode_with_type(item, :int)
#
def decode_with_type(item, type)
if type == :int
Eth::Utils.big_endian_decode(item)
elsif type == :bool
if item == Eth::Utils.big_endian_encode(0x01)
true
elsif item == Eth::Utils.big_endian_encode(0x80)
false
else
raise InvalidValueError.new "invalid bool value #{item}"
end
elsif type.is_a?(Array)
item.map {|i| decode_with_type(i, type[0])}
else
item
end
end
end

attr_reader :data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'ethruby/devp2p/serializable'
require 'ethruby/rlp/serializable'

my_class = Class.new do
include Eth::DevP2P::Serializable
include Eth::RLP::Serializable

schema [
{got_plain: :bool},
Expand All @@ -12,7 +12,7 @@
default_data(got_plain: false)
end

RSpec.describe Eth::DevP2P::Serializable do
RSpec.describe Eth::RLP::Serializable do
it 'apply default value' do
msg = my_class.new(signature: '123', nonce: [1, 2, 3], version: 4)
expect(msg.got_plain).to be_falsey
Expand All @@ -21,7 +21,7 @@
it 'raise invalid if missing key' do
expect do
my_class.new(signature: '123', nonce: [1, 2, 3])
end.to raise_error(Eth::DevP2P::Serializable::Schema::InvalidSchemaError)
end.to raise_error(Eth::RLP::Serializable::Schema::InvalidSchemaError)
end

it 'rlp encoding/decoding' do
Expand Down

0 comments on commit 55ea189

Please sign in to comment.