diff --git a/lib/ckb/api.rb b/lib/ckb/api.rb index f644f20f..1f8940ac 100644 --- a/lib/ckb/api.rb +++ b/lib/ckb/api.rb @@ -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] @@ -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] diff --git a/lib/ckb/types/address_info.rb b/lib/ckb/types/address_info.rb new file mode 100644 index 00000000..9fdebebd --- /dev/null +++ b/lib/ckb/types/address_info.rb @@ -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 diff --git a/lib/ckb/types/peer.rb b/lib/ckb/types/peer.rb new file mode 100644 index 00000000..da2df778 --- /dev/null +++ b/lib/ckb/types/peer.rb @@ -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 diff --git a/lib/ckb/types/types.rb b/lib/ckb/types/types.rb index df9bc201..d11f3092 100644 --- a/lib/ckb/types/types.rb +++ b/lib/ckb/types/types.rb @@ -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 diff --git a/spec/ckb/api_spec.rb b/spec/ckb/api_spec.rb index e89cfdeb..e853832e 100644 --- a/spec/ckb/api_spec.rb +++ b/spec/ckb/api_spec.rb @@ -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 diff --git a/spec/ckb/types/peer_spec.rb b/spec/ckb/types/peer_spec.rb new file mode 100644 index 00000000..6330b0af --- /dev/null +++ b/spec/ckb/types/peer_spec.rb @@ -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