Skip to content

Commit

Permalink
feat: add TxPoolInfo type
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `api.tx_pool_info` returns `TxPoolInfo` instead of `Hash`
  • Loading branch information
classicalliu committed May 9, 2019
1 parent 933da45 commit 0f257a4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ def get_peers
rpc.get_peers.map { |peer| Types::Peer.from_h(peer) }
end

# @return [Hash]
# @return [CKB::Types::TxPoolInfo]
def tx_pool_info
rpc.tx_pool_info
Types::TxPoolInfo.from_h(
rpc.tx_pool_info
)
end

# @return [CKB::Types::ChainInfo]
Expand Down
42 changes: 42 additions & 0 deletions lib/ckb/types/tx_pool_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module CKB
module Types
class TxPoolInfo
attr_reader :pending, :staging, :orphan, :last_txs_updated_at

# @param pending [Integer]
# @param staging [Integer]
# @param orphan [Integer]
# @param last_txs_updated_at [String] timestamp
def initialize(pending:, staging:, orphan:, last_txs_updated_at:)
@pending = pending
@staging = staging
@orphan = orphan
@last_txs_updated_at = last_txs_updated_at
end

def to_h
{
pending: @pending,
staging: @staging,
orphan: @orphan,
last_txs_updated_at: @last_txs_updated_at
}
end

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

return hash if hash.is_a?(self.class)

new(
pending: hash[:pending],
staging: hash[:staging],
orphan: hash[:orphan],
last_txs_updated_at: hash[:last_txs_updated_at]
)
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 @@ -18,6 +18,7 @@
require_relative "peer_state"
require_relative "address_info"
require_relative "peer"
require_relative "tx_pool_info"

module CKB
module Types
Expand Down
3 changes: 2 additions & 1 deletion spec/ckb/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@

it "tx pool info" do
result = api.tx_pool_info
expect(result[:pending] >= 0).to be true
expect(result).to be_a(Types::TxPoolInfo)
expect(result.pending >= 0).to be true
end

it "get blockchain info" do
Expand Down
16 changes: 16 additions & 0 deletions spec/ckb/types/tx_pool_info_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
RSpec.describe CKB::Types::TxPoolInfo do
let(:tx_pool_info_h) do
{
"pending": 34,
"staging": 22,
"orphan": 33,
"last_txs_updated_at": "1555507787683"
}
end

it "from h" do
tx_pool_info = CKB::Types::TxPoolInfo.from_h(tx_pool_info_h)
expect(tx_pool_info).to be_a(CKB::Types::TxPoolInfo)
expect(tx_pool_info.pending).to eq tx_pool_info_h[:pending]
end
end

0 comments on commit 0f257a4

Please sign in to comment.