From c03734ee68c275164791f340fa511b32dc06a5ed Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Wed, 30 Oct 2024 20:40:07 +0900 Subject: [PATCH 1/5] fix: parse data2 hash_type address (#2268) Signed-off-by: Miles Zhang --- Gemfile | 2 +- Gemfile.lock | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 434a09bed..115a7d75a 100644 --- a/Gemfile +++ b/Gemfile @@ -38,7 +38,7 @@ gem "dotenv-rails" gem "config" # CKB SDK -gem "ckb-sdk-ruby", git: "https://github.com/nervosnetwork/ckb-sdk-ruby.git", require: "ckb" +gem "ckb-sdk-ruby", git: "https://github.com/zmcNotafraid/ckb-sdk-ruby", branch: "data2-index", require: "ckb" # Redis gem "digest-crc" diff --git a/Gemfile.lock b/Gemfile.lock index abdcff7e7..80af83c2b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GIT - remote: https://github.com/nervosnetwork/ckb-sdk-ruby.git - revision: 1790c72fb886bda3e30936a73bc3d1cbcee6c91f + remote: https://github.com/zmcNotafraid/ckb-sdk-ruby + revision: 5eb4c049ea3f6974b3edf3af75a24b9b71683ef4 + branch: data2-index specs: ckb-sdk-ruby (0.103.0) net-http-persistent (~> 4.0.1) @@ -295,7 +296,7 @@ GEM ruby2_keywords (>= 0.0.5) msgpack (1.6.0) murmurhash3 (0.1.7) - net-http-persistent (4.0.2) + net-http-persistent (4.0.4) connection_pool (~> 2.2) net-imap (0.4.10) date @@ -321,7 +322,7 @@ GEM parser (3.2.2.0) ast (~> 2.4.1) pg (1.4.5) - pkg-config (1.5.2) + pkg-config (1.5.7) protocol-hpack (1.4.2) protocol-http (0.24.7) protocol-http1 (0.15.1) @@ -390,8 +391,8 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) - rbnacl (7.1.1) - ffi + rbnacl (7.1.2) + ffi (~> 1) rbsecp256k1 (5.1.1) mini_portile2 (~> 2.8) pkg-config (~> 1.5) From 3d127f5b88bc676132dafcbeb2c8b53fb70b87df Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Wed, 30 Oct 2024 21:44:10 +0900 Subject: [PATCH 2/5] fix: usd sdk's ckb address parser (#2270) Signed-off-by: Miles Zhang --- Gemfile.lock | 2 +- app/utils/ckb_address_parser.rb | 132 -------------------------------- app/utils/ckb_utils.rb | 2 +- test/utils/ckb_utils_test.rb | 2 +- 4 files changed, 3 insertions(+), 135 deletions(-) delete mode 100644 app/utils/ckb_address_parser.rb diff --git a/Gemfile.lock b/Gemfile.lock index 80af83c2b..ab44c64c1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/zmcNotafraid/ckb-sdk-ruby - revision: 5eb4c049ea3f6974b3edf3af75a24b9b71683ef4 + revision: 164a506ee6d8107d6acab10b988df98f10ea9a7f branch: data2-index specs: ckb-sdk-ruby (0.103.0) diff --git a/app/utils/ckb_address_parser.rb b/app/utils/ckb_address_parser.rb deleted file mode 100644 index b689427b4..000000000 --- a/app/utils/ckb_address_parser.rb +++ /dev/null @@ -1,132 +0,0 @@ -# Address parser (copied from CKB Ruby SDK) -class CkbAddressParser - # @param address_hash [String] - def initialize(address_hash) - @address_hash = address_hash - end - - # @return [OpenStruct(mode, script, address_type)] - def parse - decoded_prefix, data, = CKB::ConvertAddress.decode(address_hash) - format_type = data[0].unpack1("H*") - case format_type - when CKB::Address::SHORT_FORMAT - parse_short_payload_address(decoded_prefix, data) - when CKB::Address::FULL_DATA_FORMAT, CKB::Address::FULL_TYPE_FORMAT - parse_full_payload_address(decoded_prefix, data) - when CKB::Address::FULL_WITH_IDENTIFIER_FORMAT - parse_new_full_payload_address(decoded_prefix, data) - else - raise InvalidFormatTypeError, "Invalid format type" - end - end - - private - - attr_reader :address_hash - - def parse_address_type(format_type, code_hash_index = nil) - return "FULL" if format_type != CKB::Address::SHORT_FORMAT - - case code_hash_index - when CKB::Address::CODE_HASH_INDEX_SINGLESIG - "SHORTSINGLESIG" - when CKB::Address::CODE_HASH_INDEX_MULTISIG_SIG - "SHORTMULTISIG" - when CKB::Address::CODE_HASH_INDEX_ANYONE_CAN_PAY - "SHORTANYONECANPAY" - else - raise InvalidCodeHashIndexError, "Invalid code hash index" - end - end - - def parse_short_payload_address(decoded_prefix, data) - format_type = data[0].unpack1("H*") - code_hash_index = data[1].unpack1("H*") - mode = parse_mode(decoded_prefix) - code_hash = parse_code_hash(code_hash_index, mode) - args = CKB::Utils.bin_to_hex(data.slice(2..-1)) - - OpenStruct.new(mode: mode, - script: CKB::Types::Script.new(code_hash: code_hash, - args: args, - hash_type: CKB::ScriptHashType::TYPE), - address_type: parse_address_type(format_type, code_hash_index)) - end - - def parse_full_payload_address(decoded_prefix, data) - format_type = data[0].unpack1("H*") - mode = parse_mode(decoded_prefix) - hash_type = parse_hash_type(format_type) - offset = 1 - code_hash_size = 32 - code_hash = "0x#{data.slice(1..code_hash_size).unpack1('H*')}" - offset += code_hash_size - args = CKB::Utils.bin_to_hex(data[offset..-1]) - - OpenStruct.new(mode: mode, - script: CKB::Types::Script.new(code_hash: code_hash, - args: args, - hash_type: hash_type), - address_type: parse_address_type(format_type)) - end - - def parse_new_full_payload_address(decoded_prefix, data) - format_type = data[0].unpack1("H*") - mode = parse_mode(decoded_prefix) - code_hash_size = 32 - code_hash = "0x#{data.slice(1..code_hash_size).unpack1('H*')}" - hash_type = CKB::Utils.bin_to_hex(data[code_hash_size + 1...code_hash_size + 2]).hex - args = CKB::Utils.bin_to_hex(data[code_hash_size + 2..]) - OpenStruct.new(mode: mode, - script: CKB::Types::Script.new(code_hash: code_hash, - args: args, - hash_type: CKB::ScriptHashType::TYPES[hash_type]), - address_type: parse_address_type(format_type)) - end - - def parse_hash_type(format_type) - case format_type - when CKB::Address::FULL_DATA_FORMAT - CKB::ScriptHashType::DATA - when CKB::Address::FULL_TYPE_FORMAT - CKB::ScriptHashType::TYPE - else - raise InvalidFormatTypeError, "Invalid format type" - end - end - - def parse_code_hash(code_hash_index, mode) - case code_hash_index - when CKB::Address::CODE_HASH_INDEX_SINGLESIG - CKB::SystemCodeHash::SECP256K1_BLAKE160_SIGHASH_ALL_TYPE_HASH - when CKB::Address::CODE_HASH_INDEX_MULTISIG_SIG - CKB::SystemCodeHash::SECP256K1_BLAKE160_MULTISIG_ALL_TYPE_HASH - when CKB::Address::CODE_HASH_INDEX_ANYONE_CAN_PAY - if mode == CKB::MODE::TESTNET - CKB::SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_AGGRON - else - CKB::SystemCodeHash::ANYONE_CAN_PAY_CODE_HASH_ON_LINA - end - else - raise InvalidCodeHashIndexError, "Invalid code hash index" - end - end - - def parse_mode(decoded_prefix) - case decoded_prefix - when CKB::Address::PREFIX_TESTNET - CKB::MODE::TESTNET - when CKB::Address::PREFIX_MAINNET - CKB::MODE::MAINNET - else - raise InvalidPrefixError, "Invalid prefix" - end - end - - class InvalidFormatTypeError < StandardError; end - class InvalidArgSizeError < StandardError; end - class InvalidPrefixError < StandardError; end - class InvalidCodeHashIndexError < StandardError; end - class InvalidCodeHashSizeError < StandardError; end -end diff --git a/app/utils/ckb_utils.rb b/app/utils/ckb_utils.rb index 7c7aa2851..e5cc46d2b 100644 --- a/app/utils/ckb_utils.rb +++ b/app/utils/ckb_utils.rb @@ -91,7 +91,7 @@ def self.generate_address(lock_script, version = CKB::Address::Version::CKB2021) end def self.parse_address(address_hash) - CkbAddressParser.new(address_hash).parse + CKB::AddressParser.new(address_hash).parse end def self.block_reward(block_number, block_economic_state) diff --git a/test/utils/ckb_utils_test.rb b/test/utils/ckb_utils_test.rb index 7b0c082f7..e492954d8 100644 --- a/test/utils/ckb_utils_test.rb +++ b/test/utils/ckb_utils_test.rb @@ -36,7 +36,7 @@ class CkbUtilsTest < ActiveSupport::TestCase end test ".parse_address raise error when address is mainnet address and mode is testnet" do - assert_raises CkbAddressParser::InvalidPrefixError do + assert_raises CKB::AddressParser::InvalidPrefixError do CkbUtils.parse_address("haha1qygndsefa43s6m882pcj53m4gdnj4k440axqsm2hnz") end end From 7048650ea3954f956286be941e651d7d94cd83c7 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Fri, 1 Nov 2024 18:36:24 +0900 Subject: [PATCH 3/5] feat: verify stable coin as xudt compatible (#2272) Signed-off-by: Miles Zhang --- app/models/ckb_sync/api.rb | 2 +- config/settings.testnet.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/ckb_sync/api.rb b/app/models/ckb_sync/api.rb index 55638428b..424484a9c 100644 --- a/app/models/ckb_sync/api.rb +++ b/app/models/ckb_sync/api.rb @@ -108,7 +108,7 @@ def xudt_data_hash end def xudt_compatible_code_hashes - [Settings.xudt_compatible_code_hash, Settings.xudt_compatible2_code_hash] + [Settings.xudt_compatible_code_hash, Settings.xudt_compatible2_code_hash, Settings.xudt_compatible3_code_hash] end def unique_cell_code_hash diff --git a/config/settings.testnet.yml b/config/settings.testnet.yml index 15e6306f1..035fee2c1 100644 --- a/config/settings.testnet.yml +++ b/config/settings.testnet.yml @@ -42,6 +42,7 @@ unique_cell_code_hash: "0x8e341bcfec6393dcd41e635733ff2dca00a6af546949f70c57a706 # xudt compatible xudt_compatible_code_hash: "0x98701eaf939113606a8a70013fd2e8f27b8f1e234acdc329f3d71f9e9d3d3233" xudt_compatible2_code_hash: "0x1142755a044bf2ee358cba9f2da187ce928c91cd4dc8692ded0337efa677d21a" +xudt_compatible3_code_hash: "0xcc9dc33ef234e14bc788c43a4848556a5fb16401a04662fc55db9bb201987037" # stable coin # hash length of an attribute(especially which comes from bytea column), # e.g. Block.uncle_block_hashes: "0x587f354162afd133b4a4f7a4b621d11e043c3c08b0af2801f1686b5403b14953", which has a length of 66 ( 2 + 64) From 472a5d4c1d28a3fce4312be35191575169c0ab3e Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Sat, 9 Nov 2024 11:58:33 +0900 Subject: [PATCH 4/5] chore: use offical ckb-ruby-sdk gem (#2275) Signed-off-by: Miles Zhang --- Gemfile | 2 +- Gemfile.lock | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 115a7d75a..434a09bed 100644 --- a/Gemfile +++ b/Gemfile @@ -38,7 +38,7 @@ gem "dotenv-rails" gem "config" # CKB SDK -gem "ckb-sdk-ruby", git: "https://github.com/zmcNotafraid/ckb-sdk-ruby", branch: "data2-index", require: "ckb" +gem "ckb-sdk-ruby", git: "https://github.com/nervosnetwork/ckb-sdk-ruby.git", require: "ckb" # Redis gem "digest-crc" diff --git a/Gemfile.lock b/Gemfile.lock index ab44c64c1..b59939032 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,6 @@ GIT - remote: https://github.com/zmcNotafraid/ckb-sdk-ruby - revision: 164a506ee6d8107d6acab10b988df98f10ea9a7f - branch: data2-index + remote: https://github.com/nervosnetwork/ckb-sdk-ruby.git + revision: 4b43de58fb69cc046b3cef234d70fd9657711ff6 specs: ckb-sdk-ruby (0.103.0) net-http-persistent (~> 4.0.1) From e9cecdc3e0fe44155c4935c7221c01ef14e19d85 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Sun, 10 Nov 2024 10:36:14 +0900 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20spore=20cluster=20cell=20and=20spore?= =?UTF-8?q?=20cell=20may=20exist=20in=20same=20block=20and=20al=E2=80=A6?= =?UTF-8?q?=20(#2277)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: spore cluster cell and spore cell may exist in same block and all pending Signed-off-by: Miles Zhang --- app/models/ckb_sync/new_node_data_processor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/ckb_sync/new_node_data_processor.rb b/app/models/ckb_sync/new_node_data_processor.rb index 4d73a77d9..a5f60d51c 100644 --- a/app/models/ckb_sync/new_node_data_processor.rb +++ b/app/models/ckb_sync/new_node_data_processor.rb @@ -646,7 +646,7 @@ def build_udts!(local_block, outputs, outputs_data) binary_hashes = CkbUtils.hexes_to_bins_sql(CkbSync::Api.instance.spore_cluster_code_hashes) spore_cluster_type_ids = TypeScript.where("code_hash IN (#{binary_hashes})").where(hash_type: "data1", args: parsed_spore_cell[:cluster_id]).pluck(:id) if spore_cluster_type_ids.present? - spore_cluster_cell = CellOutput.live.where(type_script_id: spore_cluster_type_ids).last + spore_cluster_cell = CellOutput.where(type_script_id: spore_cluster_type_ids, status: %i[pending live]).last parsed_cluster_data = CkbUtils.parse_spore_cluster_data(spore_cluster_cell.data) nft_token_attr[:full_name] = parsed_cluster_data[:name] end