Skip to content

Commit

Permalink
Merge pull request #696 from nervosnetwork/rc/v0.10.1
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda authored Jul 17, 2020
2 parents 29df590 + 21f451c commit 313c38e
Show file tree
Hide file tree
Showing 35 changed files with 205 additions and 140 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# [v0.10.1](https://github.com/shaojunda/ckb-explorer/compare/v0.10.0...v0.10.1) (2020-07-17)


### Bug Fixes

* [#695](https://github.com/nervosnetwork/ckb-explorer/pull/695): fix average block time missing data


### Performance Improvements

* [#683](https://github.com/nervosnetwork/ckb-explorer/pull/683): perf lock info
* [#687](https://github.com/nervosnetwork/ckb-explorer/pull/687): perf use DB data replace RPC call
* [#688](https://github.com/nervosnetwork/ckb-explorer/pull/688): perf transaction index
* [#691](https://github.com/nervosnetwork/ckb-explorer/pull/691): perf address unclaimed compensation worker
* [#692](https://github.com/nervosnetwork/ckb-explorer/pull/692): perf add cache on lock_script
* [#693](https://github.com/nervosnetwork/ckb-explorer/pull/693): perf use cache on blocks controller
* [#694](https://github.com/nervosnetwork/ckb-explorer/pull/694): perf contract show


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


Expand Down
15 changes: 7 additions & 8 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gem "rails", "~> 6.0.0"
# Use postgresql as the database for Active Record
gem "pg", ">= 0.18", "< 2.0"
# Use Puma as the app server
gem "puma", "~> 4.3.5"
gem "puma", "~> 4.3.5", require: false
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
Expand All @@ -34,9 +34,6 @@ gem "dotenv-rails"
# manage environment specific settings by config
gem "config"

# daemons
gem "daemons", "~> 1.2", ">= 1.2.6"

# CKB SDK
gem "ckb-sdk-ruby", git: "https://github.com/nervosnetwork/ckb-sdk-ruby.git", require: "ckb", branch: "develop"

Expand Down Expand Up @@ -64,12 +61,14 @@ gem "kaminari"

gem "ruby-progressbar", require: false

gem "sentry-raven"
group :production do
gem "sentry-raven"
gem "newrelic_rpm"
end

# Deployment
gem "mina", require: false
gem "mina-multistage", require: false
gem "newrelic_rpm"
gem "rack-attack"

group :development, :test do
Expand All @@ -88,7 +87,7 @@ group :test do
gem "database_cleaner"
gem "mocha"
gem "factory_bot_rails"
gem "faker", git: "https://github.com/stympy/faker.git", branch: "master"
gem "faker", git: "https://github.com/faker-ruby/faker.git", branch: "master"
gem "codecov", require: false
end

Expand All @@ -100,7 +99,7 @@ group :development do
gem "rubocop", require: false
gem "rubocop-rails", require: false
gem "rubocop-performance", require: false
gem "awesome_print"
gem "awesome_print", require: false
gem "annotate"
end

Expand Down
45 changes: 10 additions & 35 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
GIT
remote: https://github.com/faker-ruby/faker.git
revision: 484f776b9e734eea24114f2c46d9dad9622f0f4f
branch: master
specs:
faker (2.13.0)
i18n (>= 1.6, < 2)

GIT
remote: https://github.com/nervosnetwork/ckb-sdk-ruby.git
revision: c9e651c25953c0c6173f8c1fa75e98e546f1164b
Expand All @@ -8,19 +16,6 @@ GIT
net-http-persistent (~> 3.1.0)
rbnacl (~> 7.1.1)

GIT
remote: https://github.com/stympy/faker.git
revision: 0e8cc7eeb160532c84d8665aef649788e48a2c9c
branch: master
specs:
faker (1.9.3)
i18n (>= 0.7)
pastel (~> 0.7.2)
thor (~> 0.20.0)
tty-pager (~> 0.12.0)
tty-screen (~> 0.6.5)
tty-tree (~> 0.3.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -91,7 +86,7 @@ GEM
awesome_print (1.8.0)
bitcoin-secp256k1 (0.5.2)
ffi (>= 1.9.25)
bootsnap (1.4.4)
bootsnap (1.4.6)
msgpack (~> 1.0)
builder (3.2.4)
chronic_duration (0.10.6)
Expand All @@ -110,7 +105,6 @@ GEM
crack (0.4.3)
safe_yaml (~> 1.0.0)
crass (1.0.6)
daemons (1.3.1)
database_cleaner (1.7.0)
deep_merge (1.2.1)
docile (1.3.2)
Expand Down Expand Up @@ -146,7 +140,6 @@ GEM
dry-equalizer (~> 0.2)
dry-logic (~> 0.5, >= 0.5.0)
dry-types (~> 0.14.0)
equatable (0.5.0)
erubi (1.9.0)
et-orbi (1.2.2)
tzinfo
Expand Down Expand Up @@ -212,7 +205,7 @@ GEM
ruby-progressbar
mocha (1.8.0)
metaclass (~> 0.0.1)
msgpack (1.2.10)
msgpack (1.3.3)
multipart-post (2.1.1)
net-http-persistent (3.1.0)
connection_pool (~> 2.2)
Expand All @@ -225,9 +218,6 @@ GEM
parallel (1.17.0)
parser (2.6.3.0)
ast (~> 2.4.0)
pastel (0.7.2)
equatable (~> 0.5.0)
tty-color (~> 0.4.0)
pg (1.1.4)
pry (0.12.2)
coderay (~> 1.1.0)
Expand Down Expand Up @@ -338,26 +328,12 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
strings (0.1.5)
strings-ansi (~> 0.1)
unicode-display_width (~> 1.5)
unicode_utils (~> 1.4)
strings-ansi (0.1.0)
thor (0.20.3)
thread_safe (0.3.6)
tilt (2.0.9)
tty-color (0.4.3)
tty-pager (0.12.1)
strings (~> 0.1.4)
tty-screen (~> 0.6)
tty-which (~> 0.4)
tty-screen (0.6.5)
tty-tree (0.3.0)
tty-which (0.4.1)
tzinfo (1.2.7)
thread_safe (~> 0.1)
unicode-display_width (1.6.0)
unicode_utils (1.4.0)
url (0.3.2)
vcr (5.0.0)
webmock (3.6.0)
Expand All @@ -380,7 +356,6 @@ DEPENDENCIES
ckb-sdk-ruby!
codecov
config
daemons (~> 1.2, >= 1.2.6)
database_cleaner
dotenv-rails
factory_bot_rails
Expand Down
19 changes: 12 additions & 7 deletions app/controllers/api/v1/blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ class BlocksController < ApplicationController

def index
if from_home_page?
block_timestamps = Block.recent.limit(ENV["HOMEPAGE_BLOCK_RECORDS_COUNT"].to_i).pluck(:timestamp)
blocks = Block.where(timestamp: block_timestamps).select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes).recent
options = {}
blocks = Block.recent.limit(ENV["HOMEPAGE_BLOCK_RECORDS_COUNT"].to_i).select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes)
json =
Rails.cache.realize(blocks.cache_key, version: blocks.cache_version, race_condition_ttl: 3.seconds) do
BlockListSerializer.new(blocks).serialized_json
end
else
block_timestamps = Block.recent.select(:timestamp).page(@page).per(@page_size)
blocks = Block.where(timestamp: block_timestamps.pluck(:timestamp)).select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes).recent
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: block_timestamps, page: @page, page_size: @page_size).call
blocks = Block.recent.select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes).page(@page).per(@page_size)
json =
Rails.cache.realize(blocks.cache_key, version: blocks.cache_version, race_condition_ttl: 3.seconds) do
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: blocks, page: @page, page_size: @page_size).call
BlockListSerializer.new(blocks, options).serialized_json
end
end

render json: BlockListSerializer.new(blocks, options)
render json: json
end

def show
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def index
if from_home_page?
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
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version, race_condition_ttl: 3.seconds) do
CkbTransactionListSerializer.new(ckb_transactions).serialized_json
end
render json: json
else
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
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version, race_condition_ttl: 3.seconds) 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
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/api/v1/contracts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ class ContractsController < ApplicationController
def show
raise Api::V1::Exceptions::ContractNotFoundError if params[:id] != DaoContract::CONTRACT_NAME

render json: DaoContractSerializer.new(DaoContract.default_contract)
dao_contract = DaoContract.where(id: 1)
json =
Rails.cache.realize(dao_contract.cache_key, version: dao_contract.cache_version, race_condition_ttl: 3.seconds) do
DaoContractSerializer.new(dao_contract.first).serialized_json
end

render json: json
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/dao_depositors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Api
module V1
class DaoDepositorsController < ApplicationController
def index
addresses = Address.select(:id, :address_hash, :dao_deposit, :average_deposit_time).where("dao_deposit > 0").order(dao_deposit: :desc).limit(100)
addresses = Address.select(:id, :address_hash, :dao_deposit, :average_deposit_time).where(is_depositor: true).order(dao_deposit: :desc).limit(100)

render json: DaoDepositorSerializer.new(addresses)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def catch_404
private

def set_raven_context
Raven.extra_context(params: params.to_unsafe_h, url: request.url)
Raven.extra_context(params: params.to_unsafe_h, url: request.url) if Rails.env.production?
end

def api_error(error)
Expand Down
9 changes: 4 additions & 5 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def lock_info
end

def lock_script
LockScript.where(address: self).first
Rails.cache.realize(["Address", "lock_script", id], race_condition_ttl: 3.seconds) do
LockScript.where(address: self).first
end
end

def self.find_or_create_address(lock_script, block_timestamp)
Expand Down Expand Up @@ -133,10 +135,7 @@ def phase1_dao_interests
def unmade_dao_interests
tip_dao = Block.recent.first.dao
cell_outputs.nervos_dao_deposit.live.find_each.reduce(0) do |memo, cell_output|
dao = cell_output.block.dao
parse_dao = CkbUtils.parse_dao(dao)
tip_parse_dao = CkbUtils.parse_dao(tip_dao)
memo + (cell_output.capacity - cell_output.occupied_capacity).to_i * tip_parse_dao.ar_i / parse_dao.ar_i - (cell_output.capacity - cell_output.occupied_capacity)
memo + DaoCompensationCalculator.new(cell_output, tip_dao).call
end
end
end
Expand Down
1 change: 1 addition & 0 deletions app/models/cell_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def flush_cache
# consumed_block_timestamp :decimal(30, )
# type_hash :string
# udt_amount :decimal(40, )
# dao :string
#
# Indexes
#
Expand Down
4 changes: 3 additions & 1 deletion app/models/ckb_sync/node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def process_take_away_all_deposit(dao_contract, dao_events)
take_away_all_deposit_dao_events = dao_events.where(event_type: "take_away_all_deposit")
take_away_all_deposit_dao_events.each do |event|
dao_contract.decrement!(:depositors_count)
event.address.update(is_depositor: false)
event.processed!
end
end
Expand Down Expand Up @@ -514,7 +515,8 @@ def build_cell_output(ckb_transaction, output, address, cell_index, output_data)
generated_by: ckb_transaction,
cell_type: cell_type(output.type, output_data),
block_timestamp: ckb_transaction.block_timestamp,
type_hash: output.type&.compute_hash
type_hash: output.type&.compute_hash,
dao: ckb_transaction.block.dao
)

