Skip to content

Commit 29df590

Browse files
authored
Merge pull request #690 from nervosnetwork/rc/v0.10.0
2 parents 1e8373f + e12a5b4 commit 29df590

File tree

10 files changed

+57
-15
lines changed

10 files changed

+57
-15
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# [v0.10.0](https://github.com/nervosnetwork/ckb-explorer/compare/v0.9.9...v0.10.0) (2020-07-10)
2+
3+
4+
### Features
5+
6+
* [#683](https://github.com/nervosnetwork/ckb-explorer/pull/683): set timeout config on ckb api
7+
8+
9+
### Performance Improvements
10+
11+
* [#687](https://github.com/nervosnetwork/ckb-explorer/pull/687): use DB data replace RPC call on lock info
12+
* [#688](https://github.com/nervosnetwork/ckb-explorer/pull/688): perf transaction index API
13+
14+
15+
116
# [v0.9.9](https://github.com/nervosnetwork/ckb-explorer/compare/v0.9.8...v0.9.9) (2020-07-09)
217

318

Gemfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
GIT
22
remote: https://github.com/nervosnetwork/ckb-sdk-ruby.git
3-
revision: e0b1c3046331de14175abf0864866947d4e7a941
3+
revision: c9e651c25953c0c6173f8c1fa75e98e546f1164b
44
branch: develop
55
specs:
66
ckb-sdk-ruby (0.33.0)
77
bitcoin-secp256k1 (~> 0.5.2)
8-
net-http-persistent (~> 3.0.0)
8+
net-http-persistent (~> 3.1.0)
99
rbnacl (~> 7.1.1)
1010

1111
GIT
@@ -214,7 +214,7 @@ GEM
214214
metaclass (~> 0.0.1)
215215
msgpack (1.2.10)
216216
multipart-post (2.1.1)
217-
net-http-persistent (3.0.1)
217+
net-http-persistent (3.1.0)
218218
connection_pool (~> 2.2)
219219
newrelic_rpm (6.4.0.356)
220220
nio4r (2.5.2)

app/controllers/api/v1/ckb_transactions_controller.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@ class CkbTransactionsController < ApplicationController
66

77
def index
88
if from_home_page?
9-
ckb_transactions = CkbTransaction.recent.normal.limit(ENV["HOMEPAGE_TRANSACTIONS_RECORDS_COUNT"].to_i)
10-
render json: CkbTransactionListSerializer.new(ckb_transactions)
9+
ckb_transactions = CkbTransaction.recent.normal.limit(ENV["HOMEPAGE_TRANSACTIONS_RECORDS_COUNT"].to_i).select(:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved)
10+
json =
11+
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do
12+
CkbTransactionListSerializer.new(ckb_transactions).serialized_json
13+
end
14+
render json: json
1115
else
12-
ckb_transactions = CkbTransaction.recent.normal.page(@page).per(@page_size)
13-
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call
14-
render json: CkbTransactionListSerializer.new(ckb_transactions, options)
16+
ckb_transactions = CkbTransaction.recent.normal.page(@page).per(@page_size).select(:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved)
17+
json =
18+
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do
19+
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call
20+
CkbTransactionListSerializer.new(ckb_transactions, options).serialized_json
21+
end
22+
render json: json
1523
end
1624
end
1725

app/models/ckb_sync/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Api
55
METHOD_NAMES = %w(system_script_out_point dry_run_transaction set_system_script_cell system_script_cell system_script_cell_hash genesis_block get_block_by_number genesis_block_hash get_block_hash get_block get_tip_header get_tip_block_number get_cells_by_lock_hash get_transaction get_live_cell local_node_info get_current_epoch get_epoch_by_number get_peers tx_pool_info get_blockchain_info get_peers_state compute_transaction_hash get_cellbase_output_capacity_details calculate_dao_maximum_withdraw compute_script_hash get_block_economic_state).freeze
66

77
def initialize
8-
@api = CKB::API.new(host: ENV["CKB_NODE_URL"])
8+
@api = CKB::API.new(host: ENV["CKB_NODE_URL"], timeout_config: { open_timeout: 1, read_timeout: 3, write_timeout: 1 })
99
end
1010

1111
METHOD_NAMES.each do |name|

app/models/ckb_transaction.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CkbTransaction < ApplicationRecord
1515
attribute :tx_hash, :ckb_hash
1616
attribute :header_deps, :ckb_array_hash, hash_length: ENV["DEFAULT_HASH_LENGTH"]
1717

18-
scope :recent, -> { order(block_timestamp: :desc) }
18+
scope :recent, -> { order("block_timestamp desc nulls last") }
1919
scope :cellbase, -> { where(is_cellbase: true) }
2020
scope :normal, -> { where(is_cellbase: false) }
2121
scope :created_after, ->(block_timestamp) { where("block_timestamp >= ?", block_timestamp) }
@@ -151,6 +151,7 @@ def recover_dead_cell
151151
# Indexes
152152
#
153153
# index_ckb_transactions_on_block_id_and_block_timestamp (block_id,block_timestamp)
154+
# index_ckb_transactions_on_block_timestamp (block_timestamp)
154155
# index_ckb_transactions_on_is_cellbase (is_cellbase)
155156
# index_ckb_transactions_on_tx_hash_and_block_id (tx_hash,block_id) UNIQUE
156157
#

app/models/lock_script.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ def lock_info
2525
since_value = SinceParser.new(since).parse
2626
return if since_value.blank?
2727

28-
tip_epoch = CkbUtils.parse_epoch(CkbSync::Api.instance.get_tip_header.epoch)
28+
tip_block = Block.recent.first
29+
tip_epoch = tip_epoch(tip_block)
30+
2931
epoch_number, since_value_index = set_since_epoch_number_and_index(since_value)
3032
block_interval = (epoch_number * 1800 + since_value_index * 1800 / since_value.length) - (tip_epoch.number * 1800 + tip_epoch.index * 1800 / tip_epoch.length)
3133

@@ -35,7 +37,7 @@ def lock_info
3537
block_timestamp = Block.where(number: block.start_number + new_index).pick(:timestamp)
3638
estimated_unlock_time = DateTime.strptime(block_timestamp.to_s, "%Q")
3739
else
38-
tip_block_timestamp = Block.recent.where(epoch: tip_epoch.number).pick(:timestamp)
40+
tip_block_timestamp = tip_block.timestamp
3941
tip_block_time = DateTime.strptime(tip_block_timestamp.to_s, "%Q")
4042
estimated_unlock_time = tip_block_time + (block_interval * 8).seconds
4143
end
@@ -68,6 +70,14 @@ def lock_info_status(since_value, tip_epoch)
6870

6971
after_lock_epoch_number || at_lock_epoch_number_but_exceeded_index ? "unlocked" : "locked"
7072
end
73+
74+
def tip_epoch(tip_block)
75+
@tip_epoch ||=
76+
begin
77+
tip_epoch_index = tip_block.number - tip_block.start_number
78+
OpenStruct.new(number: tip_block.epoch, index: tip_epoch_index, length: tip_block.length)
79+
end
80+
end
7181
end
7282

7383
# == Schema Information
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddDescendingIndexOnBlockTimestampToCkbTransactions < ActiveRecord::Migration[6.0]
2+
disable_ddl_transaction!
3+
4+
def change
5+
add_index :ckb_transactions, :block_timestamp, order: { block_timestamp: "DESC NULLS LAST" }, algorithm: :concurrently
6+
end
7+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_07_03_043629) do
13+
ActiveRecord::Schema.define(version: 2020_07_09_100457) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -182,6 +182,7 @@
182182
t.integer "live_cell_changes"
183183
t.decimal "capacity_involved", precision: 30
184184
t.index ["block_id", "block_timestamp"], name: "index_ckb_transactions_on_block_id_and_block_timestamp"
185+
t.index ["block_timestamp"], name: "index_ckb_transactions_on_block_timestamp", order: "DESC NULLS LAST"
185186
t.index ["is_cellbase"], name: "index_ckb_transactions_on_is_cellbase"
186187
t.index ["tx_hash", "block_id"], name: "index_ckb_transactions_on_tx_hash_and_block_id", unique: true
187188
end

test/models/lock_script_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class LockScriptTest < ActiveSupport::TestCase
9898
address = create(:address)
9999
create(:block, number: 107036, start_number: 106327, epoch: 118, timestamp: 1576648516881, length: 796)
100100
lock_script = create(:lock_script, address: address, args: "0x691fdcdc80ca82a4cb15826dcb7f0cf04cd821367600004506080720", code_hash: ENV["SECP_MULTISIG_CELL_TYPE_HASH"])
101-
expected_lock_info = { status: "unlocked", epoch_number: "118", epoch_index: "1605", estimated_unlock_time: "1576648516881" }
101+
expected_lock_info = { status: "locked", epoch_number: "118", epoch_index: "1605", estimated_unlock_time: "1576648532881" }
102102

103103
assert_equal expected_lock_info, lock_script.lock_info
104104
end

test/models/null_address_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class NullAddressTest < ActiveSupport::TestCase
8282
)
8383
)
8484
null_address = NullAddress.new("ckt1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn323k5v49yzmvm0q0kfqw0hk0kyal6z32nwjvcqqr7qyzq8yqtec2wj")
85-
expected_lock_info = { status: "locked", epoch_number: "51", epoch_index: "764", estimated_unlock_time: "1576212722613" }
85+
expected_lock_info = { status: "locked", epoch_number: "51", epoch_index: "764", estimated_unlock_time: "1576226962613" }
8686

8787
assert_equal expected_lock_info.to_a.sort, null_address.lock_info.to_a.sort
8888
end

0 commit comments

Comments
 (0)