Skip to content

Commit 04899fc

Browse files
authored
Merge pull request #550 from shaojunda/rc/v0.9.1
[ᚬmaster] Rc/v0.9.1
2 parents 1f1d5c5 + 6ff92fb commit 04899fc

File tree

9 files changed

+105
-20
lines changed

9 files changed

+105
-20
lines changed

CHANGELOG.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
# [0.9.1](https://github.com/shaojunda/ckb-explorer/compare/v0.8.4...v0.9.0) (2020-01-13)
2+
3+
4+
### Features
5+
6+
* add block list serializer ([8d729d7](https://github.com/shaojunda/ckb-explorer/commit/8d729d7))
7+
* add block statistic generator service ([db91614](https://github.com/shaojunda/ckb-explorer/commit/db91614))
8+
* add block timestamp to dao event ([a40453b](https://github.com/shaojunda/ckb-explorer/commit/a40453b))
9+
* add capacity_involved column to ckb transaction ([7bcd7a5](https://github.com/shaojunda/ckb-explorer/commit/7bcd7a5))
10+
* add chart forked event processor ([83927bf](https://github.com/shaojunda/ckb-explorer/commit/83927bf))
11+
* add consumed block timestamp to cell output ([0f4986f](https://github.com/shaojunda/ckb-explorer/commit/0f4986f))
12+
* add epoch statistic generator service ([a99926c](https://github.com/shaojunda/ckb-explorer/commit/a99926c))
13+
* add estimated_apc to dao contract ([cb31d1c](https://github.com/shaojunda/ckb-explorer/commit/cb31d1c))
14+
* add forked event model ([2cf8bff](https://github.com/shaojunda/ckb-explorer/commit/2cf8bff))
15+
* add hash rate to epoch statistic ([e0746df](https://github.com/shaojunda/ckb-explorer/commit/e0746df))
16+
* add index action to ckb transactions controller ([e71fef7](https://github.com/shaojunda/ckb-explorer/commit/e71fef7))
17+
* add live_cell_changes to block and ckb_transaction ([2f04c62](https://github.com/shaojunda/ckb-explorer/commit/2f04c62))
18+
* add live_cell_changes to forked blocks ([a2d42b2](https://github.com/shaojunda/ckb-explorer/commit/a2d42b2))
19+
* add miner reward to block ([efe97fa](https://github.com/shaojunda/ckb-explorer/commit/efe97fa))
20+
* add more field to daily statistics ([3912139](https://github.com/shaojunda/ckb-explorer/commit/3912139))
21+
* add pagination to ckb transactions controller ([2251ba2](https://github.com/shaojunda/ckb-explorer/commit/2251ba2))
22+
* add ratio scale ([3b68742](https://github.com/shaojunda/ckb-explorer/commit/3b68742))
23+
* calculate estimated apc ([4a4f04f](https://github.com/shaojunda/ckb-explorer/commit/4a4f04f))
24+
* create forked event when forked ([ce61a1f](https://github.com/shaojunda/ckb-explorer/commit/ce61a1f))
25+
* implement ckb transactions index action ([26e5a36](https://github.com/shaojunda/ckb-explorer/commit/26e5a36))
26+
* regenerate block statistic data when block forked ([c5a444e](https://github.com/shaojunda/ckb-explorer/commit/c5a444e))
27+
* return hash_rate ([354ebf1](https://github.com/shaojunda/ckb-explorer/commit/354ebf1))
28+
* save capacity involved to ckb_transaction ([61fc5ba](https://github.com/shaojunda/ckb-explorer/commit/61fc5ba))
29+
* save hash rate on epoch statistic worker ([48be8e4](https://github.com/shaojunda/ckb-explorer/commit/48be8e4))
30+
* save live_cell_changes to block ([76f3efc](https://github.com/shaojunda/ckb-explorer/commit/76f3efc))
31+
* save live_cell_changes to ckb_transaction ([0c5589f](https://github.com/shaojunda/ckb-explorer/commit/0c5589f))
32+
* show more attributes on dao contract ([6369a86](https://github.com/shaojunda/ckb-explorer/commit/6369a86))
33+
* use ckb transaction list serializer ([7d12caa](https://github.com/shaojunda/ckb-explorer/commit/7d12caa))
34+
35+
36+
### Performance Improvements
37+
38+
* add index on block timestamp, status and event type ([bce28f4](https://github.com/shaojunda/ckb-explorer/commit/bce28f4))
39+
* use redis pipeline and use delete replace delete_matched ([da666e0](https://github.com/shaojunda/ckb-explorer/commit/da666e0))
40+
41+
42+
143
# [0.9.0](https://github.com/shaojunda/ckb-explorer/compare/v0.8.3...v0.9.0) (2020-01-02)
244

345

app/models/address.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Address < ApplicationRecord
99
validates :balance, :cell_consumed, :ckb_transactions_count, :interest, :dao_deposit, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
1010

1111
scope :visible, -> { where(visible: true) }
12+
scope :created_after, ->(block_timestamp) { where("block_timestamp >= ?", block_timestamp) }
13+
scope :created_before, ->(block_timestamp) { where("block_timestamp <= ?", block_timestamp) }
1214

1315
after_commit :flush_cache
1416

app/models/cell_output.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class CellOutput < ApplicationRecord
1616

1717
attribute :tx_hash, :ckb_hash
1818

19+
scope :consumed_before, -> (block_timestamp) { where("consumed_block_timestamp <= ?", block_timestamp) }
20+
scope :unconsumed_at, -> (block_timestamp) { where("consumed_block_timestamp > ? or consumed_block_timestamp is null", block_timestamp) }
21+
scope :generated_after, -> (block_timestamp) { where("block_timestamp >= ?", block_timestamp) }
22+
scope :generated_before, -> (block_timestamp) { where("block_timestamp <= ?", block_timestamp) }
23+
1924
after_commit :flush_cache
2025

2126
def address_hash

app/models/ckb_transaction.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class CkbTransaction < ApplicationRecord
1818
scope :recent, -> { order(block_timestamp: :desc) }
1919
scope :cellbase, -> { where(is_cellbase: true) }
2020
scope :normal, -> { where(is_cellbase: false) }
21+
scope :created_after, -> (block_timestamp) { where("block_timestamp >= ?", block_timestamp) }
22+
scope :created_before, -> (block_timestamp) { where("block_timestamp <= ?", block_timestamp) }
2123

2224
after_commit :flush_cache
2325
before_destroy :recover_dead_cell

app/models/dao_event.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class DaoEvent < ApplicationRecord
66
belongs_to :block
77
belongs_to :ckb_transaction
88
belongs_to :address
9+
10+
scope :created_before, ->(block_timestamp) { where("block_timestamp <= ?", block_timestamp) }
911
end
1012

1113
# == Schema Information
@@ -26,5 +28,7 @@ class DaoEvent < ApplicationRecord
2628
#
2729
# Indexes
2830
#
29-
# index_dao_events_on_block_id (block_id)
31+
# index_dao_events_on_block_id (block_id)
32+
# index_dao_events_on_block_timestamp (block_timestamp)
33+
# index_dao_events_on_status_and_event_type (status,event_type)
3034
#

app/services/charts/daily_statistic_generator.rb

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ module Charts
22
class DailyStatisticGenerator
33
MILLISECONDS_IN_DAY = BigDecimal(24 * 60 * 60 * 1000)
44

5-
def initialize(datetime = nil)
5+
def initialize(datetime = nil, from_scratch = false)
66
raise "datetime must be a Time" if datetime.present? && !datetime.is_a?(Time)
77

88
@datetime = datetime
9+
@from_scratch = from_scratch
910
end
1011

1112
def call
12-
daily_ckb_transactions_count = CkbTransaction.where("block_timestamp >= ? and block_timestamp <= ?", started_at, ended_at).count
13-
addresses_count = Address.where("block_timestamp <= ?", ended_at).count
13+
daily_ckb_transactions_count = CkbTransaction.created_after(started_at).created_before(ended_at).count
1414
cell_outputs = CellOutput.where.not(cell_type: "normal")
15-
current_tip_block = Block.where("timestamp <= ?", ended_at).recent.first
1615
mining_reward = Block.where("timestamp <= ?", ended_at).sum(:secondary_reward)
1716
deposit_compensation = unclaimed_compensation(cell_outputs, current_tip_block) + claimed_compensation(cell_outputs)
1817
estimated_apc = DaoContract.default_contract.estimated_apc(current_tip_block.fraction_epoch)
1918
block_timestamp = Block.created_after(started_at).created_before(ended_at).recent.pick(:timestamp)
19+
addresses_count = processed_addresses_count
2020
daily_statistic = ::DailyStatistic.find_or_create_by!(created_at_unixtimestamp: to_be_counted_date.to_i)
2121
daily_statistic.update(block_timestamp: block_timestamp, transactions_count: daily_ckb_transactions_count,
2222
addresses_count: addresses_count, total_dao_deposit: total_dao_deposit,
@@ -28,14 +28,35 @@ def call
2828

2929
private
3030

31+
attr_reader :datetime, :from_scratch
32+
33+
def processed_addresses_count
34+
if from_scratch
35+
Address.created_before(ended_at).count
36+
else
37+
Address.created_after(started_at).created_before(ended_at).count + latest_daily_statistic.addresses_count.to_i
38+
end
39+
end
40+
41+
def current_tip_block
42+
@current_tip_block ||=
43+
begin
44+
if from_scratch
45+
Block.created_before(ended_at).recent.first
46+
else
47+
Block.created_after(started_at).created_before(ended_at).recent.first
48+
end
49+
end
50+
end
51+
3152
def total_dao_deposit
32-
deposit_amount = DaoEvent.processed.where("block_timestamp <= ?", ended_at).where(event_type: "deposit_to_dao").sum(:value)
33-
withdraw_amount = DaoEvent.processed.where("block_timestamp <= ?", ended_at).where(event_type: "withdraw_from_dao").sum(:value)
53+
deposit_amount = DaoEvent.processed.deposit_to_dao.created_before(ended_at).sum(:value)
54+
withdraw_amount = DaoEvent.processed.withdraw_from_dao.created_before(ended_at).sum(:value)
3455
deposit_amount - withdraw_amount
3556
end
3657

3758
def dao_depositors_count
38-
DaoEvent.processed.where(event_type: "new_dao_depositor").where("block_timestamp <= ?", ended_at).count - DaoEvent.processed.where(event_type: "take_away_all_deposit").where("block_timestamp <= ?", ended_at).count
59+
DaoEvent.processed.new_dao_depositor.created_before(ended_at).count - DaoEvent.processed.take_away_all_deposit.created_before(ended_at).count
3960
end
4061

4162
def to_be_counted_date
@@ -50,8 +71,6 @@ def ended_at
5071
@ended_at ||= time_in_milliseconds(to_be_counted_date.end_of_day)
5172
end
5273

53-
attr_reader :datetime
54-
5574
def unclaimed_compensation(cell_outputs, current_tip_block)
5675
@unclaimed_compensation ||=
5776
begin
@@ -62,22 +81,22 @@ def unclaimed_compensation(cell_outputs, current_tip_block)
6281
def claimed_compensation(cell_outputs)
6382
@claimed_compensation ||=
6483
begin
65-
cell_outputs.where("consumed_block_timestamp <= ?", ended_at).nervos_dao_withdrawing.dead.reduce(0) do |memo, nervos_dao_withdrawing_cell|
84+
cell_outputs.nervos_dao_withdrawing.consumed_before(ended_at).reduce(0) do |memo, nervos_dao_withdrawing_cell|
6685
memo + CkbUtils.dao_interest(nervos_dao_withdrawing_cell)
6786
end
6887
end
6988
end
7089

7190
def phase1_dao_interests(cell_outputs)
72-
cell_outputs.where("block_timestamp <= ?", ended_at).nervos_dao_withdrawing.live.reduce(0) do |memo, nervos_dao_withdrawing_cell|
91+
cell_outputs.nervos_dao_withdrawing.generated_before(ended_at).unconsumed_at(ended_at).reduce(0) do |memo, nervos_dao_withdrawing_cell|
7392
memo + CkbUtils.dao_interest(nervos_dao_withdrawing_cell)
7493
end
7594
end
7695

7796
def unmade_dao_interests(cell_outputs, current_tip_block)
7897
@unmade_dao_interests ||=
7998
begin
80-
cell_outputs.where("block_timestamp <= ?", ended_at).nervos_dao_deposit.live.reduce(0) do |memo, cell_output|
99+
cell_outputs.nervos_dao_deposit.generated_before(ended_at).unconsumed_at(ended_at).reduce(0) do |memo, cell_output|
81100
dao = cell_output.block.dao
82101
tip_dao = current_tip_block.dao
83102
parse_dao = CkbUtils.parse_dao(dao)
@@ -90,17 +109,16 @@ def unmade_dao_interests(cell_outputs, current_tip_block)
90109
def average_deposit_time(cell_outputs)
91110
interest_bearing_deposits = 0
92111
uninterest_bearing_deposits = 0
93-
sum_interest_bearing = cell_outputs.where("block_timestamp <= ?", ended_at).nervos_dao_withdrawing.live.reduce(0) do |memo, nervos_dao_withdrawing_cell|
112+
sum_interest_bearing = cell_outputs.nervos_dao_withdrawing.generated_before(ended_at).unconsumed_at(ended_at).reduce(0) do |memo, nervos_dao_withdrawing_cell|
94113
nervos_dao_withdrawing_cell_generated_tx = nervos_dao_withdrawing_cell.generated_by
95114
nervos_dao_deposit_cell = nervos_dao_withdrawing_cell_generated_tx.cell_inputs.order(:id)[nervos_dao_withdrawing_cell.cell_index].previous_cell_output
96115
interest_bearing_deposits += nervos_dao_deposit_cell.capacity
97116
memo + nervos_dao_deposit_cell.capacity * (nervos_dao_withdrawing_cell.block_timestamp - nervos_dao_deposit_cell.block_timestamp) / MILLISECONDS_IN_DAY
98117
end
99-
sum_uninterest_bearing = cell_outputs.where("block_timestamp <= ?", ended_at).nervos_dao_deposit.live.reduce(0) do |memo, nervos_dao_deposit_cell|
100-
current_time = time_in_milliseconds(Time.current)
118+
sum_uninterest_bearing = cell_outputs.nervos_dao_deposit.generated_before(ended_at).unconsumed_at(ended_at).reduce(0) do |memo, nervos_dao_deposit_cell|
101119
uninterest_bearing_deposits += nervos_dao_deposit_cell.capacity
102120

103-
memo + nervos_dao_deposit_cell.capacity * (current_time - nervos_dao_deposit_cell.block_timestamp) / MILLISECONDS_IN_DAY
121+
memo + nervos_dao_deposit_cell.capacity * (ended_at - nervos_dao_deposit_cell.block_timestamp) / MILLISECONDS_IN_DAY
104122
end
105123

106124
(sum_interest_bearing + sum_uninterest_bearing) / (interest_bearing_deposits + uninterest_bearing_deposits)
@@ -114,5 +132,9 @@ def treasury_amount(cell_outputs, current_tip_block)
114132
def time_in_milliseconds(time)
115133
(time.to_f * 1000).floor
116134
end
135+
136+
def latest_daily_statistic
137+
::DailyStatistic.order(created_at_unixtimestamp: :desc).first || OpenStruct.new(addresses_count: 0, total_dao_deposit: 0, dao_depositors_count: 0, unclaimed_compensation: 0, claimed_compensation: 0, average_deposit_time: 0, mining_reward: 0, deposit_compensation: 0, treasury_amount: 0)
138+
end
117139
end
118140
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddIndexToBlockTimestampOnDaoEvents < ActiveRecord::Migration[6.0]
2+
def change
3+
add_index :dao_events, :block_timestamp
4+
add_index :dao_events, [:status, :event_type]
5+
end
6+
end

db/schema.rb

Lines changed: 3 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_01_03_051008) do
13+
ActiveRecord::Schema.define(version: 2020_01_10_123617) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -197,6 +197,8 @@
197197
t.datetime "updated_at", precision: 6, null: false
198198
t.decimal "block_timestamp", precision: 30
199199
t.index ["block_id"], name: "index_dao_events_on_block_id"
200+
t.index ["block_timestamp"], name: "index_dao_events_on_block_timestamp"
201+
t.index ["status", "event_type"], name: "index_dao_events_on_status_and_event_type"
200202
end
201203

202204
create_table "epoch_statistics", force: :cascade do |t|

test/services/charts/daily_statistic_generator_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
module Charts
44
class DailyStatisticGeneratorTest < ActiveSupport::TestCase
55
test "should create daily statistic record" do
6-
block = create(:block, dao: "0xaff1568bbe49672f8a02516252ab2300df8c9e15dad428000035a1d671700007")
6+
block = create(:block, dao: "0xaff1568bbe49672f8a02516252ab2300df8c9e15dad428000035a1d671700007", timestamp: (Time.current - 1.day).end_of_day.to_i * 1000)
77
tx = create(:ckb_transaction, block: block)
8-
create(:cell_output, cell_type: "nervos_dao_deposit", generated_by: tx, ckb_transaction: tx, block: block, capacity: 10**8 * 1000, block_timestamp: (Time.current - 1.day).end_of_day.strftime("%Q"))
8+
create(:cell_output, cell_type: "nervos_dao_deposit", generated_by: tx, ckb_transaction: tx, block: block, capacity: 10**8 * 1000, block_timestamp: (Time.current - 1.day).end_of_day.to_i * 1000)
99
assert_difference -> { ::DailyStatistic.count }, 1 do
1010
Charts::DailyStatisticGenerator.new.call
1111
end

0 commit comments

Comments
 (0)