Skip to content

Commit b1199bd

Browse files
authored
[ᚬmaster] Rc/v0.8.3 (#526)
[ᚬmaster] Rc/v0.8.3
2 parents b5de582 + 974497d commit b1199bd

26 files changed

+333
-98
lines changed

CHANGELOG.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
# [0.8.3](https://github.com/shaojunda/ckb-explorer/compare/v0.8.2...v.8.3) (2019-12-19)
2+
3+
4+
### Bug Fixes
5+
6+
* wrong lock info after the lockout period ([69697c0](https://github.com/shaojunda/ckb-explorer/commit/69697c0))
7+
8+
9+
### Features
10+
11+
* add block index ([2361df5](https://github.com/shaojunda/ckb-explorer/commit/2361df5))
12+
* add live cells count to address serializer ([f9bd5b4](https://github.com/shaojunda/ckb-explorer/commit/f9bd5b4))
13+
* add live_cells_count to addresses ([34de215](https://github.com/shaojunda/ckb-explorer/commit/34de215))
14+
* add mined_blocks_count to addresses ([838802c](https://github.com/shaojunda/ckb-explorer/commit/838802c))
15+
* add mining info ([4e4d1e9](https://github.com/shaojunda/ckb-explorer/commit/4e4d1e9))
16+
* add mining info generation task ([55eb569](https://github.com/shaojunda/ckb-explorer/commit/55eb569))
17+
* add mining info model ([06f914f](https://github.com/shaojunda/ckb-explorer/commit/06f914f))
18+
* add new attributes to lock hash serializer ([e96d119](https://github.com/shaojunda/ckb-explorer/commit/e96d119))
19+
* add update addresses live cells count task ([dac2584](https://github.com/shaojunda/ckb-explorer/commit/dac2584))
20+
* return special address when the address is special ([29afdfb](https://github.com/shaojunda/ckb-explorer/commit/29afdfb))
21+
* return special address when the address is special ([6afd77e](https://github.com/shaojunda/ckb-explorer/commit/6afd77e))
22+
* return special tag for special addresses ([02bf4a6](https://github.com/shaojunda/ckb-explorer/commit/02bf4a6))
23+
* return special tag for special addresses ([c9d6b09](https://github.com/shaojunda/ckb-explorer/commit/c9d6b09))
24+
* revert mining info when fork ([190125f](https://github.com/shaojunda/ckb-explorer/commit/190125f))
25+
* revert mining infos ([4210f9e](https://github.com/shaojunda/ckb-explorer/commit/4210f9e))
26+
* save mining info on process block data ([b637784](https://github.com/shaojunda/ckb-explorer/commit/b637784))
27+
28+
29+
130
# [0.8.2](https://github.com/shaojunda/ckb-explorer/compare/v0.8.1...v.8.2) (2019-12-14)
231

332

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ gem "rails", "~> 6.0.0"
88
# Use postgresql as the database for Active Record
99
gem "pg", ">= 0.18", "< 2.0"
1010
# Use Puma as the app server
11-
gem "puma", "~> 4.1.1"
11+
gem "puma", "~> 4.3.1"
1212
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
1313
# gem 'jbuilder', '~> 2.5'
1414
# Use Redis adapter to run Action Cable in production

Gemfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ GEM
163163
raabro (~> 1.1)
164164
globalid (0.4.2)
165165
activesupport (>= 4.2.0)
166-
hashdiff (0.4.0)
166+
hashdiff (1.0.0)
167167
hiredis (0.6.3)
168168
i18n (1.7.0)
169169
concurrent-ruby (~> 1.0)
@@ -214,7 +214,7 @@ GEM
214214
net-http-persistent (3.0.1)
215215
connection_pool (~> 2.2)
216216
newrelic_rpm (6.4.0.356)
217-
nio4r (2.4.0)
217+
nio4r (2.5.2)
218218
nokogiri (1.10.5)
219219
mini_portile2 (~> 2.4.0)
220220
numerizer (0.1.1)
@@ -232,10 +232,10 @@ GEM
232232
pry-nav (0.3.0)
233233
pry (>= 0.9.10, < 0.13.0)
234234
public_suffix (3.1.0)
235-
puma (4.1.1)
235+
puma (4.3.1)
236236
nio4r (~> 2.0)
237237
raabro (1.1.6)
238-
rack (2.0.7)
238+
rack (2.0.8)
239239
rack-accept (0.4.5)
240240
rack (>= 0.4)
241241
rack-attack (6.2.1)
@@ -399,7 +399,7 @@ DEPENDENCIES
399399
pg (>= 0.18, < 2.0)
400400
pry
401401
pry-nav
402-
puma (~> 4.1.1)
402+
puma (~> 4.3.1)
403403
rack-attack
404404
rack-cors
405405
rails (~> 6.0.0)

app/models/address.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Address < ApplicationRecord
55
has_many :cell_outputs, dependent: :destroy
66
has_many :account_books, dependent: :destroy
77
has_many :ckb_transactions, through: :account_books
8+
has_many :mining_infos
89
validates :balance, :cell_consumed, :ckb_transactions_count, :interest, :dao_deposit, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
910

1011
scope :visible, -> { where(visible: true) }
@@ -58,19 +59,20 @@ def special?
5859
#
5960
# Table name: addresses
6061
#
61-
# id :bigint not null, primary key
62-
# balance :decimal(30, )
63-
# address_hash :binary
64-
# cell_consumed :decimal(30, )
65-
# ckb_transactions_count :decimal(30, ) default(0)
66-
# created_at :datetime not null
67-
# updated_at :datetime not null
68-
# lock_hash :binary
69-
# pending_reward_blocks_count :integer default(0)
70-
# dao_deposit :decimal(30, ) default(0)
71-
# interest :decimal(30, ) default(0)
72-
# block_timestamp :decimal(30, )
73-
# visible :boolean default(TRUE)
62+
# id :bigint not null, primary key
63+
# balance :decimal(30, )
64+
# address_hash :binary
65+
# cell_consumed :decimal(30, )
66+
# ckb_transactions_count :decimal(30, ) default(0)
67+
# created_at :datetime not null
68+
# updated_at :datetime not null
69+
# lock_hash :binary
70+
# dao_deposit :decimal(30, ) default(0)
71+
# interest :decimal(30, ) default(0)
72+
# block_timestamp :decimal(30, )
73+
# visible :boolean default(TRUE)
74+
# live_cells_count :decimal(30, ) default(0)
75+
# mined_blocks_count :integer default(0)
7476
#
7577
# Indexes
7678
#

app/models/block.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Block < ApplicationRecord
1212
has_many :cell_outputs
1313
has_many :cell_inputs
1414
has_many :dao_events
15+
has_many :mining_infos
1516

1617
validates_presence_of :block_hash, :number, :parent_hash, :timestamp, :transactions_root, :proposals_hash, :uncles_count, :uncles_hash, :version, :cell_consumed, :reward, :total_transaction_fee, :ckb_transactions_count, :total_cell_capacity, on: :create
1718
validates :reward, :total_transaction_fee, :ckb_transactions_count, :total_cell_capacity, :cell_consumed, numericality: { greater_than_or_equal_to: 0 }
@@ -55,6 +56,10 @@ def difficulty
5556
CkbUtils.compact_to_difficulty(compact_target)
5657
end
5758

59+
def block_index_in_epoch
60+
number - start_number
61+
end
62+
5863
def self.find_block!(query_key)
5964
cached_find(query_key) || raise(Api::V1::Exceptions::BlockNotFoundError)
6065
end

app/models/ckb_sync/node_data_processor.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def process_block(node_block)
3434
update_tx_fee_related_data(local_block, input_capacities)
3535
calculate_tx_fee(local_block, ckb_transactions, input_capacities, outputs.group_by(&:ckb_transaction_id))
3636

37-
update_miner_pending_rewards(local_block.miner_address)
37+
update_current_block_mining_info(local_block)
3838
update_block_contained_address_info(local_block)
3939
update_block_reward_info(local_block)
4040
dao_events = build_new_dao_depositor_events(new_dao_depositor_events)
@@ -121,7 +121,6 @@ def update_block_reward_info(current_block)
121121

122122
ApplicationRecord.transaction do
123123
issue_block_reward!(current_block)
124-
CkbUtils.update_target_block_miner_address_pending_rewards(current_block)
125124
end
126125
end
127126

@@ -131,7 +130,6 @@ def issue_block_reward!(current_block)
131130
end
132131

133132
def revert_block_rewards(local_tip_block)
134-
revert_miner_pending_reward_blocks_count(local_tip_block)
135133
target_block = local_tip_block.target_block
136134
target_block_number = local_tip_block.target_block_number
137135
return if target_block_number < 1 || target_block.blank?
@@ -149,17 +147,10 @@ def revert_reward_status(target_block)
149147
target_block.update!(received_tx_fee_status: "pending")
150148
end
151149

152-
def revert_miner_pending_reward_blocks_count(local_tip_block)
153-
miner_address = local_tip_block.miner_address
154-
Address.decrement_counter(:pending_reward_blocks_count, miner_address.id, touch: true) if miner_address.present?
155-
end
156-
157150
def invalid_block(local_tip_block)
158151
ApplicationRecord.transaction do
159-
dao_events = DaoEvent.where(block: local_tip_block).processed
160-
dao_contract = DaoContract.default_contract
161-
revert_dao_contract_related_operations(dao_contract, dao_events)
162-
152+
revert_dao_contract_related_operations(local_tip_block)
153+
revert_mining_info(local_tip_block)
163154
local_tip_block.invalid!
164155
local_tip_block.contained_addresses.each(&method(:update_address_balance_and_ckb_transactions_count))
165156
revert_block_rewards(local_tip_block)
@@ -168,7 +159,15 @@ def invalid_block(local_tip_block)
168159
end
169160
end
170161

171-
def revert_dao_contract_related_operations(dao_contract, dao_events)
162+
def revert_mining_info(local_tip_block)
163+
local_tip_block.mining_infos.first.reverted!
164+
miner_address = local_tip_block.miner_address
165+
miner_address.decrement!(:mined_blocks_count)
166+
end
167+
168+
def revert_dao_contract_related_operations(local_tip_block)
169+
dao_events = DaoEvent.where(block: local_tip_block).processed
170+
dao_contract = DaoContract.default_contract
172171
revert_withdraw_from_dao(dao_contract, dao_events)
173172
revert_issue_interest(dao_contract, dao_events)
174173
revert_deposit_to_dao(dao_contract, dao_events)
@@ -489,6 +488,7 @@ def update_previous_cell_output_status(ckb_transaction_id, previous_cell_output)
489488
def update_address_balance_and_ckb_transactions_count(address)
490489
address.balance = address.cell_outputs.live.sum(:capacity)
491490
address.ckb_transactions_count = AccountBook.where(address: address).select(:ckb_transaction_id).distinct.count
491+
address.live_cells_count = address.cell_outputs.live.count
492492
address.save!
493493
end
494494

@@ -518,8 +518,8 @@ def update_transaction_fee(ckb_transaction, input_capacities, output_capacities)
518518
ckb_transaction.transaction_fee = [transaction_fee, 0].max
519519
end
520520

521-
def update_miner_pending_rewards(miner_address)
522-
CkbUtils.update_current_block_miner_address_pending_rewards(miner_address)
521+
def update_current_block_mining_info(block)
522+
CkbUtils.update_current_block_mining_info(block)
523523
end
524524
end
525525
end

app/models/mining_info.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class MiningInfo < ApplicationRecord
2+
enum status: { mined: 0, reverted: 2 }
3+
4+
belongs_to :block
5+
belongs_to :address
6+
end
7+
8+
# == Schema Information
9+
#
10+
# Table name: mining_infos
11+
#
12+
# id :bigint not null, primary key
13+
# address_id :bigint
14+
# block_id :bigint
15+
# block_number :decimal(30, )
16+
# status :integer default("mined")
17+
# created_at :datetime not null
18+
# updated_at :datetime not null
19+
#
20+
# Indexes
21+
#
22+
# index_mining_infos_on_block_id (block_id)
23+
# index_mining_infos_on_block_number (block_number)
24+
#

app/models/null_address.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ def lock_info
2626

2727
def cached_lock_script; end
2828

29-
def pending_reward_blocks_count
30-
0
31-
end
32-
3329
def dao_deposit
3430
0
3531
end
@@ -42,6 +38,14 @@ def special?
4238
Settings.special_addresses[address_hash].present?
4339
end
4440

41+
def live_cells_count
42+
0
43+
end
44+
45+
def mined_blocks_count
46+
0
47+
end
48+
4549
def lock_script
4650
parsed_address = CKB::AddressParser.new(address_hash).parse
4751
raise Api::V1::Exceptions::AddressNotMatchEnvironmentError.new(ENV["CKB_NET_MODE"]) if parsed_address.mode != ENV["CKB_NET_MODE"]

app/presenters/address_presenter.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ def interest
2323
object.reduce(0) { |sum, addr| sum + addr.interest.to_i }
2424
end
2525

26-
def lock_script
27-
object.first.cached_lock_script
26+
def live_cells_count
27+
object.reduce(0) { |sum, addr| sum + addr.live_cells_count.to_i }
28+
end
29+
30+
def mined_blocks_count
31+
object.reduce(0) { |sum, addr| sum + addr.mined_blocks_count.to_i }
2832
end
2933

30-
def pending_reward_blocks_count
31-
[object.reduce(0) { |sum, addr| sum + addr.pending_reward_blocks_count }, 0].max
34+
def lock_script
35+
object.first.cached_lock_script
3236
end
3337

3438
def ckb_transactions_count

app/serializers/address_serializer.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ class AddressSerializer
88
attribute :transactions_count do |object|
99
object.ckb_transactions_count.to_s
1010
end
11-
attribute :pending_reward_blocks_count do |object|
12-
object.pending_reward_blocks_count.to_s
13-
end
1411
attribute :dao_deposit do |object|
1512
object.dao_deposit.to_s
1613
end
@@ -20,8 +17,13 @@ class AddressSerializer
2017
attribute :is_special do |object|
2118
object.special?.to_s
2219
end
23-
2420
attribute :special_address, if: Proc.new { |record| record.special? } do |object|
2521
Settings.special_addresses[object.address_hash]
2622
end
23+
attribute :live_cells_count do |object|
24+
object.live_cells_count.to_s
25+
end
26+
attribute :mined_blocks_count do |object|
27+
object.mined_blocks_count.to_s
28+
end
2729
end

0 commit comments

Comments
 (0)