Skip to content

Commit

Permalink
[ᚬmaster] Rc/v0.9.2 (#558)
Browse files Browse the repository at this point in the history
[ᚬmaster] Rc/v0.9.2
  • Loading branch information
shaojunda authored Jan 31, 2020
2 parents 04899fc + d8b182d commit 08ee674
Show file tree
Hide file tree
Showing 59 changed files with 495 additions and 177 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
.vscode
.idea/
TAGS
ckb-explorer.iml
.generators
.rakeTasks
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# [0.9.2](https://github.com/shaojunda/ckb-explorer/compare/v0.9.1...v0.9.2) (2020-01-31)


### Features

* add address average deposit time generator ([0618138](https://github.com/shaojunda/ckb-explorer/commit/0618138))
* add average deposit time to address ([81469df](https://github.com/shaojunda/ckb-explorer/commit/81469df))
* add claimed and unclaimed compensation ([268858d](https://github.com/shaojunda/ckb-explorer/commit/268858d))
* add compensation and lock period ([e42b072](https://github.com/shaojunda/ckb-explorer/commit/e42b072))
* add more elements to daily chart ([0e239b1](https://github.com/shaojunda/ckb-explorer/commit/0e239b1))
* add new columns to daily statistic ([0a013c4](https://github.com/shaojunda/ckb-explorer/commit/0a013c4))
* add unlaimed compenstaion generator worker ([ba0e105](https://github.com/shaojunda/ckb-explorer/commit/ba0e105))



# [0.9.1](https://github.com/shaojunda/ckb-explorer/compare/v0.8.4...v0.9.0) (2020-01-13)


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.where("dao_deposit > 0").order(dao_deposit: :desc).limit(100)
addresses = Address.select(:id, :address_hash, :dao_deposit, :average_deposit_time).where("dao_deposit > 0").order(dao_deposit: :desc).limit(100)

render json: DaoDepositorSerializer.new(addresses)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/epoch_statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class EpochStatisticsController < ApplicationController
before_action :validate_query_params

def show
epoch_statistics = EpochStatistic.order(id: :desc).reverse
epoch_statistics = EpochStatistic.order(epoch_number: :desc).limit(90).reverse
render json: EpochStatisticSerializer.new(epoch_statistics, { params: { indicator: params[:id] } })
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def api_error(error)
end

def check_header_info
raise Api::V1::Exceptions::WrongContentTypeError if request.headers["Content-Type"] != "application/vnd.api+json"
raise Api::V1::Exceptions::WrongAcceptError if request.headers["Accept"] != "application/vnd.api+json"
raise Api::V1::Exceptions::InvalidContentTypeError if request.headers["Content-Type"] != "application/vnd.api+json"
raise Api::V1::Exceptions::InvalidAcceptError if request.headers["Accept"] != "application/vnd.api+json"
end

def validate_pagination_params
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/validations/daily_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def error_object
attr_accessor :query_key

def query_key_format_must_be_correct
if query_key.blank? || !query_key.in?(::DailyStatistic::VALID_INDICATORS)
if query_key.blank? || !(query_key.split("-") - ::DailyStatistic::VALID_INDICATORS).empty?
errors.add(:query_key, "indicator name is invalid")
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/lib/api/v1/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def initialize(code:, status:, title:, detail:, href:)
end
end

class WrongContentTypeError < Error
class InvalidContentTypeError < Error
def initialize
super code: 1001, status: 415, title: "Unsupported Media Type", detail: "Content Type must be application/vnd.api+json", href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html"
end
end

class WrongAcceptError < Error
class InvalidAcceptError < Error
def initialize
super code: 1002, status: 406, title: "Not Acceptable", detail: "Accept must be application/vnd.api+json", href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html"
end
Expand Down
1 change: 1 addition & 0 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def special?
# visible :boolean default(TRUE)
# live_cells_count :decimal(30, ) default(0)
# mined_blocks_count :integer default(0)
# average_deposit_time :decimal(, )
#
# Indexes
#
Expand Down
2 changes: 1 addition & 1 deletion app/models/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def difficulty
end

def block_index_in_epoch
number - start_number
number - start_number
end

def fraction_epoch
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 @@ -16,6 +16,7 @@ class CellOutput < ApplicationRecord

attribute :tx_hash, :ckb_hash

scope :consumed_after, -> (block_timestamp) { where("consumed_block_timestamp >= ?", block_timestamp) }
scope :consumed_before, -> (block_timestamp) { where("consumed_block_timestamp <= ?", block_timestamp) }
scope :unconsumed_at, -> (block_timestamp) { where("consumed_block_timestamp > ? or consumed_block_timestamp is null", block_timestamp) }
scope :generated_after, -> (block_timestamp) { where("block_timestamp >= ?", block_timestamp) }
Expand Down
4 changes: 2 additions & 2 deletions app/models/ckb_sync/node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def process_take_away_all_deposit(dao_contract, dao_events)
def process_issue_interest(dao_contract, dao_events)
issue_interest_dao_events = dao_events.where(event_type: "issue_interest")
issue_interest_dao_events.each do |event|
dao_contract.increment!(:interest_granted, event.value)
dao_contract.increment!(:claimed_compensation, event.value)
address = event.address
address.increment!(:interest, event.value)
event.processed!
Expand Down Expand Up @@ -190,7 +190,7 @@ def revert_take_away_all_deposit(dao_contract, dao_events)
def revert_issue_interest(dao_contract, dao_events)
issue_interest_dao_events = dao_events.where(event_type: "issue_interest")
issue_interest_dao_events.each do |event|
dao_contract.decrement!(:interest_granted, event.value)
dao_contract.decrement!(:claimed_compensation, event.value)
address = event.address
address.decrement!(:interest, event.value)
event.reverted!
Expand Down
16 changes: 10 additions & 6 deletions app/models/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def normal_tx_display_outputs(previews)
cell_outputs_for_display.map do |output|
consumed_tx_hash = output.live? ? nil : output.consumed_by.tx_hash
display_output = { id: output.id, capacity: output.capacity, address_hash: output.address_hash, status: output.status, consumed_tx_hash: consumed_tx_hash, cell_type: output.cell_type }
display_output[:dao_type_hash] = ENV["DAO_TYPE_HASH"] unless output.normal?

CkbUtils.hash_value_to_s(display_output)
end
Expand All @@ -89,21 +88,26 @@ def normal_tx_display_inputs(previews)
previous_cell_output = cell_input.previous_cell_output
display_input = { id: previous_cell_output.id, from_cellbase: false, capacity: previous_cell_output.capacity, address_hash: previous_cell_output.address_hash, generated_tx_hash: previous_cell_output.generated_by.tx_hash, cell_type: previous_cell_output.cell_type }
display_input.merge!(attributes_for_dao_input(previous_cell_output)) if previous_cell_output.nervos_dao_withdrawing?
display_input.merge!(attributes_for_dao_input(cell_outputs[index])) if previous_cell_output.nervos_dao_deposit?
display_input.merge!(attributes_for_dao_input(cell_outputs[index], false)) if previous_cell_output.nervos_dao_deposit?

CkbUtils.hash_value_to_s(display_input)
end
end
end

def attributes_for_dao_input(nervos_dao_withdrawing_cell)
def attributes_for_dao_input(nervos_dao_withdrawing_cell, is_phase2 = true)
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
started_block_number = Block.find(nervos_dao_deposit_cell.block.id).number
ended_block_number = Block.find(block_id).number
compensation_started_block = Block.select(:number, :timestamp).find(nervos_dao_deposit_cell.block.id)
compensation_ended_block = Block.select(:number, :timestamp).find(nervos_dao_withdrawing_cell_generated_tx.block_id)
interest = CkbUtils.dao_interest(nervos_dao_withdrawing_cell)
attributes = { compensation_started_block_number: compensation_started_block.number, compensation_ended_block_number: compensation_ended_block.number, compensation_started_timestamp: compensation_started_block.timestamp, compensation_ended_timestamp: compensation_ended_block.timestamp, interest: interest }
if is_phase2
locked_until_block = Block.select(:number, :timestamp).find(block_id)
attributes.merge!({ locked_until_block_number: locked_until_block.number, locked_until_block_timestamp: locked_until_block.timestamp })
end

CkbUtils.hash_value_to_s({ started_block_number: started_block_number, ended_block_number: ended_block_number, interest: interest, dao_type_hash: ENV["DAO_TYPE_HASH"] })
CkbUtils.hash_value_to_s(attributes)
end

def cellbase_display_inputs
Expand Down
8 changes: 7 additions & 1 deletion app/models/daily_statistic.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class DailyStatistic < ApplicationRecord
VALID_INDICATORS = %w(transactions_count addresses_count total_dao_deposit).freeze
VALID_INDICATORS = %w(transactions_count addresses_count total_dao_deposit live_cells_count dead_cells_count avg_hash_rate avg_difficulty uncle_rate total_depositors_count).freeze
end

# == Schema Information
Expand All @@ -22,4 +22,10 @@ class DailyStatistic < ApplicationRecord
# mining_reward :string default("0")
# deposit_compensation :string default("0")
# treasury_amount :string default("0")
# live_cells_count :string default("0")
# dead_cells_count :string default("0")
# avg_hash_rate :string default("0")
# avg_difficulty :string default("0")
# uncle_rate :string default("0")
# total_depositors_count :string default("0")
#
21 changes: 5 additions & 16 deletions app/models/dao_contract.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class DaoContract < ApplicationRecord
validates :total_deposit, :interest_granted, :deposit_transactions_count, :withdraw_transactions_count, :depositors_count, :total_depositors_count, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :total_deposit, :claimed_compensation, :deposit_transactions_count, :withdraw_transactions_count, :depositors_count, :total_depositors_count, presence: true, numericality: { greater_than_or_equal_to: 0 }
CONTRACT_NAME = "nervos_dao".freeze
GENESIS_ISSUANCE = 336 * 10**8
ANNUAL_PRIMARY_ISSUANCE_BASE = GENESIS_ISSUANCE / 8
Expand Down Expand Up @@ -54,19 +54,11 @@ def depositor_changes
end

def unclaimed_compensation_changes
latest_daily_statistic.unclaimed_compensation.to_d - penultimate_daily_statistic.unclaimed_compensation.to_d
unclaimed_compensation.to_d - latest_daily_statistic.unclaimed_compensation.to_d
end

def claimed_compensation_changes
latest_daily_statistic.claimed_compensation.to_d - penultimate_daily_statistic.claimed_compensation.to_d
end

def unclaimed_compensation
latest_daily_statistic.unclaimed_compensation
end

def claimed_compensation
latest_daily_statistic.claimed_compensation
claimed_compensation - latest_daily_statistic.claimed_compensation.to_d
end

def average_deposit_time
Expand Down Expand Up @@ -95,10 +87,6 @@ def latest_daily_statistic
@latest_daily_statistic ||= DailyStatistic.order(created_at_unixtimestamp: :desc).first || OpenStruct.new(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)
end

def penultimate_daily_statistic
@penultimate_daily_statistic ||= DailyStatistic.order(created_at_unixtimestamp: :desc).second || OpenStruct.new(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)
end

def alpha(start_epoch_number)
i = ((start_epoch_number + 1) / EPOCHS_IN_PERIOD).floor
p = PRIMARY_ISSUANCE_PER_YEAR_BASE / 2 ** i / 2190
Expand Down Expand Up @@ -134,11 +122,12 @@ def secondary_issuance(start_epoch)
#
# id :bigint not null, primary key
# total_deposit :decimal(30, ) default(0)
# interest_granted :decimal(30, ) default(0)
# claimed_compensation :decimal(30, ) default(0)
# deposit_transactions_count :bigint default(0)
# withdraw_transactions_count :bigint default(0)
# depositors_count :integer default(0)
# total_depositors_count :bigint default(0)
# created_at :datetime not null
# updated_at :datetime not null
# unclaimed_compensation :decimal(30, )
#
1 change: 1 addition & 0 deletions app/models/dao_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class DaoEvent < ApplicationRecord
belongs_to :ckb_transaction
belongs_to :address

scope :created_after, ->(block_timestamp) { where("block_timestamp >= ?", block_timestamp) }
scope :created_before, ->(block_timestamp) { where("block_timestamp <= ?", block_timestamp) }
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/null_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ def lock_script
script = parsed_address.script
LockScript.new(code_hash: script.code_hash, args: script.args, hash_type: script.hash_type)
end

def average_deposit_time
0
end
end
4 changes: 4 additions & 0 deletions app/presenters/address_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def lock_info
object.first.lock_script.lock_info
end

def average_deposit_time
object.first.average_deposit_time
end

private

attr_reader :object
Expand Down
3 changes: 3 additions & 0 deletions app/serializers/address_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ class AddressSerializer
attribute :mined_blocks_count do |object|
object.mined_blocks_count.to_s
end
attribute :average_deposit_time do |object|
object.average_deposit_time.to_s
end
end
30 changes: 27 additions & 3 deletions app/serializers/daily_statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,38 @@ class DailyStatisticSerializer
end

attribute :transactions_count, if: Proc.new { |_record, params|
params && params[:indicator] == "transactions_count"
params && params[:indicator].include?("transactions_count")
}

attribute :addresses_count, if: Proc.new { |_record, params|
params && params[:indicator] == "addresses_count"
params && params[:indicator].include?("addresses_count")
}

attribute :total_dao_deposit, if: Proc.new { |_record, params|
params && params[:indicator] == "total_dao_deposit"
params && params[:indicator].include?("total_dao_deposit")
}

attribute :live_cells_count, if: Proc.new { |_record, params|
params && params[:indicator].include?("live_cells_count")
}

attribute :dead_cells_count, if: Proc.new { |_record, params|
params && params[:indicator].include?("dead_cells_count")
}

attribute :avg_hash_rate, if: Proc.new { |_record, params|
params && params[:indicator].include?("avg_hash_rate")
}

attribute :avg_difficulty, if: Proc.new { |_record, params|
params && params[:indicator].include?("avg_difficulty")
}

attribute :uncle_rate, if: Proc.new { |_record, params|
params && params[:indicator].include?("uncle_rate")
}

attribute :total_depositors_count, if: Proc.new { |_record, params|
params && params[:indicator].include?("total_depositors_count")
}
end
3 changes: 3 additions & 0 deletions app/serializers/dao_contract_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ class DaoContractSerializer
attribute :estimated_apc do |object|
object.estimated_apc.to_s
end
attribute :claimed_compensation do |object|
object.claimed_compensation.to_s
end
end
3 changes: 3 additions & 0 deletions app/serializers/dao_depositor_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ class DaoDepositorSerializer
attribute :dao_deposit do |object|
object.dao_deposit.to_s
end
attribute :average_deposit_time do |object|
object.average_deposit_time.to_s
end
end
Loading

0 comments on commit 08ee674

Please sign in to comment.