Skip to content

Commit

Permalink
feat: add get_raw_tx_pool RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Jan 11, 2021
1 parent 1be5c70 commit 87479b5
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 33 deletions.
5 changes: 3 additions & 2 deletions lib/ckb/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Address
CODE_HASH_INDEX_SINGLESIG = "00"
CODE_HASH_INDEX_MULTISIG_SIG = "01"
CODE_HASH_INDEX_ANYONE_CAN_PAY = "02"
SHORT_PAYLOAD_AVAILABLE_ARGS_LEN = [20, 21, 22]
SHORT_PAYLOAD_AVAILABLE_ARGS_LEN = [20, 21, 22].freeze

# @param script [CKB::Types::Script]
# @param mode [String]
Expand All @@ -27,9 +27,10 @@ def initialize(script, mode: DEFAULT_MODE)
# payload = type(01) | code hash index(00) | pubkey blake160
# see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md for more info.
def generate
unless CKB::ScriptHashType::TYPE == script.hash_type && script.has_args? && (SHORT_PAYLOAD_AVAILABLE_ARGS_LEN.include?(CKB::Utils.hex_to_bin(script.args).bytesize))
unless CKB::ScriptHashType::TYPE == script.hash_type && script.has_args? && SHORT_PAYLOAD_AVAILABLE_ARGS_LEN.include?(CKB::Utils.hex_to_bin(script.args).bytesize)
return generate_full_payload_address
end

if SystemCodeHash::SECP256K1_BLAKE160_SIGHASH_ALL_TYPE_HASH == script.code_hash
generate_short_payload_singlesig_address
elsif SystemCodeHash::SECP256K1_BLAKE160_MULTISIG_ALL_TYPE_HASH == script.code_hash
Expand Down
24 changes: 11 additions & 13 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,11 @@

module CKB
class API
attr_reader :rpc
attr_reader :secp_group_out_point
attr_reader :secp_code_out_point
attr_reader :secp_data_out_point
attr_reader :secp_cell_type_hash
attr_reader :secp_cell_code_hash
attr_reader :dao_out_point
attr_reader :dao_code_hash
attr_reader :dao_type_hash

attr_reader :multi_sign_secp_cell_type_hash
attr_reader :multi_sign_secp_group_out_point
attr_reader :rpc, :secp_group_out_point, :secp_code_out_point, :secp_data_out_point, :secp_cell_type_hash, :secp_cell_code_hash, :dao_out_point, :dao_code_hash, :dao_type_hash, :multi_sign_secp_cell_type_hash, :multi_sign_secp_group_out_point

def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET, timeout_config: {})
@rpc = CKB::RPC.new(host: host, timeout_config: timeout_config)
if mode == MODE::TESTNET || mode == MODE::MAINNET
if [MODE::TESTNET, MODE::MAINNET].include?(mode)
# For testnet chain, we can assume the second cell of the first transaction
# in the genesis block contains default lock script we can use here.
system_cell_transaction = genesis_block.transactions.first
Expand Down Expand Up @@ -296,6 +285,15 @@ def clear_tx_pool
rpc.clear_tx_pool
end

def get_raw_tx_pool(verbose = false)
rs = rpc.get_raw_tx_pool(verbose)
if verbose
Types::TxPoolVerbosity.from_h(rs)
else
Types::TxPoolIds.from_h(rs)
end
end

# @return sync_state [SyncState]
def sync_state
Types::SyncState.from_h(rpc.sync_state)
Expand Down
1 change: 1 addition & 0 deletions lib/ckb/indexer/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module CKB
module Indexer
class API
attr_reader :rpc

DEFAULT_LIMIT = 1000

def initialize(indexer_host = CKB::RPC::DEFAULT_INDEXER_URL, timeout_config = {})
Expand Down
1 change: 1 addition & 0 deletions lib/ckb/indexer/types/live_cells.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Indexer
module Types
class LiveCells
attr_reader :last_cursor, :objects

# @param last_cursor [String]
# @param objects [CKB::Types::LiveCell[]]
def initialize(last_cursor:, objects:)
Expand Down
11 changes: 2 additions & 9 deletions lib/ckb/multi_sign_wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

module CKB
class MultiSignConfiguration
attr_reader :require_n
attr_reader :threshold
attr_reader :pubkeys
attr_reader :since
attr_reader :require_n, :threshold, :pubkeys, :since

def initialize(require_n:, threshold:, pubkeys:, since:)
raise "require_n should be less than 256" if require_n > 255 || require_n < 0
Expand Down Expand Up @@ -48,11 +45,7 @@ def lock_args
end

