Skip to content

Commit

Permalink
Merge pull request #2143 from nervosnetwork/develop
Browse files Browse the repository at this point in the history
Deploy to testnet
  • Loading branch information
zmcNotafraid authored Aug 25, 2024
2 parents ba6bc86 + e6de418 commit 29ceb65
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
6 changes: 2 additions & 4 deletions app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1246,11 +1246,10 @@ def cell_input_attributes(input, ckb_transaction_id, local_block_id,
def build_ckb_transactions!(node_block, local_block, inputs, outputs, outputs_data)
cycles = CkbSync::Api.instance.get_block_cycles node_block.header.hash
ckb_transactions_attributes = []
tx_index = 0
hashes = []
header_deps = {}
witnesses = {}
node_block.transactions.each do |tx|
node_block.transactions.each_with_index do |tx, tx_index|
attrs = ckb_transaction_attributes(local_block, tx, tx_index)
if cycles
attrs[:cycles] = tx_index > 0 ? cycles[tx_index - 1]&.hex : nil
Expand All @@ -1263,8 +1262,6 @@ def build_ckb_transactions!(node_block, local_block, inputs, outputs, outputs_da
inputs[tx_index] = tx.inputs
outputs[tx_index] = tx.outputs
outputs_data[tx_index] = tx.outputs_data

tx_index += 1
end
# First update status thus we can use upsert later. otherwise, we may not be able to
# locate correct record according to tx_hash
Expand Down Expand Up @@ -1358,6 +1355,7 @@ def ckb_transaction_attributes(local_block, tx, tx_index)
is_cellbase: tx_index.zero?,
live_cell_changes: live_cell_changes(tx, tx_index),
bytes: tx.serialized_size_in_block,
tx_index: tx_index
}
end

Expand Down
1 change: 1 addition & 0 deletions app/models/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def log_deletion_chain
# bytes :bigint default(0)
# cycles :bigint
# confirmation_time :integer
# tx_index :integer
#
# Indexes
#
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240822024448_add_tx_index_to_ckb_transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTxIndexToCkbTransaction < ActiveRecord::Migration[7.0]
def change
add_column :ckb_transactions, :tx_index, :integer
end
end
27 changes: 14 additions & 13 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--

-- *not* creating schema, since initdb creates it


--
-- Name: btree_gin; Type: EXTENSION; Schema: -; Owner: -
--
Expand Down Expand Up @@ -1350,7 +1343,8 @@ CREATE TABLE public.ckb_transactions (
tags character varying[] DEFAULT '{}'::character varying[],
bytes bigint DEFAULT 0,
cycles bigint,
confirmation_time integer
confirmation_time integer,
tx_index integer
)
PARTITION BY LIST (tx_status);

Expand Down Expand Up @@ -1395,7 +1389,8 @@ CREATE TABLE public.ckb_transactions_committed (
tags character varying[] DEFAULT '{}'::character varying[],
bytes bigint DEFAULT 0,
cycles bigint,
confirmation_time integer
confirmation_time integer,
tx_index integer
);


Expand All @@ -1420,7 +1415,8 @@ CREATE TABLE public.ckb_transactions_pending (
tags character varying[] DEFAULT '{}'::character varying[],
bytes bigint DEFAULT 0,
cycles bigint,
confirmation_time integer
confirmation_time integer,
tx_index integer
);


Expand All @@ -1445,7 +1441,8 @@ CREATE TABLE public.ckb_transactions_proposed (
tags character varying[] DEFAULT '{}'::character varying[],
bytes bigint DEFAULT 0,
cycles bigint,
confirmation_time integer
confirmation_time integer,
tx_index integer
);


Expand All @@ -1470,7 +1467,8 @@ CREATE TABLE public.ckb_transactions_rejected (
tags character varying[] DEFAULT '{}'::character varying[],
bytes bigint DEFAULT 0,
cycles bigint,
confirmation_time integer
confirmation_time integer,
tx_index integer
);


Expand Down Expand Up @@ -6098,4 +6096,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20240709131020'),
('20240709131132'),
('20240709131713'),
('20240709142013');
('20240709142013'),
('20240822024448');


43 changes: 43 additions & 0 deletions lib/tasks/migration/fill_tx_index.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace :migration do
desc "Usage: RAILS_ENV=production bundle exec rake migration:fill_tx_index"
task fill_tx_index: :environment do
$retry_ids = Set.new
@api = CKB::API.new(host: ENV["CKB_NODE_URL"],

Check warning on line 5 in lib/tasks/migration/fill_tx_index.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_tx_index.rake#L4-L5

Added lines #L4 - L5 were not covered by tests
timeout_config: {
open_timeout: 1, read_timeout: 3,
write_timeout: 1
})
first_tx = CkbTransaction.tx_committed.where(tx_index: nil).order("block_number asc").select(:block_number).first
last_tx = CkbTransaction.tx_committed.where(tx_index: nil).order("block_number desc").select(:block_number).first

Check warning on line 11 in lib/tasks/migration/fill_tx_index.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_tx_index.rake#L10-L11

Added lines #L10 - L11 were not covered by tests

(first_tx.block_number..last_tx.block_number).to_a.each_slice(100).to_a.each do |range|
fill_missed_tx_index(range, 0)

Check warning on line 14 in lib/tasks/migration/fill_tx_index.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_tx_index.rake#L13-L14

Added lines #L13 - L14 were not covered by tests
end; nil

puts "retry IDS:"
puts $retry_ids.join(",")
puts "done"

Check warning on line 19 in lib/tasks/migration/fill_tx_index.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_tx_index.rake#L17-L19

Added lines #L17 - L19 were not covered by tests
end

def fill_missed_tx_index(range, retry_count)
request_body =
range.map do |number|
["get_block_by_number", number]

Check warning on line 25 in lib/tasks/migration/fill_tx_index.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_tx_index.rake#L24-L25

Added lines #L24 - L25 were not covered by tests
end
response = @api.batch_request(*request_body)
attrs = []
response.each do |r|
r[:transactions].each_with_index do |tx, index|
attrs << { tx_hash: tx[:hash], tx_status: "committed", tx_index: index }

Check warning on line 31 in lib/tasks/migration/fill_tx_index.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_tx_index.rake#L27-L31

Added lines #L27 - L31 were not covered by tests
end
end; nil
CkbTransaction.upsert_all(attrs, unique_by: %i[tx_status tx_hash])

Check warning on line 34 in lib/tasks/migration/fill_tx_index.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_tx_index.rake#L34

Added line #L34 was not covered by tests
rescue StandardError => _e
retry_count += 1
if retry_count > 2
$retry_ids << range.first

Check warning on line 38 in lib/tasks/migration/fill_tx_index.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_tx_index.rake#L36-L38

Added lines #L36 - L38 were not covered by tests
else
fill_missed_tx_index(range, retry_count)

Check warning on line 40 in lib/tasks/migration/fill_tx_index.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_tx_index.rake#L40

Added line #L40 was not covered by tests
end
end
end

0 comments on commit 29ceb65

Please sign in to comment.