Skip to content

Commit

Permalink
Add timestamp, quota_used, proposer to blocks and add `timestam…
Browse files Browse the repository at this point in the history
…p` to transactions
  • Loading branch information
classicalliu committed Dec 29, 2018
1 parent 69ce3d5 commit 02e1b38
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
16 changes: 11 additions & 5 deletions app/models/cita_sync/persist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,26 @@ def save_block(hex_num_str)
# error is nil now, if result is also nil, means result is nil (like after snapshot)
return if result.nil?

block_number_hex_str = result.dig("header", "number")
block_header = result["header"]
block_number_hex_str = block_header["number"]
block_number = HexUtils.to_decimal(block_number_hex_str)
block_hash = result["hash"]
transactions_data = result.dig("body", "transactions")
timestamp = block_header["timestamp"]
block = Block.new(
version: result["version"],
block_hash: block_hash,
header: result["header"],
# body: result["body"],
block_number: block_number,
transaction_count: transactions_data.count
transaction_count: transactions_data.count,
timestamp: timestamp,
proposer: block_header["proposer"],
quota_used: block_header["quotaUsed"].hex
)
block.save! if save_blocks?

transaction_params = transactions_data.map.with_index { |tx_data, index| [tx_data, index, block_number, block_hash] }
transaction_params = transactions_data.map.with_index { |tx_data, index| [tx_data, index, block_number, block_hash, timestamp] }
SaveTransactionWorker.push_bulk(transaction_params) { |param| param }

block
Expand All @@ -61,7 +66,7 @@ def save_block(hex_num_str)
# @param block_number [Integer]
# @param block_hash [String]
# @return [Transaction, SyncError] return SyncError if rpc return an error
def save_transaction(tx_data, index, block_number, block_hash)
def save_transaction(tx_data, index, block_number, block_hash, timestamp)
hash = tx_data["hash"]
content = tx_data["content"]
receipt_data = CitaSync::Api.get_transaction_receipt(hash)
Expand Down Expand Up @@ -89,7 +94,8 @@ def save_transaction(tx_data, index, block_number, block_hash)
chain_id: message.chain_id,
contract_address: receipt_result["contractAddress"],
quota_used: HexUtils.to_decimal(receipt_result["quotaUsed"] || receipt_result["gasUsed"]),
error_message: receipt_result["errorMessage"]
error_message: receipt_result["errorMessage"],
timestamp: timestamp
)

ApplicationRecord.transaction do
Expand Down
2 changes: 0 additions & 2 deletions app/models/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class Transaction < ApplicationRecord
has_many :event_logs, foreign_key: "transaction_hash", class_name: "EventLog", primary_key: %i(transaction_hash log_index), inverse_of: "tx"
has_many :erc20_transfers, foreign_key: "transaction_hash", class_name: "Erc20Transfer", primary_key: %i(transaction_hash log_index), inverse_of: "tx"

delegate :timestamp, to: :block, allow_nil: true

# validates :block, presence: true
validates :tx_hash, presence: true, uniqueness: true

Expand Down
4 changes: 2 additions & 2 deletions app/workers/save_transaction_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
class SaveTransactionWorker
include Sidekiq::Worker

def perform(tx_data, index, block_number, block_hash)
def perform(tx_data, index, block_number, block_hash, timestamp)
# compatibility
block_number = HexUtils.to_decimal(block_number) if block_number.is_a?(String) && block_hash.start_with?("0x")

CitaSync::Persist.save_transaction(tx_data, index, block_number, block_hash)
CitaSync::Persist.save_transaction(tx_data, index, block_number, block_hash, timestamp)
end
end
13 changes: 13 additions & 0 deletions db/migrate/20181227091201_add_some_info_to_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddSomeInfoToBlocks < ActiveRecord::Migration[5.2]
def change
add_column :blocks, :timestamp, :bigint
add_column :blocks, :proposer, :string
add_column :blocks, :quota_used, :decimal, precision: 100

reversible do |dir|
dir.up do
execute "UPDATE blocks SET timestamp = subquery.timestamp::bigint, proposer = subquery.proposer::varchar, quota_used = subquery.quota_used::decimal(100) FROM (SELECT (b.header ->> 'timestamp') AS timestamp, (b.header ->> 'proposer') AS proposer, ('x'||lpad(substr((b.header ->> 'quotaUsed'), 3, char_length(b.header ->> 'quotaUsed')), 16, '0'))::bit(64)::bigint AS quota_used, block_hash FROM blocks AS b) AS subquery WHERE subquery.block_hash = blocks.block_hash;"
end
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20181229030703_add_timestamp_to_transactions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddTimestampToTransactions < ActiveRecord::Migration[5.2]
def change
add_column :transactions, :timestamp, :bigint

reversible do |dir|
dir.up do
execute %{UPDATE transactions AS tx SET "timestamp" = subquery.timestamp FROM (SELECT block_hash, "timestamp" FROM blocks AS b) AS subquery WHERE tx.block_hash = subquery.block_hash;}
end
end
end
end
6 changes: 5 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: 2018_12_26_103138) do
ActiveRecord::Schema.define(version: 2018_12_29_030703) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -39,6 +39,9 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "transaction_count"
t.bigint "timestamp"
t.string "proposer"
t.decimal "quota_used", precision: 100
t.index ["block_hash"], name: "index_blocks_on_block_hash", unique: true
t.index ["block_number"], name: "index_blocks_on_block_number", unique: true
t.index ["body"], name: "index_blocks_on_body", using: :gin
Expand Down Expand Up @@ -116,6 +119,7 @@
t.integer "index"
t.decimal "value", precision: 100
t.decimal "quota_used", precision: 100
t.bigint "timestamp"
t.index ["block_hash"], name: "index_transactions_on_block_hash"
t.index ["from"], name: "index_transactions_on_from"
t.index ["to"], name: "index_transactions_on_to"
Expand Down

0 comments on commit 02e1b38

Please sign in to comment.