cell_output.udt_amount = CkbUtils.parse_udt_cell_data(output_data) if cell_output.udt?
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/address_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class AddressSerializer
include FastJsonapi::ObjectSerializer

attributes :address_hash, :lock_script, :lock_info
attributes :address_hash, :lock_info
attribute :balance do |object|
object.balance.to_s
end
Expand Down
10 changes: 4 additions & 6 deletions app/services/charts/daily_statistic_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def avg_block_time_rolling_by_hour_sql
avg(avg_block_time_per_hour) over(order by stat_timestamp rows between 7 * 24 preceding and current row) as avg_bt1
from block_time_statistics
)
select stat_timestamp, round(avg_bt, 2) avg_bt1, round(avg_bt1, 2) avg_bt2 from avg_block_time_24_hours_rolling_by_hour where stat_timestamp >= #{35.days.ago.to_i} order by stat_timestamp limit 720
-- 804 = 24 * 35, show data for the last 35 days
select stat_timestamp, round(avg_bt, 2) avg_bt1, round(avg_bt1, 2) avg_bt2 from avg_block_time_24_hours_rolling_by_hour where stat_timestamp >= #{35.days.ago.end_of_day.to_i} order by stat_timestamp limit 840
SQL
end

Expand Down Expand Up @@ -318,12 +319,9 @@ def phase1_dao_interests
def unmade_dao_interests
@unmade_dao_interests ||=
begin
tip_dao = current_tip_block.dao
CellOutput.nervos_dao_deposit.generated_before(ended_at).unconsumed_at(ended_at).reduce(0) do |memo, cell_output|
dao = cell_output.block.dao
tip_dao = current_tip_block.dao
parse_dao = CkbUtils.parse_dao(dao)
tip_parse_dao = CkbUtils.parse_dao(tip_dao)
memo + (cell_output.capacity - cell_output.occupied_capacity).to_i * tip_parse_dao.ar_i / parse_dao.ar_i - (cell_output.capacity - cell_output.occupied_capacity)
memo + DaoCompensationCalculator.new(cell_output, tip_dao).call
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions app/utils/ckb_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,14 @@ def self.normal_tx_fee(input_capacities, output_capacities)
def self.dao_withdraw_tx_fee(ckb_transaction)
nervos_dao_withdrawing_cells = ckb_transaction.inputs.nervos_dao_withdrawing
interests = nervos_dao_withdrawing_cells.reduce(0) { |memo, nervos_dao_withdrawing_cell| memo + dao_interest(nervos_dao_withdrawing_cell) }

