Skip to content

Commit 90aea3a

Browse files
authored
Merge pull request #2493 from nervosnetwork/testnet
Deploy to mainnet
2 parents c6c0c1e + edfed1a commit 90aea3a

File tree

13 files changed

+142
-108
lines changed

13 files changed

+142
-108
lines changed

Gemfile.lock

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ GEM
8989
activerecord (>= 3.2, < 8.0)
9090
rake (>= 10.4, < 14.0)
9191
ansi (1.5.0)
92-
ast (2.4.2)
92+
ast (2.4.3)
9393
async (2.3.1)
9494
console (~> 1.10)
9595
io-event (~> 1.1)
@@ -248,7 +248,7 @@ GEM
248248
jbuilder (2.11.5)
249249
actionview (>= 5.0.0)
250250
activesupport (>= 5.0.0)
251-
json (2.6.3)
251+
json (2.10.2)
252252
json_pure (2.7.1)
253253
jwt (2.7.1)
254254
kaminari (1.2.2)
@@ -266,7 +266,9 @@ GEM
266266
kredis (1.3.0.1)
267267
activesupport (>= 6.0.0)
268268
redis (>= 4.2, < 6)
269+
language_server-protocol (3.17.0.4)
269270
leb128 (1.0.0)
271+
lint_roller (1.1.0)
270272
listen (3.7.1)
271273
rb-fsevent (~> 0.10, >= 0.10.3)
272274
rb-inotify (~> 0.9, >= 0.9.10)
@@ -313,13 +315,15 @@ GEM
313315
racc (~> 1.4)
314316
pagy (5.10.1)
315317
activesupport
316-
parallel (1.22.1)
318+
parallel (1.26.3)
317319
paranoia (2.6.4)
318320
activerecord (>= 5.1, < 7.2)
319-
parser (3.2.2.0)
321+
parser (3.3.7.4)
320322
ast (~> 2.4.1)
323+
racc
321324
pg (1.4.5)
322325
pkg-config (1.5.7)
326+
prism (1.4.0)
323327
protocol-hpack (1.4.2)
324328
protocol-http (0.24.7)
325329
protocol-http1 (0.15.1)
@@ -400,20 +404,22 @@ GEM
400404
connection_pool
401405
redis-objects (2.0.0.beta)
402406
redis (~> 5.0)
403-
regexp_parser (2.7.0)
404-
rexml (3.3.9)
405-
rubocop (1.50.1)
407+
regexp_parser (2.10.0)
408+
rexml (3.4.1)
409+
rubocop (1.75.2)
406410
json (~> 2.3)
411+
language_server-protocol (~> 3.17.0.2)
412+
lint_roller (~> 1.1.0)
407413
parallel (~> 1.10)
408-
parser (>= 3.2.0.0)
414+
parser (>= 3.3.0.2)
409415
rainbow (>= 2.2.2, < 4.0)
410-
regexp_parser (>= 1.8, < 3.0)
411-
rexml (>= 3.2.5, < 4.0)
412-
rubocop-ast (>= 1.28.0, < 2.0)
416+
regexp_parser (>= 2.9.3, < 3.0)
417+
rubocop-ast (>= 1.44.0, < 2.0)
413418
ruby-progressbar (~> 1.7)
414-
unicode-display_width (>= 2.4.0, < 3.0)
415-
rubocop-ast (1.28.0)
416-
parser (>= 3.2.1.0)
419+
unicode-display_width (>= 2.4.0, < 4.0)
420+
rubocop-ast (1.44.0)
421+
parser (>= 3.3.7.2)
422+
prism (~> 1.4)
417423
rubocop-performance (1.15.0)
418424
rubocop (>= 1.7.0, < 2.0)
419425
rubocop-ast (>= 0.4.0)
@@ -469,7 +475,9 @@ GEM
469475
unf (0.1.4)
470476
unf_ext
471477
unf_ext (0.0.8.2)
472-
unicode-display_width (2.4.2)
478+
unicode-display_width (3.1.4)
479+
unicode-emoji (~> 4.0, >= 4.0.4)
480+
unicode-emoji (4.0.4)
473481
vcr (6.1.0)
474482
webmock (3.18.1)
475483
addressable (>= 2.8.0)

app/jobs/revert_block_job.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def update_address_balance_and_ckb_transactions_count(local_tip_block)
4848
address.update!(attrs)
4949
else
5050
address.live_cells_count = address.cell_outputs.live.count
51-
# address.ckb_transactions_count = address.custom_ckb_transactions.count
5251
address.ckb_transactions_count = AccountBook.where(address_id: address.id).count
5352
address.dao_transactions_count = DaoEvent.processed.where(address_id: address.id).distinct(:ckb_transaction_id).count
5453
address.cal_balance!

app/models/address.rb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Address < ApplicationRecord
1818
has_one :bitcoin_address_mapping, foreign_key: "ckb_address_id"
1919
has_one :bitcoin_address, through: :bitcoin_address_mapping
2020

21-
validates :balance, :cell_consumed, :ckb_transactions_count, :interest, :dao_deposit,
21+
validates :balance, :ckb_transactions_count, :interest, :dao_deposit,
2222
numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
2323
validates :lock_hash, presence: true, uniqueness: true
2424

