Skip to content

Commit 7902f0a

Browse files
authored
Merge pull request #2832 from nervosnetwork/testnet
Deploy to mainnet
2 parents fa5d10d + e9cfae4 commit 7902f0a

File tree

8 files changed

+129
-13
lines changed

8 files changed

+129
-13
lines changed

app/interactions/addresses/ckb_transactions.rb

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,31 @@ class CkbTransactions < ActiveInteraction::Base
1010

1111
def execute
1212
is_bitcoin = BitcoinUtils.valid_address?(key)
13-
address = Explore.run!(key:)
14-
raise AddressNotFoundError if address.is_a?(NullAddress)
1513

16-
address_id = address.map(&:id)
17-
account_books = AccountBook.tx_committed.where(address_id:).
18-
order(account_books_ordering).
19-
select(:ckb_transaction_id).
20-
distinct.page(page).per(page_size)
14+
if is_bitcoin
15+
account_books = BtcAccountBook.includes(:bitcoin_address).
16+
where(bitcoin_address: { address_hash: key }).
17+
order(account_books_ordering).
18+
page(page).per(page_size)
19+
else
20+
address = Explore.run!(key:)
21+
raise AddressNotFoundError if address.is_a?(NullAddress)
22+
23+
address_id = address.map(&:id)
24+
account_books = AccountBook.tx_committed.where(address_id:).
25+
order(account_books_ordering).
26+
select(:ckb_transaction_id).
27+
page(page).per(page_size)
28+
end
29+
30+
ckb_transaction_ids = account_books.map(&:ckb_transaction_id)
2131

2232
includes = { :cell_inputs => [:previous_cell_output], :outputs => {}, :bitcoin_annotation => [] }
2333
includes[:bitcoin_transfers] = {} if is_bitcoin
2434

25-
records = CkbTransaction.where(id: account_books.map(&:ckb_transaction_id))
35+
records = CkbTransaction.where(id: ckb_transaction_ids)
2636
.includes(includes)
27-
# .select(select_fields + ["COUNT(cell_inputs.id) AS cell_inputs_count", "COUNT(cell_outputs.id) AS cell_outputs_count"])
2837
.select(select_fields)
29-
# .joins("LEFT JOIN cell_inputs ON cell_inputs.ckb_transaction_id = ckb_transactions.id")
30-
# .joins("LEFT JOIN cell_outputs ON cell_outputs.ckb_transaction_id = ckb_transactions.id")
31-
# .group(select_fields.join(','))
3238
.order(transactions_ordering)
3339

3440
options = paginate_options(records, address_id)

app/jobs/import_rgbpp_cells_job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def build_address!(address_hash, cell_output)
172172
create_with(bitcoin_address_id: bitcoin_address.id).
173173
find_or_create_by!(ckb_address_id: cell_output.address_id)
174174

175+
BtcAccountBook.find_or_create_by!(ckb_transaction_id: cell_output.ckb_transaction_id, bitcoin_address_id: bitcoin_address.id)
176+
175177
bitcoin_address
176178
end
177179

app/models/btc_account_book.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class BtcAccountBook < ApplicationRecord
2+
belongs_to :bitcoin_address
3+
end
4+
5+
# == Schema Information
6+
#
7+
# Table name: btc_account_books
8+
#
9+
# id :bigint not null, primary key
10+
# ckb_transaction_id :bigint
11+
# bitcoin_address_id :bigint
12+
#
13+
# Indexes
14+
#
15+
# index_btc_account_books_on_bitcoin_address_id (bitcoin_address_id)
16+
#
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateBtcAccountBooks < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :btc_account_books do |t|
4+
t.bigint :ckb_transaction_id
5+
t.bigint :bitcoin_address_id
6+
end
7+
8+
add_index :btc_account_books, :bitcoin_address_id
9+
end
10+
end

db/structure.sql

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,36 @@ CREATE SEQUENCE public.blocks_id_seq
627627
ALTER SEQUENCE public.blocks_id_seq OWNED BY public.blocks.id;
628628

629629

