Skip to content

Commit

Permalink
feat: change all number to integer and convert to hex string in RPC i…
Browse files Browse the repository at this point in the history
…nterface

BREAKING CHANGE: All number type changed to integer and will convert to hex string in RPC interface
  • Loading branch information
classicalliu committed Sep 16, 2019
1 parent 8bfa661 commit a1f0495
Show file tree
Hide file tree
Showing 53 changed files with 928 additions and 431 deletions.
20 changes: 10 additions & 10 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
secp_group_cell_transaction = genesis_block.transactions[1]
secp_group_out_point = Types::OutPoint.new(
tx_hash: secp_group_cell_transaction.hash,
index: "0"
index: 0
)

secp_cell_type_hash = system_cell_transaction.outputs[1].type.compute_hash
Expand All @@ -43,7 +43,7 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)

dao_out_point = Types::OutPoint.new(
tx_hash: system_cell_transaction.hash,
index: "2"
index: 2
)
dao_cell_data = CKB::Utils.hex_to_bin(system_cell_transaction.outputs_data[2])
dao_code_hash = CKB::Blake2b.hexdigest(dao_cell_data)
Expand All @@ -65,16 +65,16 @@ def set_dao_dep(out_point, code_hash)
end

def genesis_block
@genesis_block ||= get_block_by_number("0")
@genesis_block ||= get_block_by_number(0)
end

def genesis_block_hash
@genesis_block_hash ||= get_block_hash("0")
@genesis_block_hash ||= get_block_hash(0)
end

# @return [String | Integer]
def get_block_hash(block_number)
rpc.get_block_hash(block_number.to_s)
rpc.get_block_hash(block_number)
end

# @param block_hash [String] 0x...
Expand All @@ -89,7 +89,7 @@ def get_block(block_hash)
#
# @return [CKB::Types::Block]
def get_block_by_number(block_number)
block_h = rpc.get_block_by_number(block_number.to_s)
block_h = rpc.get_block_by_number(block_number)
Types::Block.from_h(block_h)
end

Expand All @@ -101,7 +101,7 @@ def get_tip_header

# @return [String]
def get_tip_block_number
rpc.get_tip_block_number
Utils.to_int(rpc.get_tip_block_number)
end

# @param hash [String] 0x...
Expand All @@ -110,7 +110,7 @@ def get_tip_block_number
#
# @return [CKB::Types::Output[]]
def get_cells_by_lock_hash(hash, from, to)
outputs = rpc.get_cells_by_lock_hash(hash, from.to_s, to.to_s)
outputs = rpc.get_cells_by_lock_hash(hash, from, to)
outputs.map { |output| Types::Output.from_h(output) }
end

Expand Down Expand Up @@ -246,7 +246,7 @@ def get_transactions_by_lock_hash(lock_hash, page, per, reverse_order: false)
# @param index_from [String]
#
# @return [Types::LockHashIndexState]
def index_lock_hash(lock_hash, index_from: "0")
def index_lock_hash(lock_hash, index_from: 0)
state = rpc.index_lock_hash(lock_hash, index_from: index_from)
Types::LockHashIndexState.from_h(state)
end
Expand All @@ -263,7 +263,7 @@ def get_header(block_hash)
#
# @return [CKB::Types::BlockHeader]
def get_header_by_number(block_number)
block_header_h = rpc.get_header_by_number(block_number.to_s)
block_header_h = rpc.get_header_by_number(block_number)
Types::BlockHeader.from_h(block_header_h)
end

Expand Down
24 changes: 12 additions & 12 deletions lib/ckb/rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ def initialize(host: DEFAULT_URL)
end

def genesis_block
@genesis_block ||= get_block_by_number("0")
@genesis_block ||= get_block_by_number(0)
end

def genesis_block_hash
@genesis_block_hash ||= get_block_hash("0")
@genesis_block_hash ||= get_block_hash(0)
end

def get_block_hash(block_number)
rpc_request("get_block_hash", params: [block_number])
rpc_request("get_block_hash", params: [Utils.to_hex(block_number)])
end

def get_block(block_hash)
rpc_request("get_block", params: [block_hash])
end

def get_block_by_number(block_number)
rpc_request("get_block_by_number", params: [block_number.to_s])
rpc_request("get_block_by_number", params: [Utils.to_hex(block_number)])
end

def get_tip_header
Expand All @@ -62,7 +62,7 @@ def get_tip_block_number
end

def get_cells_by_lock_hash(hash, from, to)
rpc_request("get_cells_by_lock_hash", params: [hash, from, to])
rpc_request("get_cells_by_lock_hash", params: [hash, Utils.to_hex(from), Utils.to_hex(to)])
end

def get_transaction(tx_hash)
Expand All @@ -86,7 +86,7 @@ def get_current_epoch
end

def get_epoch_by_number(number)
rpc_request("get_epoch_by_number", params: [number.to_s])
rpc_request("get_epoch_by_number", params: [Utils.to_hex(number)])
end

# @return [Hash[]]
Expand Down Expand Up @@ -132,27 +132,27 @@ def deindex_lock_hash(lock_hash)
end

def get_live_cells_by_lock_hash(lock_hash, page, per, reverse_order: false)
rpc_request("get_live_cells_by_lock_hash", params: [lock_hash, page.to_s, per.to_s, reverse_order])
rpc_request("get_live_cells_by_lock_hash", params: [lock_hash, Utils.to_hex(page), Utils.to_hex(per), reverse_order])
end

def get_lock_hash_index_states
rpc_request("get_lock_hash_index_states")
end

def get_transactions_by_lock_hash(lock_hash, page, per, reverse_order: false)
rpc_request("get_transactions_by_lock_hash", params: [lock_hash, page.to_s, per.to_s, reverse_order])
rpc_request("get_transactions_by_lock_hash", params: [lock_hash, Utils.to_hex(page), Utils.to_hex(per), reverse_order])
end

def index_lock_hash(lock_hash, index_from: '0')
rpc_request("index_lock_hash", params: [lock_hash, index_from.to_s])
def index_lock_hash(lock_hash, index_from: 0)
rpc_request("index_lock_hash", params: [lock_hash, Utils.to_hex(index_from)])
end

def get_header(block_hash)
rpc_request("get_header", params: [block_hash])
end

def get_header_by_number(block_number)
rpc_request("get_header_by_number", params: [block_number.to_s])
rpc_request("get_header_by_number", params: [Utils.to_hex(block_number)])
end

def get_cellbase_output_capacity_details(block_hash)
Expand All @@ -165,7 +165,7 @@ def get_cellbase_output_capacity_details(block_hash)
# @param absolute [Boolean | nil]
# @param reason [String | nil]
def set_ban(address, command, ban_time = nil, absolute = nil, reason = nil)
rpc_request("set_ban", params: [address, command, ban_time, absolute, reason])
rpc_request("set_ban", params: [address, command, Utils.to_hex(ban_time), absolute, reason])
end

def get_banned_addresses
Expand Down
6 changes: 3 additions & 3 deletions lib/ckb/types/address_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ class AddressInfo
attr_accessor :address, :score

# @param address [String]
# @param score [String] number
# @param score [String | Integer] integer or hex number
def initialize(address:, score:)
@address = address
@score = score.to_s
@score = Utils.to_int(score)
end

def to_h
{
address: @address,
score: @score
score: Utils.to_hex(@score)
}
end

Expand Down
18 changes: 9 additions & 9 deletions lib/ckb/types/alert_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ module Types
class AlertMessage
attr_accessor :id, :priority, :notice_until, :message

# @param id [String]
# @param priority [String]
# @param notice_until [String]
# @param id [String | Integer] integer or hex number
# @param priority [String | Integer] integer or hex number
# @param notice_until [String] timestamp
# @param message [String]
def initialize(id:, priority:, notice_until:, message:)
@id = id
@priority = priority
@notice_until = notice_until
@id = Utils.to_int(id)
@priority = Utils.to_int(priority)
@notice_until = Utils.to_int(notice_until)
@message = message
end

def to_h
{
id: @id,
priority: @priority,
notice_until: @notice_until,
id: Utils.to_hex(@id),
priority: Utils.to_hex(@priority),
notice_until: Utils.to_hex(@notice_until),
message: @message
}
end
Expand Down
12 changes: 6 additions & 6 deletions lib/ckb/types/banned_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ class BannedAddress
attr_accessor :address, :ban_until, :ban_reason, :created_at

# @param address [String]
# @param ban_until [String] timestamp
# @param ban_until [String | Integer] timestamp
# @param ban_reason [String]
# @param created_at [String] timestamp
# @param created_at [String | Integer] timestamp
def initialize(address:, ban_until:, ban_reason:, created_at:)
@address = address
@ban_until = ban_until
@ban_until = Utils.to_int(ban_until)
@ban_reason = ban_reason
@created_at = created_at
@created_at = Utils.to_int(created_at)
end

def to_h
{
address: @address,
ban_until: @ban_until,
ban_until: Utils.to_hex(@ban_until),
ban_reason: @ban_reason,
created_at: @created_at
created_at: Utils.to_hex(@created_at)
}
end

Expand Down
42 changes: 21 additions & 21 deletions lib/ckb/types/block_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ class BlockHeader
attr_accessor :difficulty, :hash, :number, :parent_hash, :nonce, :timestamp, :transactions_root, :proposals_hash, \
:uncles_count, :uncles_hash, :version, :witnesses_root, :epoch, :dao, :chain_root

# @param difficulty [String] 0x...
# @param difficulty [String | Integer] integer or hex number
# @param hash [String] 0x...
# @param number [String] number
# @param number [String | Integer] integer or hex number
# @param parent_hash [String] 0x...
# @param nonce [String] decimal number
# @param timestamp [String]
# @param nonce [String | Integer] integer or hex number
# @param timestamp [String | Integer] integer or hex number
# @param transactions_root [String] 0x...
# @param proposals_hash [String] 0x...
# @param uncles_count [String] number
# @param uncles_count [String | Integer] integer or hex number
# @param uncles_hash [String] 0x...
# @param version [String] number
# @param version [String | Integer] integer or hex number
# @param witnesses_root [String] 0x...
# @param epoch [String] number
# @param epoch [String | Integer] integer or hex number
# @param dao [String] 0x...
# @param chain_root [String] 0x...
def initialize(
Expand All @@ -38,38 +38,38 @@ def initialize(
dao: ,
chain_root:
)
@difficulty = difficulty
@difficulty = Utils.to_int(difficulty)
@hash = hash
@number = number.to_s
@number = Utils.to_int(number)
@parent_hash = parent_hash
@nonce = nonce
@timestamp = timestamp.to_s
@nonce = Utils.to_int(nonce)
@timestamp = Utils.to_int(timestamp)
@transactions_root = transactions_root
@proposals_hash = proposals_hash
@uncles_count = uncles_count.to_s
@uncles_count = Utils.to_int(uncles_count)
@uncles_hash = uncles_hash
@version = version.to_s
@version = Utils.to_int(version)
@witnesses_root = witnesses_root
@epoch = epoch
@epoch = Utils.to_int(epoch)
@dao = dao
@chain_root = chain_root
end

def to_h
{
difficulty: @difficulty,
difficulty: Utils.to_hex(@difficulty),
hash: @hash,
number: @number,
number: Utils.to_hex(@number),
parent_hash: parent_hash,
nonce: nonce,
timestamp: @timestamp,
nonce: Utils.to_hex(nonce),
timestamp: Utils.to_hex(@timestamp),
transactions_root: @transactions_root,
proposals_hash: @proposals_hash,
uncles_count: @uncles_count,
uncles_count: Utils.to_hex(@uncles_count),
uncles_hash: @uncles_hash,
version: @version,
version: Utils.to_hex(@version),
witnesses_root: @witnesses_root,
epoch: @epoch,
epoch: Utils.to_hex(@epoch),
dao: @dao,
chain_root: @chain_root
}
Expand Down
30 changes: 15 additions & 15 deletions lib/ckb/types/block_reward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ module Types
class BlockReward
attr_accessor :total, :primary, :secondary, :tx_fee, :proposal_reward

# @param total [String] number
# @param primary [String] number
# @param secondary [String] number
# @param tx_fee [String] number
# @param proposal_reward [String] number
# @param total [String | Integer] integer or hex number
# @param primary [String | Integer] integer or hex number
# @param secondary [String | Integer] integer or hex number
# @param tx_fee [String | Integer] integer or hex number
# @param proposal_reward [String | Integer] integer or hex number
def initialize(total:, primary:, secondary:, tx_fee:, proposal_reward:)
@total = total
@primary = primary
@secondary = secondary
@tx_fee = tx_fee
@proposal_reward = proposal_reward
@total = Utils.to_int(total)
@primary = Utils.to_int(primary)
@secondary = Utils.to_int(secondary)
@tx_fee = Utils.to_int(tx_fee)
@proposal_reward = Utils.to_int(proposal_reward)
end

def to_h
{
total: @total,
primary: @primary,
secondary: @secondary,
tx_fee: @tx_fee,
proposal_reward: @proposal_reward
total: Utils.to_hex(@total),
primary: Utils.to_hex(@primary),
secondary: Utils.to_hex(@secondary),
tx_fee: Utils.to_hex(@tx_fee),
proposal_reward: Utils.to_hex(@proposal_reward)
}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ckb/types/cell_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(created_by:, consumed_by: nil)

def to_h
{
created_by: @created_by,
created_by: @created_by.to_h,
consumed_by: @consumed_by
}
end
Expand Down
Loading

0 comments on commit a1f0495

Please sign in to comment.