Skip to content

Commit

Permalink
Merge pull request #1427 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
zmcNotafraid committed Sep 4, 2023
2 parents 5618021 + 24a4224 commit d7a633d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v1/udt_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def show
address = Address.find_address!(params[:address_hash])
raise Api::V1::Exceptions::AddressNotFoundError if address.is_a?(NullAddress)

ckb_transactions = ckb_transactions.includes(:contained_udt_addresses).
where(address_udt_transactions: { address_id: address.id })
ckb_transactions = ckb_transactions.joins(:account_books).
where(account_books: { address_id: address.id })
end

if stale?(ckb_transactions)
Expand Down
19 changes: 13 additions & 6 deletions app/models/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,20 @@ def self.find_block!(query_key)
end

def self.cached_find(query_key)
Rails.cache.fetch([name, query_key], race_condition_ttl: 3.seconds) do
if QueryKeyUtils.valid_hex?(query_key)
block = where(block_hash: query_key).first
else
block = where(number: query_key).first
if Rails.cache.exist?([name, query_key])
Rails.cache.read([name, query_key])
else
block =
if QueryKeyUtils.valid_hex?(query_key)
where(block_hash: query_key).first
else
where(number: query_key).first
end
if block.present?
Rails.cache.fetch([name, query_key], race_condition_ttl: 3.seconds, expires_in: 30.minutes) do
BlockSerializer.new(block)
end
end
BlockSerializer.new(block) if block.present?
end
end

Expand Down
42 changes: 31 additions & 11 deletions test/models/block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class BlockTest < ActiveSupport::TestCase
assert_not_empty local_block.ckb_transactions

VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
assert_changes -> { CkbTransaction.where(block: local_block).count }, from: local_block.ckb_transactions.count, to: 0 do
assert_changes -> {
CkbTransaction.where(block: local_block).count
}, from: local_block.ckb_transactions.count, to: 0 do
local_block.invalid!
end
end
Expand Down Expand Up @@ -138,7 +140,9 @@ class BlockTest < ActiveSupport::TestCase
assert_not_empty origin_lock_scripts

VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
assert_changes -> { CellOutput.where(block: local_block).map(&:lock_script).count }, from: origin_lock_scripts.count, to: 0 do
assert_changes -> {
CellOutput.where(block: local_block).map(&:lock_script).count
}, from: origin_lock_scripts.count, to: 0 do
local_block.invalid!
end
end
Expand All @@ -152,7 +156,9 @@ class BlockTest < ActiveSupport::TestCase
assert_not_empty origin_type_scripts

VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
assert_changes -> { CellOutput.where(block: local_block).map(&:type_script).count }, from: origin_type_scripts.count, to: 0 do
assert_changes -> {
CellOutput.where(block: local_block).map(&:type_script).count
}, from: origin_type_scripts.count, to: 0 do
local_block.invalid!
end
end
Expand Down Expand Up @@ -218,15 +224,17 @@ class BlockTest < ActiveSupport::TestCase
block = Block.find_by(number: HAS_UNCLES_BLOCK_NUMBER)
uncle_block_hashes = block.uncle_block_hashes

assert_equal unpack_array_attribute(block, "uncle_block_hashes", block.uncles_count, Settings.default_hash_length), uncle_block_hashes
assert_equal unpack_array_attribute(block, "uncle_block_hashes", block.uncles_count, Settings.default_hash_length),
uncle_block_hashes
end
end

test "#uncle_block_hashes should return super when uncle block hashes is empty" do
block = create(:block, :with_uncle_block_hashes)
uncle_block_hashes = block.uncle_block_hashes

assert_equal unpack_array_attribute(block, "uncle_block_hashes", block.uncles_count, Settings.default_hash_length), uncle_block_hashes
assert_equal unpack_array_attribute(block, "uncle_block_hashes", block.uncles_count, Settings.default_hash_length),
uncle_block_hashes
end

test "#proposals should decodes packed string" do
Expand All @@ -246,14 +254,16 @@ class BlockTest < ActiveSupport::TestCase
block = Block.find_by(number: HAS_UNCLES_BLOCK_NUMBER)
proposals = block.proposals

assert_equal unpack_array_attribute(block, "proposals", block.proposals_count, Settings.default_short_hash_length), proposals
assert_equal unpack_array_attribute(block, "proposals", block.proposals_count, Settings.default_short_hash_length),
proposals
end
end

test "#proposals should return super when proposal transactions is empty" do
block = create(:block, :with_proposals)
proposals = block.proposals
assert_equal unpack_array_attribute(block, "proposals", block.proposals_count, Settings.default_short_hash_length), proposals
assert_equal unpack_array_attribute(block, "proposals", block.proposals_count, Settings.default_short_hash_length),
proposals
end

test "#proposals= should encode proposals" do
Expand All @@ -262,7 +272,8 @@ class BlockTest < ActiveSupport::TestCase
block.proposals_count = block.proposals.size
block.save

assert_equal unpack_array_attribute(block, "proposals", block.proposals_count, Settings.default_short_hash_length), block.proposals
assert_equal unpack_array_attribute(block, "proposals", block.proposals_count, Settings.default_short_hash_length),
block.proposals
end

test "it should get last_7_days_ckb_node_version" do
Expand All @@ -276,7 +287,8 @@ class BlockTest < ActiveSupport::TestCase

test "it should update_counter_for_ckb_node_version" do
block1 = create(:block, block_hash: "001")
tx = create(:ckb_transaction, is_cellbase: true, block_id: block1.id, witnesses: ["0x640000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce801140000004a336470564d07ca7059b7980481c2d59809d6370b000000302e3130342e3120282029"])
tx = create(:ckb_transaction, is_cellbase: true, block_id: block1.id,
witnesses: ["0x640000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce801140000004a336470564d07ca7059b7980481c2d59809d6370b000000302e3130342e3120282029"])

block1.update_counter_for_ckb_node_version

Expand All @@ -285,15 +297,23 @@ class BlockTest < ActiveSupport::TestCase

test "it should update the block version" do
block2 = create(:block, :with_block_hash)
tx = create(:ckb_transaction, is_cellbase: true, block: block2, witnesses: ["0x800000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000dde7801c073dfb3464c7b1f05b806bb2bbb84e9927000000302e3130332e302028353161383134612d646972747920323032322d30342d3230292000000000"])
tx = create(:ckb_transaction, is_cellbase: true, block: block2,
witnesses: ["0x800000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000dde7801c073dfb3464c7b1f05b806bb2bbb84e9927000000302e3130332e302028353161383134612d646972747920323032322d30342d3230292000000000"])
block3 = create(:block, :with_block_hash)
tx = create(:ckb_transaction, is_cellbase: true, block: block3, witnesses: ["0x640000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce801140000004a336470564d07ca7059b7980481c2d59809d6370b000000302e3130342e3120282029"])
tx = create(:ckb_transaction, is_cellbase: true, block: block3,
witnesses: ["0x640000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce801140000004a336470564d07ca7059b7980481c2d59809d6370b000000302e3130342e3120282029"])

Block.set_ckb_node_versions_from_miner_message
assert_equal GlobalStatistic.find_by(name: "ckb_node_version_0.104.1").value, 1
assert_equal GlobalStatistic.find_by(name: "ckb_node_version_0.103.0").value, 1
end

test "cached_find not always return nil" do
assert_nil Block.cached_find("111111")
create(:block, number: 111111)
assert_not_nil Block.cached_find("111111")
end

def node_data_processor
CkbSync::NewNodeDataProcessor.new
end
Expand Down

0 comments on commit d7a633d

Please sign in to comment.