Skip to content

Commit 79b31a2

Browse files
authored
Merge pull request #2891 from nervosnetwork/develop
Deploy to testnet
2 parents af37e8a + 5b79632 commit 79b31a2

19 files changed

+34
-208
lines changed

app/models/address.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ def unmade_dao_interests
222222
# average_deposit_time :bigint
223223
# unclaimed_compensation :decimal(30, )
224224
# is_depositor :boolean default(FALSE)
225-
# dao_transactions_count :bigint default(0)
226225
# lock_script_id :bigint
227226
# balance_occupied :decimal(30, ) default(0)
228227
# last_updated_block_number :bigint

app/models/address_block_snapshot.rb

Lines changed: 0 additions & 22 deletions
This file was deleted.

app/models/ckb_sync/new_node_data_processor.rb

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class NewNodeDataProcessor
88
include Redis::Objects
99

1010
value :reorg_started_at, global: true
11-
attr_accessor :local_tip_block, :pending_raw_block, :ckb_txs, :target_block, :target_block_number, :addrs_changes,
11+
attr_accessor :local_tip_block, :pending_raw_block, :ckb_txs, :target_block, :target_block_number,
1212
:outputs, :inputs, :outputs_data, :udt_address_ids, :contained_address_ids,
1313
:contained_udt_ids, :cell_datas, :enable_cota, :token_transfer_ckb_tx_ids, :addr_tx_changes, :redis_keys, :tx_previous_outputs
1414

@@ -75,11 +75,10 @@ def process_block(node_block, refresh_balance: true)
7575

7676
benchmark :process_ckb_txs, node_block, ckb_txs, contained_address_ids,
7777
contained_udt_ids, tags, udt_address_ids
78-
@addrs_changes = Hash.new { |hash, key| hash[key] = {} }
7978
@addr_tx_changes = Hash.new { |h, k| h[k] = Hash.new(0) }
8079

8180
input_capacities, output_capacities = benchmark :build_cells_and_locks!, local_block, node_block, ckb_txs, inputs, outputs,
82-
tags, udt_address_ids, contained_udt_ids, contained_address_ids, addrs_changes, token_transfer_ckb_tx_ids, cell_deps
81+
tags, udt_address_ids, contained_udt_ids, contained_address_ids, token_transfer_ckb_tx_ids, cell_deps
8382

8483
# update explorer data
8584
benchmark :update_ckb_txs_rel_and_fee, ckb_txs, tags, input_capacities, output_capacities, udt_address_ids,
@@ -91,7 +90,6 @@ def process_block(node_block, refresh_balance: true)
9190
benchmark :update_or_create_udt_accounts!, local_block
9291
# maybe can be changed to asynchronous update
9392
benchmark :process_dao_events!, local_block
94-
# benchmark :update_addresses_info, addrs_changes, local_block, refresh_balance
9593
end
9694

