Skip to content

Commit 035d43e

Browse files
committed
feat: add epoch type and add get_current_epoch RPC
1 parent bd34baa commit 035d43e

File tree

6 files changed

+83
-4
lines changed

6 files changed

+83
-4
lines changed

lib/ckb/api.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ def get_transaction_trace(hash)
134134
rpc.get_transaction_trace(hash)
135135
end
136136

137+
# @return [CKB::Types::Epoch]
138+
def get_current_epoch
139+
Types::Epoch.from_h(rpc.get_current_epoch)
140+
end
141+
137142
def inspect
138143
"\#<API@#{uri}>"
139144
end

lib/ckb/rpc.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def get_transaction_trace(hash)
7474
rpc_request("get_transaction_trace", params: [hash])
7575
end
7676

77+
def get_current_epoch
78+
rpc_request("get_current_epoch")
79+
end
80+
7781
def inspect
7882
"\#<RPC@#{uri}>"
7983
end

lib/ckb/types/block_header.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def self.from_h(hash)
2828
end
2929

3030
class BlockHeader
31-
attr_reader :difficulty, :hash, :number, :parent_hash, :seal, :timestamp, :transactions_root, :proposals_root, :uncles_count, :uncles_hash, :version, :witness_root
31+
attr_reader :difficulty, :hash, :number, :parent_hash, :seal, :timestamp, :transactions_root, :proposals_root, \
32+
:uncles_count, :uncles_hash, :version, :witness_root, :epoch
3233

3334
def initialize(
3435
difficulty:,
@@ -42,7 +43,8 @@ def initialize(
4243
uncles_count:,
4344
uncles_hash:,
4445
version:,
45-
witness_root:
46+
witness_root:,
47+
epoch:
4648
)
4749
@difficulty = difficulty
4850
@hash = hash
@@ -56,6 +58,7 @@ def initialize(
5658
@uncles_hash = uncles_hash
5759
@version = version
5860
@witness_root = witness_root
61+
@epoch = epoch
5962
end
6063

6164
def to_h
@@ -71,7 +74,8 @@ def to_h
7174
uncles_count: @uncles_count.to_i,
7275
uncles_hash: @uncles_hash,
7376
version: @version,
74-
witness_root: @witness_root
77+
witness_root: @witness_root,
78+
epoch: @epoch
7579
}
7680
end
7781

@@ -88,7 +92,8 @@ def self.from_h(hash)
8892
uncles_count: hash[:uncles_count],
8993
uncles_hash: hash[:uncles_hash],
9094
version: hash[:version],
91-
witness_root: hash[:witnesses_root]
95+
witness_root: hash[:witnesses_root],
96+
epoch: hash[:epoch]
9297
)
9398
end
9499
end

lib/ckb/types/epoch.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
module CKB
4+
module Types
5+
class Epoch
6+
attr_reader :block_reward, :difficulty, :last_block_hash_in_previous_epoch, :length, :number, :remainder_reward, :start_number
7+
8+
# @param block_reward [String] number
9+
# @param difficulty [String] 0x...
10+
# @param last_block_hash_in_previous_epoch [String] 0x...
11+
# @param length [String] number
12+
# @param number [String] number
13+
# @param remainer_reward [String] number
14+
# @param start_number [Sring] number
15+
def initialize(
16+
block_reward:,
17+
difficulty:,
18+
last_block_hash_in_previous_epoch:,
19+
length:,
20+
number:,
21+
remainder_reward:,
22+
start_number:
23+
)
24+
@block_reward = block_reward
25+
@difficulty = difficulty
26+
@last_block_hash_in_previous_epoch = last_block_hash_in_previous_epoch
27+
@length = length
28+
@number = number
29+
@remainder_reward = remainder_reward
30+
@start_number = start_number
31+
end
32+
33+
def to_h
34+
{
35+
block_reward: @block_reward,
36+
difficulty: @difficulty,
37+
last_block_hash_in_previous_epoch: @last_block_hash_in_previous_epoch,
38+
length: @length,
39+
number: @number,
40+
remainder_reward: @remainder_reward,
41+
start_number: @start_number
42+
}
43+
end
44+
45+
def self.from_h(hash)
46+
new(
47+
block_reward: hash[:block_reward],
48+
difficulty: hash[:difficulty],
49+
last_block_hash_in_previous_epoch: hash[:last_block_hash_in_previous_epoch],
50+
length: hash[:length],
51+
number: hash[:number],
52+
remainder_reward: hash[:remainder_reward],
53+
start_number: hash[:start_number]
54+
)
55+
end
56+
end
57+
end
58+
end

lib/ckb/types/types.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require_relative "transaction"
1212
require_relative "tx_status"
1313
require_relative "witness"
14+
require_relative "epoch"
1415

1516
module CKB
1617
module Types

spec/ckb/api_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@
7070
api.send_transaction(tx)
7171
}.to raise_error(CKB::RPCError, /:code=>-3/)
7272
end
73+
74+
it "get current epoch" do
75+
result = api.get_current_epoch
76+
expect(result).not_to be nil
77+
expect(result).to be_a(Types::Epoch)
78+
end
7379
end

0 commit comments

Comments
 (0)