Skip to content

Commit

Permalink
feat: add state RPCs
Browse files Browse the repository at this point in the history
add `get_blockchain_info` and `get_peers_state` RPCs
  • Loading branch information
classicalliu committed May 9, 2019
1 parent d71e3f5 commit 2ec5580
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ def tx_pool_info
rpc.tx_pool_info
end

# @return [CKB::Types::ChainInfo]
def get_blockchain_info
Types::ChainInfo.from_h(
rpc.get_blockchain_info
)
end

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

def inspect
"\#<API@#{rpc.uri}>"
end
Expand Down
8 changes: 8 additions & 0 deletions lib/ckb/rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ def tx_pool_info
rpc_request("tx_pool_info")
end

def get_blockchain_info
rpc_request("get_blockchain_info")
end

def get_peers_state
rpc_request("get_peers_state")
end

def inspect
"\#<RPC@#{uri}>"
end
Expand Down
55 changes: 55 additions & 0 deletions lib/ckb/types/chain_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

module CKB
module Types
class ChainInfo
attr_reader :is_initial_block_download, :epoch, :difficulty, :median_time, :chain, :warnings

# @param is_initialize_block_download [Boolean]
# @param epoch [String] number
# @param difficulty [String] 0x...
# @param median_time [String] timestamp
# @param chain [String]
# @param warnings [String]
def initialize(
is_initial_block_download:,
epoch:,
difficulty:,
median_time:,
chain:,
warnings:
)
@is_initial_block_download = is_initial_block_download
@epoch = epoch
@difficulty = difficulty
@median_time = median_time
@chain = chain
@warnings = warnings
end

def to_h
{
is_initial_block_download: @is_initial_block_download,
epoch: @epoch,
difficulty: @difficulty,
median_time: @median_time,
chain: @chain,
warnings: @warnings
}
end

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

new(
is_initial_block_download: hash[:is_initial_block_download],
epoch: hash[:epoch],
difficulty: hash[:difficulty],
median_time: hash[:median_time],
chain: hash[:chain],
warnings: hash[:warnings]
)
end
end
end
end
36 changes: 36 additions & 0 deletions lib/ckb/types/peer_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module CKB
module Types
class PeerState
attr_reader :last_updated, :blocks_in_flight, :peer

# @param last_updated [String]
# @param block_in_flight [String] number
# @param peer [String] number
def initialize(last_updated:, blocks_in_flight:, peer:)
@last_updated = last_updated
@blocks_in_flight = blocks_in_flight
@peer = peer
end

def to_h
{
last_updated: @last_updated,
blocks_in_flight: @blocks_in_flight,
peer: peer
}
end

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

new(
last_updated: hash[:last_updated],
blocks_in_flight: hash[:blocks_in_flight],
peer: hash[:peer]
)
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 @@ -14,6 +14,8 @@
require_relative "tx_status"
require_relative "witness"
require_relative "epoch"
require_relative "chain_info"
require_relative "peer_state"

module CKB
module Types
Expand Down
11 changes: 11 additions & 0 deletions spec/ckb/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,15 @@
result = api.tx_pool_info
expect(result[:pending] >= 0).to be true
end

it "get blockchain info" do
result = api.get_blockchain_info
expect(result).to be_a(Types::ChainInfo)
expect(result.epoch.to_i >= 0).to be true
end

it "get peers state" do
result = api.get_peers_state
expect(result).to be_an(Array)
end
end
File renamed without changes.
17 changes: 17 additions & 0 deletions spec/ckb/types/chain_info_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
RSpec.describe CKB::Types::ChainInfo do
let(:chain_info_h) do
{
"is_initial_block_download": false,
"epoch": "0",
"difficulty": "0x100",
"median_time": "1557287480008",
"chain": "ckb_dev",
"warnings": ""
}
end

it "from h" do
chain_info = CKB::Types::ChainInfo::from_h(chain_info_h)
expect(chain_info).to be_a(CKB::Types::ChainInfo)
end
end
14 changes: 14 additions & 0 deletions spec/ckb/types/peer_state_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
RSpec.describe CKB::Types::PeerState do
let(:peer_state_h) do
{
"last_updated": "1557289448237",
"blocks_in_flight": "86",
"peer": "1"
}
end

it "from h" do
peer_state = CKB::Types::PeerState.from_h(peer_state_h)
expect(peer_state).to be_a(CKB::Types::PeerState)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

it "to_hash" do
expect(
lock.to_hash
always_success.to_hash
).to eq always_success_type_hash
end
end

0 comments on commit 2ec5580

Please sign in to comment.