ckb_transaction.inputs.sum(:capacity) + interests - ckb_transaction.outputs.sum(:capacity)
end

def self.dao_interest(nervos_dao_withdrawing_cell)
nervos_dao_withdrawing_cell_generated_tx = nervos_dao_withdrawing_cell.generated_by
nervos_dao_deposit_cell = nervos_dao_withdrawing_cell_generated_tx.cell_inputs.order(:id)[nervos_dao_withdrawing_cell.cell_index].previous_cell_output
deposit_out_point = CKB::Types::OutPoint.new(tx_hash: nervos_dao_deposit_cell.tx_hash, index: nervos_dao_deposit_cell.cell_index)
withdrawing_dao_cell_block_hash = nervos_dao_withdrawing_cell.block.block_hash
CkbSync::Api.instance.calculate_dao_maximum_withdraw(deposit_out_point, withdrawing_dao_cell_block_hash).hex - nervos_dao_deposit_cell.capacity.to_i
withdrawing_dao_cell_block_dao = nervos_dao_withdrawing_cell.dao
DaoCompensationCalculator.new(nervos_dao_deposit_cell, withdrawing_dao_cell_block_dao).call
rescue CKB::RPCError
0
end
Expand Down
26 changes: 26 additions & 0 deletions app/utils/dao_compensation_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class DaoCompensationCalculator
attr_reader :deposit_cell_output, :withdraw_block_dao

def initialize(deposit_cell_output, withdraw_block_dao)
@deposit_cell_output = deposit_cell_output
@withdraw_block_dao = withdraw_block_dao
end

def call
compensation_generating_capacity * parsed_withdraw_block_dao.ar_i / parsed_deposit_block_dao.ar_i - compensation_generating_capacity
end

private

def parsed_withdraw_block_dao
CkbUtils.parse_dao(withdraw_block_dao)
end

def parsed_deposit_block_dao
CkbUtils.parse_dao(deposit_cell_output.dao)
end

def compensation_generating_capacity
@compensation_generating_capacity ||= (deposit_cell_output.capacity - deposit_cell_output.occupied_capacity).to_i
end
end
Loading

0 comments on commit 313c38e

Please sign in to comment.