Skip to content

Commit 4904ab0

Browse files
authored
Merge pull request #2473 from nervosnetwork/testnet
Deploy to mainnet
2 parents 077a67d + c02913c commit 4904ab0

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

app/models/fiber_graph_node.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def deleted_at_timestamp
4646

4747
(deleted_at.to_f * 1000).to_i.to_s
4848
end
49+
50+
def created_timestamp
51+
(created_at.to_f * 1000).to_i.to_s
52+
end
4953
end
5054

5155
# == Schema Information

app/views/api/v2/fiber/graph_nodes/index.jbuilder

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
json.data do
22
json.fiber_graph_nodes @nodes do |node|
3-
json.(node, :node_name, :node_id, :addresses, :peer_id, :timestamp, :chain_hash, :connected_node_ids, :open_channels_count, :last_updated_timestamp, :deleted_at_timestamp)
3+
json.(node, :node_name, :node_id, :addresses, :peer_id, :timestamp, :chain_hash, :connected_node_ids, :open_channels_count,
4+
:last_updated_timestamp, :deleted_at_timestamp, :created_timestamp)
45
json.timestamp node.timestamp.to_s
56
json.auto_accept_min_ckb_funding_amount node.auto_accept_min_ckb_funding_amount.to_s
67
json.total_capacity node.total_capacity.to_s

app/views/api/v2/fiber/graph_nodes/show.jbuilder

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
json.data do
2-
json.(@node, :node_name, :node_id, :addresses, :peer_id, :timestamp, :chain_hash, :connected_node_ids, :last_updated_timestamp, :deleted_at_timestamp)
2+
json.(@node, :node_name, :node_id, :addresses, :peer_id, :timestamp, :chain_hash, :connected_node_ids, :last_updated_timestamp,
3+
:deleted_at_timestamp, :created_timestamp)
34
json.timestamp @node.timestamp.to_s
45
json.auto_accept_min_ckb_funding_amount @node.auto_accept_min_ckb_funding_amount.to_s
56
json.total_capacity @node.total_capacity.to_s

lib/tasks/migration/fill_account_book_block_number_and_tx_index.rake

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
namespace :migration do
22
desc "Usage: RAILS_ENV=production bundle exec rake migration:fill_account_book_block_number_and_tx_index[0,1000000]"
33
task :fill_account_book_block_number_and_tx_index, %i[start_block end_block] => :environment do |_, args|
4-
ActiveRecord::Base.connection.execute("SET statement_timeout = 0")
54
$missed_tx_ids = []
65
(args[:start_block].to_i..args[:end_block].to_i).to_a.each do |block_number|
76
puts block_number
87
attrs = Set.new
9-
CkbTransaction.joins(:block).includes(:inputs, :outputs).where(block: { number: block_number }).where(is_cellbase: false).each do |tx|
8+
CkbTransaction.joins(:block).includes(:inputs, :outputs).where(block: { number: block_number }).each do |tx|
109
outputs = tx.outputs.pluck(:address_id, :capacity).group_by { |item| item[0] }.
1110
transform_values { |values| values.sum { |v| v[1] } }
1211
inputs = tx.inputs.pluck(:address_id, :capacity).group_by { |item| item[0] }.
1312
transform_values { |values| values.sum { |v| v[1] } }
1413
address_ids = (outputs.keys + inputs.keys).uniq
15-
exists = ensure_all_data_exists(address_ids, tx.id)
16-
if exists
17-
address_ids.each do |address_id|
18-
income = (outputs[address_id] || 0) - (inputs[address_id] || 0)
19-
attrs << { address_id:, ckb_transaction_id: tx.id, income:, block_number: tx.block_number, tx_index: tx.tx_index }
14+
if address_ids.present?
15+
if ensure_all_data_exists(address_ids, tx.id)
16+
address_ids.each do |address_id|
17+
income = (outputs[address_id] || 0) - (inputs[address_id] || 0)
18+
attrs << { address_id:, ckb_transaction_id: tx.id, income:, block_number: tx.block_number, tx_index: tx.tx_index }
19+
end
20+
else
21+
$missed_tx_ids << tx.id
2022
end
21-
else
22-
$missed_tx_ids << tx.id
2323
end
2424
end
2525
AccountBook.upsert_all(attrs.to_a, unique_by: %i[address_id ckb_transaction_id]) if attrs.present?
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace :migration do
2+
desc "Usage: RAILS_ENV=production bundle exec rake migration:fill_account_book_block_number_and_tx_index_for_cellbase[0,1000000]"
3+
task :fill_account_book_block_number_and_tx_index_for_cellbase, %i[start_block end_block] => :environment do |_, args|
4+
$missed_tx_ids = []
5+
(args[:start_block].to_i..args[:end_block].to_i).to_a.each do |block_number|
6+
puts block_number
7+
attrs = Set.new
8+
CkbTransaction.joins(:block).includes(:inputs, :outputs).where(block: { number: block_number }).where(is_cellbase: true).each do |tx|
9+
outputs = tx.outputs.pluck(:address_id, :capacity).group_by { |item| item[0] }.
10+
transform_values { |values| values.sum { |v| v[1] } }
11+
inputs = tx.inputs.pluck(:address_id, :capacity).group_by { |item| item[0] }.
12+
transform_values { |values| values.sum { |v| v[1] } }
13+
address_ids = (outputs.keys + inputs.keys).uniq
14+
if address_ids.present?
15+
if ensure_all_data_exists(address_ids, tx.id)
16+
address_ids.each do |address_id|
17+
income = (outputs[address_id] || 0) - (inputs[address_id] || 0)
18+
attrs << { address_id:, ckb_transaction_id: tx.id, income:, block_number: tx.block_number, tx_index: tx.tx_index }
19+
end
20+
else
21+
$missed_tx_ids << tx.id
22+
end
23+
end
24+
end
25+
AccountBook.upsert_all(attrs.to_a, unique_by: %i[address_id ckb_transaction_id]) if attrs.present?
26+
end
27+
28+
puts $missed_tx_ids.join(",")
29+
puts "done"
30+
end
31+
def ensure_all_data_exists(address_ids, tx_id)
32+
data =
33+
address_ids.map do |address_id|
34+
{ address_id:, ckb_transaction_id: tx_id }
35+
end
36+
query_conditions = data.map { |d| "(address_id = #{d[:address_id]} AND ckb_transaction_id = #{d[:ckb_transaction_id]})" }.join(" OR ")
37+
existing_records = AccountBook.where(query_conditions).pluck(:address_id, :ckb_transaction_id)
38+
data.map(&:values).to_set.subset?(existing_records.to_set)
39+
end
40+
end

0 commit comments

Comments
 (0)