From ad546815c51a72eefa56acfe6c3053f3fc26b0b6 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 2 Jun 2020 12:00:32 +0800 Subject: [PATCH 01/23] chore: support last 90 miner distribution --- app/models/distribution_data.rb | 9 ++++++--- app/serializers/distribution_data_serializer.rb | 8 +++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/models/distribution_data.rb b/app/models/distribution_data.rb index f1e632250..a4578d7c0 100644 --- a/app/models/distribution_data.rb +++ b/app/models/distribution_data.rb @@ -77,9 +77,12 @@ def transaction_propagation_delay_history TransactionPropagationDelay.connection.select_all(sql) end - def miner_address_distribution - Rails.cache.realize("miner_address_distribution", expires_in: 1.day) do - result = Block.where("timestamp >= ?", CkbUtils.time_in_milliseconds(7.days.ago.to_i)).group(:miner_hash).order("count(miner_hash) desc").count.to_a + def miner_address_distribution(checkpoint = 7) + supported_checkpoints = [7, 90] + return unless checkpoint.in?(supported_checkpoints) + + Rails.cache.realize("miner_address_distribution_#{checkpoint}", expires_in: 1.day) do + result = Block.where("timestamp >= ?", CkbUtils.time_in_milliseconds(checkpoint.days.ago.to_i)).group(:miner_hash).order("count(miner_hash) desc").count.to_a if result.present? (result[0..9].map{ |item| [item[0], item[1].to_s] } + [["other", result[10..-1].map { |item| item[1] }.reduce(:+).to_s]]).to_h else diff --git a/app/serializers/distribution_data_serializer.rb b/app/serializers/distribution_data_serializer.rb index 136a5c0a1..5c81fc2ce 100644 --- a/app/serializers/distribution_data_serializer.rb +++ b/app/serializers/distribution_data_serializer.rb @@ -51,5 +51,11 @@ class DistributionDataSerializer attribute :miner_address_distribution, if: Proc.new { |_record, params| params && params[:indicator].include?("miner_address_distribution") - } + } do |object, params| + if rs = params[:indicator].match(/(\d+)/) + object.miner_address_distribution(rs[1].to_i) + else + object.miner_address_distribution + end + end end From c3db35fa58ae46e86c2edc08cf3f30b8adf17bbf Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 2 Jun 2020 12:14:15 +0800 Subject: [PATCH 02/23] chore: update distribution data validator --- app/controllers/validations/distribution_data.rb | 8 +++++++- app/controllers/validations/monetary_data.rb | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/validations/distribution_data.rb b/app/controllers/validations/distribution_data.rb index a6e55bcc6..3139643c3 100644 --- a/app/controllers/validations/distribution_data.rb +++ b/app/controllers/validations/distribution_data.rb @@ -25,9 +25,15 @@ def error_object attr_accessor :query_key def query_key_format_must_be_correct - if query_key.blank? || !(query_key.split("-") - ::DistributionData::VALID_INDICATORS).empty? + if query_key.blank? || !query_key_valid? errors.add(:query_key, "indicator name is invalid") end end + + def query_key_valid? + query_keys = query_key.split("-") + extra_keys = query_keys - ::DistributionData::VALID_INDICATORS + extra_keys.blank? || extra_keys.size == 1 && /^miner_address_distribution(\d+)$/ =~ extra_keys.first + end end end diff --git a/app/controllers/validations/monetary_data.rb b/app/controllers/validations/monetary_data.rb index bb1444249..1a1da42d6 100644 --- a/app/controllers/validations/monetary_data.rb +++ b/app/controllers/validations/monetary_data.rb @@ -33,7 +33,7 @@ def query_key_format_must_be_correct def query_key_valid? query_keys = query_key.split("-") extra_keys = query_keys - ::MonetaryData::VALID_INDICATORS - extra_keys.blank? || extra_keys.size == 1 && extra_keys.first =~ /^nominal_apc(\d+)$/ + extra_keys.blank? || extra_keys.size == 1 && /^nominal_apc(\d+)$/ =~ extra_keys.first end end end From 59b68d5c4b15b0af49183cd6589f57d3b23572ff Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 2 Jun 2020 18:29:54 +0800 Subject: [PATCH 03/23] chore: set checkpoint to miner distribution --- app/models/distribution_data.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/distribution_data.rb b/app/models/distribution_data.rb index a4578d7c0..5f32c3868 100644 --- a/app/models/distribution_data.rb +++ b/app/models/distribution_data.rb @@ -83,8 +83,9 @@ def miner_address_distribution(checkpoint = 7) Rails.cache.realize("miner_address_distribution_#{checkpoint}", expires_in: 1.day) do result = Block.where("timestamp >= ?", CkbUtils.time_in_milliseconds(checkpoint.days.ago.to_i)).group(:miner_hash).order("count(miner_hash) desc").count.to_a + cut_off_point = (result.count * 0.7).ceil if result.present? - (result[0..9].map{ |item| [item[0], item[1].to_s] } + [["other", result[10..-1].map { |item| item[1] }.reduce(:+).to_s]]).to_h + (result[0..cut_off_point].map{ |item| [item[0], item[1].to_s] } + [["other", result[(cut_off_point + 1)..-1].map { |item| item[1] }.reduce(:+).to_s]]).to_h else result end From 0c167fec662f1ebaa3e2644e88b0b8a4acd48836 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 1 Jun 2020 15:14:27 +0800 Subject: [PATCH 04/23] test: status code and header info for index action --- .../api/v1/udts_controller_test.rb | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/controllers/api/v1/udts_controller_test.rb b/test/controllers/api/v1/udts_controller_test.rb index b237c89cb..ac15b7579 100644 --- a/test/controllers/api/v1/udts_controller_test.rb +++ b/test/controllers/api/v1/udts_controller_test.rb @@ -108,6 +108,58 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest response_tx_transaction = json["data"] assert_equal %w(symbol full_name total_amount addresses_count decimal icon_file).sort, response_tx_transaction["attributes"].keys.sort end + + test "should get success code when call index" do + udt = create(:udt, published: true) + + valid_get api_v1_udts_url(udt.type_hash) + + assert_response :success + end + + test "should set right content type when call index" do + udt = create(:udt) + + valid_get api_v1_udts_url(udt.type_hash) + + assert_equal "application/vnd.api+json", response.media_type + end + + test "should respond with 415 Unsupported Media Type when Content-Type is wrong when call index" do + udt = create(:udt) + + get api_v1_udts_url(udt.type_hash), headers: { "Content-Type": "text/plain" } + + assert_equal 415, response.status + end + + test "should respond with error object when Content-Type is wrong when call index" do + udt = create(:udt) + error_object = Api::V1::Exceptions::InvalidContentTypeError.new + response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + + get api_v1_udts_url(udt.type_hash), headers: { "Content-Type": "text/plain" } + + assert_equal response_json, response.body + end + + test "should respond with 406 Not Acceptable when Accept is wrong when call index" do + udt = create(:udt) + + get api_v1_udts_url(udt.type_hash), headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } + + assert_equal 406, response.status + end + + test "should respond with error object when Accept is wrong when call index" do + udt = create(:udt) + error_object = Api::V1::Exceptions::InvalidAcceptError.new + response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + + get api_v1_udts_url(udt.type_hash), headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } + + assert_equal response_json, response.body + end end end end From fb12617426f466434b6871386db2327b5953957c Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 1 Jun 2020 15:14:45 +0800 Subject: [PATCH 05/23] feat: add udt index action --- app/controllers/api/v1/udts_controller.rb | 4 ++++ config/routes.rb | 2 +- .../api/v1/udts_controller_test.rb | 22 +++++-------------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/app/controllers/api/v1/udts_controller.rb b/app/controllers/api/v1/udts_controller.rb index 6b28451b5..b4ddbf4b5 100644 --- a/app/controllers/api/v1/udts_controller.rb +++ b/app/controllers/api/v1/udts_controller.rb @@ -1,6 +1,10 @@ class Api::V1::UdtsController < ApplicationController before_action :validate_query_params, only: :show + def index + render json: {} + end + def show udt = Udt.find_by!(type_hash: params[:id], published: true) diff --git a/config/routes.rb b/config/routes.rb index 050022217..b046b4a1b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -38,7 +38,7 @@ resources :block_statistics, only: :show resources :epoch_statistics, only: :show resources :market_data, only: :show - resources :udts, only: :show + resources :udts, only: %i(index show) resources :udt_transactions, only: :show resources :address_udt_transactions, only: :show resources :distribution_data, only: :show diff --git a/test/controllers/api/v1/udts_controller_test.rb b/test/controllers/api/v1/udts_controller_test.rb index ac15b7579..51e3470db 100644 --- a/test/controllers/api/v1/udts_controller_test.rb +++ b/test/controllers/api/v1/udts_controller_test.rb @@ -110,53 +110,43 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest end test "should get success code when call index" do - udt = create(:udt, published: true) - - valid_get api_v1_udts_url(udt.type_hash) + valid_get api_v1_udts_url assert_response :success end test "should set right content type when call index" do - udt = create(:udt) - - valid_get api_v1_udts_url(udt.type_hash) + valid_get api_v1_udts_url assert_equal "application/vnd.api+json", response.media_type end test "should respond with 415 Unsupported Media Type when Content-Type is wrong when call index" do - udt = create(:udt) - - get api_v1_udts_url(udt.type_hash), headers: { "Content-Type": "text/plain" } + get api_v1_udts_url, headers: { "Content-Type": "text/plain" } assert_equal 415, response.status end test "should respond with error object when Content-Type is wrong when call index" do - udt = create(:udt) error_object = Api::V1::Exceptions::InvalidContentTypeError.new response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json - get api_v1_udts_url(udt.type_hash), headers: { "Content-Type": "text/plain" } + get api_v1_udts_url, headers: { "Content-Type": "text/plain" } assert_equal response_json, response.body end test "should respond with 406 Not Acceptable when Accept is wrong when call index" do - udt = create(:udt) - - get api_v1_udts_url(udt.type_hash), headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } + get api_v1_udts_url, headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } assert_equal 406, response.status end test "should respond with error object when Accept is wrong when call index" do - udt = create(:udt) error_object = Api::V1::Exceptions::InvalidAcceptError.new response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json - get api_v1_udts_url(udt.type_hash), headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } + get api_v1_udts_url, headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } assert_equal response_json, response.body end From a411feaa8846a6fe5829c2bf08c3171cafd0e808 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 1 Jun 2020 20:13:28 +0800 Subject: [PATCH 06/23] test: udt index action logic --- .../api/v1/udts_controller_test.rb | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/controllers/api/v1/udts_controller_test.rb b/test/controllers/api/v1/udts_controller_test.rb index 51e3470db..7bd3638f4 100644 --- a/test/controllers/api/v1/udts_controller_test.rb +++ b/test/controllers/api/v1/udts_controller_test.rb @@ -150,6 +150,37 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest assert_equal response_json, response.body end + + test "should get empty array when there are no udts" do + valid_get api_v1_udts_url + + assert_empty json["data"] + end + + test "should return udts in order of descending addresses count" do + udt1 = create(:udt, addresses_count: 1, published: true) + udt2 = create(:udt, addresses_count: 2) + udt3 = create(:udt, addresses_count: 3) + + valid_get api_v1_udts_url + + expected_udts = UdtSerializer.new([udt3, udt2, udt1]).serialized_json + + assert_equal expected_udts, response.body + end + + test "unpublished udt symbol field should show args last 2 bytes" do + udt1 = create(:udt, addresses_count: 1, published: true) + udt2 = create(:udt, addresses_count: 2, args: "0x94bbc8327e16d195de87815c391e7b9131e80419c51a405a0b21227c6ee05129") + + valid_get api_v1_udts_url + + expected_published_udt_name = udt1.symbol + expected_unpublished_udt_name = udt2.args + + assert_equal expected_unpublished_udt_name, json["data"][0] + assert_equal expected_published_udt_name, json["data"][1] + end end end end From 067c4c4ec99848a326a042394cac9d14c5ae110d Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 1 Jun 2020 20:15:39 +0800 Subject: [PATCH 07/23] chore: return udts order by addresses count desc --- app/controllers/api/v1/udts_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/udts_controller.rb b/app/controllers/api/v1/udts_controller.rb index b4ddbf4b5..8b259a22e 100644 --- a/app/controllers/api/v1/udts_controller.rb +++ b/app/controllers/api/v1/udts_controller.rb @@ -2,7 +2,8 @@ class Api::V1::UdtsController < ApplicationController before_action :validate_query_params, only: :show def index - render json: {} + udts = Udt.order(addresses_count: :desc).limit(1000) + render json: UdtSerializer.new(udts) end def show From 7e451ee641ac0f523a9ab430bded5ebb1d641bae Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 1 Jun 2020 20:24:45 +0800 Subject: [PATCH 08/23] feat: add block_timestamp to udt --- app/models/udt.rb | 1 + db/migrate/20200601121842_add_block_timestamp_to_udts.rb | 5 +++++ db/schema.rb | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20200601121842_add_block_timestamp_to_udts.rb diff --git a/app/models/udt.rb b/app/models/udt.rb index 46dc2f85b..b25f599c4 100644 --- a/app/models/udt.rb +++ b/app/models/udt.rb @@ -36,6 +36,7 @@ def ckb_transactions # published :boolean default(FALSE) # created_at :datetime not null # updated_at :datetime not null +# block_timestamp :decimal(30, ) # # Indexes # diff --git a/db/migrate/20200601121842_add_block_timestamp_to_udts.rb b/db/migrate/20200601121842_add_block_timestamp_to_udts.rb new file mode 100644 index 000000000..a9d0eaf88 --- /dev/null +++ b/db/migrate/20200601121842_add_block_timestamp_to_udts.rb @@ -0,0 +1,5 @@ +class AddBlockTimestampToUdts < ActiveRecord::Migration[6.0] + def change + add_column :udts, :block_timestamp, :decimal, precision: 30 + end +end diff --git a/db/schema.rb b/db/schema.rb index 52ae6c958..a6083dd0a 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_05_25_100826) do +ActiveRecord::Schema.define(version: 2020_06_01_121842) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -388,6 +388,7 @@ t.boolean "published", default: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.decimal "block_timestamp", precision: 30 t.index ["type_hash"], name: "index_udts_on_type_hash", unique: true end From 18933d776414da7041d81152b2aac0c75a85fafc Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 1 Jun 2020 20:25:10 +0800 Subject: [PATCH 09/23] chore: save block timestamp to udt --- app/models/ckb_sync/node_data_processor.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/ckb_sync/node_data_processor.rb b/app/models/ckb_sync/node_data_processor.rb index c90e64eb3..d045ac8e9 100644 --- a/app/models/ckb_sync/node_data_processor.rb +++ b/app/models/ckb_sync/node_data_processor.rb @@ -39,7 +39,7 @@ def process_block(node_block) update_current_block_mining_info(local_block) update_block_contained_address_info(local_block) update_block_reward_info(local_block) - update_udt_accounts(udt_infos) + update_udt_accounts(udt_infos, local_block.timestamp) update_udt_info(udt_infos) dao_events = build_new_dao_depositor_events(new_dao_depositor_events) DaoEvent.import!(dao_events, validate: false) @@ -67,7 +67,7 @@ def update_udt_info(udt_infos) Udt.import columns, import_values, validate: false, on_duplicate_key_update: { conflict_target: [:type_hash], columns: [:total_amount, :addresses_count] } end - def update_udt_accounts(udt_infos) + def update_udt_accounts(udt_infos, block_timestamp) return if udt_infos.blank? udt_infos.each do |udt_output| @@ -78,7 +78,7 @@ def update_udt_accounts(udt_infos) if udt_account.present? udt_account.update!(amount: amount) else - udt = Udt.find_or_create_by!(type_hash: udt_output[:type_hash], code_hash: ENV["SUDT_CELL_TYPE_HASH"], udt_type: "sudt") + udt = Udt.find_or_create_by!(type_hash: udt_output[:type_hash], code_hash: ENV["SUDT_CELL_TYPE_HASH"], udt_type: "sudt", block_timestamp: block_timestamp) address.udt_accounts.create!(udt_type: udt.udt_type, full_name: udt.full_name, symbol: udt.symbol, decimal: udt.decimal, published: udt.published, code_hash: udt.code_hash, type_hash: udt.type_hash, amount: amount, udt: udt) end end From bfa41a1f56521269fc8ff2b805eabd8094a350e9 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 1 Jun 2020 20:31:59 +0800 Subject: [PATCH 10/23] feat: add h24_ckb_transactions_count to udt --- app/models/udt.rb | 6 ++++++ app/serializers/udt_serializer.rb | 3 +++ 2 files changed, 9 insertions(+) diff --git a/app/models/udt.rb b/app/models/udt.rb index b25f599c4..6ac3bd7fe 100644 --- a/app/models/udt.rb +++ b/app/models/udt.rb @@ -13,6 +13,12 @@ def ckb_transactions ckb_transaction_ids = CellOutput.udt.where(type_hash: type_hash).pluck("generated_by_id") + CellOutput.udt.where(type_hash: type_hash).pluck("consumed_by_id").compact CkbTransaction.where(id: ckb_transaction_ids.uniq) end + + def h24_ckb_transactions_count + Rails.cache.realize("udt_h24_ckb_transactions_count_#{id}", expires_in: 1.hour) do + ckb_transactions.where("block_timestamp >= ?", CkbUtils.time_in_milliseconds(24.hours.ago)).count + end + end end # == Schema Information diff --git a/app/serializers/udt_serializer.rb b/app/serializers/udt_serializer.rb index 0d3b9a176..536983468 100644 --- a/app/serializers/udt_serializer.rb +++ b/app/serializers/udt_serializer.rb @@ -12,4 +12,7 @@ class UdtSerializer attribute :decimal do |object| object.decimal.to_s end + attribute :h24_ckb_transactions_count do |object| + object.h24_ckb_transactions_count.to_s + end end From f781bc15473bea54faae2358292be87f65781a6a Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 1 Jun 2020 20:36:31 +0800 Subject: [PATCH 11/23] chore: add task to fill block timestamp to udt --- .../fill_block_timestamp_to_udt.rake | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lib/tasks/migration/fill_block_timestamp_to_udt.rake diff --git a/lib/tasks/migration/fill_block_timestamp_to_udt.rake b/lib/tasks/migration/fill_block_timestamp_to_udt.rake new file mode 100644 index 000000000..e2f21586c --- /dev/null +++ b/lib/tasks/migration/fill_block_timestamp_to_udt.rake @@ -0,0 +1,20 @@ +namespace :migration do + task fill_block_timestamp_to_udt: :environment do + udts = Udt.all + progress_bar = ProgressBar.create({ + total: udts.count, + format: "%e %B %p%% %c/%C" + }) + + values = + udts.map do |udt| + progress_bar.increment + cell_output = CellOutput.find_by(type_hash: udt.type_hash) + { id: udt.id, block_timestamp: cell_output.block_timestamp, created_at: udt.created_at, updated_at: Time.current } + end + + Udt.upsert_all(values) + + puts "done" + end +end From 592715dedd543f3ee73787da0ca4c679d9854a61 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 2 Jun 2020 11:09:32 +0800 Subject: [PATCH 12/23] chore: adjust tests --- test/controllers/api/v1/udts_controller_test.rb | 15 +-------------- test/models/ckb_sync/node_data_processor_test.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/test/controllers/api/v1/udts_controller_test.rb b/test/controllers/api/v1/udts_controller_test.rb index 7bd3638f4..728b8f918 100644 --- a/test/controllers/api/v1/udts_controller_test.rb +++ b/test/controllers/api/v1/udts_controller_test.rb @@ -106,7 +106,7 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest valid_get api_v1_udt_url(udt.type_hash) response_tx_transaction = json["data"] - assert_equal %w(symbol full_name total_amount addresses_count decimal icon_file).sort, response_tx_transaction["attributes"].keys.sort + assert_equal %w(symbol full_name total_amount addresses_count decimal icon_file h24_ckb_transactions_count).sort, response_tx_transaction["attributes"].keys.sort end test "should get success code when call index" do @@ -168,19 +168,6 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest assert_equal expected_udts, response.body end - - test "unpublished udt symbol field should show args last 2 bytes" do - udt1 = create(:udt, addresses_count: 1, published: true) - udt2 = create(:udt, addresses_count: 2, args: "0x94bbc8327e16d195de87815c391e7b9131e80419c51a405a0b21227c6ee05129") - - valid_get api_v1_udts_url - - expected_published_udt_name = udt1.symbol - expected_unpublished_udt_name = udt2.args - - assert_equal expected_unpublished_udt_name, json["data"][0] - assert_equal expected_published_udt_name, json["data"][1] - end end end end diff --git a/test/models/ckb_sync/node_data_processor_test.rb b/test/models/ckb_sync/node_data_processor_test.rb index cf76c5f60..5cf9a71df 100644 --- a/test/models/ckb_sync/node_data_processor_test.rb +++ b/test/models/ckb_sync/node_data_processor_test.rb @@ -1822,7 +1822,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase create(:block, :with_block_hash, number: node_block.header.number - 1) node_output = node_block.transactions.first.outputs.first node_output.type = CKB::Types::Script.new(code_hash: ENV["SUDT_CELL_TYPE_HASH"], args: "0xb2e61ff569acf041b3c2c17724e2379c581eeac3") - create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash) + create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash, block_timestamp: node_block.header.timestamp) local_block = node_data_processor.process_block(node_block) assert_equal ["udt"], local_block.cell_outputs.pluck(:cell_type).uniq @@ -1836,7 +1836,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase create(:block, :with_block_hash, number: node_block.header.number - 1) node_output = node_block.transactions.first.outputs.first node_output.type = CKB::Types::Script.new(code_hash: ENV["SUDT_CELL_TYPE_HASH"], args: "0xb2e61ff569acf041b3c2c17724e2379c581eeac3") - create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash) + create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash, block_timestamp: node_block.header.timestamp) address_hash = CkbUtils.generate_address(node_output.lock) address = Address.find_by(address_hash: address_hash) @@ -1923,7 +1923,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase new_node_output.type = CKB::Types::Script.new(code_hash: ENV["SUDT_CELL_TYPE_HASH"], args: "0xb2e61ff569acf041b3c2c17724e2379c581eeac2") new_node_output.lock = CKB::Types::Script.new(code_hash: ENV["SECP_CELL_TYPE_HASH"], args: "0xc2e61ff569acf041b3c2c17724e2379c581eeac2") node_output.type = CKB::Types::Script.new(code_hash: ENV["SUDT_CELL_TYPE_HASH"], args: "0xb2e61ff569acf041b3c2c17724e2379c581eeac2") - udt = create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash, published: true) + udt = create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash, published: true, block_timestamp: node_block.header.timestamp) node_block.transactions.first.outputs_data[0] = "0x000050ad321ea12e0000000000000000" node_block.transactions.first.outputs_data[1] = "0x0000909dceda82370000000000000000" address_hash = CkbUtils.generate_address(node_output.lock) @@ -1977,7 +1977,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase node_output = node_block.transactions.first.outputs.first node_output.type = CKB::Types::Script.new(code_hash: ENV["SUDT_CELL_TYPE_HASH"], args: "0xb2e61ff569acf041b3c2c17724e2379c581eeac3") node_block.transactions.first.outputs_data[0] = "0x000050ad321ea12e0000000000000000" - create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash) + create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash, block_timestamp: node_block.header.timestamp) node_data_processor.process_block(node_block) block = Block.find_by(number: 21) block.update(block_hash: "0x419c632366c8eb9635acbb39ea085f7552ae62e1fdd480893375334a0f37d1bx") @@ -2006,8 +2006,8 @@ class NodeDataProcessorTest < ActiveSupport::TestCase node_output.type = CKB::Types::Script.new(code_hash: ENV["SUDT_CELL_TYPE_HASH"], args: "0xb2e61ff569acf041b3c2c17724e2379c581eeac3") node_block.transactions.first.outputs_data[0] = "0x000050ad321ea12e0000000000000000" node_block.transactions.first.outputs_data[1] = "0x0000909dceda82370000000000000000" - create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash) - create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: new_node_output.type.compute_hash) + create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: node_output.type.compute_hash, block_timestamp: node_block.header.timestamp) + create(:udt, code_hash: ENV["SUDT_CELL_TYPE_HASH"], type_hash: new_node_output.type.compute_hash, block_timestamp: node_block.header.timestamp) node_data_processor.process_block(node_block) block = Block.find_by(number: 21) block.update(block_hash: "0x419c632366c8eb9635acbb39ea085f7552ae62e1fdd480893375334a0f37d1bx") From 71b743c161db12f58fc9361bb0aff3cf082199a7 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 2 Jun 2020 19:07:51 +0800 Subject: [PATCH 13/23] chore: return more fields on udt --- app/serializers/udt_serializer.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/serializers/udt_serializer.rb b/app/serializers/udt_serializer.rb index 536983468..09dc5e886 100644 --- a/app/serializers/udt_serializer.rb +++ b/app/serializers/udt_serializer.rb @@ -1,7 +1,7 @@ class UdtSerializer include FastJsonapi::ObjectSerializer - attributes :symbol, :full_name, :icon_file + attributes :symbol, :full_name, :icon_file, :published, :description attribute :total_amount do |object| object.total_amount.to_s @@ -15,4 +15,7 @@ class UdtSerializer attribute :h24_ckb_transactions_count do |object| object.h24_ckb_transactions_count.to_s end + attributes :created_at do |object| + object.block_timestamp.to_s + end end From ecdd60a864f4a9a1ce76fb18f9e61fa7f20090ae Mon Sep 17 00:00:00 2001 From: shaojunda Date: Tue, 2 Jun 2020 19:17:54 +0800 Subject: [PATCH 14/23] chore: update api doc --- doc/api.raml | 78 +++++++++++++++++++++++++++++++++++++++++++++ public/api_doc.html | 53 +++++++++++++++++++++++++++++- 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/doc/api.raml b/doc/api.raml index 0cdfef5b0..2a6130096 100644 --- a/doc/api.raml +++ b/doc/api.raml @@ -779,6 +779,15 @@ types: }, "icon_file": { type: "string" + }, + "description": { + type: "string" + }, + "created_at": { + type: "string" + }, + "published": { + type: "boolean" } } } @@ -1014,6 +1023,15 @@ types: } } + udts_response: { + type: object, + properties: { + "data": { + "type": "udt[]", + } + } + } + index_statistic_response: { type: object, properties: { @@ -3690,6 +3708,66 @@ types: } /udts: + get: + description: Returns udts in reverse chronological order by addresses count. + responses: + 200: + body: + application/vnd.api+json: + type: udts_response + example: | + { + "data": [ + { + "id": "1", + "type": "udt", + "attributes": { + "symbol": "kfc", + "full_name": "Kingdom Fly Coin", + "icon_file": "data:image/png;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAQDAwQDAwQEAwQFBAQFBgoHBgYGBg0JCggKDw0QEA8NDw4RExgUERIXEg4PFRwVFxkZGxsbEBQdHx0aHxgaGxr/2wBDAQQFBQYFBgwHBwwaEQ8RGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhr/wAARCAAoACgDASIAAhEBAxEB/8QAGwAAAgIDAQAAAAAAAAAAAAAAAAYEBwEFCAP/xAAtEAABAgUCBAUEAwAAAAAAAAABAgQAAwUREgYTFCExUSJBYXHwBxUygRaRwf/EABoBAAIDAQEAAAAAAAAAAAAAAAACAwUGBAf/xAAiEQABAwQCAgMAAAAAAAAAAAABAAIRAwQhMUFRBhMiQoH/2gAMAwEAAhEDEQA/AO83TtDWVnM6eXeFhzWJ7hRCV4pvyANvn7vGKu6LlyoXOKPCB8+c4pmhUKo6Zq8qq1yTw1PkZ7kwzQvHIEDkCT1IhXOhVd5ePtH0wKZc1xyeGjGTjWZzGlbW4c872Ve94nNa1ObqSCvNPmFG/wA/UUt9QdQU+tCn/anO+ZRmblkqTa+NuoHYxpadpOt1NtJdsmxmt5hulW8kXsbHkT6GENTMAKmr+QOFy6hbUfbHLTPXQOphdWNHct5KEyUfcdoIVqM6LV0hJJwX4VD5/f6giVa1QJmQUTMvl53ipdQfUEVqlOaf9vMgzbDc3srWUD0t6RdFYaqbuZhIOK7qSf8APnaOfdJ06RU9Rtmz+SZreYZmSTcA2SSOY9oiqE4A5WS8gr3LXUbag6PbLT+wOjG+F76T0mNTh2S7LXhyjpLzyyv6jtDKNUjRJTQOEL7hbDf3NvLLxfjY2tlbr5QaqvokNP4ungi7Kt8AbmWNrfle1sj07xsqDQafqalN6rXG3Ev54O7NKlIviogcgQByAhACMDaqrS0fbPNpaENuGj5O20tOYEznLfqNHPbvLyuDLvl5WgifR2hcOkGxCEG6j7QR0L0JNDtpLeSjLmj2PaFZ3R3DVRKZe4i/JSReCCBCgbaivAgFV7RsGdHcuFC6DLRfmoi0EECE0NGktnKEuUPc94IIIEL/2Q==", + "total_amount": "10000000000", + "addresses_count": "3", + "decimal": "6", + "h24_ckb_transactions_count": "0", + "published": true, + "description": "Kingdom Fly Coin", + "created_at": "1591096409" + } + }, + { + "id": "4", + "type": "udt", + "attributes": { + "symbol": null, + "full_name": null, + "icon_file": null, + "total_amount": "100000000000000000000", + "addresses_count": "3", + "decimal": "", + "h24_ckb_transactions_count": "0", + "published": false, + "description": "", + "created_at": "1591096409" + } + }, + { + "id": "14", + "type": "udt", + "attributes": { + "symbol": null, + "full_name": null, + "icon_file": null, + "total_amount": "9999900000", + "addresses_count": "1", + "decimal": "", + "h24_ckb_transactions_count": "0", + "published": false, + "description": "", + "created_at": "1591096409" + } + }] + } + /{type_hash}: uriParameters: type_hash: diff --git a/public/api_doc.html b/public/api_doc.html index 67b351a84..62071c180 100644 --- a/public/api_doc.html +++ b/public/api_doc.html @@ -2150,7 +2150,58 @@

/market_data

get

Returns market data by given indicator

/udts

get

Returns a UDT by type hash

/udts

get

Returns udts in reverse chronological order by addresses count.

get

Returns a UDT by type hash