Skip to content

Commit

Permalink
[ᚬmaster] Rc/v0.8.3 (#526)
Browse files Browse the repository at this point in the history
[ᚬmaster] Rc/v0.8.3
  • Loading branch information
shaojunda authored Dec 21, 2019
2 parents b5de582 + 974497d commit b1199bd
Show file tree
Hide file tree
Showing 26 changed files with 333 additions and 98 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# [0.8.3](https://github.com/shaojunda/ckb-explorer/compare/v0.8.2...v.8.3) (2019-12-19)


### Bug Fixes

* wrong lock info after the lockout period ([69697c0](https://github.com/shaojunda/ckb-explorer/commit/69697c0))


### Features

* add block index ([2361df5](https://github.com/shaojunda/ckb-explorer/commit/2361df5))
* add live cells count to address serializer ([f9bd5b4](https://github.com/shaojunda/ckb-explorer/commit/f9bd5b4))
* add live_cells_count to addresses ([34de215](https://github.com/shaojunda/ckb-explorer/commit/34de215))
* add mined_blocks_count to addresses ([838802c](https://github.com/shaojunda/ckb-explorer/commit/838802c))
* add mining info ([4e4d1e9](https://github.com/shaojunda/ckb-explorer/commit/4e4d1e9))
* add mining info generation task ([55eb569](https://github.com/shaojunda/ckb-explorer/commit/55eb569))
* add mining info model ([06f914f](https://github.com/shaojunda/ckb-explorer/commit/06f914f))
* add new attributes to lock hash serializer ([e96d119](https://github.com/shaojunda/ckb-explorer/commit/e96d119))
* add update addresses live cells count task ([dac2584](https://github.com/shaojunda/ckb-explorer/commit/dac2584))
* return special address when the address is special ([29afdfb](https://github.com/shaojunda/ckb-explorer/commit/29afdfb))
* return special address when the address is special ([6afd77e](https://github.com/shaojunda/ckb-explorer/commit/6afd77e))
* return special tag for special addresses ([02bf4a6](https://github.com/shaojunda/ckb-explorer/commit/02bf4a6))
* return special tag for special addresses ([c9d6b09](https://github.com/shaojunda/ckb-explorer/commit/c9d6b09))
* revert mining info when fork ([190125f](https://github.com/shaojunda/ckb-explorer/commit/190125f))
* revert mining infos ([4210f9e](https://github.com/shaojunda/ckb-explorer/commit/4210f9e))
* save mining info on process block data ([b637784](https://github.com/shaojunda/ckb-explorer/commit/b637784))



# [0.8.2](https://github.com/shaojunda/ckb-explorer/compare/v0.8.1...v.8.2) (2019-12-14)


Expand Down
2 changes: 1 addition & 1 deletion 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.1.1"
gem "puma", "~> 4.3.1"
# 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 Down
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ GEM
raabro (~> 1.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
hashdiff (0.4.0)
hashdiff (1.0.0)
hiredis (0.6.3)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -214,7 +214,7 @@ GEM
net-http-persistent (3.0.1)
connection_pool (~> 2.2)
newrelic_rpm (6.4.0.356)
nio4r (2.4.0)
nio4r (2.5.2)
nokogiri (1.10.5)
mini_portile2 (~> 2.4.0)
numerizer (0.1.1)
Expand All @@ -232,10 +232,10 @@ GEM
pry-nav (0.3.0)
pry (>= 0.9.10, < 0.13.0)
public_suffix (3.1.0)
puma (4.1.1)
puma (4.3.1)
nio4r (~> 2.0)
raabro (1.1.6)
rack (2.0.7)
rack (2.0.8)
rack-accept (0.4.5)
rack (>= 0.4)
rack-attack (6.2.1)
Expand Down Expand Up @@ -399,7 +399,7 @@ DEPENDENCIES
pg (>= 0.18, < 2.0)
pry
pry-nav
puma (~> 4.1.1)
puma (~> 4.3.1)
rack-attack
rack-cors
rails (~> 6.0.0)
Expand Down
28 changes: 15 additions & 13 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Address < ApplicationRecord
has_many :cell_outputs, dependent: :destroy
has_many :account_books, dependent: :destroy
has_many :ckb_transactions, through: :account_books
has_many :mining_infos
validates :balance, :cell_consumed, :ckb_transactions_count, :interest, :dao_deposit, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true

scope :visible, -> { where(visible: true) }
Expand Down Expand Up @@ -58,19 +59,20 @@ def special?
#
# Table name: addresses
#
# id :bigint not null, primary key
# balance :decimal(30, )
# address_hash :binary
# cell_consumed :decimal(30, )
# ckb_transactions_count :decimal(30, ) default(0)
# created_at :datetime not null
# updated_at :datetime not null
# lock_hash :binary
# pending_reward_blocks_count :integer default(0)
# dao_deposit :decimal(30, ) default(0)
# interest :decimal(30, ) default(0)
# block_timestamp :decimal(30, )
# visible :boolean default(TRUE)
# id :bigint not null, primary key
# balance :decimal(30, )
# address_hash :binary
# cell_consumed :decimal(30, )
# ckb_transactions_count :decimal(30, ) default(0)
# created_at :datetime not null
# updated_at :datetime not null
# lock_hash :binary
# dao_deposit :decimal(30, ) default(0)
# interest :decimal(30, ) default(0)
# block_timestamp :decimal(30, )
# visible :boolean default(TRUE)
# live_cells_count :decimal(30, ) default(0)
# mined_blocks_count :integer default(0)
#
# Indexes
#
Expand Down
5 changes: 5 additions & 0 deletions app/models/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Block < ApplicationRecord
has_many :cell_outputs
has_many :cell_inputs
has_many :dao_events
has_many :mining_infos

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
validates :reward, :total_transaction_fee, :ckb_transactions_count, :total_cell_capacity, :cell_consumed, numericality: { greater_than_or_equal_to: 0 }
Expand Down Expand Up @@ -55,6 +56,10 @@ def difficulty
CkbUtils.compact_to_difficulty(compact_target)
end

def block_index_in_epoch
number - start_number
end

def self.find_block!(query_key)
cached_find(query_key) || raise(Api::V1::Exceptions::BlockNotFoundError)
end
Expand Down
30 changes: 15 additions & 15 deletions app/models/ckb_sync/node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def process_block(node_block)
update_tx_fee_related_data(local_block, input_capacities)
calculate_tx_fee(local_block, ckb_transactions, input_capacities, outputs.group_by(&:ckb_transaction_id))

update_miner_pending_rewards(local_block.miner_address)
update_current_block_mining_info(local_block)
update_block_contained_address_info(local_block)
update_block_reward_info(local_block)
dao_events = build_new_dao_depositor_events(new_dao_depositor_events)
Expand Down Expand Up @@ -121,7 +121,6 @@ def update_block_reward_info(current_block)

ApplicationRecord.transaction do
issue_block_reward!(current_block)
CkbUtils.update_target_block_miner_address_pending_rewards(current_block)
end
end

Expand All @@ -131,7 +130,6 @@ def issue_block_reward!(current_block)
end

def revert_block_rewards(local_tip_block)
revert_miner_pending_reward_blocks_count(local_tip_block)
target_block = local_tip_block.target_block
target_block_number = local_tip_block.target_block_number
return if target_block_number < 1 || target_block.blank?
Expand All @@ -149,17 +147,10 @@ def revert_reward_status(target_block)
target_block.update!(received_tx_fee_status: "pending")
end

def revert_miner_pending_reward_blocks_count(local_tip_block)
miner_address = local_tip_block.miner_address
Address.decrement_counter(:pending_reward_blocks_count, miner_address.id, touch: true) if miner_address.present?
end

def invalid_block(local_tip_block)
ApplicationRecord.transaction do
dao_events = DaoEvent.where(block: local_tip_block).processed
dao_contract = DaoContract.default_contract
revert_dao_contract_related_operations(dao_contract, dao_events)

revert_dao_contract_related_operations(local_tip_block)
revert_mining_info(local_tip_block)
local_tip_block.invalid!
local_tip_block.contained_addresses.each(&method(:update_address_balance_and_ckb_transactions_count))
revert_block_rewards(local_tip_block)
Expand All @@ -168,7 +159,15 @@ def invalid_block(local_tip_block)
end
end

def revert_dao_contract_related_operations(dao_contract, dao_events)
def revert_mining_info(local_tip_block)
local_tip_block.mining_infos.first.reverted!
miner_address = local_tip_block.miner_address
miner_address.decrement!(:mined_blocks_count)
end

def revert_dao_contract_related_operations(local_tip_block)
dao_events = DaoEvent.where(block: local_tip_block).processed
dao_contract = DaoContract.default_contract
revert_withdraw_from_dao(dao_contract, dao_events)
revert_issue_interest(dao_contract, dao_events)
revert_deposit_to_dao(dao_contract, dao_events)
Expand Down Expand Up @@ -489,6 +488,7 @@ def update_previous_cell_output_status(ckb_transaction_id, previous_cell_output)
def update_address_balance_and_ckb_transactions_count(address)
address.balance = address.cell_outputs.live.sum(:capacity)
address.ckb_transactions_count = AccountBook.where(address: address).select(:ckb_transaction_id).distinct.count
address.live_cells_count = address.cell_outputs.live.count
address.save!
end

Expand Down Expand Up @@ -518,8 +518,8 @@ def update_transaction_fee(ckb_transaction, input_capacities, output_capacities)
ckb_transaction.transaction_fee = [transaction_fee, 0].max
end

def update_miner_pending_rewards(miner_address)
CkbUtils.update_current_block_miner_address_pending_rewards(miner_address)
def update_current_block_mining_info(block)
CkbUtils.update_current_block_mining_info(block)
end
end
end
24 changes: 24 additions & 0 deletions app/models/mining_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class MiningInfo < ApplicationRecord
enum status: { mined: 0, reverted: 2 }

belongs_to :block
belongs_to :address
end

# == Schema Information
#
# Table name: mining_infos
#
# id :bigint not null, primary key
# address_id :bigint
# block_id :bigint
# block_number :decimal(30, )
# status :integer default("mined")
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_mining_infos_on_block_id (block_id)
# index_mining_infos_on_block_number (block_number)
#
12 changes: 8 additions & 4 deletions app/models/null_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ def lock_info

def cached_lock_script; end

def pending_reward_blocks_count
0
end

def dao_deposit
0
end
Expand All @@ -42,6 +38,14 @@ def special?
Settings.special_addresses[address_hash].present?
end

def live_cells_count
0
end

def mined_blocks_count
0
end

def lock_script
parsed_address = CKB::AddressParser.new(address_hash).parse
raise Api::V1::Exceptions::AddressNotMatchEnvironmentError.new(ENV["CKB_NET_MODE"]) if parsed_address.mode != ENV["CKB_NET_MODE"]
Expand Down
12 changes: 8 additions & 4 deletions app/presenters/address_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ def interest
object.reduce(0) { |sum, addr| sum + addr.interest.to_i }
end

def lock_script
object.first.cached_lock_script
def live_cells_count
object.reduce(0) { |sum, addr| sum + addr.live_cells_count.to_i }
end

def mined_blocks_count
object.reduce(0) { |sum, addr| sum + addr.mined_blocks_count.to_i }
end

def pending_reward_blocks_count
[object.reduce(0) { |sum, addr| sum + addr.pending_reward_blocks_count }, 0].max
def lock_script
object.first.cached_lock_script
end

def ckb_transactions_count
Expand Down
10 changes: 6 additions & 4 deletions app/serializers/address_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ class AddressSerializer
attribute :transactions_count do |object|
object.ckb_transactions_count.to_s
end
attribute :pending_reward_blocks_count do |object|
object.pending_reward_blocks_count.to_s
end
attribute :dao_deposit do |object|
object.dao_deposit.to_s
end
Expand All @@ -20,8 +17,13 @@ class AddressSerializer
attribute :is_special do |object|
object.special?.to_s
end

attribute :special_address, if: Proc.new { |record| record.special? } do |object|
Settings.special_addresses[object.address_hash]
end
attribute :live_cells_count do |object|
object.live_cells_count.to_s
end
attribute :mined_blocks_count do |object|
object.mined_blocks_count.to_s
end
end
3 changes: 3 additions & 0 deletions app/serializers/block_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class BlockSerializer
attribute :epoch do |object|
object.epoch.to_s
end
attribute :block_index_in_epoch do |object|
object.block_index_in_epoch.to_s
end
attribute :nonce do |object|
object.nonce.to_s
end
Expand Down
23 changes: 18 additions & 5 deletions app/serializers/lock_hash_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,34 @@ class LockHashSerializer

attributes :address_hash, :lock_hash

attribute :lock_info do |object|
object.lock_script.lock_info
end
attribute :lock_script do |object|
object.lock_script.to_node_lock
end

attribute :transactions_count do |object|
object.ckb_transactions_count.to_s
end

attribute :balance do |object|
object.balance.to_s
end
attribute :transactions_count do |object|
object.ckb_transactions_count.to_s
end
attribute :dao_deposit do |object|
object.dao_deposit.to_s
end
attribute :interest do |object|
object.interest.to_s
end
attribute :is_special do |object|
object.special?.to_s
end
attribute :special_address, if: Proc.new { |record| record.special? } do |object|
Settings.special_addresses[object.address_hash]
end
attribute :live_cells_count do |object|
object.live_cells_count.to_s
end
attribute :mined_blocks_count do |object|
object.mined_blocks_count.to_s
end
end
15 changes: 5 additions & 10 deletions app/utils/ckb_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,12 @@ def self.calculate_received_tx_fee!(current_block)
target_block.update!(received_tx_fee: received_tx_fee, received_tx_fee_status: "calculated")
end

def self.update_current_block_miner_address_pending_rewards(miner_address)
Address.increment_counter(:pending_reward_blocks_count, miner_address.id, touch: true) if miner_address.present?
end

def self.update_target_block_miner_address_pending_rewards(current_block)
target_block_number = current_block.target_block_number
target_block = current_block.target_block
return if target_block_number < 1 || target_block.blank?
def self.update_current_block_mining_info(block)
return if block.blank?

miner_address = target_block.miner_address
Address.decrement_counter(:pending_reward_blocks_count, miner_address.id, touch: true) if miner_address.present?
miner_address = block.miner_address
MiningInfo.create!(block: block, block_number:block.number, address: miner_address, status: "mined")
miner_address.increment!(:mined_blocks_count)
end

def self.normal_tx_fee(input_capacities, output_capacities)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLiveCellsCountToAddresses < ActiveRecord::Migration[6.0]
def change
add_column :addresses, :live_cells_count, :decimal, precision: 30, scale: 0, default: 0
end
end
Loading

0 comments on commit b1199bd

Please sign in to comment.