Skip to content

Commit

Permalink
feat: replace difficulty with compact_target
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Sep 29, 2019
1 parent ca6417f commit a88ef6b
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 39 deletions.
12 changes: 6 additions & 6 deletions lib/ckb/types/block_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
module CKB
module Types
class BlockHeader
attr_accessor :difficulty, :hash, :number, :parent_hash, :nonce, :timestamp, :transactions_root, :proposals_hash, \
attr_accessor :compact_target, :hash, :number, :parent_hash, :nonce, :timestamp, :transactions_root, :proposals_hash, \
:uncles_hash, :version, :epoch, :dao

# @param difficulty [String | Integer] integer or hex number
# @param compact_target [String | Integer] integer or hex number
# @param hash [String] 0x...
# @param number [String | Integer] integer or hex number
# @param parent_hash [String] 0x...
Expand All @@ -19,7 +19,7 @@ class BlockHeader
# @param epoch [String | Integer] integer or hex number
# @param dao [String] 0x...
def initialize(
difficulty:,
compact_target:,
hash:,
number:,
parent_hash:,
Expand All @@ -32,7 +32,7 @@ def initialize(
epoch:,
dao:
)
@difficulty = Utils.to_int(difficulty)
@compact_target = Utils.to_int(compact_target)
@hash = hash
@number = Utils.to_int(number)
@parent_hash = parent_hash
Expand All @@ -48,7 +48,7 @@ def initialize(

def to_h
{
difficulty: Utils.to_hex(@difficulty),
compact_target: Utils.to_hex(@compact_target),
hash: @hash,
number: Utils.to_hex(@number),
parent_hash: parent_hash,
Expand All @@ -67,7 +67,7 @@ def self.from_h(hash)
return if hash.nil?

new(
difficulty: hash[:difficulty],
compact_target: hash[:compact_target],
hash: hash[:hash],
number: hash[:number],
parent_hash: hash[:parent_hash],
Expand Down
12 changes: 6 additions & 6 deletions lib/ckb/types/block_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
module CKB
module Types
class BlockTemplate
attr_accessor :version, :difficulty, :current_time, :number, :epoch, :parent_hash, :cycles_limit,
attr_accessor :version, :compact_target, :current_time, :number, :epoch, :parent_hash, :cycles_limit,
:bytes_limit, :uncles_count_limit, :uncles, :transactions, :proposals, :cellbase, :work_id, :dao

# @param version [String | Integer] integer or hex number
# @param difficulty [String | Integer] integer or hex number
# @param compact_target [String | Integer] integer or hex number
# @param current_time [String | Integer] integer or hex number
# @param number [String | Integer] integer or hex number
# @param epoch [String | Integer] integer or hex number
Expand All @@ -23,7 +23,7 @@ class BlockTemplate
# @param dao [String] 0x...
def initialize(
version:,
difficulty:,
compact_target:,
current_time:,
number:,
epoch:,
Expand All @@ -39,7 +39,7 @@ def initialize(
dao:
)
@version = Utils.to_int(version)
@difficulty = Utils.to_int(difficulty)
@compact_target = Utils.to_int(compact_target)
@current_time = Utils.to_int(current_time)
@number = Utils.to_int(number)
@epoch = Utils.to_int(epoch)
Expand All @@ -58,7 +58,7 @@ def initialize(
def to_h
{
version: Utils.to_hex(version),
difficulty: Utils.to_hex(difficulty),
compact_target: Utils.to_hex(compact_target),
current_time: Utils.to_hex(current_time),
number: Utils.to_hex(number),
epoch: Utils.to_hex(epoch),
Expand All @@ -80,7 +80,7 @@ def self.from_h(hash)

new(
version: hash[:version],
difficulty: hash[:difficulty],
compact_target: hash[:compact_target],
current_time: hash[:current_time],
number: hash[:number],
epoch: hash[:epoch],
Expand Down
12 changes: 6 additions & 6 deletions lib/ckb/types/epoch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
module CKB
module Types
class Epoch
attr_accessor :difficulty, :length, :number, :start_number
attr_accessor :compact_target, :length, :number, :start_number

# @param difficulty [String | Integer] integer or hex number
# @param compact_target [String | Integer] integer or hex number
# @param length [String | Integer] integer or hex number
# @param number [String | Integer] integer or hex number
# @param start_number [String | Integer] integer or hex number
def initialize(
difficulty:,
compact_target:,
length:,
number:,
start_number:
)
@difficulty = Utils.to_int(difficulty)
@compact_target = Utils.to_int(compact_target)
@length = Utils.to_int(length)
@number = Utils.to_int(number)
@start_number = Utils.to_int(start_number)
end

def to_h
{
difficulty: Utils.to_hex(@difficulty),
compact_target: Utils.to_hex(@compact_target),
length: Utils.to_hex(@length),
number: Utils.to_hex(@number),
start_number: Utils.to_hex(@start_number)
Expand All @@ -34,7 +34,7 @@ def self.from_h(hash)
return if hash.nil?

new(
difficulty: hash[:difficulty],
compact_target: hash[:compact_target],
length: hash[:length],
number: hash[:number],
start_number: hash[:start_number]
Expand Down
12 changes: 6 additions & 6 deletions spec/ckb/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
outputs_data: ["0x"],
witnesses: ["0x490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce801140000003954acece65096bfa81258983ddb83915fc56bd8"],
hash: "0x249ba6cc038ccb16a18b1da03b994f87a6e32bf128b553a66a5805e9d6f36c50" }],
header: { difficulty: "0x100",
hash: "0x358df0eb01609fe8631a8d666ab4fd3ffac4025b46f2fbafeb92783181e6ea80",
header: { compact_target: "0x20010000",
hash: "0x22946a72d17b745d950ae1b67326d77f708ba5c0411cbf424c7f4df04c1ac317",
number: "0x1",
parent_hash: "0xe10b8035540bf0976aa991dbcc1dfb2237a81706e6848596aca8773a69efb85c",
nonce: "0x58df949326a72a42",
timestamp: "0x16d70d9f580",
transactions_root: "0x249ba6cc038ccb16a18b1da03b994f87a6e32bf128b553a66a5805e9d6f36c50",
parent_hash: "0x70396940ae2e81bd2627a8e0e75f3d277585bb1afd78839cfd8f2c54e8697bbc",
nonce: "0x3a7937c78d7ae069",
timestamp: "0x16d7ad5d9de",
transactions_root: "0xfcf1ec1c5da7f80b23ad81e72885d381ee04ad58c79003100d38ad285ae44959",
proposals_hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
uncles_hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
version: "0x0",
Expand Down
13 changes: 7 additions & 6 deletions spec/ckb/rpc_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

RSpec.describe CKB::RPC do
before do
skip "not test rpc" if ENV["SKIP_RPC_TESTS"]
Expand All @@ -17,13 +18,13 @@
hash_type: "type" },
type: nil }],
outputs_data: ["0x"],
witnesses: ["0x490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce801140000003954acece65096bfa81258983ddb83915fc56bd8"]}],
header: { difficulty: "0x100",
witnesses: ["0x490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce801140000003954acece65096bfa81258983ddb83915fc56bd8"] }],
header: { compact_target: "0x20010000",
number: "0x1",
parent_hash: "0xe10b8035540bf0976aa991dbcc1dfb2237a81706e6848596aca8773a69efb85c",
nonce: "0x58df949326a72a42",
timestamp: "0x16d70d9f580",
transactions_root: "0x249ba6cc038ccb16a18b1da03b994f87a6e32bf128b553a66a5805e9d6f36c50",
parent_hash: "0x70396940ae2e81bd2627a8e0e75f3d277585bb1afd78839cfd8f2c54e8697bbc",
nonce: "0x3a7937c78d7ae069",
timestamp: "0x16d7ad5d9de",
transactions_root: "0xfcf1ec1c5da7f80b23ad81e72885d381ee04ad58c79003100d38ad285ae44959",
proposals_hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
uncles_hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
version: "0x0",
Expand Down
6 changes: 3 additions & 3 deletions spec/ckb/types/block_header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
let(:block_header_h) do
{
"dao": "0x0100000000000000005827f2ba13b000d77fa3d595aa00000061eb7ada030000",
"difficulty": "0x7a1200",
"compact_target": "0x7a1200",
"epoch": "0x1",
"hash": "0xd629a10a08fb0f43fcb97e948fc2b6eb70ebd28536490fe3864b0e40d08397d1",
"nonce": "0x0",
Expand All @@ -20,7 +20,7 @@

it "from_h" do
expect(block_header).to be_a(CKB::Types::BlockHeader)
number_keys = %i(difficulty epoch nonce number timestamp version)
number_keys = %i(compact_target epoch nonce number timestamp version)
number_keys.each do |key|
expect(block_header.public_send(key)).to eq block_header_h[key].hex
end
Expand All @@ -40,7 +40,7 @@

api = CKB::API.new
tip_header = api.get_tip_header
expected_attributes = %w(dao difficulty epoch hash nonce number parent_hash proposals_hash timestamp transactions_root uncles_hash version)
expected_attributes = %w(dao compact_target epoch hash nonce number parent_hash proposals_hash timestamp transactions_root uncles_hash version).sort

expect(expected_attributes).to eq(tip_header.instance_variables.map { |attribute| attribute.to_s.gsub("@", "") }.sort)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ckb/types/block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"header": {
"dao": "0x0100000000000000005827f2ba13b000d77fa3d595aa00000061eb7ada030000",
"difficulty": "0x7a1200",
"compact_target": "0x7a1200",
"epoch": "0x1",
"hash": "0xd629a10a08fb0f43fcb97e948fc2b6eb70ebd28536490fe3864b0e40d08397d1",
"nonce": "0x0",
Expand Down
6 changes: 3 additions & 3 deletions spec/ckb/types/block_template_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.describe CKB::Types::BlockTemplate do
let(:block_template_h) do
{:version=>"0x0",
:difficulty=>"0x48",
:compact_target=>"0x48",
:current_time=>"0x16d6b6111b3",
:number=>"0xab3",
:epoch=>"0x70806cb000001",
Expand Down Expand Up @@ -46,7 +46,7 @@
end

it "block_template's attributes value should equal with block_template_h" do
number_keys = %i(version difficulty current_time number epoch parent_hash cycles_limit bytes_limit uncles_count_limit uncles transactions proposals cellbase work_id dao)
number_keys = %i(version compact_target current_time number epoch parent_hash cycles_limit bytes_limit uncles_count_limit uncles transactions proposals cellbase work_id dao)
number_keys.each do |key|
expect(block_template.to_h[key]).to eq block_template_h[key]
end
Expand All @@ -63,7 +63,7 @@

api = CKB::API.new
block_template = api.get_block_template
expected_attributes = %w(version difficulty current_time number epoch parent_hash cycles_limit bytes_limit uncles_count_limit uncles transactions proposals cellbase work_id dao).sort
expected_attributes = %w(version compact_target current_time number epoch parent_hash cycles_limit bytes_limit uncles_count_limit uncles transactions proposals cellbase work_id dao).sort

expect(expected_attributes).to eq(block_template.instance_variables.map { |attribute| attribute.to_s.gsub("@", "") }.sort)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ckb/types/epoch_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.describe CKB::Types::Epoch do
let(:epoch_h) do
{
"difficulty": "0x7a1200",
"compact_target": "0x7a1200",
"length": "0x708",
"number": "0x1",
"start_number": "0x3e8"
Expand Down
2 changes: 1 addition & 1 deletion spec/ckb/types/uncle_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:proposals=>[],
:header => {
:dao=>"0x0100000000000000005827f2ba13b000d77fa3d595aa00000061eb7ada030000",
:difficulty=>"0x7a1200",
:compact_target=>"0x7a1200",
:epoch=>"0x1",
:hash=>"0xd629a10a08fb0f43fcb97e948fc2b6eb70ebd28536490fe3864b0e40d08397d1",
:nonce=>"0x0",
Expand Down

0 comments on commit a88ef6b

Please sign in to comment.