Skip to content

Commit

Permalink
Issue 419 2 (#2145)
Browse files Browse the repository at this point in the history
* feat: add unique index to lock_scripts and type_scripts

Signed-off-by: Miles Zhang <[email protected]>

* fix: use upsert_all to save lock_script and type_script

Signed-off-by: Miles Zhang <[email protected]>

* test: fix test

Signed-off-by: Miles Zhang <[email protected]>

---------

Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid authored Sep 10, 2024
1 parent 1901d01 commit 43dc3e9
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 65 deletions.
17 changes: 7 additions & 10 deletions app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,10 @@ def build_cells_and_locks!(
lock_scripts_attributes.map! do |attr|
attr.merge!(created_at: Time.current, updated_at: Time.current)
end
lock_script_ids = LockScript.insert_all!(lock_scripts_attributes).map do |e|
e["id"]
end
lock_script_ids = LockScript.upsert_all(lock_scripts_attributes, unique_by: :script_hash, returning: [:id])

lock_script_ids.each do |lock_script_id|
lock_script_ids.each do |row|
lock_script_id = row["id"]
lock_script = LockScript.find lock_script_id
contract = Contract.find_by code_hash: lock_script.code_hash
temp_hash = { script_hash: lock_script&.script_hash, is_contract: false }
Expand All @@ -824,11 +823,9 @@ def build_cells_and_locks!(
type_scripts_attributes.map! do |attr|
attr.merge!(created_at: Time.current, updated_at: Time.current)
end
type_script_ids = TypeScript.insert_all!(type_scripts_attributes).map do |e|
e["id"]
end

type_script_ids.each do |type_script_id|
type_script_ids = TypeScript.upsert_all(type_scripts_attributes, unique_by: :script_hash, returning: [:id])
type_script_ids.each do |row|
type_script_id = row["id"]
type_script = TypeScript.find(type_script_id)
temp_hash = { script_hash: type_script&.script_hash, is_contract: false }
contract = Contract.find_by code_hash: type_script.code_hash
Expand Down Expand Up @@ -1355,7 +1352,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
tx_index:,
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/lock_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ def tip_epoch(tip_block)
# index_lock_scripts_on_address_id (address_id)
# index_lock_scripts_on_cell_output_id (cell_output_id)
# index_lock_scripts_on_code_hash_and_hash_type_and_args (code_hash,hash_type,args)
# index_lock_scripts_on_script_hash (script_hash) USING hash
# index_lock_scripts_on_script_hash (script_hash) UNIQUE
# index_lock_scripts_on_script_id (script_id)
#
2 changes: 1 addition & 1 deletion app/models/type_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ def calculate_bytesize
#
# index_type_scripts_on_cell_output_id (cell_output_id)
# index_type_scripts_on_code_hash_and_hash_type_and_args (code_hash,hash_type,args)
# index_type_scripts_on_script_hash (script_hash) USING hash
# index_type_scripts_on_script_hash (script_hash) UNIQUE
# index_type_scripts_on_script_id (script_id)
#
13 changes: 13 additions & 0 deletions db/migrate/20240823071323_add_unique_index_to_lock_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddUniqueIndexToLockScript < ActiveRecord::Migration[7.0]
def up
# set 1 hour
execute "SET statement_timeout = 3600000"

remove_index :lock_scripts, :script_hash
add_index :lock_scripts, :script_hash, unique: true
end

def down
execute "RESET statement_timeout"
end
end
13 changes: 13 additions & 0 deletions db/migrate/20240823071420_add_unique_index_to_type_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddUniqueIndexToTypeScript < ActiveRecord::Migration[7.0]
def up
# set 1 hour
execute "SET statement_timeout = 3600000"

remove_index :type_scripts, :script_hash
add_index :type_scripts, :script_hash, unique: true
end

def down
execute "RESET statement_timeout"
end
end
8 changes: 4 additions & 4 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4788,7 +4788,7 @@ CREATE INDEX index_lock_scripts_on_code_hash_and_hash_type_and_args ON public.lo
-- Name: index_lock_scripts_on_script_hash; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_lock_scripts_on_script_hash ON public.lock_scripts USING hash (script_hash);
CREATE UNIQUE INDEX index_lock_scripts_on_script_hash ON public.lock_scripts USING btree (script_hash);


--
Expand Down Expand Up @@ -4998,7 +4998,7 @@ CREATE INDEX index_type_scripts_on_code_hash_and_hash_type_and_args ON public.ty
-- Name: index_type_scripts_on_script_hash; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_type_scripts_on_script_hash ON public.type_scripts USING hash (script_hash);
CREATE UNIQUE INDEX index_type_scripts_on_script_hash ON public.type_scripts USING btree (script_hash);


--
Expand Down Expand Up @@ -5942,6 +5942,6 @@ INSERT INTO "schema_migrations" (version) VALUES
('20240709131713'),
('20240709142013'),
('20240822024448'),
('20240823071323'),
('20240823071420'),
('20240902025657');


1 change: 1 addition & 0 deletions test/factories/type_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
code_hash { "0x00000000000000000000000000000000000000000000000000545950455f4944" }
hash_type { "type" }
end
after(:build) { |ts, _context| ts.script_hash = CKB::Types::Script.new(code_hash: ts.code_hash, args: ts.args, hash_type: ts.hash_type).compute_hash }
end
end
39 changes: 5 additions & 34 deletions test/models/ckb_sync/node_data_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2839,14 +2839,8 @@ class NodeDataProcessorTest < ActiveSupport::TestCase

create(:type_script, args: udt_script.args, code_hash: Settings.sudt_cell_type_hash, hash_type: "data",
cell_output: output1)
create(:type_script, args: udt_script.args, code_hash: Settings.sudt_cell_type_hash, hash_type: "data",
cell_output: output2)
create(:type_script, args: udt_script.args, code_hash: Settings.sudt_cell_type_hash, hash_type: "data",
cell_output: output3)
create(:type_script, args: udt_script1.args, code_hash: Settings.sudt_cell_type_hash, hash_type: "data",
cell_output: output4)
create(:type_script, args: udt_script1.args, code_hash: Settings.sudt_cell_type_hash, hash_type: "data",
cell_output: output5)
Address.create(lock_hash: udt_script.args,
address_hash: "0x#{SecureRandom.hex(32)}")
Address.create(lock_hash: udt_script1.args,
Expand Down Expand Up @@ -3112,15 +3106,6 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
create(:type_script, args: udt_script.args,
code_hash: Settings.sudt_cell_type_hash,
hash_type: "data")
create(:type_script, args: udt_script.args,
code_hash: Settings.sudt_cell_type_hash,
hash_type: "data")
create(:type_script, args: udt_script.args,
code_hash: Settings.sudt_cell_type_hash,
hash_type: "data")
create(:type_script, args: udt_script1.args,
code_hash: Settings.sudt_cell_type_hash,
hash_type: "data")
create(:type_script, args: udt_script1.args,
code_hash: Settings.sudt_cell_type_hash,
hash_type: "data")
Expand Down Expand Up @@ -3434,12 +3419,6 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
tx_hash: tx4.tx_hash, cell_index: 0, address: input_address4, cell_type: "udt", lock_script_id: address4_lock.id, type_script_id: type_script1.id, type_hash: udt_script1.compute_hash)
output5 = create(:cell_output, ckb_transaction: tx5, block: block2, capacity: 70000 * 10**8,
tx_hash: tx5.tx_hash, cell_index: 0, address: input_address5, cell_type: "udt", lock_script_id: address5_lock.id, type_script_id: type_script2.id, type_hash: udt_script2.compute_hash)
create(:type_script, args: udt_script1.args, code_hash: Settings.sudt_cell_type_hash, hash_type: "data",
cell_output: output1)
create(:type_script, args: udt_script1.args, code_hash: Settings.sudt_cell_type_hash, hash_type: "data",
cell_output: output2)
create(:type_script, args: udt_script2.args, code_hash: Settings.sudt_cell_type_hash, hash_type: "data",
cell_output: output3)
output1.update(type_hash: CKB::Types::Script.new(**output1.type_script.to_node).compute_hash)
output2.update(type_hash: CKB::Types::Script.new(**output2.type_script.to_node).compute_hash)
output3.update(type_hash: CKB::Types::Script.new(**output3.type_script.to_node).compute_hash)
Expand All @@ -3449,7 +3428,6 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
address_hash: "0x#{SecureRandom.hex(32)}")
Address.create(lock_hash: udt_script2.args,
address_hash: "0x#{SecureRandom.hex(32)}")

header = CKB::Types::BlockHeader.new(compact_target: "0x1000", hash: "0x#{SecureRandom.hex(32)}",
number: DEFAULT_NODE_BLOCK_NUMBER, parent_hash: "0x#{SecureRandom.hex(32)}", nonce: 1757392074788233522, timestamp: CkbUtils.time_in_milliseconds(Time.current), transactions_root: "0x#{SecureRandom.hex(32)}", proposals_hash: "0x#{SecureRandom.hex(32)}", extra_hash: "0x#{SecureRandom.hex(32)}", version: 0, epoch: 1, dao: "0x01000000000000000000c16ff286230000a3a65e97fd03000057c138586f0000")
inputs = [
Expand Down Expand Up @@ -3643,12 +3621,6 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
hash_type: "data")
type_script2 = create(:type_script, args: udt_script2.args, code_hash: Settings.sudt_cell_type_hash,
hash_type: "data")
type_script3 = create(:type_script, args: udt_script1.args, code_hash: Settings.sudt_cell_type_hash,
hash_type: "data")
type_script4 = create(:type_script, args: udt_script1.args, code_hash: Settings.sudt_cell_type_hash,
hash_type: "data")
type_script5 = create(:type_script, args: udt_script2.args, code_hash: Settings.sudt_cell_type_hash,
hash_type: "data")
udt1 = create(:udt, type_hash: CKB::Types::Script.new(**type_script1.to_node).compute_hash,
args: udt_script1.args, ckb_transactions_count: 3)
udt2 = create(:udt, type_hash: CKB::Types::Script.new(**type_script2.to_node).compute_hash,
Expand Down Expand Up @@ -3688,11 +3660,11 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
output2 = create(:cell_output, ckb_transaction: tx2, block: block2, capacity: 60000 * 10**8,
tx_hash: tx2.tx_hash, cell_index: 1, address: input_address2, cell_type: "udt", lock_script_id: address2_lock.id, type_script_id: type_script2.id)
output3 = create(:cell_output, ckb_transaction: tx3, block: block2, capacity: 70000 * 10**8,
tx_hash: tx3.tx_hash, cell_index: 2, address: input_address3, cell_type: "udt", lock_script_id: address3_lock.id, type_script_id: type_script3.id)
tx_hash: tx3.tx_hash, cell_index: 2, address: input_address3, cell_type: "udt", lock_script_id: address3_lock.id, type_script_id: type_script1.id)
output4 = create(:cell_output, ckb_transaction: tx4, block: block2, capacity: 70000 * 10**8,
tx_hash: tx4.tx_hash, cell_index: 0, address: input_address4, cell_type: "udt", lock_script_id: address4_lock.id, type_script_id: type_script4.id)
tx_hash: tx4.tx_hash, cell_index: 0, address: input_address4, cell_type: "udt", lock_script_id: address4_lock.id, type_script_id: type_script1.id)
output5 = create(:cell_output, ckb_transaction: tx5, block: block2, capacity: 70000 * 10**8,
tx_hash: tx5.tx_hash, cell_index: 0, address: input_address5, cell_type: "udt", lock_script_id: address5_lock.id, type_script_id: type_script5.id)
tx_hash: tx5.tx_hash, cell_index: 0, address: input_address5, cell_type: "udt", lock_script_id: address5_lock.id, type_script_id: type_script2.id)
output1.update(type_hash: CKB::Types::Script.new(**output1.type_script.to_node).compute_hash)
output2.update(type_hash: CKB::Types::Script.new(**output2.type_script.to_node).compute_hash)
output3.update(type_hash: CKB::Types::Script.new(**output3.type_script.to_node).compute_hash)
Expand Down Expand Up @@ -3960,9 +3932,8 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
hash_type: "type",
args: old_factory_cell.args,
)
type_script1 = create(:type_script, args: factory_cell_script.args,
code_hash: factory_cell_script.code_hash,
hash_type: "type")
type_script1 = TypeScript.find_by(script_hash: factory_cell_script.compute_hash)

