diff --git a/lib/ckb/types/peer.rb b/lib/ckb/types/peer.rb index fe686f04..3089d5b6 100644 --- a/lib/ckb/types/peer.rb +++ b/lib/ckb/types/peer.rb @@ -3,17 +3,25 @@ module CKB module Types class Peer - attr_accessor :addresses, :is_outbound, :node_id, :version + attr_accessor :addresses, :is_outbound, :node_id, :version, :connected_duration, :last_ping_duration, :protocols, :sync_state # @param addresses [AddressInfo[]] # @param is_outbound [Boolean] # @param node_id [String] # @param version [String] - def initialize(addresses:, is_outbound:, node_id:, version:) + # @param connected_duration [String | Integer] integer or hex number + # @param last_ping_duration [String | Integer] integer or hex number + # @param protocols [String[Hash]] + # @param sync_state [PeerSyncState] + def initialize(addresses:, is_outbound:, node_id:, version:, connected_duration:, last_ping_duration:, protocols:, sync_state:) @addresses = addresses @is_outbound = is_outbound @node_id = node_id @version = version + @connected_duration = connected_duration + @last_ping_duration = last_ping_duration + @protocols = protocols + @sync_state = sync_state end def to_h @@ -21,7 +29,11 @@ def to_h addresses: @addresses.map(&:to_h), is_outbound: @is_outbound, node_id: @node_id, - version: @version + version: @version, + connected_duration: Utils.to_hex(@connected_duration), + last_ping_duration: Utils.to_hex(@last_ping_duration), + protocols: @protocols, + sync_state: @sync_state.to_h } end @@ -32,7 +44,11 @@ def self.from_h(hash) addresses: hash[:addresses].map { |addr| AddressInfo.from_h(addr) }, is_outbound: hash[:is_outbound], node_id: hash[:node_id], - version: hash[:version] + version: hash[:version], + connected_duration: hash[:connected_duration], + last_ping_duration: hash[:last_ping_duration], + protocols: hash[:protocols], + sync_state: PeerSyncState.from_h(hash[:sync_state]) ) end end diff --git a/lib/ckb/types/peer_sync_state.rb b/lib/ckb/types/peer_sync_state.rb new file mode 100644 index 00000000..d8e14a42 --- /dev/null +++ b/lib/ckb/types/peer_sync_state.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +module CKB + module Types + class PeerSyncState + attr_accessor :best_known_header_hash, :best_known_header_number, :last_common_header_hash, :last_common_header_number, :unknown_header_list_size, :inflight_count, :can_fetch_count + + # @param best_known_header_hash [String] 0x + # @param best_known_header_number [String | Integer] integer or hex number + # @param last_common_header_hash [String] 0x + # @param last_common_header_number [String | Integer] integer or hex number + # @param unknown_header_list_size [String | Integer] integer or hex number + # @param inflight_count [String | Integer] integer or hex number + # @param can_fetch_count [String | Integer] integer or hex number + def initialize(best_known_header_hash:, best_known_header_number:, last_common_header_hash:, last_common_header_number:, unknown_header_list_size:, inflight_count:, can_fetch_count:) + @best_known_header_hash = best_known_header_hash + @best_known_header_number = Utils.to_int(best_known_header_number) + @last_common_header_hash = last_common_header_hash + @last_common_header_number = Utils.to_int(last_common_header_number) + @unknown_header_list_size = Utils.to_int(unknown_header_list_size) + @inflight_count = Utils.to_int(inflight_count) + @can_fetch_count = Utils.to_int(can_fetch_count) + end + + def to_h + { + best_known_header_hash: best_known_header_hash, + best_known_header_number: Utils.to_hex(best_known_header_number), + last_common_header_hash: last_common_header_hash, + last_common_header_number: Utils.to_hex(last_common_header_number), + unknown_header_list_size: Utils.to_hex(unknown_header_list_size), + inflight_count: Utils.to_hex(inflight_count), + can_fetch_count: Utils.to_hex(can_fetch_count) + } + end + + def self.from_h(hash) + return if hash.nil? + + new( + best_known_header_hash: hash[:best_known_header_hash], + best_known_header_number: hash[:best_known_header_number], + last_common_header_hash: hash[:last_common_header_hash], + last_common_header_number: hash[:last_common_header_number], + unknown_header_list_size: hash[:unknown_header_list_size], + inflight_count: hash[:inflight_count], + can_fetch_count: hash[:can_fetch_count], + ) + end + end + end +end diff --git a/lib/ckb/types/types.rb b/lib/ckb/types/types.rb index 2a86bc83..4d5f748f 100644 --- a/lib/ckb/types/types.rb +++ b/lib/ckb/types/types.rb @@ -40,6 +40,7 @@ require_relative "block_issuance" require_relative "miner_reward" require_relative "sync_state" +require_relative "peer_sync_state" module CKB module Types diff --git a/spec/ckb/types/peer_spec.rb b/spec/ckb/types/peer_spec.rb index 63c70035..df6b9030 100644 --- a/spec/ckb/types/peer_spec.rb +++ b/spec/ckb/types/peer_spec.rb @@ -11,9 +11,54 @@ "score": "0x1" } ], + "connected_duration": "0x2f", "is_outbound": nil, + "last_ping_duration": "0x1a", "node_id": "QmTRHCdrRtgUzYLNCin69zEvPvLYdxUZLLfLYyHVY3DZAS", - "version": "0.0.0" + "protocols": [ + { + "id": "0x4", + "version": "0.0.1" + }, + { + "id": "0x2", + "version": "0.0.1" + }, + { + "id": "0x1", + "version": "0.0.1" + }, + { + "id": "0x64", + "version": "1" + }, + { + "id": "0x6e", + "version": "1" + }, + { + "id": "0x66", + "version": "1" + }, + { + "id": "0x65", + "version": "1" + }, + { + "id": "0x0", + "version": "0.0.1" + } + ], + "sync_state": { + "best_known_header_hash": nil, + "best_known_header_number": nil, + "can_fetch_count": "0x80", + "inflight_count": "0xa", + "last_common_header_hash": nil, + "last_common_header_number": nil, + "unknown_header_list_size": "0x20" + }, + "version": "0.0.0", } end @@ -22,6 +67,7 @@ it "from h" do expect(peer).to be_a(CKB::Types::Peer) expect(peer.addresses).to all(be_a(CKB::Types::AddressInfo)) + expect(peer.sync_state).to be_a(CKB::Types::PeerSyncState) end it "to_h" do