From 69697c0066cdc814c708ecb56579875e5cc08541 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Wed, 18 Dec 2019 17:35:52 +0800 Subject: [PATCH 1/2] fix: wrong lock info after the lockout period --- app/models/lock_script.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/lock_script.rb b/app/models/lock_script.rb index 2902b4019..4cd46b829 100644 --- a/app/models/lock_script.rb +++ b/app/models/lock_script.rb @@ -27,8 +27,9 @@ def lock_info tip_epoch = CkbUtils.parse_epoch(CkbSync::Api.instance.get_tip_header.epoch) block_interval = (since_value.number * 1800 + since_value.index * 1800 / since_value.length) - (tip_epoch.number * 1800 + tip_epoch.index * 1800 / tip_epoch.length) if block_interval.negative? - start_number = Block.where(epoch: since_value.number).recent.pick(:start_number) - block_timestamp = Block.where(number: start_number + since_value.index).pick(:timestamp) + block = Block.where(epoch: since_value.number).recent.first + since_value_index = since_value.index <= block.length ? since_value.index : since_value.index * block.length / since_value.length + block_timestamp = Block.where(number: block.start_number + since_value_index).pick(:timestamp) estimated_unlock_time = DateTime.strptime(block_timestamp.to_s, "%Q") else tip_block_timestamp = Block.recent.where(epoch: tip_epoch.number).pick(:timestamp) From a67e8e378b21e2a4dd812151c0637f1e066c9613 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Wed, 18 Dec 2019 17:36:10 +0800 Subject: [PATCH 2/2] test: lock info --- app/models/lock_script.rb | 2 +- test/models/lock_script_test.rb | 72 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/app/models/lock_script.rb b/app/models/lock_script.rb index 4cd46b829..1cb8bb84e 100644 --- a/app/models/lock_script.rb +++ b/app/models/lock_script.rb @@ -28,7 +28,7 @@ def lock_info block_interval = (since_value.number * 1800 + since_value.index * 1800 / since_value.length) - (tip_epoch.number * 1800 + tip_epoch.index * 1800 / tip_epoch.length) if block_interval.negative? block = Block.where(epoch: since_value.number).recent.first - since_value_index = since_value.index <= block.length ? since_value.index : since_value.index * block.length / since_value.length + since_value_index = since_value.index < block.length ? since_value.index : since_value.index * block.length / since_value.length block_timestamp = Block.where(number: block.start_number + since_value_index).pick(:timestamp) estimated_unlock_time = DateTime.strptime(block_timestamp.to_s, "%Q") else diff --git a/test/models/lock_script_test.rb b/test/models/lock_script_test.rb index eb53ed197..e371aceed 100644 --- a/test/models/lock_script_test.rb +++ b/test/models/lock_script_test.rb @@ -29,4 +29,76 @@ class LockScriptTest < ActiveSupport::TestCase assert_equal unpack_attribute(lock_script, "code_hash"), lock_script.code_hash end end + + test "#lock_info should return nil when code hash is not secp multisig cell type hash" do + CkbSync::Api.any_instance.stubs(:get_tip_header).returns( + CKB::Types::BlockHeader.new( + compact_target: "0x1a29391f", + hash: "0xc68b7a63e8b0ab82d7e13fe8c580e61d7c156d13d002f3283bf34fdbed5c0cb2", + number: "0x36330", + parent_hash: "0xff5b1f89d8672fed492ebb34be8b2f12ff6cdfb5347e41448d2710f8a7ba1517", + nonce: "0x7f22eaf01000000000000002c14a3d1", + timestamp: "0x16ef4a6ae35", + transactions_root: "0x304b48778593f4aa6677298289e05b0764e94a1f84c7b771e34138849ceeec3f", + proposals_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", + uncles_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", + version: "0x0", + epoch: "0x5eb00a3000089", + dao: "0x39375e92e46d1c2faf11706ba29d2300aca3fbd5ca6d1900004fe04913440007" + ) + ) + address = create(:address) + lock_script = create(:lock_script, address: address, args: "0xda648442dbb7347e467d1d09da13e5cd3a0ef0e1", code_hash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8") + + assert_nil lock_script.lock_info + end + + test "#lock_info should return nil when args size is not equal to 28" do + CkbSync::Api.any_instance.stubs(:get_tip_header).returns( + CKB::Types::BlockHeader.new( + compact_target: "0x1a29391f", + hash: "0xc68b7a63e8b0ab82d7e13fe8c580e61d7c156d13d002f3283bf34fdbed5c0cb2", + number: "0x36330", + parent_hash: "0xff5b1f89d8672fed492ebb34be8b2f12ff6cdfb5347e41448d2710f8a7ba1517", + nonce: "0x7f22eaf01000000000000002c14a3d1", + timestamp: "0x16ef4a6ae35", + transactions_root: "0x304b48778593f4aa6677298289e05b0764e94a1f84c7b771e34138849ceeec3f", + proposals_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", + uncles_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", + version: "0x0", + epoch: "0x317025a000077", + dao: "0x39375e92e46d1c2faf11706ba29d2300aca3fbd5ca6d1900004fe04913440007" + ) + ) + address = create(:address) + lock_script = create(:lock_script, address: address, args: "0xda648442dbb7347e467d1d09da13e5cd3a0ef0e1", code_hash: ENV["SECP_MULTISIG_CELL_TYPE_HASH"] ) + + assert_nil lock_script.lock_info + end + + test "#lock_info should return specific unlock time when since index is larger than epoch length" do + CkbSync::Api.any_instance.stubs(:get_tip_header).returns( + CKB::Types::BlockHeader.new( + compact_target: "0x1a29391f", + hash: "0xc68b7a63e8b0ab82d7e13fe8c580e61d7c156d13d002f3283bf34fdbed5c0cb2", + number: "0x1a4cd", + parent_hash: "0xff5b1f89d8672fed492ebb34be8b2f12ff6cdfb5347e41448d2710f8a7ba1517", + nonce: "0x7f22eaf01000000000000002c14a3d1", + timestamp: "0x16ef4a6ae35", + transactions_root: "0x304b48778593f4aa6677298289e05b0764e94a1f84c7b771e34138849ceeec3f", + proposals_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", + uncles_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", + version: "0x0", + epoch: "0x317025a000077", + dao: "0x39375e92e46d1c2faf11706ba29d2300aca3fbd5ca6d1900004fe04913440007" + ) + ) + address = create(:address) + create(:block, number: 107036, start_number: 106327, epoch: 118, timestamp: 1576648516881, length: 796) + lock_script = create(:lock_script, address: address, args: "0x691fdcdc80ca82a4cb15826dcb7f0cf04cd821367600004506080720", code_hash: ENV["SECP_MULTISIG_CELL_TYPE_HASH"] ) + expected_lock_info = { status: "unlocked", epoch_number: "118", epoch_index: "1605", estimated_unlock_time: "1576648516881" } + + assert_equal expected_lock_info, lock_script.lock_info + end + end