Skip to content

Commit

Permalink
Issue 500 (#1673)
Browse files Browse the repository at this point in the history
* feat: remove unused controller action

Signed-off-by: Miles Zhang <[email protected]>

* feat: return contract resource distributed api

Signed-off-by: Miles Zhang <[email protected]>

* test: fix test

Signed-off-by: Miles Zhang <[email protected]>

---------

Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid authored Mar 12, 2024
1 parent bbe9455 commit 0c0c7b8
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 37 deletions.
7 changes: 0 additions & 7 deletions app/controllers/api/v2/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ def referring_cells
end
end

def referring_capacities
expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
data = Contract.all.map { { _1.code_hash => _1.total_referring_cells_capacity.to_s } }

render json: { data: }
end

private

def get_script_content
Expand Down
18 changes: 17 additions & 1 deletion app/controllers/api/v2/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@ def transaction_fees
render json: {
transaction_fee_rates: stats_info.transaction_fee_rates,
pending_transaction_fee_rates: stats_info.pending_transaction_fee_rates,
last_n_days_transaction_fee_rates: stats_info.last_n_days_transaction_fee_rates
last_n_days_transaction_fee_rates: stats_info.last_n_days_transaction_fee_rates,
}
end

def contract_resource_distributed
expires_in 30.minutes, public: true

json = Contract.all.map do |contract|
{
name: contract.name,
code_hash: contract.code_hash,
tx_count: contract.ckb_transactions_count,
capacity_amount: contract.total_referring_cells_capacity,
address_count: contract.addresses_count,
}
end

render json:
end
end
end
1 change: 1 addition & 0 deletions app/models/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def self.create_initial_data
# referring_cells_count :decimal(30, ) default(0)
# total_deployed_cells_capacity :decimal(30, ) default(0)
# total_referring_cells_capacity :decimal(30, ) default(0)
# addresses_count :integer
#
# Indexes
#
Expand Down
1 change: 0 additions & 1 deletion app/models/referring_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,5 @@ def self.create_initial_data_for_ckb_transaction(ckb_transaction)
#
# Indexes
#
# index_referring_cells_on_cell_output_id (cell_output_id) UNIQUE
# index_referring_cells_on_contract_id_and_cell_output_id (contract_id,cell_output_id) UNIQUE
#
3 changes: 2 additions & 1 deletion app/workers/contract_statistic_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def perform
deployed_cells_count: contract.deployed_cell_outputs&.live&.size,
referring_cells_count: contract.referring_cell_outputs&.live&.size,
total_deployed_cells_capacity: contract.deployed_cell_outputs&.live&.sum(:capacity),
total_referring_cells_capacity: contract.referring_cell_outputs&.live&.sum(:capacity)
total_referring_cells_capacity: contract.referring_cell_outputs&.live&.sum(:capacity),
addresses_count: contract.referring_cell_outputs&.live&.select(:address_id)&.distinct&.count,
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion config/routes/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
get :deployed_cells
get :referring_cells
get :general_info
get :referring_capacities
end
end

Expand All @@ -75,6 +74,7 @@
resources :statistics, only: [] do
collection do
get :transaction_fees
get :contract_resource_distributed
end
end

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240312050057_add_addresses_count_to_contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAddressesCountToContract < ActiveRecord::Migration[7.0]
def change
add_column :contracts, :addresses_count, :integer
end
end
13 changes: 4 additions & 9 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,8 @@ CREATE TABLE public.contracts (
deployed_cells_count numeric(30,0) DEFAULT 0.0,
referring_cells_count numeric(30,0) DEFAULT 0.0,
total_deployed_cells_capacity numeric(30,0) DEFAULT 0.0,
total_referring_cells_capacity numeric(30,0) DEFAULT 0.0
total_referring_cells_capacity numeric(30,0) DEFAULT 0.0,
addresses_count integer
);


Expand Down Expand Up @@ -4371,13 +4372,6 @@ CREATE INDEX index_pool_transaction_entries_on_tx_status ON public.pool_transact
CREATE UNIQUE INDEX index_portfolios_on_user_id_and_address_id ON public.portfolios USING btree (user_id, address_id);


--
-- Name: index_referring_cells_on_cell_output_id; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX index_referring_cells_on_cell_output_id ON public.referring_cells USING btree (cell_output_id);


--
-- Name: index_referring_cells_on_contract_id_and_cell_output_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -5190,6 +5184,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20240228102716'),
('20240301025505'),
('20240305100337'),
('20240311143030');
('20240311143030'),
('20240312050057');


4 changes: 2 additions & 2 deletions test/controllers/api/v1/suggest_queries_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SuggestQueriesControllerTest < ActionDispatch::IntegrationTest
end

test "should set right content type when call show" do
valid_get api_v1_suggest_queries_url("0x3b238b3326d10ec000417b68bc715f17e86293d6cdbcb3fd8a628ad4a0b756f6")
valid_get api_v1_suggest_queries_url, params: { q: "0x3b238b3326d10ec000417b68bc715f17e86293d6cdbcb3fd8a628ad4a0b756f6" }

assert_equal "application/vnd.api+json", response.media_type
end
Expand Down Expand Up @@ -191,7 +191,7 @@ class SuggestQueriesControllerTest < ActionDispatch::IntegrationTest
:type_script,
code_hash: Settings.type_id_code_hash,
args: "0x8536c9d5d908bd89fc70099e4284870708b6632356aad98734fcf43f6f71c304",
script_hash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"
script_hash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
)
response_json = TypeScriptSerializer.new(type_script).serialized_json

