-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add tx_index to ckb_transactions Signed-off-by: Miles Zhang <[email protected]> * feat: use task to fill tx_index Signed-off-by: Miles Zhang <[email protected]> * feat: save tx_index when sync node Signed-off-by: Miles Zhang <[email protected]> --------- Signed-off-by: Miles Zhang <[email protected]>
- Loading branch information
1 parent
0755668
commit e6de418
Showing
5 changed files
with
65 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
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 | ||
|
||
(first_tx.block_number..last_tx.block_number).to_a.each_slice(100).to_a.each do |range| | ||
fill_missed_tx_index(range, 0) | ||
end; nil | ||
|
||
puts "retry IDS:" | ||
puts $retry_ids.join(",") | ||
puts "done" | ||
end | ||
|
||
def fill_missed_tx_index(range, retry_count) | ||
request_body = | ||
range.map do |number| | ||
["get_block_by_number", number] | ||
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 } | ||
end | ||
end; nil | ||
CkbTransaction.upsert_all(attrs, unique_by: %i[tx_status tx_hash]) | ||
rescue StandardError => _e | ||
retry_count += 1 | ||
if retry_count > 2 | ||
$retry_ids << range.first | ||
else | ||
fill_missed_tx_index(range, retry_count) | ||
end | ||
end | ||
end |