From e0ccf8c30138b60c21e4b9e360d34ce52821dccc Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 13 Jul 2020 21:34:16 +0800 Subject: [PATCH 01/21] perf: use is_depositor column to filter addresses --- app/controllers/api/v1/dao_depositors_controller.rb | 2 +- app/workers/address_unclaimed_compensation_generator.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/dao_depositors_controller.rb b/app/controllers/api/v1/dao_depositors_controller.rb index 3e5fcc2b2..516cccc2f 100644 --- a/app/controllers/api/v1/dao_depositors_controller.rb +++ b/app/controllers/api/v1/dao_depositors_controller.rb @@ -2,7 +2,7 @@ module Api module V1 class DaoDepositorsController < ApplicationController def index - addresses = Address.select(:id, :address_hash, :dao_deposit, :average_deposit_time).where("dao_deposit > 0").order(dao_deposit: :desc).limit(100) + addresses = Address.select(:id, :address_hash, :dao_deposit, :average_deposit_time).where(is_depositor: true).order(dao_deposit: :desc).limit(100) render json: DaoDepositorSerializer.new(addresses) end diff --git a/app/workers/address_unclaimed_compensation_generator.rb b/app/workers/address_unclaimed_compensation_generator.rb index 7d0eff309..97ae55587 100644 --- a/app/workers/address_unclaimed_compensation_generator.rb +++ b/app/workers/address_unclaimed_compensation_generator.rb @@ -2,7 +2,7 @@ class AddressUnclaimedCompensationGenerator include Sidekiq::Worker def perform - Address.where("dao_deposit > 0").find_in_batches do |addresses| + Address.where(is_depositor: true).find_in_batches do |addresses| values = addresses.map do |address| { id: address.id, unclaimed_compensation: address.cal_unclaimed_compensation, created_at: address.created_at, updated_at: Time.current } From ee749e44a6a4fa87d4a99c39948af896e626f49c Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 13 Jul 2020 21:36:20 +0800 Subject: [PATCH 02/21] chore: update is_depositor column when taking away event occur --- app/models/ckb_sync/node_data_processor.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/ckb_sync/node_data_processor.rb b/app/models/ckb_sync/node_data_processor.rb index 55ca26598..89af13119 100644 --- a/app/models/ckb_sync/node_data_processor.rb +++ b/app/models/ckb_sync/node_data_processor.rb @@ -114,6 +114,7 @@ def process_take_away_all_deposit(dao_contract, dao_events) take_away_all_deposit_dao_events = dao_events.where(event_type: "take_away_all_deposit") take_away_all_deposit_dao_events.each do |event| dao_contract.decrement!(:depositors_count) + event.address.update(is_depositor: false) event.processed! end end From feb1c0fa00d6d8c8c53a2098f93052ca49fb08bd Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 14 Jul 2020 12:38:33 +0800 Subject: [PATCH 03/21] refactor: use dao compensation calculator --- app/models/address.rb | 5 +--- app/utils/ckb_utils.rb | 6 ++--- app/utils/dao_compensation_calculator.rb | 26 +++++++++++++++++++ .../address_average_deposit_time_generator.rb | 2 +- 4 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 app/utils/dao_compensation_calculator.rb diff --git a/app/models/address.rb b/app/models/address.rb index 77499054c..27bf73e8c 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -133,10 +133,7 @@ def phase1_dao_interests def unmade_dao_interests tip_dao = Block.recent.first.dao cell_outputs.nervos_dao_deposit.live.find_each.reduce(0) do |memo, cell_output| - dao = cell_output.block.dao - parse_dao = CkbUtils.parse_dao(dao) - tip_parse_dao = CkbUtils.parse_dao(tip_dao) - memo + (cell_output.capacity - cell_output.occupied_capacity).to_i * tip_parse_dao.ar_i / parse_dao.ar_i - (cell_output.capacity - cell_output.occupied_capacity) + memo + DaoCompensationCalculator.new(cell_output, tip_dao).call end end end diff --git a/app/utils/ckb_utils.rb b/app/utils/ckb_utils.rb index 5fdc8cdcb..d4d588254 100644 --- a/app/utils/ckb_utils.rb +++ b/app/utils/ckb_utils.rb @@ -198,16 +198,14 @@ def self.normal_tx_fee(input_capacities, output_capacities) def self.dao_withdraw_tx_fee(ckb_transaction) nervos_dao_withdrawing_cells = ckb_transaction.inputs.nervos_dao_withdrawing interests = nervos_dao_withdrawing_cells.reduce(0) { |memo, nervos_dao_withdrawing_cell| memo + dao_interest(nervos_dao_withdrawing_cell) } - ckb_transaction.inputs.sum(:capacity) + interests - ckb_transaction.outputs.sum(:capacity) end def self.dao_interest(nervos_dao_withdrawing_cell) nervos_dao_withdrawing_cell_generated_tx = nervos_dao_withdrawing_cell.generated_by nervos_dao_deposit_cell = nervos_dao_withdrawing_cell_generated_tx.cell_inputs.order(:id)[nervos_dao_withdrawing_cell.cell_index].previous_cell_output - deposit_out_point = CKB::Types::OutPoint.new(tx_hash: nervos_dao_deposit_cell.tx_hash, index: nervos_dao_deposit_cell.cell_index) - withdrawing_dao_cell_block_hash = nervos_dao_withdrawing_cell.block.block_hash - CkbSync::Api.instance.calculate_dao_maximum_withdraw(deposit_out_point, withdrawing_dao_cell_block_hash).hex - nervos_dao_deposit_cell.capacity.to_i + withdrawing_dao_cell_block_dao = nervos_dao_withdrawing_cell.block.dao + DaoCompensationCalculator.new(nervos_dao_deposit_cell, withdrawing_dao_cell_block_dao).call rescue CKB::RPCError 0 end diff --git a/app/utils/dao_compensation_calculator.rb b/app/utils/dao_compensation_calculator.rb new file mode 100644 index 000000000..56e63d838 --- /dev/null +++ b/app/utils/dao_compensation_calculator.rb @@ -0,0 +1,26 @@ +class DaoCompensationCalculator + attr_reader :deposit_cell_output, :withdraw_block_dao + + def initialize(deposit_cell_output, withdraw_block_dao) + @deposit_cell_output = deposit_cell_output + @withdraw_block_dao = withdraw_block_dao + end + + def call + compensation_generating_capacity * parsed_withdraw_block_dao.ar_i / parsed_deposit_block_dao.ar_i - compensation_generating_capacity + end + + private + + def parsed_withdraw_block_dao + CkbUtils.parse_dao(withdraw_block_dao) + end + + def parsed_deposit_block_dao + CkbUtils.parse_dao(deposit_cell_output.block.dao) + end + + def compensation_generating_capacity + @compensation_generating_capacity ||= (deposit_cell_output.capacity - deposit_cell_output.occupied_capacity).to_i + end +end \ No newline at end of file diff --git a/app/workers/address_average_deposit_time_generator.rb b/app/workers/address_average_deposit_time_generator.rb index 105f53891..3b6349414 100644 --- a/app/workers/address_average_deposit_time_generator.rb +++ b/app/workers/address_average_deposit_time_generator.rb @@ -2,7 +2,7 @@ class AddressAverageDepositTimeGenerator include Sidekiq::Worker def perform - addresses = Address.where("dao_deposit > 0") + addresses = Address.where(is_depositor: true) values = addresses.map do |address| [address.id, cal_average_deposit_time(address)] From a5eda871d4eb0daa6b6cbe5c62e1f1550b6d47cd Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 14 Jul 2020 12:39:45 +0800 Subject: [PATCH 04/21] chore: adjust tests --- .../api/v1/dao_depositors_controller_test.rb | 12 +++---- test/models/address_test.rb | 8 ++--- .../ckb_sync/node_data_processor_test.rb | 32 +++++++++---------- test/models/ckb_transaction_test.rb | 22 +++++++------ test/utils/ckb_utils_test.rb | 8 ++--- 5 files changed, 42 insertions(+), 40 deletions(-) diff --git a/test/controllers/api/v1/dao_depositors_controller_test.rb b/test/controllers/api/v1/dao_depositors_controller_test.rb index e7b84208a..1e4ba7792 100644 --- a/test/controllers/api/v1/dao_depositors_controller_test.rb +++ b/test/controllers/api/v1/dao_depositors_controller_test.rb @@ -40,8 +40,8 @@ class DaoDepositorsControllerTest < ActionDispatch::IntegrationTest end test "should get serialized dao depositors" do - create_list(:address, 10, dao_deposit: 1000) - addresses = Address.select(:id, :address_hash, :dao_deposit, :average_deposit_time).where("dao_deposit > 0").order(dao_deposit: :desc).limit(100) + create_list(:address, 10, dao_deposit: 1000, is_depositor: true) + addresses = Address.select(:id, :address_hash, :dao_deposit, :average_deposit_time).where(is_depositor: true).order(dao_deposit: :desc).limit(100) valid_get api_v1_dao_depositors_url @@ -49,8 +49,8 @@ class DaoDepositorsControllerTest < ActionDispatch::IntegrationTest end test "serialized dao depositors should order by dao deposit" do - create_list(:address, 3, dao_deposit: 1000) - addresses = Address.where("dao_deposit > 0").order("dao_deposit desc") + create_list(:address, 3, dao_deposit: 1000, is_depositor: true) + addresses = Address.where(is_depositor: true).order("dao_deposit desc") valid_get api_v1_dao_depositors_url @@ -58,7 +58,7 @@ class DaoDepositorsControllerTest < ActionDispatch::IntegrationTest end test "should contain right keys in the serialized dao depositors" do - create(:address, dao_deposit: 1000) + create(:address, dao_deposit: 1000, is_depositor: true) valid_get api_v1_dao_depositors_url @@ -66,7 +66,7 @@ class DaoDepositorsControllerTest < ActionDispatch::IntegrationTest end test "should return up to 100 records" do - create_list(:address, 103, dao_deposit: 1000) + create_list(:address, 103, dao_deposit: 1000, is_depositor: true) valid_get api_v1_dao_depositors_url diff --git a/test/models/address_test.rb b/test/models/address_test.rb index 24d81e477..a043dc408 100644 --- a/test/models/address_test.rb +++ b/test/models/address_test.rb @@ -89,10 +89,10 @@ class AddressTest < ActiveSupport::TestCase address = create(:address, is_depositor: true) deposit_block = create(:block, :with_block_hash, dao: "0xea43d76640436a33337e7de7ee60240035099074a869fc0000165f8ab3750207") deposit_tx = create(:ckb_transaction, block: deposit_block) - previous_output_block = create(:block, :with_block_hash) + previous_output_block = create(:block, :with_block_hash, dao: "0x28fbce93e82cbd2ff345ba74f2ba2300b0cd2c97f2953a000060983e29c50007") previous_output_tx = create(:ckb_transaction, block: previous_output_block) - create(:cell_output, block: previous_output_block, capacity: 50000 * 10**8, ckb_transaction: previous_output_tx, tx_hash: previous_output_tx.tx_hash, generated_by: previous_output_tx, cell_type: "nervos_dao_deposit", cell_index: 0) - create(:cell_output, block: previous_output_block, capacity: 50000 * 10**8, ckb_transaction: previous_output_tx, tx_hash: previous_output_tx.tx_hash, generated_by: previous_output_tx, cell_type: "nervos_dao_deposit", cell_index: 1) + create(:cell_output, block: previous_output_block, capacity: 50000 * 10**8, ckb_transaction: previous_output_tx, tx_hash: previous_output_tx.tx_hash, generated_by: previous_output_tx, cell_type: "nervos_dao_deposit", cell_index: 0, occupied_capacity: 6100000000) + create(:cell_output, block: previous_output_block, capacity: 50000 * 10**8, ckb_transaction: previous_output_tx, tx_hash: previous_output_tx.tx_hash, generated_by: previous_output_tx, cell_type: "nervos_dao_deposit", cell_index: 1, occupied_capacity: 6100000000) nervos_dao_withdrawing_block = create(:block, :with_block_hash, dao: "0x9a7a7ce1f34c6a332d147991f0602400aaf7346eb06bfc0000e2abc108760207", timestamp: CkbUtils.time_in_milliseconds(Time.current)) nervos_dao_withdrawing_tx = create(:ckb_transaction, block: nervos_dao_withdrawing_block) create(:cell_input, block: nervos_dao_withdrawing_block, previous_output: { tx_hash: previous_output_tx.tx_hash, index: 0 }, ckb_transaction: nervos_dao_withdrawing_tx) @@ -102,7 +102,7 @@ class AddressTest < ActiveSupport::TestCase deposit_cell = create(:cell_output, block: deposit_block, address: address, cell_type: "nervos_dao_deposit", capacity: 60000 * 10**8, ckb_transaction: deposit_tx, generated_by: deposit_tx, cell_index: 0, occupied_capacity: 6100000000) - expected_phase1_dao_interests = 20000000000 + expected_phase1_dao_interests = 181251857496 parse_dao_ar_i = 10239678363827763 tip_dao_ar_i = 10239685510632493 expected_unmade_dao_interests = (deposit_cell.capacity - deposit_cell.occupied_capacity).to_i * tip_dao_ar_i / parse_dao_ar_i - (deposit_cell.capacity - deposit_cell.occupied_capacity) diff --git a/test/models/ckb_sync/node_data_processor_test.rb b/test/models/ckb_sync/node_data_processor_test.rb index ea7c7088b..978db03e2 100644 --- a/test/models/ckb_sync/node_data_processor_test.rb +++ b/test/models/ckb_sync/node_data_processor_test.rb @@ -719,7 +719,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "#process_block should create dao_event which event_type is withdraw_from_dao when previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -736,7 +736,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "#process_block should create dao_event which event_type is issue interest when previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(100800000000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -752,7 +752,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "#process_block should create dao_event which event_type is take away all deposit when previous output is a dao cell and address interest change to zero" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -771,7 +771,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "#process_block should increase dao contract withdraw transactions count when previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -788,7 +788,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "#process_block should decrease dao contract total deposit when previous output is a withdrawing cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -806,7 +806,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "#process_block should increase dao contract interest granted when previous output is a withdrawing cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -823,7 +823,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "#process_block should decrease dao contract depositors count when previous output is a dao cell and address interest change to zero" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -843,7 +843,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "#process_block should decrease address deposit when previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -862,7 +862,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "#process_block should increase address interest when previous output is a withdrawing cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(100800000000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -873,7 +873,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase address = output.address address.update(dao_deposit: output.capacity) - assert_difference -> { address.reload.interest }, "0x174876ebe8".hex - nervos_dao_deposit_cell.capacity do + assert_difference -> { address.reload.interest }, 100800000000 do node_data_processor.process_block(node_block) end @@ -1056,7 +1056,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "should decrease dao contract withdraw_transactions_count when block is invalid and previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -1082,7 +1082,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "should increase dao contract total_deposit when block is invalid and previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(100800000000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -1108,7 +1108,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "should increase address dao_deposit when block is invalid and previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(100800000000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) target_address = nil @@ -1135,7 +1135,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "should decrease dao contract interest_granted when block is invalid and previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -1161,7 +1161,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "should decrease address interest when block is invalid and previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) target_address = nil @@ -1188,7 +1188,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase end test "should increase dao contract depositors_count when block is invalid and previous output is a dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x174876ebe8") + DaoCompensationCalculator.any_instance.stubs(:call).returns(1000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do diff --git a/test/models/ckb_transaction_test.rb b/test/models/ckb_transaction_test.rb index e456a4ab9..97d93b41b 100644 --- a/test/models/ckb_transaction_test.rb +++ b/test/models/ckb_transaction_test.rb @@ -117,7 +117,7 @@ class CkbTransactionTest < ActiveSupport::TestCase test "#display_inputs should return dao display input when cell type is nervos_dao_withdrawing" do prepare_node_data - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x177825f000") + DaoCompensationCalculator.any_instance.stubs(:call).returns(100800000000) ckb_transaction = create(:ckb_transaction, :with_multiple_inputs_and_outputs, header_deps: [DEFAULT_NODE_BLOCK_HASH, "0xf85f8fe0d85a73a93e0a289ef14b4fb94228e47098a8da38986d6229c5606ea2"]) nervos_dao_withdrawing_cell = ckb_transaction.cell_inputs.first.previous_cell_output nervos_dao_withdrawing_cell_generated_tx = nervos_dao_withdrawing_cell.generated_by @@ -127,14 +127,15 @@ class CkbTransactionTest < ActiveSupport::TestCase deposit_cell = create(:cell_output, ckb_transaction: nervos_dao_withdrawing_cell.generated_by, cell_index: 0, tx_hash: "0x398315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e2", generated_by: nervos_dao_withdrawing_cell.generated_by, block: nervos_dao_withdrawing_cell.generated_by.block, consumed_by: nervos_dao_withdrawing_cell.generated_by, cell_type: "nervos_dao_deposit", capacity: 10**8 * 1000, data: CKB::Utils.bin_to_hex("\x00" * 8)) nervos_dao_deposit_cell = nervos_dao_withdrawing_cell_generated_tx.inputs.nervos_dao_deposit.first started_block = Block.select(:number, :timestamp).find(nervos_dao_deposit_cell.block_id) - interest = CkbSync::Api.instance.calculate_dao_maximum_withdraw(deposit_cell, nervos_dao_deposit_cell).hex - deposit_cell.capacity.to_i - expected_display_input = CkbUtils.hash_value_to_s({ + interest = DaoCompensationCalculator.new(deposit_cell, nervos_dao_withdrawing_cell.block.dao).call + expected_display_input = CkbUtils.hash_value_to_s( id: nervos_dao_withdrawing_cell.id, from_cellbase: false, capacity: nervos_dao_withdrawing_cell.capacity, address_hash: nervos_dao_withdrawing_cell.address_hash, generated_tx_hash: nervos_dao_withdrawing_cell.generated_by.tx_hash, compensation_started_block_number: started_block.number, compensation_ended_block_number: ended_block.number, compensation_started_timestamp: started_block.timestamp, compensation_ended_timestamp: ended_block.timestamp, locked_until_block_number: ckb_transaction.block.number, locked_until_block_timestamp: ckb_transaction.block.timestamp, - interest: interest, cell_type: nervos_dao_withdrawing_cell.cell_type, cell_index: nervos_dao_withdrawing_cell.cell_index }).sort + interest: interest, cell_type: nervos_dao_withdrawing_cell.cell_type, cell_index: nervos_dao_withdrawing_cell.cell_index + ).sort expected_attributes = %i(id from_cellbase capacity address_hash generated_tx_hash compensation_started_block_number compensation_ended_block_number compensation_started_timestamp compensation_ended_timestamp interest cell_type locked_until_block_number locked_until_block_timestamp cell_index).sort assert_equal expected_attributes, ckb_transaction.display_inputs.first.keys.sort @@ -142,7 +143,7 @@ class CkbTransactionTest < ActiveSupport::TestCase end test "#display_inputs should return dao display input when previous cell type is nervos_dao_deposit" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x177825f000") + DaoCompensationCalculator.any_instance.stubs(:call).returns(100800000000) block = create(:block, :with_block_hash, timestamp: Time.current.to_i) ckb_transaction = create(:ckb_transaction, block: block, tx_hash: "0xe8a116ec65f7d2d0d4748ba2bbcf8691cbd31202908ccfa3a975414fef801042") deposit_output_cell = create(:cell_output, block: ckb_transaction.block, capacity: 138 * 10**8, tx_hash: "0xe8a116ec65f7d2d0d4748ba2bbcf8691cbd31202908ccfa3a975414fef801042", cell_index: 0, ckb_transaction: ckb_transaction, generated_by: ckb_transaction, consumed_by: ckb_transaction, cell_type: "nervos_dao_deposit", data: "0x0000000000000000") @@ -153,12 +154,13 @@ class CkbTransactionTest < ActiveSupport::TestCase started_block = Block.select(:number, :timestamp).find(ckb_transaction.block_id) interest = CkbUtils.dao_interest(nervos_dao_withdrawing_cell) ended_block = Block.select(:number, :timestamp).find(phase1_transaction.block_id) - expected_display_input = CkbUtils.hash_value_to_s({ + expected_display_input = CkbUtils.hash_value_to_s( id: deposit_output_cell.id, from_cellbase: false, capacity: deposit_output_cell.capacity, address_hash: deposit_output_cell.address_hash, generated_tx_hash: deposit_output_cell.generated_by.tx_hash, compensation_started_block_number: started_block.number, compensation_ended_block_number: ended_block.number, compensation_started_timestamp: started_block.timestamp, compensation_ended_timestamp: ended_block.timestamp, - interest: interest, cell_type: deposit_output_cell.cell_type, cell_index: deposit_output_cell.cell_index }).sort + interest: interest, cell_type: deposit_output_cell.cell_type, cell_index: deposit_output_cell.cell_index + ).sort expected_attributes = %i(id from_cellbase capacity address_hash generated_tx_hash interest cell_type compensation_ended_block_number compensation_ended_timestamp compensation_started_block_number compensation_started_timestamp cell_index).sort assert_equal expected_attributes, phase1_transaction.display_inputs.first.keys.sort @@ -171,7 +173,7 @@ class CkbTransactionTest < ActiveSupport::TestCase dao_output.update(cell_type: "nervos_dao_withdrawing") expected_attributes = %i(id capacity address_hash status consumed_tx_hash cell_type).sort consumed_tx_hash = dao_output.live? ? nil : dao_output.consumed_by.tx_hash - expected_display_output = CkbUtils.hash_value_to_s({ id: dao_output.id, capacity: dao_output.capacity, address_hash: dao_output.address_hash, status: dao_output.status, consumed_tx_hash: consumed_tx_hash, cell_type: dao_output.cell_type }).sort + expected_display_output = CkbUtils.hash_value_to_s(id: dao_output.id, capacity: dao_output.capacity, address_hash: dao_output.address_hash, status: dao_output.status, consumed_tx_hash: consumed_tx_hash, cell_type: dao_output.cell_type).sort assert_equal expected_attributes, ckb_transaction.display_outputs.first.keys.sort assert_equal expected_display_output, ckb_transaction.display_outputs.first.sort @@ -189,7 +191,7 @@ class CkbTransactionTest < ActiveSupport::TestCase cell_input.update(previous_output: { "tx_hash": udt_input_transaction.tx_hash, "index": "0" }) expected_attributes = %i(id from_cellbase capacity address_hash generated_tx_hash udt_info cell_index cell_type).sort expected_udt_attributes = %i(symbol amount decimal type_hash published).sort - expected_display_input = CkbUtils.hash_value_to_s({ id: udt_cell_output.id, from_cellbase: false, capacity: udt_cell_output.capacity, address_hash: udt_cell_output.address_hash, generated_tx_hash: udt_cell_output.generated_by.tx_hash, cell_index: udt_cell_output.cell_index, cell_type: udt_cell_output.cell_type, udt_info: udt_cell_output.udt_info }) + expected_display_input = CkbUtils.hash_value_to_s(id: udt_cell_output.id, from_cellbase: false, capacity: udt_cell_output.capacity, address_hash: udt_cell_output.address_hash, generated_tx_hash: udt_cell_output.generated_by.tx_hash, cell_index: udt_cell_output.cell_index, cell_type: udt_cell_output.cell_type, udt_info: udt_cell_output.udt_info) assert_equal expected_attributes, ckb_transaction.display_inputs.first.keys.sort assert_equal expected_udt_attributes, ckb_transaction.display_inputs.first[:udt_info].keys.sort @@ -205,7 +207,7 @@ class CkbTransactionTest < ActiveSupport::TestCase expected_attributes = %i(id capacity address_hash status consumed_tx_hash cell_type udt_info).sort expected_udt_attributes = %i(symbol amount decimal type_hash published).sort - expected_display_input = CkbUtils.hash_value_to_s({ id: udt_cell_output.id, capacity: udt_cell_output.capacity, address_hash: udt_cell_output.address_hash, status: udt_cell_output.status, consumed_tx_hash: nil, cell_type: udt_cell_output.cell_type, udt_info: udt_cell_output.udt_info }) + expected_display_input = CkbUtils.hash_value_to_s(id: udt_cell_output.id, capacity: udt_cell_output.capacity, address_hash: udt_cell_output.address_hash, status: udt_cell_output.status, consumed_tx_hash: nil, cell_type: udt_cell_output.cell_type, udt_info: udt_cell_output.udt_info) assert_equal expected_attributes, udt_output_transaction.display_outputs.first.keys.sort assert_equal expected_udt_attributes, udt_output_transaction.display_outputs.first[:udt_info].keys.sort diff --git a/test/utils/ckb_utils_test.rb b/test/utils/ckb_utils_test.rb index b179b13f8..865fbcaba 100644 --- a/test/utils/ckb_utils_test.rb +++ b/test/utils/ckb_utils_test.rb @@ -216,7 +216,7 @@ class CkbUtilsTest < ActiveSupport::TestCase end test ".ckb_transaction_fee should return right tx_fee when tx is dao withdraw tx" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x177825f000") + DaoCompensationCalculator.any_instance.stubs(:call).returns(100800000000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -238,14 +238,14 @@ class CkbUtilsTest < ActiveSupport::TestCase ckb_transaction = CkbTransaction.find_by(tx_hash: node_tx.hash) input_capacities = ckb_transaction.inputs.sum(:capacity) output_capacities = ckb_transaction.outputs.sum(:capacity) - expected_tx_fee = ckb_transaction.inputs.sum(:capacity) + "0x177825f000".hex - 10**8 * 8 - ckb_transaction.outputs.sum(:capacity) + expected_tx_fee = ckb_transaction.inputs.sum(:capacity) + 100800000000 - ckb_transaction.outputs.sum(:capacity) assert_equal expected_tx_fee, CkbUtils.ckb_transaction_fee(ckb_transaction, input_capacities, output_capacities) end end test ".ckb_transaction_fee should return right tx_fee when tx is dao withdraw tx and have multiple dao cell" do - CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x177825f000") + DaoCompensationCalculator.any_instance.stubs(:call).returns(100800000000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do @@ -269,7 +269,7 @@ class CkbUtilsTest < ActiveSupport::TestCase ckb_transaction = CkbTransaction.find_by(tx_hash: node_tx.hash) input_capacities = ckb_transaction.inputs.sum(:capacity) output_capacities = ckb_transaction.outputs.sum(:capacity) - expected_tx_fee = ckb_transaction.inputs.sum(:capacity) + "0x177825f000".hex - 10**8 * 8 - ckb_transaction.outputs.sum(:capacity) + expected_tx_fee = ckb_transaction.inputs.sum(:capacity) + 100800000000 - ckb_transaction.outputs.sum(:capacity) assert_equal expected_tx_fee, CkbUtils.ckb_transaction_fee(ckb_transaction, input_capacities, output_capacities) end From 69feaae1b584bef250302c96665f37231cd50b4c Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 14 Jul 2020 12:48:46 +0800 Subject: [PATCH 05/21] feat: add dao column to cell output --- app/models/cell_output.rb | 1 + app/models/ckb_sync/node_data_processor.rb | 3 ++- db/migrate/20200714044614_add_dao_cell_outputs.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20200714044614_add_dao_cell_outputs.rb diff --git a/app/models/cell_output.rb b/app/models/cell_output.rb index f3c037ae3..0dd0939fb 100644 --- a/app/models/cell_output.rb +++ b/app/models/cell_output.rb @@ -85,6 +85,7 @@ def flush_cache # consumed_block_timestamp :decimal(30, ) # type_hash :string # udt_amount :decimal(40, ) +# dao :string # # Indexes # diff --git a/app/models/ckb_sync/node_data_processor.rb b/app/models/ckb_sync/node_data_processor.rb index 89af13119..bf8c0b4d5 100644 --- a/app/models/ckb_sync/node_data_processor.rb +++ b/app/models/ckb_sync/node_data_processor.rb @@ -515,7 +515,8 @@ def build_cell_output(ckb_transaction, output, address, cell_index, output_data) generated_by: ckb_transaction, cell_type: cell_type(output.type, output_data), block_timestamp: ckb_transaction.block_timestamp, - type_hash: output.type&.compute_hash + type_hash: output.type&.compute_hash, + dao: ckb_transaction.block.dao ) cell_output.udt_amount = CkbUtils.parse_udt_cell_data(output_data) if cell_output.udt? diff --git a/db/migrate/20200714044614_add_dao_cell_outputs.rb b/db/migrate/20200714044614_add_dao_cell_outputs.rb new file mode 100644 index 000000000..855afc157 --- /dev/null +++ b/db/migrate/20200714044614_add_dao_cell_outputs.rb @@ -0,0 +1,5 @@ +class AddDaoCellOutputs < ActiveRecord::Migration[6.0] + def change + add_column :cell_outputs, :dao, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 925a5b4dc..22b304499 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_07_09_100457) do +ActiveRecord::Schema.define(version: 2020_07_14_044614) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -157,6 +157,7 @@ t.decimal "consumed_block_timestamp", precision: 30 t.string "type_hash" t.decimal "udt_amount", precision: 40 + t.string "dao" t.index ["address_id", "status"], name: "index_cell_outputs_on_address_id_and_status" t.index ["block_id"], name: "index_cell_outputs_on_block_id" t.index ["ckb_transaction_id"], name: "index_cell_outputs_on_ckb_transaction_id" From cc68ad920a7a0ebf33f538be4a684078e67c143a Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 14 Jul 2020 12:58:36 +0800 Subject: [PATCH 06/21] chore: pick up dao directly from the cell_outputs --- app/services/charts/daily_statistic_generator.rb | 7 ++----- app/utils/ckb_utils.rb | 2 +- app/utils/dao_compensation_calculator.rb | 2 +- .../dao_contract_unclaimed_compensation_generator.rb | 4 ++-- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app/services/charts/daily_statistic_generator.rb b/app/services/charts/daily_statistic_generator.rb index db840d9c1..ee6d989bf 100644 --- a/app/services/charts/daily_statistic_generator.rb +++ b/app/services/charts/daily_statistic_generator.rb @@ -318,12 +318,9 @@ def phase1_dao_interests def unmade_dao_interests @unmade_dao_interests ||= begin + tip_dao = current_tip_block.dao CellOutput.nervos_dao_deposit.generated_before(ended_at).unconsumed_at(ended_at).reduce(0) do |memo, cell_output| - dao = cell_output.block.dao - tip_dao = current_tip_block.dao - parse_dao = CkbUtils.parse_dao(dao) - tip_parse_dao = CkbUtils.parse_dao(tip_dao) - memo + (cell_output.capacity - cell_output.occupied_capacity).to_i * tip_parse_dao.ar_i / parse_dao.ar_i - (cell_output.capacity - cell_output.occupied_capacity) + memo + DaoCompensationCalculator.new(cell_output, tip_dao).call end end end diff --git a/app/utils/ckb_utils.rb b/app/utils/ckb_utils.rb index d4d588254..9b56d6540 100644 --- a/app/utils/ckb_utils.rb +++ b/app/utils/ckb_utils.rb @@ -204,7 +204,7 @@ def self.dao_withdraw_tx_fee(ckb_transaction) def self.dao_interest(nervos_dao_withdrawing_cell) nervos_dao_withdrawing_cell_generated_tx = nervos_dao_withdrawing_cell.generated_by nervos_dao_deposit_cell = nervos_dao_withdrawing_cell_generated_tx.cell_inputs.order(:id)[nervos_dao_withdrawing_cell.cell_index].previous_cell_output - withdrawing_dao_cell_block_dao = nervos_dao_withdrawing_cell.block.dao + withdrawing_dao_cell_block_dao = nervos_dao_withdrawing_cell.dao DaoCompensationCalculator.new(nervos_dao_deposit_cell, withdrawing_dao_cell_block_dao).call rescue CKB::RPCError 0 diff --git a/app/utils/dao_compensation_calculator.rb b/app/utils/dao_compensation_calculator.rb index 56e63d838..e35154eb8 100644 --- a/app/utils/dao_compensation_calculator.rb +++ b/app/utils/dao_compensation_calculator.rb @@ -17,7 +17,7 @@ def parsed_withdraw_block_dao end def parsed_deposit_block_dao - CkbUtils.parse_dao(deposit_cell_output.block.dao) + CkbUtils.parse_dao(deposit_cell_output.dao) end def compensation_generating_capacity diff --git a/app/workers/dao_contract_unclaimed_compensation_generator.rb b/app/workers/dao_contract_unclaimed_compensation_generator.rb index d5a631ed6..f3176be95 100644 --- a/app/workers/dao_contract_unclaimed_compensation_generator.rb +++ b/app/workers/dao_contract_unclaimed_compensation_generator.rb @@ -18,9 +18,9 @@ def phase1_dao_interests end def unmade_dao_interests + tip_dao = current_tip_block.dao CellOutput.nervos_dao_deposit.generated_before(ended_at).unconsumed_at(ended_at).reduce(0) do |memo, cell_output| - dao = cell_output.block.dao - tip_dao = current_tip_block.dao + dao = cell_output.dao parse_dao = CkbUtils.parse_dao(dao) tip_parse_dao = CkbUtils.parse_dao(tip_dao) memo + (cell_output.capacity - cell_output.occupied_capacity).to_i * tip_parse_dao.ar_i / parse_dao.ar_i - (cell_output.capacity - cell_output.occupied_capacity) From ee4f8e0788a2fa1df1d7e911f1156be568e8ff5c Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 14 Jul 2020 13:26:01 +0800 Subject: [PATCH 07/21] chore: adjust tests --- test/models/address_test.rb | 10 +++++----- test/services/charts/daily_statistic_generator_test.rb | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/models/address_test.rb b/test/models/address_test.rb index a043dc408..7fe12ffb6 100644 --- a/test/models/address_test.rb +++ b/test/models/address_test.rb @@ -91,16 +91,16 @@ class AddressTest < ActiveSupport::TestCase deposit_tx = create(:ckb_transaction, block: deposit_block) previous_output_block = create(:block, :with_block_hash, dao: "0x28fbce93e82cbd2ff345ba74f2ba2300b0cd2c97f2953a000060983e29c50007") previous_output_tx = create(:ckb_transaction, block: previous_output_block) - create(:cell_output, block: previous_output_block, capacity: 50000 * 10**8, ckb_transaction: previous_output_tx, tx_hash: previous_output_tx.tx_hash, generated_by: previous_output_tx, cell_type: "nervos_dao_deposit", cell_index: 0, occupied_capacity: 6100000000) - create(:cell_output, block: previous_output_block, capacity: 50000 * 10**8, ckb_transaction: previous_output_tx, tx_hash: previous_output_tx.tx_hash, generated_by: previous_output_tx, cell_type: "nervos_dao_deposit", cell_index: 1, occupied_capacity: 6100000000) + create(:cell_output, block: previous_output_block, capacity: 50000 * 10**8, ckb_transaction: previous_output_tx, tx_hash: previous_output_tx.tx_hash, generated_by: previous_output_tx, cell_type: "nervos_dao_deposit", cell_index: 0, occupied_capacity: 6100000000, dao: previous_output_block.dao) + create(:cell_output, block: previous_output_block, capacity: 50000 * 10**8, ckb_transaction: previous_output_tx, tx_hash: previous_output_tx.tx_hash, generated_by: previous_output_tx, cell_type: "nervos_dao_deposit", cell_index: 1, occupied_capacity: 6100000000, dao: previous_output_block.dao) nervos_dao_withdrawing_block = create(:block, :with_block_hash, dao: "0x9a7a7ce1f34c6a332d147991f0602400aaf7346eb06bfc0000e2abc108760207", timestamp: CkbUtils.time_in_milliseconds(Time.current)) nervos_dao_withdrawing_tx = create(:ckb_transaction, block: nervos_dao_withdrawing_block) create(:cell_input, block: nervos_dao_withdrawing_block, previous_output: { tx_hash: previous_output_tx.tx_hash, index: 0 }, ckb_transaction: nervos_dao_withdrawing_tx) create(:cell_input, block: nervos_dao_withdrawing_block, previous_output: { tx_hash: previous_output_tx.tx_hash, index: 1 }, ckb_transaction: nervos_dao_withdrawing_tx) - create(:cell_output, block: nervos_dao_withdrawing_block, address: address, cell_type: "nervos_dao_withdrawing", ckb_transaction: nervos_dao_withdrawing_tx, capacity: 10000 * 10**8, generated_by: nervos_dao_withdrawing_tx, cell_index: 0) - create(:cell_output, block: nervos_dao_withdrawing_block, address: address, cell_type: "nervos_dao_withdrawing", ckb_transaction: nervos_dao_withdrawing_tx, capacity: 20000 * 10**8, generated_by: nervos_dao_withdrawing_tx, cell_index: 1) + create(:cell_output, block: nervos_dao_withdrawing_block, address: address, cell_type: "nervos_dao_withdrawing", ckb_transaction: nervos_dao_withdrawing_tx, capacity: 10000 * 10**8, generated_by: nervos_dao_withdrawing_tx, cell_index: 0, dao: nervos_dao_withdrawing_block.dao) + create(:cell_output, block: nervos_dao_withdrawing_block, address: address, cell_type: "nervos_dao_withdrawing", ckb_transaction: nervos_dao_withdrawing_tx, capacity: 20000 * 10**8, generated_by: nervos_dao_withdrawing_tx, cell_index: 1, dao: nervos_dao_withdrawing_block.dao) - deposit_cell = create(:cell_output, block: deposit_block, address: address, cell_type: "nervos_dao_deposit", capacity: 60000 * 10**8, ckb_transaction: deposit_tx, generated_by: deposit_tx, cell_index: 0, occupied_capacity: 6100000000) + deposit_cell = create(:cell_output, block: deposit_block, address: address, cell_type: "nervos_dao_deposit", capacity: 60000 * 10**8, ckb_transaction: deposit_tx, generated_by: deposit_tx, cell_index: 0, occupied_capacity: 6100000000, dao: deposit_block.dao) expected_phase1_dao_interests = 181251857496 parse_dao_ar_i = 10239678363827763 diff --git a/test/services/charts/daily_statistic_generator_test.rb b/test/services/charts/daily_statistic_generator_test.rb index 7017518fe..a393ed77a 100644 --- a/test/services/charts/daily_statistic_generator_test.rb +++ b/test/services/charts/daily_statistic_generator_test.rb @@ -28,7 +28,7 @@ class DailyStatisticGeneratorTest < ActiveSupport::TestCase create(:block, :with_block_hash, epoch: 69, timestamp: 1575090866093, dao: "0xeeaf2fe1baa6df2e577fda67799223009ca127a6d1e30c00002dc77aa42b0007") create(:address, address_hash: "ckb1qyqy6mtud5sgctjwgg6gydd0ea05mr339lnslczzrc", balance: 10**8 * 1000) tx = create(:ckb_transaction, block: block, block_timestamp: block.timestamp) - create(:cell_output, cell_type: "nervos_dao_deposit", generated_by: tx, ckb_transaction: tx, block: block, capacity: 10**8 * 1000, block_timestamp: (Time.current - 1.day).end_of_day.to_i * 1000, occupied_capacity: 6100000000) + create(:cell_output, cell_type: "nervos_dao_deposit", generated_by: tx, ckb_transaction: tx, block: block, capacity: 10**8 * 1000, block_timestamp: (Time.current - 1.day).end_of_day.to_i * 1000, occupied_capacity: 6100000000, dao: block.dao) create(:daily_statistic, created_at_unixtimestamp: Time.current.yesterday.yesterday.beginning_of_day.to_i) assert_difference -> { ::DailyStatistic.count }, 1 do Charts::DailyStatisticGenerator.new.call From 34b3c1f0f03ab9d3ea3bb46a88accf43d9ec691d Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 14 Jul 2020 21:00:15 +0800 Subject: [PATCH 08/21] chore: add task to fill dao to cell outputs --- .../migration/fill_dao_to_cell_outputs.rake | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 lib/tasks/migration/fill_dao_to_cell_outputs.rake diff --git a/lib/tasks/migration/fill_dao_to_cell_outputs.rake b/lib/tasks/migration/fill_dao_to_cell_outputs.rake new file mode 100644 index 000000000..dffa1cb9f --- /dev/null +++ b/lib/tasks/migration/fill_dao_to_cell_outputs.rake @@ -0,0 +1,16 @@ +namespace :migration do + desc "Usage: RAILS_ENV=production bundle exec rake migration:fill_dao_to_cell_outputs" + task fill_dao_to_cell_outputs: :environment do + progress_bar = ProgressBar.create(total: CellOutput.count, format: "%e %B %p%% %c/%C") + CellOutput.find_in_batches do |cell_outputs| + values = cell_outputs.map do |cell_output| + progress_bar.increment + { id: cell_output.id, dao: cell_output.block.dao, created_at: cell_output.created_at, updated_at: cell_output.updated_at } + end + + CellOutput.upsert_all(values) + end + + puts "done" + end +end From 76954f925d041a58a780f976ac017a3e4273db2e Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 14 Jul 2020 22:54:39 +0800 Subject: [PATCH 09/21] chore: use dao compensation calculator on dao contract unclaimed compensation generator --- app/utils/dao_compensation_calculator.rb | 2 +- .../dao_contract_unclaimed_compensation_generator.rb | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/utils/dao_compensation_calculator.rb b/app/utils/dao_compensation_calculator.rb index e35154eb8..26527a3f5 100644 --- a/app/utils/dao_compensation_calculator.rb +++ b/app/utils/dao_compensation_calculator.rb @@ -23,4 +23,4 @@ def parsed_deposit_block_dao def compensation_generating_capacity @compensation_generating_capacity ||= (deposit_cell_output.capacity - deposit_cell_output.occupied_capacity).to_i end -end \ No newline at end of file +end diff --git a/app/workers/dao_contract_unclaimed_compensation_generator.rb b/app/workers/dao_contract_unclaimed_compensation_generator.rb index f3176be95..58fe638a0 100644 --- a/app/workers/dao_contract_unclaimed_compensation_generator.rb +++ b/app/workers/dao_contract_unclaimed_compensation_generator.rb @@ -12,18 +12,15 @@ def cal_unclaimed_compensation end def phase1_dao_interests - CellOutput.nervos_dao_withdrawing.generated_before(ended_at).unconsumed_at(ended_at).reduce(0) do |memo, nervos_dao_withdrawing_cell| + CellOutput.nervos_dao_withdrawing.live.reduce(0) do |memo, nervos_dao_withdrawing_cell| memo + CkbUtils.dao_interest(nervos_dao_withdrawing_cell) end end def unmade_dao_interests tip_dao = current_tip_block.dao - CellOutput.nervos_dao_deposit.generated_before(ended_at).unconsumed_at(ended_at).reduce(0) do |memo, cell_output| - dao = cell_output.dao - parse_dao = CkbUtils.parse_dao(dao) - tip_parse_dao = CkbUtils.parse_dao(tip_dao) - memo + (cell_output.capacity - cell_output.occupied_capacity).to_i * tip_parse_dao.ar_i / parse_dao.ar_i - (cell_output.capacity - cell_output.occupied_capacity) + CellOutput.nervos_dao_deposit.live.reduce(0) do |memo, cell_output| + memo + DaoCompensationCalculator.new(cell_output, tip_dao).call end end From 11f649b057692fa26d6449aeba7797a0e87153eb Mon Sep 17 00:00:00 2001 From: shaojunda Date: Wed, 15 Jul 2020 02:06:43 +0800 Subject: [PATCH 10/21] perf: add cache on lock_script --- app/models/address.rb | 4 +++- app/serializers/address_serializer.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/address.rb b/app/models/address.rb index 77499054c..094d85a5b 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -65,7 +65,9 @@ def lock_info end def lock_script - LockScript.where(address: self).first + Rails.cache.realize(["Address", "lock_script", id], race_condition_ttl: 3.seconds) do + LockScript.where(address: self).first + end end def self.find_or_create_address(lock_script, block_timestamp) diff --git a/app/serializers/address_serializer.rb b/app/serializers/address_serializer.rb index bed8de6f0..4d3abffe3 100644 --- a/app/serializers/address_serializer.rb +++ b/app/serializers/address_serializer.rb @@ -1,7 +1,7 @@ class AddressSerializer include FastJsonapi::ObjectSerializer - attributes :address_hash, :lock_script, :lock_info + attributes :address_hash, :lock_info attribute :balance do |object| object.balance.to_s end From 117fa1e0fca678acfea469fcd2a8d329d1e404be Mon Sep 17 00:00:00 2001 From: shaojunda Date: Wed, 15 Jul 2020 15:28:56 +0800 Subject: [PATCH 11/21] perf: use cache on blocks controller --- app/controllers/api/v1/blocks_controller.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/controllers/api/v1/blocks_controller.rb b/app/controllers/api/v1/blocks_controller.rb index a1b66233c..0e4d84367 100644 --- a/app/controllers/api/v1/blocks_controller.rb +++ b/app/controllers/api/v1/blocks_controller.rb @@ -6,16 +6,21 @@ class BlocksController < ApplicationController def index if from_home_page? - block_timestamps = Block.recent.limit(ENV["HOMEPAGE_BLOCK_RECORDS_COUNT"].to_i).pluck(:timestamp) - blocks = Block.where(timestamp: block_timestamps).select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes).recent - options = {} + blocks = Block.recent.limit(ENV["HOMEPAGE_BLOCK_RECORDS_COUNT"].to_i).select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes) + json = + Rails.cache.realize(blocks.cache_key, version: blocks.cache_version) do + BlockListSerializer.new(blocks).serialized_json + end else - block_timestamps = Block.recent.select(:timestamp).page(@page).per(@page_size) - blocks = Block.where(timestamp: block_timestamps.pluck(:timestamp)).select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes).recent - options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: block_timestamps, page: @page, page_size: @page_size).call + blocks = Block.recent.select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes).page(@page).per(@page_size) + json = + Rails.cache.realize(blocks.cache_key, version: blocks.cache_version) do + options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: blocks, page: @page, page_size: @page_size).call + BlockListSerializer.new(blocks, options).serialized_json + end end - render json: BlockListSerializer.new(blocks, options) + render json: json end def show From 9d4405961a4d0faddfd68d343f6ba37276b782b3 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Thu, 16 Jul 2020 01:57:57 +0800 Subject: [PATCH 12/21] chore: upgrade bootsnap gem --- Gemfile | 13 ++++++------- Gemfile.lock | 6 ++---- config/environments/development.rb | 1 + 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index 0e1d064da..04f65da00 100644 --- a/Gemfile +++ b/Gemfile @@ -8,7 +8,7 @@ gem "rails", "~> 6.0.0" # Use postgresql as the database for Active Record gem "pg", ">= 0.18", "< 2.0" # Use Puma as the app server -gem "puma", "~> 4.3.5" +gem "puma", "~> 4.3.5", require: false # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder # gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production @@ -34,9 +34,6 @@ gem "dotenv-rails" # manage environment specific settings by config gem "config" -# daemons -gem "daemons", "~> 1.2", ">= 1.2.6" - # CKB SDK gem "ckb-sdk-ruby", git: "https://github.com/nervosnetwork/ckb-sdk-ruby.git", require: "ckb", branch: "develop" @@ -64,12 +61,14 @@ gem "kaminari" gem "ruby-progressbar", require: false -gem "sentry-raven" +group :production do + gem "sentry-raven" + gem "newrelic_rpm" +end # Deployment gem "mina", require: false gem "mina-multistage", require: false -gem "newrelic_rpm" gem "rack-attack" group :development, :test do @@ -100,7 +99,7 @@ group :development do gem "rubocop", require: false gem "rubocop-rails", require: false gem "rubocop-performance", require: false - gem "awesome_print" + gem "awesome_print", require: false gem "annotate" end diff --git a/Gemfile.lock b/Gemfile.lock index b0e0ba6f2..43f24436e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -91,7 +91,7 @@ GEM awesome_print (1.8.0) bitcoin-secp256k1 (0.5.2) ffi (>= 1.9.25) - bootsnap (1.4.4) + bootsnap (1.4.6) msgpack (~> 1.0) builder (3.2.4) chronic_duration (0.10.6) @@ -110,7 +110,6 @@ GEM crack (0.4.3) safe_yaml (~> 1.0.0) crass (1.0.6) - daemons (1.3.1) database_cleaner (1.7.0) deep_merge (1.2.1) docile (1.3.2) @@ -212,7 +211,7 @@ GEM ruby-progressbar mocha (1.8.0) metaclass (~> 0.0.1) - msgpack (1.2.10) + msgpack (1.3.3) multipart-post (2.1.1) net-http-persistent (3.1.0) connection_pool (~> 2.2) @@ -380,7 +379,6 @@ DEPENDENCIES ckb-sdk-ruby! codecov config - daemons (~> 1.2, >= 1.2.6) database_cleaner dotenv-rails factory_bot_rails diff --git a/config/environments/development.rb b/config/environments/development.rb index 78275453f..721af54a3 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -48,4 +48,5 @@ # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. config.file_watcher = ActiveSupport::EventedFileUpdateChecker + config.console = Pry end From 7724b38632a473185e27ac3a0f9c038ed9741309 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Thu, 16 Jul 2020 12:32:25 +0800 Subject: [PATCH 13/21] chore: set raven context only in production --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 12f1f0d9d..02448e75e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,7 +17,7 @@ def catch_404 private def set_raven_context - Raven.extra_context(params: params.to_unsafe_h, url: request.url) + Raven.extra_context(params: params.to_unsafe_h, url: request.url) if Rails.env.production? end def api_error(error) From 1de72d4029deef52fe549fd7da001ac3f5367c37 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Thu, 16 Jul 2020 23:17:27 +0800 Subject: [PATCH 14/21] perf: add cache on dao contract show --- app/controllers/api/v1/contracts_controller.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/contracts_controller.rb b/app/controllers/api/v1/contracts_controller.rb index 6c9faa260..273ae2161 100644 --- a/app/controllers/api/v1/contracts_controller.rb +++ b/app/controllers/api/v1/contracts_controller.rb @@ -1,10 +1,17 @@ +require "pry" module Api module V1 class ContractsController < ApplicationController def show raise Api::V1::Exceptions::ContractNotFoundError if params[:id] != DaoContract::CONTRACT_NAME - render json: DaoContractSerializer.new(DaoContract.default_contract) + dao_contract = DaoContract.where(id: 1) + json = + Rails.cache.realize(dao_contract.cache_key, version: dao_contract.cache_version, race_condition_ttl: 3.seconds) do + DaoContractSerializer.new(dao_contract.first).serialized_json + end + + render json: json end end end From 9776722a21b7b0b58d09bff14dd988b86ecc5c10 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Thu, 16 Jul 2020 23:17:52 +0800 Subject: [PATCH 15/21] chore: use race_condition_ttl --- app/controllers/api/v1/blocks_controller.rb | 4 ++-- app/controllers/api/v1/ckb_transactions_controller.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/blocks_controller.rb b/app/controllers/api/v1/blocks_controller.rb index 0e4d84367..64e0097fb 100644 --- a/app/controllers/api/v1/blocks_controller.rb +++ b/app/controllers/api/v1/blocks_controller.rb @@ -8,13 +8,13 @@ def index if from_home_page? blocks = Block.recent.limit(ENV["HOMEPAGE_BLOCK_RECORDS_COUNT"].to_i).select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes) json = - Rails.cache.realize(blocks.cache_key, version: blocks.cache_version) do + Rails.cache.realize(blocks.cache_key, version: blocks.cache_version, race_condition_ttl: 3.seconds) do BlockListSerializer.new(blocks).serialized_json end else blocks = Block.recent.select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes).page(@page).per(@page_size) json = - Rails.cache.realize(blocks.cache_key, version: blocks.cache_version) do + Rails.cache.realize(blocks.cache_key, version: blocks.cache_version, race_condition_ttl: 3.seconds) do options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: blocks, page: @page, page_size: @page_size).call BlockListSerializer.new(blocks, options).serialized_json end diff --git a/app/controllers/api/v1/ckb_transactions_controller.rb b/app/controllers/api/v1/ckb_transactions_controller.rb index efdbc03d0..629045b6b 100644 --- a/app/controllers/api/v1/ckb_transactions_controller.rb +++ b/app/controllers/api/v1/ckb_transactions_controller.rb @@ -8,14 +8,14 @@ def index if from_home_page? ckb_transactions = CkbTransaction.recent.normal.limit(ENV["HOMEPAGE_TRANSACTIONS_RECORDS_COUNT"].to_i).select(:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved) json = - Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do + Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version, race_condition_ttl: 3.seconds) do CkbTransactionListSerializer.new(ckb_transactions).serialized_json end render json: json else ckb_transactions = CkbTransaction.recent.normal.page(@page).per(@page_size).select(:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved) json = - Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do + Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version, race_condition_ttl: 3.seconds) do options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call CkbTransactionListSerializer.new(ckb_transactions, options).serialized_json end From b38ba86bce356a27e97b942a3762d368349c3982 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Thu, 16 Jul 2020 23:45:02 +0800 Subject: [PATCH 16/21] chore: adjust tests --- app/controllers/api/v1/contracts_controller.rb | 1 - test/controllers/api/v1/contracts_controller_test.rb | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/contracts_controller.rb b/app/controllers/api/v1/contracts_controller.rb index 273ae2161..5abd715c7 100644 --- a/app/controllers/api/v1/contracts_controller.rb +++ b/app/controllers/api/v1/contracts_controller.rb @@ -1,4 +1,3 @@ -require "pry" module Api module V1 class ContractsController < ApplicationController diff --git a/test/controllers/api/v1/contracts_controller_test.rb b/test/controllers/api/v1/contracts_controller_test.rb index 620e718a2..6cc3146ca 100644 --- a/test/controllers/api/v1/contracts_controller_test.rb +++ b/test/controllers/api/v1/contracts_controller_test.rb @@ -46,12 +46,14 @@ class ContractsControllerTest < ActionDispatch::IntegrationTest end test "the returned dao contract when param is dao_contract" do + DaoContract.default_contract valid_get api_v1_contract_url(DaoContract::CONTRACT_NAME) assert_equal DaoContract::CONTRACT_NAME, json.dig("data", "type") end test "should contain right keys in the serialized object when visit show" do + DaoContract.default_contract valid_get api_v1_contract_url(DaoContract::CONTRACT_NAME) response_contract = json["data"] @@ -61,6 +63,7 @@ class ContractsControllerTest < ActionDispatch::IntegrationTest end test "should return corresponding contract with given contract name" do + DaoContract.default_contract valid_get api_v1_contract_url(DaoContract::CONTRACT_NAME) assert_equal JSON.parse(DaoContractSerializer.new(DaoContract.default_contract).serialized_json), json From 77e78b5f5a1bac7eec0d38a911b15c928b1e2c38 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Fri, 17 Jul 2020 15:35:33 +0800 Subject: [PATCH 17/21] fix: average block time missing data --- app/services/charts/daily_statistic_generator.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/services/charts/daily_statistic_generator.rb b/app/services/charts/daily_statistic_generator.rb index ee6d989bf..c8f6c451f 100644 --- a/app/services/charts/daily_statistic_generator.rb +++ b/app/services/charts/daily_statistic_generator.rb @@ -56,7 +56,8 @@ def avg_block_time_rolling_by_hour_sql avg(avg_block_time_per_hour) over(order by stat_timestamp rows between 7 * 24 preceding and current row) as avg_bt1 from block_time_statistics ) - select stat_timestamp, round(avg_bt, 2) avg_bt1, round(avg_bt1, 2) avg_bt2 from avg_block_time_24_hours_rolling_by_hour where stat_timestamp >= #{35.days.ago.to_i} order by stat_timestamp limit 720 + -- 804 = 24 * 35, show data for the last 35 days + select stat_timestamp, round(avg_bt, 2) avg_bt1, round(avg_bt1, 2) avg_bt2 from avg_block_time_24_hours_rolling_by_hour where stat_timestamp >= #{35.days.ago.end_of_day.to_i} order by stat_timestamp limit 840 SQL end From 7201ce09b6303880220e12987a32f3ec484cb8d5 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Fri, 17 Jul 2020 15:57:02 +0800 Subject: [PATCH 18/21] chore: upgrade faker --- Gemfile | 2 +- Gemfile.lock | 39 ++++++++------------------------------- 2 files changed, 9 insertions(+), 32 deletions(-) diff --git a/Gemfile b/Gemfile index 04f65da00..2d68121df 100644 --- a/Gemfile +++ b/Gemfile @@ -87,7 +87,7 @@ group :test do gem "database_cleaner" gem "mocha" gem "factory_bot_rails" - gem "faker", git: "https://github.com/stympy/faker.git", branch: "master" + gem "faker", git: "https://github.com/faker-ruby/faker.git", branch: "master" gem "codecov", require: false end diff --git a/Gemfile.lock b/Gemfile.lock index 43f24436e..29da8b53c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,11 @@ +GIT + remote: https://github.com/faker-ruby/faker.git + revision: 484f776b9e734eea24114f2c46d9dad9622f0f4f + branch: master + specs: + faker (2.13.0) + i18n (>= 1.6, < 2) + GIT remote: https://github.com/nervosnetwork/ckb-sdk-ruby.git revision: c9e651c25953c0c6173f8c1fa75e98e546f1164b @@ -8,19 +16,6 @@ GIT net-http-persistent (~> 3.1.0) rbnacl (~> 7.1.1) -GIT - remote: https://github.com/stympy/faker.git - revision: 0e8cc7eeb160532c84d8665aef649788e48a2c9c - branch: master - specs: - faker (1.9.3) - i18n (>= 0.7) - pastel (~> 0.7.2) - thor (~> 0.20.0) - tty-pager (~> 0.12.0) - tty-screen (~> 0.6.5) - tty-tree (~> 0.3.0) - GEM remote: https://rubygems.org/ specs: @@ -145,7 +140,6 @@ GEM dry-equalizer (~> 0.2) dry-logic (~> 0.5, >= 0.5.0) dry-types (~> 0.14.0) - equatable (0.5.0) erubi (1.9.0) et-orbi (1.2.2) tzinfo @@ -224,9 +218,6 @@ GEM parallel (1.17.0) parser (2.6.3.0) ast (~> 2.4.0) - pastel (0.7.2) - equatable (~> 0.5.0) - tty-color (~> 0.4.0) pg (1.1.4) pry (0.12.2) coderay (~> 1.1.0) @@ -337,26 +328,12 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - strings (0.1.5) - strings-ansi (~> 0.1) - unicode-display_width (~> 1.5) - unicode_utils (~> 1.4) - strings-ansi (0.1.0) thor (0.20.3) thread_safe (0.3.6) tilt (2.0.9) - tty-color (0.4.3) - tty-pager (0.12.1) - strings (~> 0.1.4) - tty-screen (~> 0.6) - tty-which (~> 0.4) - tty-screen (0.6.5) - tty-tree (0.3.0) - tty-which (0.4.1) tzinfo (1.2.7) thread_safe (~> 0.1) unicode-display_width (1.6.0) - unicode_utils (1.4.0) url (0.3.2) vcr (5.0.0) webmock (3.6.0) From 1407c3f3a7b35f2fd1a33c7fff60644b96f752c3 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Fri, 17 Jul 2020 15:58:51 +0800 Subject: [PATCH 19/21] test: average block time data count --- test/factories/block_time_statistic.rb | 6 ++++++ test/services/charts/daily_statistic_generator_test.rb | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 test/factories/block_time_statistic.rb diff --git a/test/factories/block_time_statistic.rb b/test/factories/block_time_statistic.rb new file mode 100644 index 000000000..776cd6a2d --- /dev/null +++ b/test/factories/block_time_statistic.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :block_time_statistic do + stat_timestamp { Faker::Time.unique.between(from: 35.days.ago, to: Time.current).to_i } + avg_block_time_per_hour { Faker::Number.decimal(l_digits: 2) } + end +end diff --git a/test/services/charts/daily_statistic_generator_test.rb b/test/services/charts/daily_statistic_generator_test.rb index a393ed77a..5358b8d8d 100644 --- a/test/services/charts/daily_statistic_generator_test.rb +++ b/test/services/charts/daily_statistic_generator_test.rb @@ -34,5 +34,11 @@ class DailyStatisticGeneratorTest < ActiveSupport::TestCase Charts::DailyStatisticGenerator.new.call end end + + test "average_block_time should return 840 points" do + create_list(:block_time_statistic, 1000) + daily_generator = Charts::DailyStatisticGenerator.new + assert_equal 24 * 35, daily_generator.send(:average_block_time).count + end end end From deea9146e8ab56bae2c63ef4112cb52bbb118046 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Fri, 17 Jul 2020 16:13:26 +0800 Subject: [PATCH 20/21] chore: adjust tests --- test/factories/block.rb | 2 +- test/factories/block_statistic.rb | 10 +++++----- test/factories/ckb_transaction.rb | 2 +- test/factories/daily_statistic.rb | 8 ++++---- test/factories/epoch_statistic.rb | 6 +++--- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/test/factories/block.rb b/test/factories/block.rb index d67b5ad6d..fa90a9a94 100644 --- a/test/factories/block.rb +++ b/test/factories/block.rb @@ -5,7 +5,7 @@ number { 10 } parent_hash { "0xcba2d1a70602a1def80efbd59629c37a9d6c36f9de7a8ed6d1ca4f76389365e1" } nonce { 1757392074788233522 } - timestamp { Faker::Time.between(2.days.ago, Date.today, :all).to_i } + timestamp { Faker::Time.between(from: 2.days.ago, to: Date.today).to_i } transactions_root { "0xe08894ef0ed80481448f7a584438a76b6bdbea178c02b4c3b40863d75c5aed3c" } proposals_hash { "0x0000000000000000000000000000000000000000000000000000000000000000" } uncles_count { 1 } diff --git a/test/factories/block_statistic.rb b/test/factories/block_statistic.rb index f65935a06..adbbfe831 100644 --- a/test/factories/block_statistic.rb +++ b/test/factories/block_statistic.rb @@ -1,9 +1,9 @@ FactoryBot.define do factory :block_statistic do - block_number { Faker::Number.number(2) } - live_cells_count { Faker::Number.number(4) } - dead_cells_count { Faker::Number.number(4) } - hash_rate { Faker::Number.number(20) } - difficulty { Faker::Number.number(20) } + block_number { Faker::Number.number(digits: 2) } + live_cells_count { Faker::Number.number(digits: 4) } + dead_cells_count { Faker::Number.number(digits: 4) } + hash_rate { Faker::Number.number(digits: 20) } + difficulty { Faker::Number.number(digits: 20) } end end diff --git a/test/factories/ckb_transaction.rb b/test/factories/ckb_transaction.rb index ca79e8ee8..1ef41276d 100644 --- a/test/factories/ckb_transaction.rb +++ b/test/factories/ckb_transaction.rb @@ -4,7 +4,7 @@ tx_hash { "0x#{SecureRandom.hex(32)}" } deps {} block_number {} - block_timestamp { Faker::Time.between(2.days.ago, Date.today, :all).to_i } + block_timestamp { Faker::Time.between(from: 2.days.ago, to: Date.today).to_i } transaction_fee { 0 } version { 0 } witnesses {} diff --git a/test/factories/daily_statistic.rb b/test/factories/daily_statistic.rb index a00fee4a9..22db60883 100644 --- a/test/factories/daily_statistic.rb +++ b/test/factories/daily_statistic.rb @@ -1,9 +1,9 @@ FactoryBot.define do factory :daily_statistic do created_at_unixtimestamp { Time.zone.now.to_i } - transactions_count { Faker::Number.number(4) } - addresses_count { Faker::Number.number(4) } - total_dao_deposit { Faker::Number.number(20) } - occupied_capacity { Faker::Number.number(4) } + transactions_count { Faker::Number.number(digits: 4) } + addresses_count { Faker::Number.number(digits: 4) } + total_dao_deposit { Faker::Number.number(digits: 20) } + occupied_capacity { Faker::Number.number(digits: 4) } end end diff --git a/test/factories/epoch_statistic.rb b/test/factories/epoch_statistic.rb index 053fa31a9..7ee7cf0b8 100644 --- a/test/factories/epoch_statistic.rb +++ b/test/factories/epoch_statistic.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :epoch_statistic do - epoch_number { Faker::Number.number(2) } - uncle_rate { Faker::Number.number(20) } - difficulty { Faker::Number.number(20) } + epoch_number { Faker::Number.number(digits: 2) } + uncle_rate { Faker::Number.number(digits: 20) } + difficulty { Faker::Number.number(digits: 20) } end end From 21f451cba2f3fd397738de9832ab7d8108981f3a Mon Sep 17 00:00:00 2001 From: shaojunda Date: Fri, 17 Jul 2020 17:44:29 +0800 Subject: [PATCH 21/21] chore: Bump version to v0.10.1 --- CHANGELOG.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7409ba7f4..c3c969d65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,22 @@ +# [v0.10.1](https://github.com/shaojunda/ckb-explorer/compare/v0.10.0...v0.10.1) (2020-07-17) + + +### Bug Fixes + +* [#695](https://github.com/nervosnetwork/ckb-explorer/pull/695): fix average block time missing data + + +### Performance Improvements + +* [#683](https://github.com/nervosnetwork/ckb-explorer/pull/683): perf lock info +* [#687](https://github.com/nervosnetwork/ckb-explorer/pull/687): perf use DB data replace RPC call +* [#688](https://github.com/nervosnetwork/ckb-explorer/pull/688): perf transaction index +* [#691](https://github.com/nervosnetwork/ckb-explorer/pull/691): perf address unclaimed compensation worker +* [#692](https://github.com/nervosnetwork/ckb-explorer/pull/692): perf add cache on lock_script +* [#693](https://github.com/nervosnetwork/ckb-explorer/pull/693): perf use cache on blocks controller +* [#694](https://github.com/nervosnetwork/ckb-explorer/pull/694): perf contract show + + # [v0.10.0](https://github.com/nervosnetwork/ckb-explorer/compare/v0.9.9...v0.10.0) (2020-07-10)