Skip to content

Commit

Permalink
Merge pull request #1586 from nervosnetwork/develop
Browse files Browse the repository at this point in the history
Deploy to testnet
  • Loading branch information
rabbitz authored Jan 18, 2024
2 parents daac744 + a96ade1 commit 014b21c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 92 deletions.
12 changes: 7 additions & 5 deletions app/models/ckb_sync/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ def chain_type
end

def mode
if chain_type == "ckb"
CKB::MODE::MAINNET
else
CKB::MODE::TESTNET
end
# if chain_type == "ckb"
# CKB::MODE::MAINNET
# else
# CKB::MODE::TESTNET
# end

ENV["CKB_NET_MODE"]
end

def issuer_script_code_hash
Expand Down
69 changes: 30 additions & 39 deletions app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,64 +70,48 @@ def process_block(node_block, refresh_balance: true)
@tx_cell_deps = {}
@ckb_txs = build_ckb_transactions!(node_block, local_block, inputs,
outputs, outputs_data).to_a
build_udts!(local_block, outputs, outputs_data)
benchmark :build_udts!, local_block, outputs, outputs_data

tags = []
@udt_address_ids = udt_address_ids = []
@dao_address_ids = dao_address_ids = []
@contained_udt_ids = contained_udt_ids = []
@contained_address_ids = contained_address_ids = []

process_ckb_txs(node_block, ckb_txs, contained_address_ids, contained_udt_ids, dao_address_ids, tags,
udt_address_ids)
benchmark :process_ckb_txs, node_block, ckb_txs, contained_address_ids,
contained_udt_ids, dao_address_ids, tags, udt_address_ids
addrs_changes = Hash.new { |hash, key| hash[key] = {} }
input_capacities, output_capacities = build_cells_and_locks!(local_block, node_block, ckb_txs, inputs, outputs,
tags, udt_address_ids, dao_address_ids, contained_udt_ids, contained_address_ids, addrs_changes)

input_capacities, output_capacities = benchmark :build_cells_and_locks!, local_block, node_block, ckb_txs, inputs, outputs,
tags, udt_address_ids, dao_address_ids, contained_udt_ids, contained_address_ids, addrs_changes

# update explorer data
update_ckb_txs_rel_and_fee(ckb_txs, tags, input_capacities, output_capacities, udt_address_ids,
dao_address_ids, contained_udt_ids, contained_address_ids)
update_block_info!(local_block)
update_block_reward_info!(local_block)
update_mining_info(local_block)
update_table_records_count(local_block)
update_or_create_udt_accounts!(local_block)
benchmark :update_ckb_txs_rel_and_fee, ckb_txs, tags, input_capacities, output_capacities, udt_address_ids,
dao_address_ids, contained_udt_ids, contained_address_ids
benchmark :update_block_info!, local_block
benchmark :update_block_reward_info!, local_block
benchmark :update_mining_info, local_block
benchmark :update_table_records_count, local_block
benchmark :update_or_create_udt_accounts!, local_block
# maybe can be changed to asynchronous update
update_udt_info(local_block)
process_dao_events!(local_block)
update_addresses_info(addrs_changes, local_block) if refresh_balance
benchmark :update_udt_info, local_block
benchmark :process_dao_events!, local_block
if refresh_balance
benchmark :update_addresses_info, addrs_changes, local_block
end
end

flush_inputs_outputs_caches(local_block)
generate_statistics_data(local_block)
generate_deployed_cells_and_referring_cells(local_block)
benchmark :flush_inputs_outputs_caches, local_block
benchmark :generate_statistics_data, local_block
benchmark :generate_deployed_cells_and_referring_cells, local_block

FetchCotaWorker.perform_async(local_block.number) if enable_cota
local_block.update_counter_for_ckb_node_version

local_block
end

add_transaction_tracer :process_block, category: :task
add_method_tracer :build_udts!, "Custom/build_udt!"
add_method_tracer :process_ckb_txs, "Custom/process_ckb_txs"
add_method_tracer :build_cells_and_locks!, "Custom/build_cells_and_locks!"
add_method_tracer :update_ckb_txs_rel_and_fee,
"Custom/update_ckb_txs_rel_and_fee"
add_method_tracer :update_block_info!, "Custom/update_block_info!"
add_method_tracer :update_block_reward_info!,
"Custom/update_block_reward_info!"
add_method_tracer :update_mining_info, "Custom/update_mining_info"
add_method_tracer :update_table_records_count,
"Custom/update_table_records_count"
add_method_tracer :update_or_create_udt_accounts!,
"Custom/update_or_create_udt_accounts!"
add_method_tracer :update_udt_info, "Custom/update_udt_info"
add_method_tracer :process_dao_events!, "Custom/process_dao_events!"
add_method_tracer :update_addresses_info, "Custom/update_addresses_info"
add_method_tracer :generate_statistics_data,
"Custom/generate_statistics_data"
add_method_tracer :generate_deployed_cells_and_referring_cells,
"Custom/generate_deployed_cells_and_referring_cells"

def check_invalid_address(address)
if (address.balance < 0) || (address.balance_occupied < 0)
Expand Down Expand Up @@ -675,7 +659,8 @@ def build_udts!(local_block, outputs, outputs_data)
udts_attributes = Set.new
outputs.each do |tx_index, items|
items.each_with_index do |output, index|
cell_type = cell_type(output.type, outputs_data[tx_index][index])
cell_type = benchmark :cell_type, output.type,
outputs_data[tx_index][index]
if cell_type == "omiga_inscription_info"
info = CkbUtils.parse_omiga_inscription_info(outputs_data[tx_index][index])
OmigaInscriptionInfo.upsert(info.merge(output.type.to_h),
Expand Down Expand Up @@ -1600,6 +1585,12 @@ def update_nrc_factory_cell_info(type_script, output_data)
)
end

