Skip to content

Commit

Permalink
Merge pull request #72 from cryptape/add-timestamp-to-tx
Browse files Browse the repository at this point in the history
Add some columns to blocks and tx
  • Loading branch information
ashchan authored Dec 29, 2018
2 parents 69ce3d5 + 3a2eb9d commit 21fc8e2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 13 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"] || block_header["gasUsed"]).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
7 changes: 4 additions & 3 deletions spec/models/cita_sync/persist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ def set_false

it "save transaction" do
block = CitaSync::Persist.save_block("0x1")
transaction = CitaSync::Persist.save_transaction(tx_data, 0, HexUtils.to_hex(block.block_number), block.block_hash)
transaction = CitaSync::Persist.save_transaction(tx_data, 0, HexUtils.to_hex(block.block_number), block.block_hash, block.timestamp)
expect(transaction.tx_hash).to eq transaction_hash
expect(transaction.errors.full_messages).to be_empty
expect(transaction.block).to eq block
end

it "save transaction with SAVE_BLOCKS set false" do
set_false
transaction = CitaSync::Persist.save_transaction(tx_data, 0, "0x0", "0x123")
transaction = CitaSync::Persist.save_transaction(tx_data, 0, "0x0", "0x123", nil)
expect(transaction.tx_hash).to eq transaction_hash
expect(transaction.errors.full_messages).to be_empty
expect(transaction.block).to be nil
Expand All @@ -85,7 +85,7 @@ def set_false
# end

it "save transaction without block will be success" do
transaction = CitaSync::Persist.save_transaction(tx_data, 0, nil, nil)
transaction = CitaSync::Persist.save_transaction(tx_data, 0, nil, nil, nil)
expect(transaction.errors.full_messages).to be_empty
end

Expand All @@ -97,6 +97,7 @@ def set_false
},
0,
nil,
nil,
nil
]

Expand Down

0 comments on commit 21fc8e2

Please sign in to comment.