@@ -249,26 +249,26 @@ def unmade_dao_interests
249249
#
250250
# Table name: addresses
251251
#
252-
# id :bigint not null, primary key
253-
# balance :decimal(30, ) default(0)
254-
# address_hash :binary
255-
# cell_consumed :bigint
256-
# ckb_transactions_count :bigint default(0)
257-
# created_at :datetime not null
258-
# updated_at :datetime not null
259-
# lock_hash :binary
260-
# dao_deposit :decimal(30, ) default(0)
261-
# interest :decimal(30, ) default(0)
262-
# block_timestamp :bigint
263-
# live_cells_count :bigint default(0)
264-
# mined_blocks_count :integer default(0)
265-
# visible :boolean default(TRUE)
266-
# average_deposit_time :bigint
267-
# unclaimed_compensation :decimal(30, )
268-
# is_depositor :boolean default(FALSE)
269-
# dao_transactions_count :bigint default(0)
270-
# lock_script_id :bigint
271-
# balance_occupied :decimal(30, ) default(0)
252+
# id :bigint not null, primary key
253+
# balance :decimal(30, ) default(0)
254+
# address_hash :binary
255+
# ckb_transactions_count :bigint default(0)
256+
# created_at :datetime not null
257+
# updated_at :datetime not null
258+
# lock_hash :binary
259+
# dao_deposit :decimal(30, ) default(0)
260+
# interest :decimal(30, ) default(0)
261+
# block_timestamp :bigint
262+
# live_cells_count :bigint default(0)
263+
# mined_blocks_count :integer default(0)
264+
# visible :boolean default(TRUE)
265+
# average_deposit_time :bigint
266+
# unclaimed_compensation :decimal(30, )
267+
# is_depositor :boolean default(FALSE)
268+
# dao_transactions_count :bigint default(0)
269+
# lock_script_id :bigint
270+
# balance_occupied :decimal(30, ) default(0)
271+
# last_updated_block_number :bigint
272272
#
273273
# Indexes
274274
#

app/models/ckb_sync/new_node_data_processor.rb

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -487,44 +487,53 @@ def update_mining_info(local_block)
487487
def update_addresses_info(addrs_change, local_block, refresh_balance)
488488
return unless refresh_balance
489489

490+
snapshot_attrs = []
490491
### because `upsert` don't validate record, so it may pass invalid data into database.
491492
### here we use one by one update (maybe slower)
492493
addrs_change.each do |addr_id, values|
493494
addr = Address.find addr_id
494495
check_invalid_address(addr)
495496

496-
balance_diff = values[:balance_diff]
497-
balance_occupied_diff = values[:balance_occupied_diff].presence || 0
498-
live_cells_diff = values[:cells_diff]
499-
dao_txs_count = values[:dao_txs].present? ? values[:dao_txs].size : 0
500-
ckb_txs_count = values[:ckb_txs].present? ? values[:ckb_txs].size : 0
501-
502-
addr.update!(
503-
balance: addr.balance + balance_diff,
504-
balance_occupied: addr.balance_occupied + balance_occupied_diff,
505-
ckb_transactions_count: addr.ckb_transactions_count + ckb_txs_count,
506-
live_cells_count: addr.live_cells_count + live_cells_diff,
507-
dao_transactions_count: addr.dao_transactions_count + dao_txs_count,
508-
)
509-
510-
save_address_block_snapshot!(addr, local_block)
497+
if addr.last_updated_block_number.nil?
498+
addr.last_updated_block_number = local_block.number
499+
addr.live_cells_count = addr.cell_outputs.live.count
500+
addr.ckb_transactions_count = AccountBook.where(address_id: addr.id).count
501+
addr.dao_transactions_count = DaoEvent.processed.where(address_id: addr.id).distinct(:ckb_transaction_id).count
502+
addr.cal_balance!
503+
addr.save!
504+
else
505+
balance_diff = values[:balance_diff]
506+
balance_occupied_diff = values[:balance_occupied_diff].presence || 0
507+
live_cells_diff = values[:cells_diff]
508+
dao_txs_count = values[:dao_txs].present? ? values[:dao_txs].size : 0
509+
ckb_txs_count = values[:ckb_txs].present? ? values[:ckb_txs].size : 0
510+
511+
addr.update!(
512+
last_updated_block_number: local_block.number,
513+
balance: addr.balance + balance_diff,
514+
balance_occupied: addr.balance_occupied + balance_occupied_diff,
515+
ckb_transactions_count: addr.ckb_transactions_count + ckb_txs_count,
516+
live_cells_count: addr.live_cells_count + live_cells_diff,
517+
dao_transactions_count: addr.dao_transactions_count + dao_txs_count,
518+
)
519+
end
520+
addr.reload
521+
snapshot_attrs << {
522+
block_number: local_block.number,
523+
final_state: {
524+
balance: addr.balance,
525+
balance_occupied: addr.balance_occupied,
526+
ckb_transactions_count: addr.ckb_transactions_count,
527+
live_cells_count: addr.live_cells_count,
528+
dao_transactions_count: addr.dao_transactions_count,
529+
last_updated_block_number: local_block.number
530+
},
531+
address_id: addr.id,
532+
block_id: local_block.id,
533+
}
511534
end
512-
end
513535

