Skip to content

Commit

Permalink
add more useful columns to transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Jul 11, 2018
1 parent 4bcd15f commit 3f027ae
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
33 changes: 25 additions & 8 deletions app/models/cita_sync/persist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,28 @@ def save_transaction(hash, block = nil)
result = data["result"]
return if result.nil?
block ||= Block.find_by_block_number(CitaSync::Basic.hex_str_to_number(result["blockNumber"]))
Transaction.create(
content = result["content"]
message = Message.new(content)
transaction = Transaction.new(
cita_hash: result["hash"],
content: result["content"],
content: content,
block_number: result["blockNumber"],
block_hash: result["blockHash"],
index: result["index"],
block: block
block: block,
from: message.from,
to: message.to,
data: message.data,
value: message.value
)
receipt_data = CitaSync::Api.get_transaction_receipt(hash)
receipt_result = receipt_data["result"]
unless receipt_result.nil?
transaction.contract_address = receipt_result["contractAddress"]
transaction.gas_used = receipt_result["gasUsed"]
end
transaction.save
transaction
end

# save a meta data
Expand Down Expand Up @@ -94,11 +108,14 @@ def save_abi(addr, block_number)

# save one block with it's transactions and meta data
def save_block_with_infos(block_number_hex_str)
block = save_block(block_number_hex_str)
_meta_data = save_meta_data(block_number_hex_str, block)
hashes = block.transactions.map { |t| t&.with_indifferent_access[:hash] }
hashes.each do |hash|
save_transaction(hash, block)
# merge to one commit, can be faster
ApplicationRecord.transaction do
block = save_block(block_number_hex_str)
_meta_data = save_meta_data(block_number_hex_str, block)
hashes = block.transactions.map { |t| t&.with_indifferent_access[:hash] }
hashes.each do |hash|
save_transaction(hash, block)
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20180711075733_add_columns_to_transactions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddColumnsToTransactions < ActiveRecord::Migration[5.2]
def change
add_column :transactions, :from, :string
add_column :transactions, :to, :string
add_column :transactions, :data, :text
add_column :transactions, :value, :string
add_column :transactions, :contract_address, :string
add_column :transactions, :gas_used, :string
end
end
8 changes: 7 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_07_09_091027) do
ActiveRecord::Schema.define(version: 2018_07_11_075733) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -71,6 +71,12 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "block_id"
t.string "from"
t.string "to"
t.text "data"
t.string "value"
t.string "contract_address"
t.string "gas_used"
t.index ["block_id"], name: "index_transactions_on_block_id"
t.index ["cita_hash"], name: "index_transactions_on_cita_hash", unique: true
end
Expand Down
4 changes: 2 additions & 2 deletions lib/blockchain.proto
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ message BlockBody {
repeated SignedTransaction transactions = 1;
}

message Block {
message ProtoBlock {
uint32 version = 1;
BlockHeader header = 2;
BlockBody body = 3;
}

message BlockWithProof {
Block blk = 1;
ProtoBlock blk = 1;
Proof proof = 2;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/blockchain_pb.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/models/cita_sync/persist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def mock_get_transaction
stub_request_wrapper("getTransaction", ["0xee969624a87a51fc4acc958a3bb83ca32539ee54ebb4215668fe1029eeab59d4"], result)
end

def mock_get_transaction_receipt
result = {
"contractAddress": "0x89be88054e2ee94911549be521ab1241c7700a1b",
"gasUsed": "0x2d483"
}

stub_request_wrapper("getTransactionReceipt", ["0xee969624a87a51fc4acc958a3bb83ca32539ee54ebb4215668fe1029eeab59d4"], result)
end

def mock_get_meta_data
result = {
"blockInterval": 3000,
Expand Down Expand Up @@ -108,6 +117,7 @@ def mock_get_abi
mock_get_block_by_number_zero
mock_get_block_by_number_one
mock_get_transaction
mock_get_transaction_receipt
mock_get_meta_data
mock_get_balance
mock_get_abi
Expand Down

0 comments on commit 3f027ae

Please sign in to comment.