Skip to content

Commit

Permalink
Merge pull request #1771 from nervosnetwork/develop
Browse files Browse the repository at this point in the history
Deploy to testnet
  • Loading branch information
rabbitz committed Apr 11, 2024
2 parents 4c466a5 + a34cc03 commit 8180421
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 23 deletions.
12 changes: 9 additions & 3 deletions app/interactions/addresses/live_cells.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ def execute
raise AddressNotFoundError if address.is_a?(NullAddress)

order_by, asc_or_desc = live_cells_ordering
records = CellOutput.live.where(address_id: address.map(&:id)).
order(order_by => asc_or_desc).
page(page).per(page_size).fast_page
records =
if order_by == "block_timestamp"
ids = CellOutput.live.where(address_id: address.map(&:id)).pluck(:id)
CellOutput.where(id: ids).order(order_by => asc_or_desc).page(page).per(page_size).fast_page
else
CellOutput.live.where(address_id: address.map(&:id)).

Check warning on line 21 in app/interactions/addresses/live_cells.rb

View check run for this annotation

Codecov / codecov/patch

app/interactions/addresses/live_cells.rb#L21

Added line #L21 was not covered by tests
order(order_by => asc_or_desc).
page(page).per(page_size).fast_page
end
options = FastJsonapi::PaginationMetaGenerator.new(
request:, records:, page:, page_size:,
).call
Expand Down
8 changes: 4 additions & 4 deletions app/interactions/rgb_transactions/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def execute
if leap_direction.present?
transactions = transactions.where(
"CASE
WHEN (SELECT COUNT(*) FROM bitcoin_vins WHERE ckb_transaction_id = ckb_transactions.id) >
(SELECT COUNT(*) FROM bitcoin_vouts WHERE ckb_transaction_id = ckb_transactions.id)
THEN 'in'
WHEN (SELECT COUNT(*) FROM bitcoin_vins WHERE ckb_transaction_id = ckb_transactions.id) <
(SELECT COUNT(*) FROM bitcoin_vouts WHERE ckb_transaction_id = ckb_transactions.id)
(SELECT COUNT(*) FROM bitcoin_vouts WHERE ckb_transaction_id = ckb_transactions.id AND op_return = false)
THEN 'in'
WHEN (SELECT COUNT(*) FROM bitcoin_vins WHERE ckb_transaction_id = ckb_transactions.id) >
(SELECT COUNT(*) FROM bitcoin_vouts WHERE ckb_transaction_id = ckb_transactions.id AND op_return = false)
THEN 'out'
ELSE 'equal'
END = ?", leap_direction
Expand Down
18 changes: 6 additions & 12 deletions app/jobs/import_bitcoin_utxo_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def perform(cell_id)
Rails.logger.info("Importing bitcoin utxo #{txid} out_index #{out_index}")
vout_attributes = []
# build bitcoin transaction
raw_tx = fetch_raw_transaction(txid)
raw_tx = rpc.getrawtransaction(txid, 2)
tx = build_transaction!(raw_tx)
# build op_returns
op_returns = build_op_returns!(raw_tx, tx, cell_output.ckb_transaction, vout_attributes)
Expand All @@ -36,14 +36,16 @@ def build_transaction!(raw_tx)
tx = BitcoinTransaction.find_by(txid: raw_tx["txid"])
return tx if tx

# avoid making multiple RPC requests
block_header = rpc.getblockheader(raw_tx["blockhash"])
# raw transactions may not include the block hash
if raw_tx["blockhash"].present?
block_header = rpc.getblockheader(raw_tx["blockhash"])
end
BitcoinTransaction.create!(
txid: raw_tx["txid"],
tx_hash: raw_tx["hash"],
time: raw_tx["time"],
block_hash: raw_tx["blockhash"],
block_height: block_header["height"],
block_height: block_header&.dig("height") || 0,
)
end

Expand Down Expand Up @@ -103,14 +105,6 @@ def build_address!(address_hash, cell_output)
bitcoin_address
end

def fetch_raw_transaction(txid)
Rails.cache.fetch("bitcoin_transactions/#{txid}", expires_in: 1.hour) do
rpc.getrawtransaction(txid, 2)
rescue StandardError => e
raise ArgumentError, "get bitcoin raw transaction #{txid} failed: #{e}"
end
end

def rpc
@rpc ||= Bitcoin::Rpc.instance
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/bitcoin_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class BitcoinStatistic < ApplicationRecord
def self.refresh
transaction do
current_time = Time.current
start_time = Time.new(current_time.year, current_time.month, current_time.day, current_time.hour, current_time.min < 30 ? 0 : 30)
start_time = Time.zone.local(current_time.year, current_time.month, current_time.day, current_time.hour, current_time.min < 30 ? 0 : 30)
end_time = start_time + 30.minutes

Check warning on line 8 in app/models/bitcoin_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/bitcoin_statistic.rb#L4-L8

Added lines #L4 - L8 were not covered by tests

# Count the number of newly generated addresses within half an hour before the current time point
Expand Down
10 changes: 10 additions & 0 deletions app/models/bitcoin_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ def confirmations
nil
end

refresh_block_height! if block_hash.blank?

Check warning on line 15 in app/models/bitcoin_transaction.rb

View check run for this annotation

Codecov / codecov/patch

app/models/bitcoin_transaction.rb#L15

Added line #L15 was not covered by tests
tip_block_height ? tip_block_height - block_height : 0
end

def ckb_transaction_hash
ckb_transaction = bitcoin_vouts&.take&.ckb_transaction
return ckb_transaction.tx_hash if ckb_transaction
end

def refresh_block_height!
rpc = Bitcoin::Rpc.instance
raw_transaction = rpc.getrawtransaction(txid, 2)
block_header = rpc.getblockheader(raw_transaction["blockhash"])
update(block_hash: raw_transaction["blockhash"], block_height: block_header["height"])

Check warning on line 28 in app/models/bitcoin_transaction.rb

View check run for this annotation

Codecov / codecov/patch

app/models/bitcoin_transaction.rb#L25-L28

Added lines #L25 - L28 were not covered by tests
rescue StandardError => e
Rails.logger.error "refresh block height error: #{e.message}"

Check warning on line 30 in app/models/bitcoin_transaction.rb

View check run for this annotation

Codecov / codecov/patch

app/models/bitcoin_transaction.rb#L30

Added line #L30 was not covered by tests
end
end

# == Schema Information
Expand Down
2 changes: 2 additions & 0 deletions app/models/bitcoin_vout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class BitcoinVout < ApplicationRecord
belongs_to :cell_output, optional: true
belongs_to :ckb_address, class_name: "Address", optional: true

scope :without_op_return, -> { where(op_return: false) }

def commitment
return unless op_return?

Expand Down
6 changes: 3 additions & 3 deletions app/models/concerns/ckb_transactions/bitcoin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ def rgb_txid
def leap_direction
return unless rgb_transaction?

Check warning on line 29 in app/models/concerns/ckb_transactions/bitcoin.rb

View check run for this annotation

Codecov / codecov/patch

app/models/concerns/ckb_transactions/bitcoin.rb#L29

Added line #L29 was not covered by tests

return "in" if bitcoin_vins.count > bitcoin_vouts.count
return "out" if bitcoin_vins.count < bitcoin_vouts.count
return "in" if bitcoin_vins.count < bitcoin_vouts.without_op_return.count
return "out" if bitcoin_vins.count > bitcoin_vouts.without_op_return.count

Check warning on line 32 in app/models/concerns/ckb_transactions/bitcoin.rb

View check run for this annotation

Codecov / codecov/patch

app/models/concerns/ckb_transactions/bitcoin.rb#L31-L32

Added lines #L31 - L32 were not covered by tests

nil
end

def rgb_cell_changes
return 0 unless rgb_transaction?

Check warning on line 38 in app/models/concerns/ckb_transactions/bitcoin.rb

View check run for this annotation

Codecov / codecov/patch

app/models/concerns/ckb_transactions/bitcoin.rb#L38

Added line #L38 was not covered by tests

bitcoin_vouts.count - bitcoin_vins.count
bitcoin_vouts.without_op_return.count - bitcoin_vins.count

Check warning on line 40 in app/models/concerns/ckb_transactions/bitcoin.rb

View check run for this annotation

Codecov / codecov/patch

app/models/concerns/ckb_transactions/bitcoin.rb#L40

Added line #L40 was not covered by tests
end

def bitcoin_transaction
Expand Down

0 comments on commit 8180421

Please sign in to comment.