block1 = create(:block, :with_block_hash,
number: DEFAULT_NODE_BLOCK_NUMBER - 1)
tx1 = create(:ckb_transaction, block: block1)
Expand Down
25 changes: 10 additions & 15 deletions test/models/ckb_transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -685,25 +685,20 @@ class CkbTransactionTest < ActiveSupport::TestCase
nrc_721_token_output_block = create(:block, :with_block_hash)
nrc_721_token_output_transaction = create(:ckb_transaction,
block: nrc_721_token_output_block)

nrc_factory_cell = create(:nrc_factory_cell,
code_hash: "0xf31f9c3241043976c93db9f203d765ce2c1d96465c918b93fbb117cd35bcfb98",
args: "0x47a6262383ad9b266c6fae7c460dc39261e4f914e77467426809ce0fd607baec",
verified: true)
nrc_721_factory_type_script = create(:type_script,
code_hash: nrc_factory_cell.code_hash,
hash_type: "type",
args: nrc_factory_cell.args,
script_hash: "0xcf4f2494ea1fbebcd07a7445415a0e8b36be214af6257065888dae0ce1434434")
nrc_721_factory_cell_output = create(:cell_output,
block: nrc_721_token_output_block,
ckb_transaction: nrc_721_token_output_transaction,
cell_type: "nrc_721_factory",
cell_index: 1,
tx_hash: nrc_721_token_output_transaction.tx_hash,
data: "0x24ff5a9ab8c38d195ce2b4ea75ca8987000a47616d62697420317374000000156465762e6b6f6c6c6563742e6d652f746f6b656e73000000000000003c000000000000000000",
type_hash: "0x",
type_script_id: nrc_721_factory_type_script.id)
nrc_721_factory_type_script = TypeScript.find_by(script_hash: "0xcf4f2494ea1fbebcd07a7445415a0e8b36be214af6257065888dae0ce1434434")
_nrc_721_factory_cell_output = create(:cell_output,
block: nrc_721_token_output_block,
ckb_transaction: nrc_721_token_output_transaction,
cell_type: "nrc_721_factory",
cell_index: 1,
tx_hash: nrc_721_token_output_transaction.tx_hash,
data: "0x24ff5a9ab8c38d195ce2b4ea75ca8987000a47616d62697420317374000000156465762e6b6f6c6c6563742e6d652f746f6b656e73000000000000003c000000000000000000",
type_hash: "0x",
type_script_id: nrc_721_factory_type_script.id)

nrc_721_token_type_script = create(:type_script,
code_hash: "0x#{SecureRandom.hex(32)}",
Expand Down

0 comments on commit 43dc3e9

Please sign in to comment.