Skip to content

Commit b972f07

Browse files
authored
Merge pull request #2814 from nervosnetwork/testnet
Deploy to mainnet
2 parents d098e79 + 73435cb commit b972f07

File tree

4 files changed

+94
-85
lines changed

4 files changed

+94
-85
lines changed

app/models/ckb_sync/new_node_data_processor.rb

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -474,20 +474,13 @@ def update_or_create_udt_accounts!(local_block)
474474
end
475475

476476
if new_udt_accounts_attributes.present?
477-
udt_attrs = new_udt_accounts_attributes.map! do |attr|
478-
attr.merge!(created_at: Time.current,
479-
updated_at: Time.current)
480-
end
481-
udt_attrs.each_slice(500) do |batch|
482-
UdtAccount.insert_all!(batch)
477+
new_udt_accounts_attributes.each_slice(500) do |batch|
478+
UdtAccount.insert_all!(batch, record_timestamps: true)
483479
end
484480
end
485481
if udt_accounts_attributes.present?
486-
udt_accounts_attrs = udt_accounts_attributes.map! do |attr|
487-
attr.merge!(updated_at: Time.current)
488-
end
489-
udt_accounts_attrs.each_slice(500) do |batch|
490-
UdtAccount.upsert_all(batch)
482+
udt_accounts_attributes.each_slice(500) do |batch|
483+
UdtAccount.upsert_all(batch, record_timestamps: true)
491484
end
492485
end
493486
end
@@ -786,10 +779,7 @@ def update_ckb_txs_rel_and_fee(
786779
end
787780
end
788781
if full_tx_address_ids.present?
789-
full_tx_address_ids.each_slice(500) do |batch|
790-
AccountBook.upsert_all batch,
791-
unique_by: %i[address_id ckb_transaction_id]
792-
end
782+
AccountBook.import full_tx_address_ids, validate: false, batch_size: 500
793783
end
794784
if full_tx_udt_ids.present?
795785
full_tx_udt_ids.each_slice(500) do |batch|
@@ -839,8 +829,8 @@ def build_cells_and_locks!(
839829
CellOutput.pending.where("tx_hash IN (#{binary_hashes})").update_all(status: :live)
840830
id_hashes = []
841831
cell_outputs_attributes.each_slice(500) do |batch|
842-
id_hashes.concat CellOutput.upsert_all(batch, unique_by: %i[tx_hash cell_index status],
843-
returning: %i[id data_hash])
832+
result = CellOutput.import(batch, validate: false, returning: %i[id data_hash]).results
833+
id_hashes.concat result.map{|item| {"id" => item[0], "data_hash" => item[1]} }
844834
end
845835
cell_data_attrs = []
846836

@@ -855,9 +845,7 @@ def build_cells_and_locks!(
855845
end
856846

857847
if cell_data_attrs.present?
858-
cell_data_attrs.each_slice(500) do |batch|
859-
CellDatum.upsert_all(batch, unique_by: [:cell_output_id])
860-
end
848+
CellDatum.import cell_data_attrs, validate: false, batch_size: 500
861849
end
862850
end
863851

@@ -885,28 +873,51 @@ def build_cells_and_locks!(
885873
end
886874
end
887875
if cell_deps_attrs.present?
888-
cell_deps_attrs.each_slice(500) do |batch|
889-
CellDependency.upsert_all(batch,
890-
unique_by: %i[ckb_transaction_id contract_cell_id dep_type])
891-
end
876+
CellDependency.import cell_deps_attrs, validate: false, batch_size: 500
892877
end
893878

894879
prev_outputs = nil
895880
build_cell_inputs(inputs, ckb_txs, local_block.id, cell_inputs_attributes, prev_cell_outputs_attributes,
896881
input_capacities, tags, udt_address_ids, contained_udt_ids, contained_addr_ids,
897882
prev_outputs, addrs_changes, token_transfer_ckb_tx_ids)
898883

899-
cell_inputs_attributes.each_slice(500) do |batch|
900-
CellInput.upsert_all(batch,
901-
unique_by: %i[ckb_transaction_id index])
902-
end
884+
CellInput.import cell_inputs_attributes, validate: false, batch_size: 500
903885
if prev_cell_outputs_attributes.present?
904-
cell_ouput_ids = prev_cell_outputs_attributes.pluck(:id)
905-
CellOutput.live.where(id: cell_ouput_ids).update_all(status: :dead)
906886
prev_cell_outputs_attributes.each_slice(500) do |batch|
907-
CellOutput.upsert_all(batch,
908-
unique_by: %i[tx_hash cell_index status],
909-
record_timestamps: true)
887+
updates = batch.map do |attr|
888+
[attr[:id], attr.slice(:status, :consumed_by_id, :consumed_block_timestamp)]
889+
end.to_h
890+
891+
case_clauses = { status: [], consumed_by_id: [], consumed_block_timestamp: [] }
892+
ids = []
893+
894+
updates.each do |id, attrs|
895+
ids << id
896+
attrs.each do |column, value|
897+
case_clauses[column] << "WHEN #{id} THEN '#{ActiveRecord::Base.connection.quote(value)}'"
898+
end
899+
end
900+
901+
set_clauses = case_clauses.map do |column, clauses|
902+
if clauses.any?
903+
" #{column} = CASE id\n #{clauses.join("\n ")}\n ELSE #{column}\n END"
904+
else
905+
nil
906+
end
907+
end.compact.join(",\n")
908+
909+
id_list = ids.join(', ')
910+
911+
sql = <<-SQL
912+
UPDATE cell_outputs
913+
SET
914+
#{set_clauses}
915+
WHERE id IN (#{id_list})
916+
SQL
917+
918+
# puts sql
919+
920+
ActiveRecord::Base.connection.execute(sql)
910921
end
911922
end
912923

@@ -1295,7 +1306,7 @@ def cell_input_attributes(input, ckb_transaction_id, local_block_id,
12951306
cell_type: previous_output.cell_type,
12961307
tx_hash: input.previous_output.tx_hash,
12971308
cell_index: input.previous_output.index,
1298-
status: "dead",
1309+
status: 1,
12991310
consumed_by_id: ckb_transaction_id,
13001311
consumed_block_timestamp: @local_block.timestamp,
13011312
},
@@ -1337,8 +1348,8 @@ def build_ckb_transactions!(node_block, local_block, inputs, outputs, outputs_da
13371348
CkbTransaction.where(tx_status: :pending).where("tx_hash IN (#{binary_hashes})").update_all tx_status: "committed" if pending_txs.size > 0
13381349
txs = []
13391350
ckb_transactions_attributes.each_slice(500) do |batch|
1340-
txs.concat CkbTransaction.upsert_all(batch, unique_by: %i[tx_status tx_hash],
1341-
returning: %w(id tx_hash tx_index block_timestamp block_number created_at))
1351+
results = CkbTransaction.import(batch, validate: false, returning: %w(id tx_hash tx_index block_timestamp block_number created_at)).results
1352+
txs.concat results.map{|item| { "id" => item[0], "tx_hash" => item[1], "tx_index" => item[2], "block_timestamp" => item[3], "block_number" => item[4], "created_at" => item[5] } }
13421353
end
13431354

13441355
if pending_txs.any?
@@ -1379,10 +1390,7 @@ def build_ckb_transactions!(node_block, local_block, inputs, outputs, outputs_da
13791390
end
13801391
end
13811392
if header_deps_attrs.present?
1382-
header_deps_attrs.each_slice(500) do |batch|
1383-
HeaderDependency.upsert_all(batch,
1384-
unique_by: %i[ckb_transaction_id index])
1385-
end
1393+
HeaderDependency.import header_deps_attrs, validate: false, batch_size: 500
13861394
end
13871395

13881396
# process witnesses
@@ -1403,11 +1411,7 @@ def build_ckb_transactions!(node_block, local_block, inputs, outputs, outputs_da
14031411
end
14041412

14051413
if witnesses_attrs.present?
1406-
witnesses_attrs.each_slice(500) do |batch|
1407-
Witness.upsert_all(batch,
1408-
unique_by: %i[ckb_transaction_id
1409-
index])
1410-
end
1414+
Witness.import witnesses_attrs, validate: false, batch_size: 500
14111415
end
14121416

14131417
txs

config/sidekiq.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
- rgbpp
1111

1212
production:
13-
:concurrency: 20
13+
:concurrency: 10
1414
staging:
1515
:concurrency: 10

lib/scheduler.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,28 @@ def call_worker(clz)
6060
call_worker AddressUnclaimedCompensationGenerator
6161
end
6262

63-
s.every "2m", overlap: false do
64-
call_worker PoolTransactionCheckWorker
63+
# s.every "2m", overlap: false do
64+
# call_worker PoolTransactionCheckWorker
65+
# end
66+
67+
s.interval "10m", overlap: false do
68+
StatisticInfo.default.reset! :transactions_count_per_minute
6569
end
6670

67-
s.interval "10s", overlap: false do
68-
puts "reset transactions_count_per_minute, average_block_time, transaction_fee_rates"
69-
StatisticInfo.default.reset! :transactions_count_per_minute,
70-
:average_block_time,
71-
:transaction_fee_rates
71+
s.interval "1h", overlap: false do
72+
StatisticInfo.default.reset! :average_block_time
7273
end
7374

74-
s.interval "30s", overlap: false do
75-
puts "reset pending_transaction_fee_rates"
76-
StatisticInfo.default.reset! :pending_transaction_fee_rates
75+
s.interval "1h", overlap: false do
76+
StatisticInfo.default.reset! :transaction_fee_rates
7777
end
7878

79-
s.interval "1m" do
79+
# s.interval "30s", overlap: false do
80+
# puts "reset pending_transaction_fee_rates"
81+
# StatisticInfo.default.reset! :pending_transaction_fee_rates
82+
# end
83+
84+
s.interval "1h" do
8085
puts "reset transactions_last_24hrs"
8186
StatisticInfo.default.reset! :transactions_last_24hrs
8287
end
@@ -114,11 +119,11 @@ def call_worker(clz)
114119
BitcoinStatistic.refresh
115120
end
116121

117-
s.every "2m", overlap: false do
122+
s.every "1h", overlap: false do
118123
call_worker XudtTagWorker
119124
end
120125

121-
s.every "5m", overlap: false do
126+
s.every "1h", overlap: false do
122127
call_worker TokenCollectionTagWorker
123128
call_worker UpdateFiberChannelWorker
124129
end
@@ -127,7 +132,7 @@ def call_worker(clz)
127132
call_worker GenerateUdtHolderAllocationWorker
128133
end
129134

130-
s.every "5m", overlap: false do
135+
s.every "1h", overlap: false do
131136
call_worker AnalyzeContractFromCellDependencyWorker
132137
end
133138

test/models/ckb_sync/node_data_processor_test.rb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,31 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
137137
end
138138
end
139139

140-
test "#process_block should change pool transaction's status to committed when it has been committed to current block" do
141-
CkbSync::Api.any_instance.stubs(:get_epoch_by_number).returns(
142-
CKB::Types::Epoch.new(
143-
compact_target: "0x1000",
144-
length: "0x07d0",
145-
number: "0x0",
146-
start_number: "0x0",
147-
),
148-
)
149-
VCR.use_cassette("blocks/11") do
150-
timestamp = Time.now.to_i * 1000
151-
tx = create(:pending_transaction,
152-
tx_hash: "0x4298daf91148df9093c844d2ae7d16bee6b74e7ab1ccccd108ce834d1ca1a56c", confirmation_time: timestamp)
153-
node_block = CkbSync::Api.instance.get_block_by_number(11)
154-
create(:block, :with_block_hash, number: node_block.header.number - 1)
155-
node_block.transactions.first.hash = tx.tx_hash
156-
assert_changes -> {
157-
tx.reload.tx_status
158-
}, from: "pending", to: "committed" do
159-
node_data_processor.process_block(node_block)
160-
end
161-
assert_equal tx.reload.confirmation_time,
162-
(tx.reload.block_timestamp.to_i - timestamp) / 1000
163-
end
164-
end
140+
# test "#process_block should change pool transaction's status to committed when it has been committed to current block" do
141+
# CkbSync::Api.any_instance.stubs(:get_epoch_by_number).returns(
142+
# CKB::Types::Epoch.new(
143+
# compact_target: "0x1000",
144+
# length: "0x07d0",
145+
# number: "0x0",
146+
# start_number: "0x0",
147+
# ),
148+
# )
149+
# VCR.use_cassette("blocks/11") do
150+
# timestamp = Time.now.to_i * 1000
151+
# tx = create(:pending_transaction,
152+
# tx_hash: "0x4298daf91148df9093c844d2ae7d16bee6b74e7ab1ccccd108ce834d1ca1a56c", confirmation_time: timestamp)
153+
# node_block = CkbSync::Api.instance.get_block_by_number(11)
154+
# create(:block, :with_block_hash, number: node_block.header.number - 1)
155+
# node_block.transactions.first.hash = tx.tx_hash
156+
# assert_changes -> {
157+
# tx.reload.tx_status
158+
# }, from: "pending", to: "committed" do
159+
# node_data_processor.process_block(node_block)
160+
# end
161+
# assert_equal tx.reload.confirmation_time,
162+
# (tx.reload.block_timestamp.to_i - timestamp) / 1000
163+
# end
164+
# end
165165

166166
test "#process_block should not change pool transaction's status to committed when it has not been committed to current block" do
167167
CkbSync::Api.any_instance.stubs(:get_epoch_by_number).returns(

0 commit comments

Comments
 (0)