class MultiSignWallet
attr_reader :api
attr_reader :configuration
attr_reader :skip_data_and_type
attr_reader :prefix
attr_reader :indexer_api
attr_reader :api, :configuration, :skip_data_and_type, :prefix, :indexer_api

def initialize(api, configuration, skip_data_and_type: true, prefix: CKB::Address::PREFIX_TESTNET, indexer_api: nil)
@api = api
Expand Down
12 changes: 5 additions & 7 deletions lib/ckb/serializers/script_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ def initialize(script)

def init_args(script)
args = script.args
args = if args
args.start_with?("0x") ? args[2..-1] : args
else
""
end

args
if args
args.start_with?("0x") ? args[2..-1] : args
else
""
end
end

attr_reader :code_hash_serializer, :hash_type_serializer, :args_serializer, :items_count
Expand Down
2 changes: 1 addition & 1 deletion lib/ckb/types/cell_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CellInfo

# @param output [CKB::Types::Output]
# @param data [CKB::Types::CellData | nil]
def initialize(output: nil, data:)
def initialize(data:, output: nil)
@output = output
@data = data
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ckb/types/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def sign(key)
Utils.hex_to_bin(CKB::Serializers::WitnessArgsSerializer.from(witness).serialize)
else
Utils.hex_to_bin(witness)
end
end
data_size = data_binary.bytesize

blake2b.update([data_size].pack("Q<"))
Expand Down
30 changes: 30 additions & 0 deletions lib/ckb/types/tx_pool_ids.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module CKB
module Types
class TxPoolIds
attr_accessor :pending, :proposed

# @param pending [String]
# @param proposed [String]
def initialize(pending:, proposed:)
@pending = pending
@proposed = proposed
end

def to_h
{
pending: pending,
proposed: proposed
}
end

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

new(
pending: hash[:pending],
proposed: hash[:proposed]
)
end
end
end
end
30 changes: 30 additions & 0 deletions lib/ckb/types/tx_pool_verbosity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module CKB
module Types
class TxPoolVerbosity
attr_accessor :pending, :proposed

# @param pending [Hash]
# @param proposed [Hash]
def initialize(pending:, proposed:)
@pending = pending
@proposed = proposed
end

def to_h
{
pending: pending,
proposed: proposed
}
end

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

new(
pending: hash[:pending].map { |k, v| [k, TxVerbosity.from_h(v)] }.to_h,
proposed: hash[:proposed].map { |k, v| [k, TxVerbosity.from_h(v)] }.to_h
)
end
end
end
end
47 changes: 47 additions & 0 deletions lib/ckb/types/tx_verbosity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module CKB
module Types
class TxVerbosity
attr_accessor :cycles, :size, :fee, :ancestors_size, :ancestors_cycles, :ancestors_count

# @param cycles [String | Integer]
# @param size [String | Integer]
# @param fee [String | Integer]
# @param ancestors_size [String | Integer]
# @param ancestors_cycles [String | Integer]
# @param ancestors_count [String | Integer]
# @param
def initialize(cycles:, size:, fee:, ancestors_size:, ancestors_cycles:, ancestors_count:)
@cycles = Utils.to_int(cycles)
@size = Utils.to_int(size)
@fee = Utils.to_int(fee)
@ancestors_size = Utils.to_int(ancestors_size)
@ancestors_cycles = Utils.to_int(ancestors_cycles)
@ancestors_count = Utils.to_int(ancestors_count)
end

def to_h
{
cycles: Utils.to_hex(cycles),
size: Utils.to_hex(size),
fee: Utils.to_hex(fee),
ancestors_size: Utils.to_hex(ancestors_size),
ancestors_cycles: Utils.to_hex(ancestors_cycles),
ancestors_count: Utils.to_hex(ancestors_count)
}
end

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

new(
cycles: hash[:cycles],
size: hash[:size],
fee: hash[:fee],
ancestors_size: hash[:ancestors_size],
ancestors_cycles: hash[:ancestors_cycles],
ancestors_count: hash[:ancestors_count]
)
end
end
end
end
3 changes: 3 additions & 0 deletions lib/ckb/types/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
require_relative "local_node_protocol"
require_relative "transaction_proof"
require_relative "merkle_proof"
require_relative "tx_pool_ids"
require_relative "tx_pool_verbosity"
require_relative "tx_verbosity"

module CKB
module Types
Expand Down

0 comments on commit 87479b5

Please sign in to comment.