def benchmark(method_name = nil, *args)
ApplicationRecord.benchmark method_name do
send(method_name, *args)
end
end

class LocalCache
attr_accessor :cache

Expand Down
31 changes: 7 additions & 24 deletions app/utils/ckb_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,25 +404,6 @@ def self.decode_header_deps(raw_header_deps)
# @param [String] output_data
# @return [String] cell type
def self.cell_type(type_script, output_data)
if type_script&.code_hash == CkbSync::Api.instance.xudt_code_hash &&
((ENV["CKB_NET_MODE"] == CKB::MODE::TESTNET && type_script&.hash_type == "type") ||
(ENV["CKB_NET_MODE"] == CKB::MODE::MAINNET && type_script&.hash_type == "data1"))
str = Kredis.string type_script.compute_hash
unless str.value
if OmigaInscriptionInfo.exists?(udt_hash: type_script.compute_hash)
str.value ="omiga_inscription"
else
str.value = "xudt"
end
end

return str.value
end

if type_script&.code_hash == CkbSync::Api.instance.omiga_inscription_info_code_hash
return "omiga_inscription_info"
end

return "normal" unless ([
Settings.dao_code_hash, Settings.dao_type_hash, Settings.sudt_cell_type_hash, Settings.sudt1_cell_type_hash,
CkbSync::Api.instance.issuer_script_code_hash, CkbSync::Api.instance.token_class_script_code_hash,
Expand All @@ -436,7 +417,7 @@ def self.cell_type(type_script, output_data)
CkbSync::Api.instance.spore_cluster_code_hash,
*CkbSync::Api.instance.spore_cell_code_hashes,
].include?(type_script&.code_hash) && type_script&.hash_type == "data1" ||
ENV["CKB_NET_MODE"] == CKB::MODE::MAINNET && [CkbSync::Api.instance.xudt_code_hash].include?(type_script&.code_hash) && type_script&.hash_type == "data1"
CkbSync::Api.instance.mode == CKB::MODE::MAINNET && [CkbSync::Api.instance.xudt_code_hash].include?(type_script&.code_hash) && type_script&.hash_type == "data1"

case type_script&.code_hash
when Settings.dao_code_hash, Settings.dao_type_hash
Expand Down Expand Up @@ -468,10 +449,12 @@ def self.cell_type(type_script, output_data)
when CkbSync::Api.instance.omiga_inscription_info_code_hash
"omiga_inscription_info"
when CkbSync::Api.instance.xudt_code_hash
if OmigaInscriptionInfo.where(udt_hash: type_script.compute_hash).exists?
"omiga_inscription"
else
"xudt"
Rails.cache.fetch(type_script.compute_hash) do
if OmigaInscriptionInfo.exists?(udt_hash: type_script.compute_hash)
"omiga_inscription"
else
"xudt"
end
end
else
if is_nrc_721_token_cell?(output_data)
Expand Down
24 changes: 0 additions & 24 deletions test/utils/ckb_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,45 +310,21 @@ class CkbUtilsTest < ActiveSupport::TestCase
assert_equal 0, CkbUtils.parse_udt_cell_data("0x01")
end

test "cell_type should return testnet m_nft_issuer when type script code_hash match m_nft_issuer code_hash" do
type_script = CKB::Types::Script.new(
code_hash: Settings.testnet_issuer_script_code_hash, hash_type: "type", args: "0x",
)
assert_equal "m_nft_issuer", CkbUtils.cell_type(type_script, "0x")
end

test "cell_type should return mainnet m_nft_issuer when type script code_hash match m_nft_issuer code_hash" do
CkbSync::Api.any_instance.stubs(:get_blockchain_info).returns(OpenStruct.new(chain: "ckb"))
type_script = CKB::Types::Script.new(
code_hash: Settings.mainnet_issuer_script_code_hash, hash_type: "type", args: "0x",
)
assert_equal "m_nft_issuer", CkbUtils.cell_type(type_script, "0x")
end

test "cell_type should return testnet m_nft_class when type script code_hash match m_nft_class code_hash" do
type_script = CKB::Types::Script.new(
code_hash: Settings.testnet_token_class_script_code_hash, hash_type: "type", args: "0x",
)
assert_equal "m_nft_class", CkbUtils.cell_type(type_script, "0x")
end

test "cell_type should return mainnet m_nft_class when type script code_hash match m_nft_class code_hash" do
CkbSync::Api.any_instance.stubs(:get_blockchain_info).returns(OpenStruct.new(chain: "ckb"))
type_script = CKB::Types::Script.new(
code_hash: Settings.mainnet_token_class_script_code_hash, hash_type: "type", args: "0x",
)
assert_equal "m_nft_class", CkbUtils.cell_type(type_script, "0x")
end

test "cell_type should return testnet m_nft_token when type script code_hash match m_nft_token code_hash" do
type_script = CKB::Types::Script.new(
code_hash: Settings.testnet_token_script_code_hash, hash_type: "type", args: "0x",
)
assert_equal "m_nft_token", CkbUtils.cell_type(type_script, "0x")
end

test "cell_type should return mainnet m_nft_token when type script code_hash match m_nft_token code_hash" do
CkbSync::Api.any_instance.stubs(:get_blockchain_info).returns(OpenStruct.new(chain: "ckb"))
type_script = CKB::Types::Script.new(
code_hash: Settings.mainnet_token_script_code_hash, hash_type: "type", args: "0x",
)
Expand Down

0 comments on commit 014b21c

Please sign in to comment.