Skip to content

Commit

Permalink
feat: add epoch length to epoch statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Apr 30, 2020
1 parent 0740580 commit 1ad1249
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/models/epoch_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EpochStatistic < ApplicationRecord
# block_time_distribution :jsonb
# epoch_time :decimal(13, )
# epoch_time_distribution :jsonb
# epoch_length :integer
#
# Indexes
#
Expand Down
4 changes: 2 additions & 2 deletions app/services/charts/epoch_statistic_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def call
first_block_in_epoch = Block.where(epoch: target_epoch_number).order(:number).select(:timestamp)[0]
last_lock_in_epoch = Block.where(epoch: target_epoch_number).order(number: :desc).select(:timestamp)[0]
epoch_time = last_lock_in_epoch.timestamp - first_block_in_epoch.timestamp
epoch_length = Block.where(epoch: target_epoch_number).count
epoch_length = Block.where(epoch: target_epoch_number).first.length
hash_rate = difficulty * epoch_length / epoch_time

epoch_statistic = ::EpochStatistic.find_or_create_by(epoch_number: target_epoch_number)
epoch_statistic.update(difficulty: difficulty, uncle_rate: uncle_rate, hash_rate: hash_rate, block_time_distribution: block_time_distribution,
epoch_time: epoch_time, epoch_time_distribution: epoch_time_distribution)
epoch_time: epoch_time, epoch_time_distribution: epoch_time_distribution, epoch_length: epoch_length)
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEpochLengthToEpochStatistics < ActiveRecord::Migration[6.0]
def change
add_column :epoch_statistics, :epoch_length, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_04_17_020812) do
ActiveRecord::Schema.define(version: 2020_04_17_092543) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -226,6 +226,7 @@
t.jsonb "block_time_distribution"
t.decimal "epoch_time", precision: 13
t.jsonb "epoch_time_distribution"
t.integer "epoch_length"
t.index ["epoch_number"], name: "index_epoch_statistics_on_epoch_number", unique: true
end

Expand Down

0 comments on commit 1ad1249

Please sign in to comment.