Skip to content

Commit

Permalink
feat: add peer and address_info types
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `local_node_info` and `get_peers` return types instead of Hash
  • Loading branch information
classicalliu committed May 9, 2019
1 parent 7de38c6 commit 933da45
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ def send_transaction(transaction)
rpc.send_transaction(transaction.to_h)
end

# @return [Hash]
# @return [CKB::Type::Peer]
def local_node_info
rpc.local_node_info
Types::Peer.from_h(
rpc.local_node_info
)
end

# @param transaction [CKB::Types::Transaction]
Expand Down Expand Up @@ -151,9 +153,9 @@ def get_epoch_by_number(number)
)
end

# @return [Hash[]]
# @return [CKB::Types::Peer[]]
def get_peers
rpc.get_peers
rpc.get_peers.map { |peer| Types::Peer.from_h(peer) }
end

# @return [Hash]
Expand Down
34 changes: 34 additions & 0 deletions lib/ckb/types/address_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module CKB
module Types
class AddressInfo
attr_reader :address, :score

# @param address [String]
# @param score [Integer]
def initialize(address:, score:)
@address = address
@score = score
end

def to_h
{
address: @address,
score: @score
}
end

def self.from_h(hash)
return if hash.nil?

return hash if hash.is_a?(self.class)

new(
address: hash[:address],
score: hash[:score]
)
end
end
end
end
42 changes: 42 additions & 0 deletions lib/ckb/types/peer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module CKB
module Types
class Peer
attr_reader :addresses, :is_outbound, :node_id, :version

# @param addresses [AddressInfo[]]
# @param is_outbound [Boolean]
# @param node_id [String]
# @param version [String]
def initialize(addresses:, is_outbound:, node_id:, version:)
@addresses = addresses
@is_outbound = is_outbound
@node_id = node_id
@version = version
end

def to_h
{
addresses: @addresses,
is_outbound: @is_outbound,
node_id: @node_id,
version: @version
}
end

def self.from_h(hash)
return if hash.nil?

return hash if hash.is_a?(self.class)

new(
addresses: hash[:addresses].map { |addr| AddressInfo.from_h(addr) },
is_outbound: hash[:is_outbound],
node_id: hash[:node_id],
version: hash[:version]
)
end
end
end
end
2 changes: 2 additions & 0 deletions lib/ckb/types/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
require_relative "epoch"
require_relative "chain_info"
require_relative "peer_state"
require_relative "address_info"
require_relative "peer"

module CKB
module Types
Expand Down
5 changes: 5 additions & 0 deletions spec/ckb/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
expect(result.number).to eq number.to_s
end

it "local node info" do
result = api.local_node_info
expect(result).to be_a(Types::Peer)
end

it "get peers" do
result = api.get_peers
expect(result).not_to be nil
Expand Down
20 changes: 20 additions & 0 deletions spec/ckb/types/peer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
RSpec.describe CKB::Types::Peer do
let(:peer_h) do
{
"addresses": [
{
"address": "/ip4/192.168.0.3/tcp/8115",
"score": 1
}
],
"is_outbound": true,
"node_id": "QmaaaLB4uPyDpZwTQGhV63zuYrKm4reyN2tF1j2ain4oE7",
"version": "unknown"
}
end

it "from h" do
peer = CKB::Types::Peer::from_h(peer_h)
expect(peer).to be_a(CKB::Types::Peer)
end
end

0 comments on commit 933da45

Please sign in to comment.