514-
def save_address_block_snapshot!(addr, local_block)
515-
AddressBlockSnapshot.create_with(
516-
block_number: local_block.number,
517-
final_state: {
518-
balance: addr.balance,
519-
balance_occupied: addr.balance_occupied,
520-
ckb_transactions_count: addr.ckb_transactions_count,
521-
live_cells_count: addr.live_cells_count,
522-
dao_transactions_count: addr.dao_transactions_count,
523-
},
524-
).find_or_create_by!(
525-
address_id: addr.id,
526-
block_id: local_block.id,
527-
)
536+
AddressBlockSnapshot.upsert_all(snapshot_attrs, unique_by: %i[block_id address_id]) if snapshot_attrs.present?
528537
end
529538

530539
def update_block_info!(local_block)

app/utils/ckb_utils.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,6 @@ def self.get_balance(address_hash)
212212
get_unspent_cells(address_hash).sum(:capacity)
213213
end
214214

215-
def self.address_cell_consumed(address_hash)
216-
return if address_hash.blank?
217-
218-
address_cell_consumed = 0
219-
get_unspent_cells(address_hash).find_each do |cell_output|
220-
address_cell_consumed += calculate_cell_min_capacity(
221-
cell_output.node_output, cell_output.data
222-
)
223-
end
224-
225-
address_cell_consumed
226-
end
227-
228215
def self.update_block_reward!(current_block)
229216
target_block_number = current_block.target_block_number
230217
target_block = current_block.target_block
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddLastUpdatedBlockNumberToAddress < ActiveRecord::Migration[7.0]
2+
def change
3+
remove_columns :addresses, :cell_consumed
4+
add_column :addresses, :last_updated_block_number, :bigint
5+
end
6+
end

db/structure.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ CREATE TABLE public.addresses (
400400
id bigint NOT NULL,
401401
balance numeric(30,0) DEFAULT 0,
402402
address_hash bytea,
403-
cell_consumed bigint,
404403
ckb_transactions_count bigint DEFAULT 0.0,
405404
created_at timestamp without time zone NOT NULL,
406405
updated_at timestamp without time zone NOT NULL,
@@ -416,7 +415,8 @@ CREATE TABLE public.addresses (
416415
is_depositor boolean DEFAULT false,
417416
dao_transactions_count bigint DEFAULT 0.0,
418417
lock_script_id bigint,
419-
balance_occupied numeric(30,0) DEFAULT 0.0
418+
balance_occupied numeric(30,0) DEFAULT 0.0,
419+
last_updated_block_number bigint
420420
);
421421

422422

@@ -6400,6 +6400,7 @@ INSERT INTO "schema_migrations" (version) VALUES
64006400
('20250126022459'),
64016401
('20250218062041'),
64026402
('20250311084903'),
6403-
('20250318021630');
6403+
('20250318021630'),
6404+
('20250403090946');
64046405

64056406

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace :migration do
2+
desc "Usage: RAILS_ENV=production bundle exec rake migration:check_address_info"
3+
task check_address_info: :environment do
4+
ActiveRecord::Base.connection.execute("SET statement_timeout = 0")
5+
Address.where(last_updated_block_number: nil).find_each do |address|
6+
puts "#{Time.now}-#{address.id}"
7+
if address.last_updated_block_number.nil?
8+
local_tip_block = Block.recent.first
9+
address.last_updated_block_number = local_tip_block.number
10+
address.live_cells_count = address.cell_outputs.live.where("block_timestamp <= ?", local_tip_block.timestamp).count
11+
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(:ckb_transaction_id).count
13+
address.cal_balance!
14+
address.save!
15+
end
16+
end
17+
puts "done"
18+
end
19+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace :migration do
2+
desc "Usage: RAILS_ENV=production bundle exec rake migration:check_cell_inputs_data[0,10000]"
3+
task :check_cell_inputs_data, %i[start_block end_block] => :environment do |_, args|
4+
ActiveRecord::Base.connection.execute("SET statement_timeout = 0")
5+
error_ids = []
6+
(args[:start_block].to_i..args[:end_block].to_i).to_a.each_slice(1000).each do |range|
7+
puts range[0]
8+
CkbTransaction.tx_committed.where(block_number: range[0]..range[-1]).where(is_cellbase: false).each do |tx|
9+
if tx.cell_inputs.count != tx.inputs.count
10+
error_ids << tx.id
11+
end
12+
end
13+
end
14+
15+
puts "error IDS:"
16+
puts error_ids.join(",")
17+
puts "=============="
18+
puts "done"
19+
end
20+
end

test/factories/address.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
end
88

99
balance { 0 }
10-
cell_consumed { 0 }
1110
ckb_transactions_count { 0 }
1211
lock_hash { "0x#{SecureRandom.hex(32)}" }
12+
last_updated_block_number { 10 }
1313

1414
transient do
1515
transactions_count { 3 }

0 commit comments

Comments
 (0)