Skip to content

Commit

Permalink
feat: add more fields to peer
Browse files Browse the repository at this point in the history
connected_duration, last_ping_duration, protocols and sync_state
  • Loading branch information
shaojunda committed Aug 20, 2020
1 parent d943de2 commit 4edf799
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
24 changes: 20 additions & 4 deletions lib/ckb/types/peer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@
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
{
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

Expand All @@ -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
Expand Down
52 changes: 52 additions & 0 deletions lib/ckb/types/peer_sync_state.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/ckb/types/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 47 additions & 1 deletion spec/ckb/types/peer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 4edf799

Please sign in to comment.