-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add peer and address_info types
BREAKING CHANGE: `local_node_info` and `get_peers` return types instead of Hash
- Loading branch information
1 parent
7de38c6
commit 933da45
Showing
6 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |