Skip to content

Commit

Permalink
feat: add consensus related types
Browse files Browse the repository at this point in the history
Consensus, ProposalWindow and RationalU256
  • Loading branch information
shaojunda committed Jan 11, 2021
1 parent 8efccba commit d29fe4a
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
116 changes: 116 additions & 0 deletions lib/ckb/types/consensus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# frozen_string_literal: true
#
module CKB
module Types
class Consensus
attr_accessor :id, :genesis_hash, :dao_type_hash, :secp256k1_blake160_sighash_all_type_hash, :secp256k1_blake160_multisig_all_type_hash, :initial_primary_epoch_reward, :secondary_epoch_reward, :max_uncles_num,
:orphan_rate_target, :epoch_duration_target, :tx_proposal_window, :proposer_reward_ratio, :cellbase_maturity, :median_time_block_count, :max_block_cycles, :max_block_bytes, :block_version, :tx_version,
:type_id_code_hash, :max_block_proposals_limit, :primary_epoch_reward_halving_interval, :permanent_difficulty_in_dummy

# @param id [String]
# @param genesis_hash [String]
# @param dao_type_hash [String]
# @param secp256k1_blake160_sighash_all_type_hash [String]
# @param secp256k1_blake160_multisig_all_type_hash [String]
# @param initial_primary_epoch_reward [Integer | String]
# @param secondary_epoch_reward [Integer | String]
# @param max_uncles_num [Integer | String]
# @param orphan_rate_target [CKB::Types::RationalU256]
# @param epoch_duration_target [Integer | String]
# @param tx_proposal_window [CKB::Types::ProposalWindow]
# @param proposer_reward_ratio [CKB::Types::RationalU256]
# @param cellbase_maturity [String]
# @param median_time_block_count [Integer | String]
# @param max_block_cycles [Integer | String]
# @param max_block_bytes [Integer | String]
# @param block_version [Integer | String]
# @param tx_version [Integer | String]
# @param type_id_code_hash [String]
# @param max_block_proposals_limit [Integer | String]
# @param primary_epoch_reward_halving_interval [Integer | String]
# @param permanent_difficulty_in_dummy [Boolean]
def initialize(id:, genesis_hash:, dao_type_hash:, secp256k1_blake160_sighash_all_type_hash:, secp256k1_blake160_multisig_all_type_hash:, initial_primary_epoch_reward:, secondary_epoch_reward:, max_uncles_num:, orphan_rate_target:, epoch_duration_target:,
tx_proposal_window:, proposer_reward_ratio:, cellbase_maturity:, median_time_block_count:, max_block_cycles:, max_block_bytes:, block_version:, tx_version:, type_id_code_hash:, max_block_proposals_limit:, primary_epoch_reward_halving_interval:, permanent_difficulty_in_dummy:)
@id = id
@genesis_hash = genesis_hash
@dao_type_hash = dao_type_hash
@secp256k1_blake160_sighash_all_type_hash = secp256k1_blake160_sighash_all_type_hash
@secp256k1_blake160_multisig_all_type_hash = secp256k1_blake160_multisig_all_type_hash
@initial_primary_epoch_reward = CKB::Utils.to_int(initial_primary_epoch_reward)
@secondary_epoch_reward = CKB::Utils.to_int(secondary_epoch_reward)
@max_uncles_num = CKB::Utils.to_int(max_uncles_num)
@orphan_rate_target = orphan_rate_target
@epoch_duration_target = CKB::Utils.to_int(epoch_duration_target)
@tx_proposal_window = tx_proposal_window
@proposer_reward_ratio = proposer_reward_ratio
@cellbase_maturity = cellbase_maturity
@median_time_block_count = CKB::Utils.to_int(median_time_block_count)
@max_block_cycles = CKB::Utils.to_int(max_block_cycles)
@max_block_bytes = CKB::Utils.to_int(max_block_bytes)
@block_version = CKB::Utils.to_int(block_version)
@tx_version = CKB::Utils.to_int(tx_version)
@type_id_code_hash = type_id_code_hash
@max_block_proposals_limit = CKB::Utils.to_int(max_block_proposals_limit)
@primary_epoch_reward_halving_interval = CKB::Utils.to_int(primary_epoch_reward_halving_interval)
@permanent_difficulty_in_dummy = permanent_difficulty_in_dummy
end

def to_h
{
id: id,
genesis_hash: genesis_hash,
dao_type_hash: dao_type_hash,
secp256k1_blake160_sighash_all_type_hash: secp256k1_blake160_sighash_all_type_hash,
secp256k1_blake160_multisig_all_type_hash: secp256k1_blake160_multisig_all_type_hash,
initial_primary_epoch_reward: CKB::Utils.to_hex(initial_primary_epoch_reward),
secondary_epoch_reward: CKB::Utils.to_hex(secondary_epoch_reward),
max_uncles_num: CKB::Utils.to_hex(max_uncles_num),
orphan_rate_target: orphan_rate_target.to_h,
epoch_duration_target: CKB::Utils.to_hex(epoch_duration_target),
tx_proposal_window: tx_proposal_window.to_h,
proposer_reward_ratio: proposer_reward_ratio.to_h,
cellbase_maturity: cellbase_maturity,
median_time_block_count: CKB::Utils.to_hex(median_time_block_count),
max_block_cycles: CKB::Utils.to_hex(max_block_cycles),
max_block_bytes: CKB::Utils.to_hex(max_block_bytes),
block_version: CKB::Utils.to_hex(block_version),
tx_version: CKB::Utils.to_hex(tx_version),
type_id_code_hash: type_id_code_hash,
max_block_proposals_limit: CKB::Utils.to_hex(max_block_proposals_limit),
primary_epoch_reward_halving_interval: CKB::Utils.to_hex(primary_epoch_reward_halving_interval),
permanent_difficulty_in_dummy: permanent_difficulty_in_dummy
}
end

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

new(
id: hash[:id],
genesis_hash: hash[:genesis_hash],
dao_type_hash: hash[:dao_type_hash],
secp256k1_blake160_sighash_all_type_hash: hash[:secp256k1_blake160_sighash_all_type_hash],
secp256k1_blake160_multisig_all_type_hash: hash[:secp256k1_blake160_multisig_all_type_hash],
initial_primary_epoch_reward: hash[:initial_primary_epoch_reward],
secondary_epoch_reward: hash[:secondary_epoch_reward],
max_uncles_num: hash[:max_uncles_num],
orphan_rate_target: CKB::Types::RationalU256.from_h(hash[:orphan_rate_target]),
epoch_duration_target: hash[:epoch_duration_target],
tx_proposal_window: CKB::Types::ProposalWindow.from_h(hash[:tx_proposal_window]),
proposer_reward_ratio: CKB::Types::RationalU256.from_h(hash[:proposer_reward_ratio]),
cellbase_maturity: hash[:cellbase_maturity],
median_time_block_count: hash[:median_time_block_count],
max_block_cycles: hash[:max_block_cycles],
max_block_bytes: hash[:max_block_bytes],
block_version: hash[:block_version],
tx_version: hash[:tx_version],
type_id_code_hash: hash[:type_id_code_hash],
max_block_proposals_limit: hash[:max_block_proposals_limit],
primary_epoch_reward_halving_interval: hash[:primary_epoch_reward_halving_interval],
permanent_difficulty_in_dummy: hash[:permanent_difficulty_in_dummy]
)
end
end
end
end

32 changes: 32 additions & 0 deletions lib/ckb/types/proposal_window.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module CKB
module Types
class ProposalWindow
attr_accessor :closest, :farthest

# @param closest [Integer | String]
# @param farthest [Integer | String]
def initialize(closest:, farthest:)
@closest = CKB::Utils.to_int(closest)
@farthest = CKB::Utils.to_int(farthest)
end

def to_h
{
closest: CKB::Utils.to_hex(closest),
farthest: CKB::Utils.to_hex(farthest)
}
end

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

new(
closest: hash[:closest],
farthest: hash[:farthest]
)
end
end
end
end
32 changes: 32 additions & 0 deletions lib/ckb/types/rational_u256.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module CKB
module Types
class RationalU256
attr_accessor :denom, :numer

# @param denom [String]
# @param numer [String]
def initialize(denom:, numer:)
@denom = denom
@numer = numer
end

def to_h
{
denom: denom,
numer: numer
}
end

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

new(
denom: hash[:denom],
numer: hash[:numer]
)
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 @@ -48,6 +48,9 @@
require_relative "tx_pool_ids"
require_relative "tx_pool_verbosity"
require_relative "tx_verbosity"
require_relative "consensus"
require_relative "rational_u256"
require_relative "proposal_window"

module CKB
module Types
Expand Down

0 comments on commit d29fe4a

Please sign in to comment.