Skip to content

Commit

Permalink
Merge pull request #2304 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
zmcNotafraid authored Dec 6, 2024
2 parents d912d09 + 804e3e5 commit 5513973
Show file tree
Hide file tree
Showing 134 changed files with 2,656 additions and 2,181 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ BITCOIN_SIGNET_PASS=""

# Dynamic CORS configuration
PARTNER_DOMAINS="/localhost:\d*/"

# -------------------------------- Fiber segment --------------------------------
FIBER_NODE_URL=""
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ gem "redis" # , "~> 4.2.0"
# Background Jobs
gem "rufus-scheduler"
gem "sidekiq"
gem "sidekiq-unique-jobs"
# bulk insertion of data into database using ActiveRecord
gem "activerecord-import"

Expand Down
21 changes: 13 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ GEM
llhttp-ffi (0.4.0)
ffi-compiler (~> 1.0)
rake (~> 13.0)
loofah (2.22.0)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
Expand All @@ -284,7 +284,7 @@ GEM
marcel (1.0.4)
method_source (1.1.0)
mini_mime (1.1.5)
mini_portile2 (2.8.7)
mini_portile2 (2.8.8)
minitest (5.25.1)
minitest-reporters (1.6.1)
ansi
Expand All @@ -308,12 +308,12 @@ GEM
net-protocol
newrelic_rpm (8.12.0)
nio4r (2.7.3)
nokogiri (1.16.7)
nokogiri (1.16.8)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.16.7-arm64-darwin)
nokogiri (1.16.8-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
nokogiri (1.16.8-x86_64-linux)
racc (~> 1.4)
pagy (5.10.1)
activesupport
Expand Down Expand Up @@ -375,9 +375,9 @@ GEM
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
rails-html-sanitizer (1.6.1)
loofah (~> 2.21)
nokogiri (~> 1.14)
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
railties (7.0.8.5)
actionpack (= 7.0.8.5)
activesupport (= 7.0.8.5)
Expand Down Expand Up @@ -439,11 +439,15 @@ GEM
shoulda-context (2.0.0)
shoulda-matchers (5.2.0)
activesupport (>= 5.2.0)
sidekiq (7.1.6)
sidekiq (7.2.0)
concurrent-ruby (< 2)
connection_pool (>= 2.3.0)
rack (>= 2.2.4)
redis-client (>= 0.14.0)
sidekiq-unique-jobs (8.0.10)
concurrent-ruby (~> 1.0, >= 1.0.5)
sidekiq (>= 7.0.0, < 8.0.0)
thor (>= 1.0, < 3.0)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand Down Expand Up @@ -544,6 +548,7 @@ DEPENDENCIES
shoulda-context
shoulda-matchers
sidekiq
sidekiq-unique-jobs
simplecov
simplecov-cobertura
spring
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/api/v2/fiber/channels_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Api
module V2
module Fiber
class ChannelsController < BaseController
def show
@channel = FiberChannel.find_by(channel_id: params[:channel_id])
raise Api::V2::Exceptions::FiberChannelNotFoundError unless @channel
end
end
end
end
end
17 changes: 17 additions & 0 deletions app/controllers/api/v2/fiber/graph_channels_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Api
module V2
module Fiber
class GraphChannelsController < BaseController
def index
@page = params.fetch(:page, 1)
@page_size = params.fetch(:page_size, FiberPeer.default_per_page)
@channels = FiberGraphChannel.all
if params[:status] == "closed"
@channels = @channels.where.not(closed_transaction_id: nil)
end
@channels = @channels.page(@page).per(@page_size)
end
end
end
end
end
31 changes: 31 additions & 0 deletions app/controllers/api/v2/fiber/graph_nodes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Api
module V2
module Fiber
class GraphNodesController < BaseController
def index
@page = params.fetch(:page, 1)
@page_size = params.fetch(:page_size, FiberGraphNode.default_per_page)
@nodes =
if params[:q].present?
FiberGraphNode.where("alias = :q or peer_id = :q or node_id = :q", q: params[:q]).page(@page).per(@page_size)
else
FiberGraphNode.all.page(@page).per(@page_size)
end
end

def show
@node = FiberGraphNode.find_by(node_id: params[:node_id])
raise Api::V2::Exceptions::FiberGraphNodeNotFoundError unless @node

@graph_channels = FiberGraphChannel.where(node1: params[:node_id]).or(
FiberGraphChannel.where(node2: params[:node_id]),
)

if params[:status] == "closed"
@graph_channels = @graph_channels.where.not(closed_transaction_id: nil)
end
end
end
end
end
end
46 changes: 46 additions & 0 deletions app/controllers/api/v2/fiber/peers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Api
module V2
module Fiber
class PeersController < BaseController
before_action :test_connection, only: :create

def index
@page = params.fetch(:page, 1)
@page_size = params.fetch(:page_size, FiberPeer.default_per_page)
@peers = FiberPeer.all.page(@page).per(@page_size)
end

def show
@peer = FiberPeer.find_by(peer_id: params[:peer_id])
raise Api::V2::Exceptions::FiberPeerNotFoundError unless @peer
end

def create
fiber_peer = FiberPeer.find_or_initialize_by(peer_id: fiber_peer_params[:peer_id])
fiber_peer.name = fiber_peer_params[:name]
new_rpc = Array(fiber_peer_params[:rpc_listening_addr])
fiber_peer.rpc_listening_addr = (fiber_peer.rpc_listening_addr + new_rpc).uniq
fiber_peer.save!

FiberDetectWorker.perform_async(fiber_peer.peer_id)

head :no_content
rescue ActiveRecord::RecordInvalid => e
raise Api::V2::Exceptions::FiberPeerParamsInvalidError.new(e.message)
end

private

def fiber_peer_params
params.permit(:name, :peer_id, :rpc_listening_addr)
end

def test_connection
FiberCoordinator.instance.list_channels(fiber_peer_params[:rpc_listening_addr], { "peer_id": nil })
rescue StandardError => e
raise Api::V2::Exceptions::FiberPeerParamsInvalidError.new(e.message)
end
end
end
end
end
126 changes: 62 additions & 64 deletions app/controllers/api/v2/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,104 +3,102 @@
module Api
module V2
class ScriptsController < BaseController
before_action :set_page_and_page_size, except: :referring_capacities
before_action :find_script, except: :referring_capacities
before_action :set_page_and_page_size
before_action :set_contracts

def general_info
head :not_found and return if @script.blank? || @contract.blank?
head :not_found and return if @contracts.blank?

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
render json: { data: get_script_content }
end

def ckb_transactions
head :not_found and return if @contract.blank?

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
@ckb_transactions =
if @contract.ckb_transactions_count.zero?
CkbTransaction.none
else
@contract.ckb_transactions.order(block_number: :desc).page(@page).per(@page_size).fast_page
end
head :not_found and return if @contracts.blank?

contract_ids = @contracts.map { |contract| contract.id }
contract_cell_ids = CellDepsOutPoint.list_contract_cell_ids_by_contract(contract_ids)
base_query = CkbTransaction.joins(:cell_dependencies).
where(cell_dependencies: { contract_cell_id: contract_cell_ids }).
order("cell_dependencies.block_number DESC, cell_dependencies.tx_index DESC").
limit(10000)
@ckb_transactions = CkbTransaction.from("(#{base_query.to_sql}) AS ckb_transactions").
order("block_number DESC, tx_index DESC").
page(@page).
per(@page_size)
end

def deployed_cells
head :not_found and return if @contract.blank?
head :not_found and return if @contracts.blank?

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
@deployed_cells =
if @contract.deployed_cells_count.zero?
CellOutput.none
else
@contract.deployed_cell_outputs.live.page(@page).per(@page_size)
end

@deployed_cells = CellOutput.live.where(id: @contracts.map(&:deployed_cell_output_id)).page(@page).per(@page_size)
end

def referring_cells
head :not_found and return if @contract.blank?
head :not_found and return if @contracts.blank?

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds

if @contract.referring_cells_count.zero?
@referring_cells = CellOutput.none
else
scope = @contract.referring_cell_outputs.live.where.not(block_id: nil)
if params[:args].present?
type_script = TypeScript.find_by(args: params[:args])
scope = scope.where(cell_outputs: { type_script_id: type_script.id })
end
if params[:address_hash].present?
address = Addresses::Explore.run!(key: params[:address_hash])
scope = if address.is_a?(NullAddress)
CellOutput.none
else
scope.where(cell_outputs: { address_id: address.map(&:id) })
end
end

@referring_cells = sort_referring_cells(scope).page(@page).per(@page_size)
scope = Contract.referring_cells_query(@contracts).
order("block_timestamp DESC, cell_index DESC").
limit(10000)
if params[:args].present?
type_script = TypeScript.find_by(args: params[:args])
scope = scope.or(CellOutput.where(type_script_id: type_script.id))
end
if params[:address_hash].present?
address = Addresses::Explore.run!(key: params[:address_hash])
scope = scope.where(address_id: address.map(&:id))
end

@referring_cells =
CellOutput.from("(#{scope.to_sql}) AS cell_outputs").
order("block_timestamp DESC, cell_index DESC").
page(@page).
per(@page_size)
end

private

def get_script_content
deployed_cells = @contract.deployed_cell_outputs
if deployed_cells.present?
deployed_type_script = deployed_cells[0].type_script
if deployed_type_script && deployed_type_script.code_hash == Settings.type_id_code_hash
type_id = deployed_type_script.script_hash
sum_hash =
@contracts.inject({
capacity_of_deployed_cells: 0,
capacity_of_referring_cells: 0,
count_of_transactions: 0,
count_of_deployed_cells: 0,
count_of_referring_cells: 0,
}) do |sum, contract|
sum[:capacity_of_deployed_cells] += contract.total_deployed_cells_capacity
sum[:capacity_of_referring_cells] += contract.total_referring_cells_capacity
sum[:count_of_transactions] += contract.ckb_transactions_count
sum[:count_of_deployed_cells] += 1
sum[:count_of_referring_cells] += contract.referring_cells_count
sum
end
end

{
id: type_id,
code_hash: @script.code_hash,
hash_type: @script.hash_type,
script_type: @script.class.to_s,
capacity_of_deployed_cells: @contract.total_deployed_cells_capacity,
capacity_of_referring_cells: @contract.total_referring_cells_capacity,
count_of_transactions: @contract.ckb_transactions_count,
count_of_deployed_cells: @contract.deployed_cells_count,
count_of_referring_cells: @contract.referring_cells_count,
}
id: @contracts.first.type_hash,
code_hash: params[:code_hash],
hash_type: params[:hash_type],
script_type: @contracts.first.is_lock_script ? "LockScript" : "TypeScript",
}.merge(sum_hash)
end

def set_page_and_page_size
@page = params[:page] || 1
@page_size = params[:page_size] || 10
end

def find_script
@script = TypeScript.find_by(code_hash: params[:code_hash],
hash_type: params[:hash_type])
if @script.blank?
@script = LockScript.find_by(code_hash: params[:code_hash],
hash_type: params[:hash_type])
end
@contract = Contract.find_by(code_hash: params[:code_hash],
hash_type: params[:hash_type])
def set_contracts
@contracts =
case params[:hash_type]
when "data", "data1", "data2"
Contract.where(data_hash: params[:code_hash])
when "type"
Contract.where(type_hash: params[:code_hash])
end
end

def sort_referring_cells(records)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v2/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def transaction_fees
end

def contract_resource_distributed
contracts = Contract.filter_nil_hash_type
contracts = Contract.active
if params[:code_hashes].present?
hashes = params[:code_hashes].split(",")
contracts = contracts.where(code_hash: hashes)
contracts = contracts.where(type_hash: hashes)
end
if stale?(contracts)
expires_in 30.minutes, public: true
Expand Down
Loading

0 comments on commit 5513973

Please sign in to comment.