Skip to content

Commit

Permalink
Merge pull request #2112 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
zmcNotafraid authored Aug 5, 2024
2 parents 4eba470 + 100644b commit e94012e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 75 deletions.
14 changes: 4 additions & 10 deletions app/interactions/addresses/ckb_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CkbTransactions < ActiveInteraction::Base

object :request, class: ActionDispatch::Request
string :key, default: nil
string :sort, default: "ckb_transaction_id.desc"
string :sort, default: "time.desc"
integer :page, default: 1
integer :page_size, default: CkbTransaction.default_per_page

Expand All @@ -29,16 +29,10 @@ def execute
private

def account_books_ordering
sort_by, sort_order = sort.split(".", 2)
sort_by =
case sort_by
when "time" then "ckb_transactions.block_timestamp"
else "ckb_transactions.id"
end
sort_by = "ckb_transactions.block_timestamp"
_, sort_order = sort.split(".", 2)

if sort_order.nil? || !sort_order.match?(/^(asc|desc)$/i)
sort_order = "asc"
end
sort_order = "asc" unless sort_order&.match?(/^(asc|desc)$/i)

[sort_by, sort_order]
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,9 @@ def build_udts!(local_block, outputs, outputs_data)
nft_token_attr[:decimal] = info[:decimal]
nft_token_attr[:published] = true
when "xudt", "xudt_compatible"
issuer_address = Address.find_by(lock_hash: output.type.args[0..65])&.address_hash
if output.type.args.length == 66
issuer_address = Address.find_by(lock_hash: output.type.args[0..65])&.address_hash
end
items.each_with_index do |output, index|
if output.type&.code_hash == CkbSync::Api.instance.unique_cell_code_hash
info = CkbUtils.parse_unique_cell(outputs_data[tx_index][index])
Expand Down
2 changes: 1 addition & 1 deletion app/models/token_collection.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TokenCollection < ApplicationRecord
VALID_TAGS = ["invalid", "suspicious", "out-of-length-range", "rgbpp-compatible", "layer-1-asset", "duplicate", "layer-2-asset"]
VALID_TAGS = ["invalid", "suspicious", "out-of-length-range", "rgb++", "layer-1-asset", "layer-2-asset"]

enum standard: { cota: "cota", spore: "spore", m_nft: "m_nft", nrc721: "nrc721" }

Expand Down
2 changes: 1 addition & 1 deletion app/models/xudt_tag.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class XudtTag < ApplicationRecord
belongs_to :udt

VALID_TAGS = ["unnamed", "invalid", "suspicious", "out-of-length-range", "rgbpp-compatible", "layer-1-asset", "supply-limited", "duplicate", "layer-2-asset", "supply-unlimited"]
VALID_TAGS = ["unnamed", "invalid", "suspicious", "out-of-length-range", "rgb++", "layer-1-asset", "supply-limited", "utility", "layer-2-asset", "supply-unlimited"]
end

# == Schema Information
Expand Down
36 changes: 16 additions & 20 deletions app/utils/ckb_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,27 +614,17 @@ def self.hexes_to_bins_sql(hex_strings)
end

def self.parse_spore_cluster_data(hex_data)
safe_encode = Proc.new do |str|
str.force_encoding("UTF-8").encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
""
end
data = hex_data.slice(2..-1)
name_offset = [data.slice(8, 8)].pack("H*").unpack1("l") * 2
description_offset = [data.slice(16, 8)].pack("H*").unpack1("l") * 2
name = [data.slice(name_offset + 8..description_offset - 1)].pack("H*")
description = [data.slice(description_offset + 8..-1)].pack("H*")
name = "#{name[0, 97]}..." if name.length > 100

begin
data = hex_data.slice(2..-1)
name_offset = [data.slice(8, 8)].pack("H*").unpack1("l") * 2
description_offset = [data.slice(16, 8)].pack("H*").unpack1("l") * 2
name = [data.slice(name_offset + 8..description_offset - 1)].pack("H*")
description = [data.slice(description_offset + 8..-1)].pack("H*")
name = "#{name[0, 97]}..." if name.length > 100
name = safe_encode.call(name)
description = safe_encode.call(description)

{ name:, description: }
rescue StandardError => e
puts "Error parsing spore cluster data: #{e.message}"
{ name: nil, description: nil }
end
{ name: sanitize_string(name), description: sanitize_string(description) }
rescue StandardError => e
puts "Error parsing spore cluster data: #{e.message}"
{ name: nil, description: nil }
end