Expand Down
8 changes: 0 additions & 8 deletions test/controllers/api/v2/scripts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ class ScriptsControllerTest < ActionDispatch::IntegrationTest
valid_get referring_cells_api_v2_scripts_url(code_hash: @code_hash, hash_type: @hash_type)
assert_response :success
end

test "should get referring_capacities" do
create_list(:contract, 10)
referring_capacities = Contract.all.map { { _1.code_hash => _1.total_referring_cells_capacity.to_s } }

valid_get referring_capacities_api_v2_scripts_url
assert_equal ({ "data" => referring_capacities }), json
end
end
end
end
21 changes: 16 additions & 5 deletions test/controllers/api/v2/statistics_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest
tx_hash1 = "0x497277029e6335c6d5f916574dc4475ee229f3c1cce3658e7dad017a8ed580d4"
tx_hash2 = "0xe9772bae467924e0feee85e9b7087993d38713bd8c19c954c4b68da69b4f4644"
create :ckb_transaction, created_at: Time.at(tx_created_at),
transaction_fee: 30000, bytes: 20, confirmation_time: confirmation_time, block: block, tx_hash: tx_hash1
transaction_fee: 30000, bytes: 20, confirmation_time:, block:, tx_hash: tx_hash1
create :ckb_transaction, created_at: Time.at(tx_created_at),
transaction_fee: 30000, bytes: 20, confirmation_time: confirmation_time, block: block, tx_hash: tx_hash2
transaction_fee: 30000, bytes: 20, confirmation_time:, block:, tx_hash: tx_hash2
create :pending_transaction, transaction_fee: 30000, bytes: 20,
tx_hash: tx_hash1
create :pending_transaction, transaction_fee: 13000, bytes: 15,
Expand All @@ -29,7 +29,8 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest
get transaction_fees_api_v2_statistics_url,
headers: {
"Content-Type": "application/vnd.api+json",
"Accept": "application/json" }
"Accept": "application/json",
}
data = JSON.parse(response.body)
assert_equal CkbTransaction.tx_pending.count,
data["transaction_fee_rates"].size
Expand All @@ -45,7 +46,8 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest
get transaction_fees_api_v2_statistics_url,
headers: {
"Content-Type": "application/vnd.api+json",
"Accept": "application/json" }
"Accept": "application/json",
}
data = JSON.parse(response.body)
assert_equal CkbTransaction.tx_pending.count,
data["transaction_fee_rates"].size
Expand Down Expand Up @@ -76,7 +78,8 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest
get transaction_fees_api_v2_statistics_url,
headers: {
"Content-Type": "application/vnd.api+json",
"Accept": "application/json" }
"Accept": "application/json",
}
data = JSON.parse(response.body)

assert_equal 3, data["last_n_days_transaction_fee_rates"].size
Expand All @@ -86,6 +89,14 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
end

test "return contracts resource distributed" do
create_list(:contract, 3)
get contract_resource_distributed_api_v2_statistics_url
data = JSON.parse(response.body)

assert_equal 3, data.size
end
end
end
end
2 changes: 2 additions & 0 deletions test/factories/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
verified { false }
deprecated { false }
total_referring_cells_capacity { SecureRandom.random_number(10**10) }
ckb_transactions_count { SecureRandom.random_number(10**10) }
addresses_count { SecureRandom.random_number(100_000_000) }

after(:create) do |contract, _eval|
tx = create :ckb_transaction, :with_single_output
Expand Down
4 changes: 2 additions & 2 deletions test/services/charts/daily_statistic_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class DailyStatisticGeneratorTest < ActiveSupport::TestCase
create(:cell_output, :with_full_transaction,
block_timestamp: @datetime.to_i * 1000, block: @block),
]
CellOutput.where(id: cells.map(&:id)).update_all(consumed_block_timestamp: (@datetime.to_i + 1) * 1000)
CellOutput.where(id: cells.map(&:id)).update_all(consumed_block_timestamp: (@datetime.to_i + 10) * 1000)
is_from_scratch = true
assert_equal "3",
Charts::DailyStatisticGenerator.new(@datetime,
Expand All @@ -246,7 +246,7 @@ class DailyStatisticGeneratorTest < ActiveSupport::TestCase
# dead_cells_count = dead_cells_count_today + yesterday_daily_statistic.dead_cells_count.to_i
# dead_cells_count_today = CellOutput.consumed_after(started_at).consumed_before(ended_at).count
#
create :daily_statistic, created_at_unixtimestamp: @datetime.yesterday.to_i, dead_cells_count: 888
create :daily_statistic, created_at_unixtimestamp: @datetime.yesterday.yesterday.to_i, dead_cells_count: 888

assert_equal (3 + 888).to_s,
Charts::DailyStatisticGenerator.new(@datetime).call.dead_cells_count
Expand Down

0 comments on commit 0c0c7b8

Please sign in to comment.