9795
async_update_udt_infos(local_block)
@@ -732,7 +730,7 @@ def update_ckb_txs_rel_and_fee(
732730

733731
def build_cells_and_locks!(
734732
local_block, node_block, ckb_txs, inputs, outputs, tags, udt_address_ids,
735-
contained_udt_ids, contained_addr_ids, addrs_changes, token_transfer_ckb_tx_ids, cell_deps
733+
contained_udt_ids, contained_addr_ids, token_transfer_ckb_tx_ids, cell_deps
736734
)
737735
cell_outputs_attributes = []
738736
cell_inputs_attributes = []
@@ -755,7 +753,7 @@ def build_cells_and_locks!(
755753
build_addresses!(outputs, local_block)
756754

757755
build_cell_outputs!(node_block, outputs, ckb_txs, local_block, cell_outputs_attributes, output_capacities, tags,
758-
udt_address_ids, contained_udt_ids, contained_addr_ids, addrs_changes, token_transfer_ckb_tx_ids)
756+
udt_address_ids, contained_udt_ids, contained_addr_ids, token_transfer_ckb_tx_ids)
759757
if cell_outputs_attributes.present?
760758
tx_hashes = cell_outputs_attributes.pluck(:tx_hash)
761759
binary_hashes = CkbUtils.hexes_to_bins_sql(tx_hashes)
@@ -812,7 +810,7 @@ def build_cells_and_locks!(
812810
prev_outputs = nil
813811
build_cell_inputs(inputs, ckb_txs, local_block.id, cell_inputs_attributes, prev_cell_outputs_attributes,
814812
input_capacities, tags, udt_address_ids, contained_udt_ids, contained_addr_ids,
815-
prev_outputs, addrs_changes, token_transfer_ckb_tx_ids)
813+
prev_outputs, token_transfer_ckb_tx_ids)
816814

817815
CellInput.import cell_inputs_attributes, validate: false, batch_size: 500
818816
if prev_cell_outputs_attributes.present?
@@ -920,7 +918,7 @@ def script_attributes(script, script_hash)
920918

921919
def build_cell_inputs(
922920
inputs, ckb_txs, local_block_id, cell_inputs_attributes, prev_cell_outputs_attributes,
923-
input_capacities, tags, udt_address_ids, contained_udt_ids, contained_addr_ids, prev_outputs, addrs_changes, token_transfer_ckb_tx_ids
921+
input_capacities, tags, udt_address_ids, contained_udt_ids, contained_addr_ids, prev_outputs, token_transfer_ckb_tx_ids
924922
)
925923
conditions = []
926924

@@ -960,7 +958,6 @@ def build_cell_inputs(
960958
# attributes[2] is previous_cell_output capacity
961959
# attributes[3] is previous_cell_output type_hash
962960
# attributes[4] is previous_cell address_id
963-
# attributes[5] is previous_cell data
964961
attributes = cell_input_attributes(item, ckb_txs[tx_index]["id"],
965962
local_block_id, prev_outputs, index)
966963
cell_inputs_attributes << attributes[:cell_input]
@@ -969,28 +966,13 @@ def build_cell_inputs(
969966
address_id = attributes[:address_id]
970967
capacity = attributes[:capacity]
971968
type_hash = attributes[:type_hash]
972-
data = attributes[:data]
973-
change_rec = addrs_changes[address_id]
974-
975969
addr_tx_changes[tx_index][address_id] -= capacity
976-
change_rec[:balance_diff] ||= 0
977-
change_rec[:balance_diff] -= capacity
978-
change_rec[:balance_occupied_diff] ||= 0
979-
change_rec[:balance_occupied_diff] -= capacity if occupied?(
980-
type_hash, data
981-
)
982-
change_rec[:cells_diff] ||= 0
983-
change_rec[:cells_diff] -= 1
984-
change_rec[:ckb_txs] ||= Set.new
985-
change_rec[:ckb_txs] << ckb_txs[tx_index]["tx_hash"]
986970

987971
prev_cell_outputs_attributes << previous_output
988972
contained_addr_ids[tx_index] << address_id
989973
cell_type = previous_output[:cell_type].to_s
990974
if cell_type.in?(%w(nervos_dao_withdrawing))
991975
tags[tx_index] << "dao"
992-
change_rec[:dao_txs] ||= Set.new
993-
change_rec[:dao_txs] << ckb_txs[tx_index]["tx_hash"]
994976
elsif cell_type.in?(%w(m_nft_token nrc_721_token spore_cell did_cell))
995977
token_transfer_ckb_tx_ids << ckb_txs[tx_index]["id"]
996978
end
@@ -1038,7 +1020,7 @@ def build_cell_inputs(
10381020

10391021
def build_cell_outputs!(
10401022
node_block, outputs, ckb_txs, local_block, cell_outputs_attributes, output_capacities,
1041-
tags, udt_address_ids, contained_udt_ids, contained_addr_ids, addrs_changes, token_transfer_ckb_tx_ids
1023+
tags, udt_address_ids, contained_udt_ids, contained_addr_ids, token_transfer_ckb_tx_ids
10421024
)
10431025
outputs.each do |tx_index, items|
10441026
cell_index = 0
@@ -1061,24 +1043,9 @@ def build_cell_outputs!(
10611043
@redis_keys << key
10621044
end
10631045
end
1064-
cell_data = node_block.transactions[tx_index].outputs_data[cell_index]
1065-
change_rec = addrs_changes[address_id]
1046+
10661047
addr_tx_changes[tx_index][address_id] += item.capacity
1067-
1068-
change_rec[:balance_diff] ||= 0
1069-
change_rec[:balance_diff] += item.capacity
1070-
1071-
change_rec[:balance_occupied_diff] ||= 0
10721048
type_script_hash = item.type&.compute_hash
1073-
change_rec[:balance_occupied_diff] += item.capacity if occupied?(
1074-
type_script_hash, cell_data
1075-
)
1076-
1077-
change_rec[:cells_diff] ||= 0
1078-
change_rec[:cells_diff] += 1
1079-
1080-
change_rec[:ckb_txs] ||= Set.new
1081-
change_rec[:ckb_txs] << ckb_txs[tx_index]["tx_hash"]
10821049

10831050
contained_addr_ids[tx_index] << address_id
10841051
attr = cell_output_attributes(item, address_id, ckb_txs[tx_index], local_block, cell_index,
@@ -1087,8 +1054,6 @@ def build_cell_outputs!(
10871054

10881055
if attr[:cell_type].in?(%w(nervos_dao_deposit nervos_dao_withdrawing))
10891056
tags[tx_index] << "dao"
1090-
change_rec[:dao_txs] ||= Set.new
1091-
change_rec[:dao_txs] << ckb_txs[tx_index]["tx_hash"]
10921057
end
10931058

10941059
if attr[:cell_type] == "udt"
@@ -1131,10 +1096,6 @@ def build_cell_outputs!(
11311096
end
11321097
end
11331098

1134-
def occupied?(type_hash, cell_data)
1135-
(cell_data.present? && cell_data != "0x") || type_hash.present?
1136-
end
1137-
11381099
def cell_output_attributes(output, address_id, ckb_transaction, local_block, cell_index, output_data)
11391100
lock_script_hash = output.lock.compute_hash
11401101
key = "lock_script_hash_#{lock_script_hash}"
@@ -1246,7 +1207,6 @@ def cell_input_attributes(input, ckb_transaction_id, local_block_id,
12461207
capacity: previous_output.capacity,
12471208
type_hash: previous_output.type_hash,
12481209
address_id: previous_output.address_id,
1249-
# data: previous_output.data
12501210
}
12511211
end
12521212
end

app/workers/calculate_address_info_worker.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@ def perform()
1616
end
1717
Rails.cache.write(key, local_tip_block.id)
1818

19-
Address.where(id: contained_address_ids).find_in_batches do |group|
20-
sleep(50) # Make sure it doesn't get too crowded in there!
19+
Address.where(id: contained_address_ids).find_in_batches(batch_size: 100).with_index do |group, batch|
20+
puts "Processing group ##{batch}"
2121
address_attributes = []
2222

2323
group.each do |addr|
24+
# puts addr.id
2425
balance, balance_occupied = addr.cal_balance
2526
address_attributes << {
2627
id: addr.id,
2728
balance: balance,
2829
balance_occupied: balance_occupied,
2930
ckb_transactions_count: AccountBook.where(address_id: addr.id).count,
3031
live_cells_count: addr.cell_outputs.live.count,
31-
dao_transactions_count: addr.ckb_dao_transactions.count,
32-
created_at: addr.created_at,
3332
updated_at: Time.current
3433
}
3534

app/workers/update_address_info_worker.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ def perform(block_number)
1515
balance_occupied: balance_occupied,
1616
ckb_transactions_count: AccountBook.where(address_id: addr.id).count,
1717
live_cells_count: addr.cell_outputs.live.count,
18-
dao_transactions_count: addr.ckb_dao_transactions.count,
19-
created_at: addr.created_at,
2018
updated_at: Time.current
2119
}
2220
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RemoveDaoTxCountOfAddress < ActiveRecord::Migration[7.0]
2+
def change
3+
remove_columns :addresses, :dao_transactions_count, type: :bigint
4+
end
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RemoveAddressBlockSnapshot < ActiveRecord::Migration[7.0]
2+
def change
3+
drop_table :address_block_snapshots, if_exists: true
4+
end
5+
end

db/structure.sql

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -60,38 +60,6 @@ CREATE SEQUENCE public.account_books_id_seq
6060
ALTER SEQUENCE public.account_books_id_seq OWNED BY public.account_books.id;
6161

6262

63-
--
64-
-- Name: address_block_snapshots; Type: TABLE; Schema: public; Owner: -
65-
--
66-
67-
CREATE TABLE public.address_block_snapshots (
68-
id bigint NOT NULL,
69-
address_id bigint,
70-
block_id bigint,
71-
block_number bigint,
72-
final_state jsonb
73-
);
74-
75-
76-
--
77-
-- Name: address_block_snapshots_id_seq; Type: SEQUENCE; Schema: public; Owner: -
78-
--
79-
80-
CREATE SEQUENCE public.address_block_snapshots_id_seq
81-
START WITH 1
82-
INCREMENT BY 1
83-
NO MINVALUE
84-
NO MAXVALUE
85-
CACHE 1;
86-
87-
88-
--
89-
-- Name: address_block_snapshots_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
90-
--
91-
92-
ALTER SEQUENCE public.address_block_snapshots_id_seq OWNED BY public.address_block_snapshots.id;
93-
94-
9563
--
9664
-- Name: address_udt_transactions; Type: TABLE; Schema: public; Owner: -
9765
--
@@ -123,7 +91,6 @@ CREATE TABLE public.addresses (
12391
average_deposit_time bigint,
12492
unclaimed_compensation numeric(30,0),
12593
is_depositor boolean DEFAULT false,
126-
dao_transactions_count bigint DEFAULT 0.0,
12794
lock_script_id bigint,
12895
balance_occupied numeric(30,0) DEFAULT 0.0,
12996
last_updated_block_number bigint
@@ -2771,13 +2738,6 @@ ALTER TABLE ONLY public.ckb_transactions ATTACH PARTITION public.ckb_transaction
27712738
ALTER TABLE ONLY public.account_books ALTER COLUMN id SET DEFAULT nextval('public.account_books_id_seq'::regclass);
27722739

27732740

2774-
--
2775-
-- Name: address_block_snapshots id; Type: DEFAULT; Schema: public; Owner: -
2776-
--
2777-
2778-
ALTER TABLE ONLY public.address_block_snapshots ALTER COLUMN id SET DEFAULT nextval('public.address_block_snapshots_id_seq'::regclass);
2779-
2780-
27812741
--
27822742
-- Name: addresses id; Type: DEFAULT; Schema: public; Owner: -
27832743
--
@@ -3199,14 +3159,6 @@ ALTER TABLE ONLY public.account_books
31993159
ADD CONSTRAINT account_books_pkey PRIMARY KEY (id);
32003160

32013161

3202-
--
3203-
-- Name: address_block_snapshots address_block_snapshots_pkey; Type: CONSTRAINT; Schema: public; Owner: -
3204-
--
3205-
3206-
ALTER TABLE ONLY public.address_block_snapshots
3207-
ADD CONSTRAINT address_block_snapshots_pkey PRIMARY KEY (id);
3208-
3209-
32103162
--
32113163
-- Name: addresses addresses_pkey; Type: CONSTRAINT; Schema: public; Owner: -
32123164
--
@@ -4362,27 +4314,6 @@ CREATE INDEX index_account_books_on_block_number_and_tx_index ON public.account_
43624314
CREATE INDEX index_account_books_on_ckb_transaction_id ON public.account_books USING btree (ckb_transaction_id);
43634315

43644316

4365-
--
4366-
-- Name: index_address_block_snapshots_on_address_id; Type: INDEX; Schema: public; Owner: -
4367-
--
4368-
4369-
CREATE INDEX index_address_block_snapshots_on_address_id ON public.address_block_snapshots USING btree (address_id);
4370-
4371-
4372-
--
4373-
-- Name: index_address_block_snapshots_on_block_id; Type: INDEX; Schema: public; Owner: -
4374-
--
4375-
4376-
CREATE INDEX index_address_block_snapshots_on_block_id ON public.address_block_snapshots USING btree (block_id);
4377-
4378-
4379-
--
4380-
-- Name: index_address_block_snapshots_on_block_id_and_address_id; Type: INDEX; Schema: public; Owner: -
4381-
--
4382-
4383-
CREATE UNIQUE INDEX index_address_block_snapshots_on_block_id_and_address_id ON public.address_block_snapshots USING btree (block_id, address_id);
4384-
4385-
43864317
--
43874318
-- Name: index_address_udt_transactions_on_ckb_transaction_id; Type: INDEX; Schema: public; Owner: -
43884319
--
@@ -6005,6 +5936,8 @@ INSERT INTO "schema_migrations" (version) VALUES
60055936
('20250827065749'),
60065937
('20250930015526'),
60075938
('20251011011714'),
6008-
('20251013082609');
5939+
('20251013082609'),
5940+
('20251027053353'),
5941+
('20251027054232');
60095942

60105943

lib/scheduler.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ def call_worker(clz)
115115
call_worker ContractStatisticWorker
116116
end
117117

118+
s.every "10h" overlap: false do
119+
call_worker CalculateAddressInfoWorker
120+
end
121+
118122
s.cron "0,30 * * * *" do
119123
BitcoinStatistic.refresh
120124
end

lib/tasks/migration/check_address_info.rake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace :migration do
99
address.last_updated_block_number = local_tip_block.number
1010
address.live_cells_count = address.cell_outputs.live.where("block_timestamp <= ?", local_tip_block.timestamp).count
1111
address.ckb_transactions_count = AccountBook.where(address_id: address.id).where("block_number <= ?", local_tip_block.number).count
12-
address.dao_transactions_count = DaoEvent.processed.where(address_id: address.id).where("block_timestamp <= ?", local_tip_block.timestamp).distinct.count(:ckb_transaction_id)
1312
address.cal_balance!
1413
address.save!
1514
end

0 commit comments

Comments
 (0)