def self.parse_spore_cell_data(hex_data)
Expand Down Expand Up @@ -772,4 +762,10 @@ def self.parse_unique_cell(hex_data)
symbol = [data.slice!(0, symbol_len * 2)].pack("H*")
{ decimal:, name: name.presence, symbol: symbol.presence }
end

def self.sanitize_string(str)
str.force_encoding("UTF-8").encode("UTF-8", invalid: :replace, undef: :replace, replace: "").gsub(/[[:cntrl:]\u2028\u2029\u200B]/, "")
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
""
end
end
14 changes: 4 additions & 10 deletions app/workers/token_collection_tag_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@ def mark_tags(token_collection)
["suspicious"]
elsif out_of_length?(token_collection.name)
["out-of-length-range"]
elsif first_token_collection?(token_collection.name, token_collection.block_timestamp, token_collection.standard)
if rgbpp_lock?(token_collection.creator.address_hash)
["rgbpp-compatible", "layer-1-asset"]
else
["rgbpp-compatible", "layer-2-asset"]
end
elsif rgbpp_lock?(token_collection.creator.address_hash)
["duplicate", "layer-1-asset"]
["rgb++", "layer-1-asset"]
else
["duplicate", "layer-2-asset"]
["rgb++", "layer-2-asset"]
end
end

Expand All @@ -39,11 +33,11 @@ def invalid_char?(name)
end

def invisible_char?(name)
(name =~ /^[\x21-\x7E]+$/).nil?
(name =~ /^[\x21-\x7E]+(?:\s[\x21-\x7E]+)?$/).nil?
end

def out_of_length?(name)
name.length > 255
name.length > 60
end

def first_token_collection?(name, block_timestamp, standard)
Expand Down
21 changes: 12 additions & 9 deletions app/workers/xudt_tag_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def perform
attrs =
udts.map do |udt|
tags = mark_tags(udt)
tags << "rgb++" if udt.xudt? && !tags.include?("rgb++")
{ udt_id: udt.id, udt_type_hash: udt.type_hash, tags: }
end

Expand All @@ -23,16 +24,14 @@ def mark_tags(udt)
["suspicious"]
elsif out_of_length?(udt.symbol)
["out-of-length-range"]
elsif first_xudt?(udt.symbol, udt.block_timestamp)
if rgbpp_lock?(udt.issuer_address)
["rgbpp-compatible", "layer-1-asset", "supply-limited"]
else
["rgbpp-compatible", "layer-2-asset", "supply-unlimited"]
end
elsif utility_lp_token?(udt.args)
["utility"]
elsif !first_xudt?(udt.symbol, udt.block_timestamp)
["suspicious"]
elsif rgbpp_lock?(udt.issuer_address)
["duplicate", "layer-1-asset", "supply-limited"]
["rgb++", "layer-1-asset", "supply-limited"]
else
["duplicate", "layer-2-asset", "supply-unlimited"]
["rgb++", "layer-2-asset", "supply-unlimited"]
end
end

Expand All @@ -41,7 +40,7 @@ def invalid_char?(symbol)
end

def invisible_char?(symbol)
(symbol =~ /^[\x21-\x7E]+$/).nil?
(symbol =~ /^[\x21-\x7E]+(?:\s[\x21-\x7E]+)?$/).nil?
end

def out_of_length?(symbol)
Expand All @@ -57,6 +56,10 @@ def rgbpp_lock?(issuer_address)
issuer_address.present? && CkbSync::Api.instance.rgbpp_code_hash.include?(address_code_hash)
end

def utility_lp_token?(args)
args.length == 74
end

## TODO: current no this condition
def omni_lock_with_supply_mode?(issuer_address); end
end
10 changes: 5 additions & 5 deletions test/controllers/api/v2/nft/collections_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class NFT::CollectionsControllerTest < ActionDispatch::IntegrationTest
end

test "should filter by union tags" do
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgb++"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgb++"]

get api_v2_nft_collections_url, params: { tags: "layer-1-asset,rgbpp-compatible", union: true }
get api_v2_nft_collections_url, params: { tags: "layer-1-asset,rgb++", union: true }
assert_response :success
assert_equal JSON.parse(response.body)["data"].size, 2
end

test "should filter by tags but not match" do
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgb++"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgb++"]

get api_v2_nft_collections_url, params: { tags: "layer-1-asset,invalid" }
assert_response :success
Expand Down
13 changes: 5 additions & 8 deletions test/workers/token_collection_tag_worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@ class TokenCollectionTagWorkerTest < ActiveJob::TestCase
end

