Skip to content

Commit

Permalink
Merge pull request #690 from nervosnetwork/rc/v0.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda authored Jul 10, 2020
2 parents 1e8373f + e12a5b4 commit 29df590
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 15 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# [v0.10.0](https://github.com/nervosnetwork/ckb-explorer/compare/v0.9.9...v0.10.0) (2020-07-10)


### Features

* [#683](https://github.com/nervosnetwork/ckb-explorer/pull/683): set timeout config on ckb api


### Performance Improvements

* [#687](https://github.com/nervosnetwork/ckb-explorer/pull/687): use DB data replace RPC call on lock info
* [#688](https://github.com/nervosnetwork/ckb-explorer/pull/688): perf transaction index API



# [v0.9.9](https://github.com/nervosnetwork/ckb-explorer/compare/v0.9.8...v0.9.9) (2020-07-09)


Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
GIT
remote: https://github.com/nervosnetwork/ckb-sdk-ruby.git
revision: e0b1c3046331de14175abf0864866947d4e7a941
revision: c9e651c25953c0c6173f8c1fa75e98e546f1164b
branch: develop
specs:
ckb-sdk-ruby (0.33.0)
bitcoin-secp256k1 (~> 0.5.2)
net-http-persistent (~> 3.0.0)
net-http-persistent (~> 3.1.0)
rbnacl (~> 7.1.1)

GIT
Expand Down Expand Up @@ -214,7 +214,7 @@ GEM
metaclass (~> 0.0.1)
msgpack (1.2.10)
multipart-post (2.1.1)
net-http-persistent (3.0.1)
net-http-persistent (3.1.0)
connection_pool (~> 2.2)
newrelic_rpm (6.4.0.356)
nio4r (2.5.2)
Expand Down
18 changes: 13 additions & 5 deletions app/controllers/api/v1/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ class CkbTransactionsController < ApplicationController

def index
if from_home_page?
ckb_transactions = CkbTransaction.recent.normal.limit(ENV["HOMEPAGE_TRANSACTIONS_RECORDS_COUNT"].to_i)
render json: CkbTransactionListSerializer.new(ckb_transactions)
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)
json =
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do
CkbTransactionListSerializer.new(ckb_transactions).serialized_json
end
render json: json
else
ckb_transactions = CkbTransaction.recent.normal.page(@page).per(@page_size)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call
render json: CkbTransactionListSerializer.new(ckb_transactions, options)
ckb_transactions = CkbTransaction.recent.normal.page(@page).per(@page_size).select(:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved)
json =
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call
CkbTransactionListSerializer.new(ckb_transactions, options).serialized_json
end
render json: json
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/ckb_sync/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Api
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

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

METHOD_NAMES.each do |name|
Expand Down
3 changes: 2 additions & 1 deletion app/models/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CkbTransaction < ApplicationRecord
attribute :tx_hash, :ckb_hash
attribute :header_deps, :ckb_array_hash, hash_length: ENV["DEFAULT_HASH_LENGTH"]

scope :recent, -> { order(block_timestamp: :desc) }
scope :recent, -> { order("block_timestamp desc nulls last") }
scope :cellbase, -> { where(is_cellbase: true) }
scope :normal, -> { where(is_cellbase: false) }
scope :created_after, ->(block_timestamp) { where("block_timestamp >= ?", block_timestamp) }
Expand Down Expand Up @@ -151,6 +151,7 @@ def recover_dead_cell
# Indexes
#
# index_ckb_transactions_on_block_id_and_block_timestamp (block_id,block_timestamp)
# index_ckb_transactions_on_block_timestamp (block_timestamp)
# index_ckb_transactions_on_is_cellbase (is_cellbase)
# index_ckb_transactions_on_tx_hash_and_block_id (tx_hash,block_id) UNIQUE
#
14 changes: 12 additions & 2 deletions app/models/lock_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def lock_info
since_value = SinceParser.new(since).parse
return if since_value.blank?

tip_epoch = CkbUtils.parse_epoch(CkbSync::Api.instance.get_tip_header.epoch)
tip_block = Block.recent.first
tip_epoch = tip_epoch(tip_block)

epoch_number, since_value_index = set_since_epoch_number_and_index(since_value)
block_interval = (epoch_number * 1800 + since_value_index * 1800 / since_value.length) - (tip_epoch.number * 1800 + tip_epoch.index * 1800 / tip_epoch.length)

Expand All @@ -35,7 +37,7 @@ def lock_info
block_timestamp = Block.where(number: block.start_number + new_index).pick(:timestamp)
estimated_unlock_time = DateTime.strptime(block_timestamp.to_s, "%Q")
else
tip_block_timestamp = Block.recent.where(epoch: tip_epoch.number).pick(:timestamp)
tip_block_timestamp = tip_block.timestamp
tip_block_time = DateTime.strptime(tip_block_timestamp.to_s, "%Q")
estimated_unlock_time = tip_block_time + (block_interval * 8).seconds
end
Expand Down Expand Up @@ -68,6 +70,14 @@ def lock_info_status(since_value, tip_epoch)

after_lock_epoch_number || at_lock_epoch_number_but_exceeded_index ? "unlocked" : "locked"
end

def tip_epoch(tip_block)
@tip_epoch ||=
begin
tip_epoch_index = tip_block.number - tip_block.start_number
OpenStruct.new(number: tip_block.epoch, index: tip_epoch_index, length: tip_block.length)
end
end
end

# == Schema Information
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddDescendingIndexOnBlockTimestampToCkbTransactions < ActiveRecord::Migration[6.0]
disable_ddl_transaction!

def change
add_index :ckb_transactions, :block_timestamp, order: { block_timestamp: "DESC NULLS LAST" }, algorithm: :concurrently
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_07_03_043629) do
ActiveRecord::Schema.define(version: 2020_07_09_100457) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -182,6 +182,7 @@
t.integer "live_cell_changes"
t.decimal "capacity_involved", precision: 30
t.index ["block_id", "block_timestamp"], name: "index_ckb_transactions_on_block_id_and_block_timestamp"
t.index ["block_timestamp"], name: "index_ckb_transactions_on_block_timestamp", order: "DESC NULLS LAST"
t.index ["is_cellbase"], name: "index_ckb_transactions_on_is_cellbase"
t.index ["tx_hash", "block_id"], name: "index_ckb_transactions_on_tx_hash_and_block_id", unique: true
end
Expand Down
2 changes: 1 addition & 1 deletion test/models/lock_script_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class LockScriptTest < ActiveSupport::TestCase
address = create(:address)
create(:block, number: 107036, start_number: 106327, epoch: 118, timestamp: 1576648516881, length: 796)
lock_script = create(:lock_script, address: address, args: "0x691fdcdc80ca82a4cb15826dcb7f0cf04cd821367600004506080720", code_hash: ENV["SECP_MULTISIG_CELL_TYPE_HASH"])
expected_lock_info = { status: "unlocked", epoch_number: "118", epoch_index: "1605", estimated_unlock_time: "1576648516881" }
expected_lock_info = { status: "locked", epoch_number: "118", epoch_index: "1605", estimated_unlock_time: "1576648532881" }

assert_equal expected_lock_info, lock_script.lock_info
end
Expand Down
2 changes: 1 addition & 1 deletion test/models/null_address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class NullAddressTest < ActiveSupport::TestCase
)
)
null_address = NullAddress.new("ckt1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn323k5v49yzmvm0q0kfqw0hk0kyal6z32nwjvcqqr7qyzq8yqtec2wj")
expected_lock_info = { status: "locked", epoch_number: "51", epoch_index: "764", estimated_unlock_time: "1576212722613" }
expected_lock_info = { status: "locked", epoch_number: "51", epoch_index: "764", estimated_unlock_time: "1576226962613" }

assert_equal expected_lock_info.to_a.sort, null_address.lock_info.to_a.sort
end
Expand Down

0 comments on commit 29df590

Please sign in to comment.