diff --git a/lib/ckb/api.rb b/lib/ckb/api.rb index f016f9c0..f644f20f 100644 --- a/lib/ckb/api.rb +++ b/lib/ckb/api.rb @@ -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 "\#" end diff --git a/lib/ckb/rpc.rb b/lib/ckb/rpc.rb index ecdf7e80..efae23e0 100644 --- a/lib/ckb/rpc.rb +++ b/lib/ckb/rpc.rb @@ -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 "\#" end diff --git a/lib/ckb/types/chain_info.rb b/lib/ckb/types/chain_info.rb new file mode 100644 index 00000000..f603817a --- /dev/null +++ b/lib/ckb/types/chain_info.rb @@ -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 diff --git a/lib/ckb/types/peer_state.rb b/lib/ckb/types/peer_state.rb new file mode 100644 index 00000000..5d527b33 --- /dev/null +++ b/lib/ckb/types/peer_state.rb @@ -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 diff --git a/lib/ckb/types/types.rb b/lib/ckb/types/types.rb index 0ba80d0f..df9bc201 100644 --- a/lib/ckb/types/types.rb +++ b/lib/ckb/types/types.rb @@ -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 diff --git a/spec/ckb/api_spec.rb b/spec/ckb/api_spec.rb index 9509c35a..e89cfdeb 100644 --- a/spec/ckb/api_spec.rb +++ b/spec/ckb/api_spec.rb @@ -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 diff --git a/spec/ckb/types/block_test.rb b/spec/ckb/types/block_spec.rb similarity index 100% rename from spec/ckb/types/block_test.rb rename to spec/ckb/types/block_spec.rb diff --git a/spec/ckb/types/chain_info_spec.rb b/spec/ckb/types/chain_info_spec.rb new file mode 100644 index 00000000..52e258ae --- /dev/null +++ b/spec/ckb/types/chain_info_spec.rb @@ -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 diff --git a/spec/ckb/types/peer_state_spec.rb b/spec/ckb/types/peer_state_spec.rb new file mode 100644 index 00000000..e7aaa9cc --- /dev/null +++ b/spec/ckb/types/peer_state_spec.rb @@ -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 diff --git a/spec/ckb/types/script_test.rb b/spec/ckb/types/script_spec.rb similarity index 97% rename from spec/ckb/types/script_test.rb rename to spec/ckb/types/script_spec.rb index 4726d733..148752a5 100644 --- a/spec/ckb/types/script_test.rb +++ b/spec/ckb/types/script_spec.rb @@ -16,7 +16,7 @@ it "to_hash" do expect( - lock.to_hash + always_success.to_hash ).to eq always_success_type_hash end end