Skip to content

Commit

Permalink
fix: update bitcoin transction created_at (#1928)
Browse files Browse the repository at this point in the history
* fix: update bitcoin transction created_at

* fix: bitcoin annotations
  • Loading branch information
rabbitz committed Jun 6, 2024
1 parent c5f10cb commit c12c474
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 4 additions & 2 deletions app/jobs/import_btc_time_cell_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def perform(cell_id)
raw_tx = fetch_raw_transaction(txid)
return unless raw_tx

tx = build_transaction!(raw_tx)
tx = build_transaction!(raw_tx, cell_output.ckb_transaction)
# build transfer
BitcoinTransfer.create_with(
bitcoin_transaction_id: tx.id,
Expand All @@ -29,16 +29,18 @@ def perform(cell_id)
end
end

def build_transaction!(raw_tx)
def build_transaction!(raw_tx, ckb_tx)
tx = BitcoinTransaction.find_by(txid: raw_tx["txid"])
return tx if tx

created_at = Time.at((ckb_tx.block_timestamp / 1000).to_i).in_time_zone
BitcoinTransaction.create!(
txid: raw_tx["txid"],
tx_hash: raw_tx["hash"],
time: raw_tx["time"],
block_hash: raw_tx["blockhash"],
block_height: 0,
created_at:,
)
end

Expand Down
10 changes: 7 additions & 3 deletions app/jobs/import_rgbpp_cell_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def perform(cell_id)
raw_tx = fetch_raw_transaction(txid)
return unless raw_tx

tx = build_transaction!(raw_tx)
tx = build_transaction!(raw_tx, cell_output.ckb_transaction)
# build op_returns
vout_attributes = []
op_returns = build_op_returns!(raw_tx, tx, cell_output.ckb_transaction)
Expand Down Expand Up @@ -47,20 +47,23 @@ def perform(cell_id)
Rails.logger.error(e.message)
end

def build_transaction!(raw_tx)
def build_transaction!(raw_tx, ckb_tx)
tx = BitcoinTransaction.find_by(txid: raw_tx["txid"])
return tx if tx

# raw transactions may not include the block hash
if raw_tx["blockhash"].present?
block_header = rpc.getblockheader(raw_tx["blockhash"])
end

created_at = Time.at((ckb_tx.block_timestamp / 1000).to_i).in_time_zone
BitcoinTransaction.create!(
txid: raw_tx["txid"],
tx_hash: raw_tx["hash"],
time: raw_tx["time"],
block_hash: raw_tx["blockhash"],
block_height: block_header&.dig("result", "height") || 0,
created_at:,
)
end

Expand Down Expand Up @@ -136,7 +139,8 @@ def build_vin!(cell_id, tx)
end

def build_address!(address_hash, cell_output)
bitcoin_address = BitcoinAddress.find_or_create_by!(address_hash:)
created_at = Time.at((cell_output.block_timestamp / 1000).to_i).in_time_zone
bitcoin_address = BitcoinAddress.create_with(created_at:).find_or_create_by!(address_hash:)
BitcoinAddressMapping.
create_with(bitcoin_address_id: bitcoin_address.id).
find_or_create_by!(ckb_address_id: cell_output.address_id)
Expand Down
7 changes: 4 additions & 3 deletions app/workers/bitcoin_transaction_detect_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ def build_bitcoin_annotations!
annotations = []

@block.ckb_transactions.each do |transaction|
next unless BitcoinTransfer.exists?(ckb_transaction_id: transaction.id)

leap_direction, transfer_step = annotation_workflow_attributes(transaction)
tags = annotation_tags(transaction)
annotations << { ckb_transaction_id: transaction.id, leap_direction:, transfer_step:, tags: }

if tags.present?
annotations << { ckb_transaction_id: transaction.id, leap_direction:, transfer_step:, tags: }

Check warning on line 99 in app/workers/bitcoin_transaction_detect_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/bitcoin_transaction_detect_worker.rb#L98-L99

Added lines #L98 - L99 were not covered by tests
end
end

BitcoinAnnotation.upsert_all(annotations, unique_by: [:ckb_transaction_id]) if annotations.present?
Expand Down

0 comments on commit c12c474

Please sign in to comment.