630+
--
631+
-- Name: btc_account_books; Type: TABLE; Schema: public; Owner: -
632+
--
633+
634+
CREATE TABLE public.btc_account_books (
635+
id bigint NOT NULL,
636+
ckb_transaction_id bigint,
637+
bitcoin_address_id bigint
638+
);
639+
640+
641+
--
642+
-- Name: btc_account_books_id_seq; Type: SEQUENCE; Schema: public; Owner: -
643+
--
644+
645+
CREATE SEQUENCE public.btc_account_books_id_seq
646+
START WITH 1
647+
INCREMENT BY 1
648+
NO MINVALUE
649+
NO MAXVALUE
650+
CACHE 1;
651+
652+
653+
--
654+
-- Name: btc_account_books_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
655+
--
656+
657+
ALTER SEQUENCE public.btc_account_books_id_seq OWNED BY public.btc_account_books.id;
658+
659+
630660
--
631661
-- Name: cell_data; Type: TABLE; Schema: public; Owner: -
632662
--
@@ -2832,6 +2862,13 @@ ALTER TABLE ONLY public.block_statistics ALTER COLUMN id SET DEFAULT nextval('pu
28322862
ALTER TABLE ONLY public.blocks ALTER COLUMN id SET DEFAULT nextval('public.blocks_id_seq'::regclass);
28332863

28342864

2865+
--
2866+
-- Name: btc_account_books id; Type: DEFAULT; Schema: public; Owner: -
2867+
--
2868+
2869+
ALTER TABLE ONLY public.btc_account_books ALTER COLUMN id SET DEFAULT nextval('public.btc_account_books_id_seq'::regclass);
2870+
2871+
28352872
--
28362873
-- Name: cell_data cell_output_id; Type: DEFAULT; Schema: public; Owner: -
28372874
--
@@ -3274,6 +3311,14 @@ ALTER TABLE ONLY public.blocks
32743311
ADD CONSTRAINT blocks_pkey PRIMARY KEY (id);
32753312

32763313

3314+
--
3315+
-- Name: btc_account_books btc_account_books_pkey; Type: CONSTRAINT; Schema: public; Owner: -
3316+
--
3317+
3318+
ALTER TABLE ONLY public.btc_account_books
3319+
ADD CONSTRAINT btc_account_books_pkey PRIMARY KEY (id);
3320+
3321+
32773322
--
32783323
-- Name: cell_data cell_data_pkey; Type: CONSTRAINT; Schema: public; Owner: -
32793324
--
@@ -4527,6 +4572,13 @@ CREATE INDEX index_blocks_on_number ON public.blocks USING btree (number);
45274572
CREATE INDEX index_blocks_on_timestamp ON public.blocks USING btree ("timestamp" DESC NULLS LAST);
45284573

45294574

4575+
--
4576+
-- Name: index_btc_account_books_on_bitcoin_address_id; Type: INDEX; Schema: public; Owner: -
4577+
--
4578+
4579+
CREATE INDEX index_btc_account_books_on_bitcoin_address_id ON public.btc_account_books USING btree (bitcoin_address_id);
4580+
4581+
45304582
--
45314583
-- Name: index_cell_dependencies_on_block_number_and_tx_index; Type: INDEX; Schema: public; Owner: -
45324584
--
@@ -5937,6 +5989,7 @@ INSERT INTO "schema_migrations" (version) VALUES
59375989
('20250715043211'),
59385990
('20250826022054'),
59395991
('20250827065749'),
5940-
('20250930015526');
5992+
('20250930015526'),
5993+
('20251011011714');
59415994

59425995

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace :migration do
2+
task fill_btc_account_book: :environment do
3+
BitcoinAddressMapping.find_in_batches.with_index do |group, batch|
4+
puts "Processing group ##{batch}"
5+
group.each do |address_mapping|
6+
ckb_address_id = address_mapping.ckb_address_id
7+
bitcoin_address_id = address_mapping.bitcoin_address_id
8+
limit = AccountBook.where(address_id: ckb_address_id).count
9+
ckb_transaction_ids = AccountBook.where(address_id: ckb_address_id).limit(limit).map(&:ckb_transaction_id)
10+
ckb_transaction_ids.each do |ckb_transaction_id|
11+
BtcAccountBook.find_or_create_by!(ckb_transaction_id: ckb_transaction_id, bitcoin_address_id: bitcoin_address_id)
12+
end
13+
Rails.cache.write("fill_btc_account_book_job", address_mapping.id)
14+
end
15+
end
16+
end
17+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FactoryBot.define do
2+
factory :btc_account_book do
3+
4+
end
5+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class BtcAccountBookTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)