test "add suspicious tag to token_collection" do
create(:token_collection, name: "CK BB", cell_id: @cell.id, creator_id: @address.id)
create(:token_collection, name: "CK BB", cell_id: @cell.id, creator_id: @address.id)
TokenCollectionTagWorker.new.perform
assert_equal ["suspicious"], TokenCollection.last.tags
end

test "add out-of-length-range tag to token_collection" do
create(:token_collection, name: "C" * 256, cell_id: @cell.id, creator_id: @address.id)
create(:token_collection, name: "C" * 66, cell_id: @cell.id, creator_id: @address.id)
TokenCollectionTagWorker.new.perform
assert_equal ["out-of-length-range"], TokenCollection.last.tags
end

test "add duplicate tag to token_collection" do
create(:token_collection, name: "CKBNFT", cell_id: @cell.id, creator_id: @address.id, block_timestamp: 1.hour.ago.to_i, tags: ["rgbpp-compatible", "layer-1-asset"])
new_tx = create(:ckb_transaction)
new_cell = create(:cell_output, address_id: @address.id, ckb_transaction_id: new_tx.id, tx_hash: new_tx.tx_hash)
create(:token_collection, name: "CKBNFT", cell_id: new_cell.id, creator_id: @address.id, block_timestamp: Time.now.to_i)
test "add rgb++ tag to token_collection" do
create(:token_collection, name: "CKBNFT", cell_id: @cell.id, creator_id: @address.id, block_timestamp: 1.hour.ago.to_i)
TokenCollectionTagWorker.new.perform
assert_equal ["duplicate", "layer-1-asset"], TokenCollection.last.tags
assert_equal ["rgb++", "layer-1-asset"], TokenCollection.last.tags
end
end
28 changes: 18 additions & 10 deletions test/workers/xudt_tag_worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class XudtTagWorkerTest < ActiveJob::TestCase
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["unnamed"], XudtTag.last.tags
assert_equal ["unnamed", "rgb++"], XudtTag.last.tags
end

test "insert to xudt_tags successfully" do
Expand All @@ -28,40 +28,48 @@ class XudtTagWorkerTest < ActiveJob::TestCase
assert_changes -> { XudtTag.count }, from: 1, to: 2 do
XudtTagWorker.new.perform
end
assert_equal ["rgbpp-compatible", "layer-1-asset", "supply-limited"], XudtTag.last.tags
assert_equal ["rgb++", "layer-1-asset", "supply-limited"], XudtTag.last.tags
end

test "insert invalid tag" do
create(:udt, :xudt, symbol: "ü")
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["invalid"], XudtTag.last.tags
assert_equal ["invalid", "rgb++"], XudtTag.last.tags
end

test "insert suspicious tag" do
create(:udt, :xudt, symbol: "CK BB")
create(:udt, :xudt, symbol: "CK BB")
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["suspicious"], XudtTag.last.tags
assert_equal ["suspicious", "rgb++"], XudtTag.last.tags
end

test "insert out-of-length-range tag" do
create(:udt, :xudt, symbol: "CKBBBB")
create(:udt, :xudt, symbol: "CK BBBB")
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["out-of-length-range", "rgb++"], XudtTag.last.tags
end

test "insert utility tag" do
create(:udt, :xudt, symbol: "CKBBB", args: "0xdd6faefeffaa7d2a7b2e6890713c5fa4bbf378add1cfc1b27672a50a6ad3e83500000040")
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["out-of-length-range"], XudtTag.last.tags
assert_equal ["utility", "rgb++"], XudtTag.last.tags
end

test "insert duplicate tag" do
test "insert suspicious tag when not lp token but duplicate" do
udt = create(:udt, :xudt, symbol: "CKBBB", block_timestamp: 1.day.ago.to_i * 1000)
create(:xudt_tag, udt_id: udt.id, udt_type_hash: udt.type_hash, tags: ["rgbpp-compatible", "layer-1-asset", "supply-limited"])
create(:xudt_tag, udt_id: udt.id, udt_type_hash: udt.type_hash, tags: ["rgb++", "layer-1-asset", "supply-limited"])
create(:udt, :xudt, symbol: "ckbbb", block_timestamp: Time.now.to_i * 1000, issuer_address: @address.address_hash)
assert_changes -> { XudtTag.count }, from: 1, to: 2 do
XudtTagWorker.new.perform
end
assert_equal ["duplicate", "layer-1-asset", "supply-limited"], XudtTag.last.tags
assert_equal ["suspicious", "rgb++"], XudtTag.last.tags
end
end

0 comments on commit e94012e

Please sign in to comment.