-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: synchronize peer and channel data in fiber network * feat: add fiber coordinator * chore: add foreign key fiber_peer_id * chore: rename sent_tlc_balance column * chore: refine fiber channel jbuilder attribute definitions * feat: fiber peers paginate * chore: change rpc_listening_addr type to array * feat: sync fiber graph nodes and channels * fix: set default page_size for fiber request * feat: sync graph node udt config info * chore: fiber udt cfg info add type_script * refactor: fiber graph detect worker * chore: add filter to fiber graph channels * feat: add filter query to fiber graph node * refactor: normalize query_key based on hex string detection * feat: fiber graph channel add closed_transaction_info
- Loading branch information
Showing
52 changed files
with
1,334 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class FiberChannel < ApplicationRecord | ||
belongs_to :fiber_peer | ||
|
||
def local_peer | ||
fiber_peer || FiberPeer.new | ||
end | ||
|
||
def remote_peer | ||
FiberPeer.find_by(peer_id:) || FiberPeer.new | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: fiber_channels | ||
# | ||
# id :bigint not null, primary key | ||
# peer_id :string | ||
# channel_id :string | ||
# state_name :string | ||
# state_flags :string default([]), is an Array | ||
# local_balance :decimal(64, 2) default(0.0) | ||
# offered_tlc_balance :decimal(64, 2) default(0.0) | ||
# remote_balance :decimal(64, 2) default(0.0) | ||
# received_tlc_balance :decimal(64, 2) default(0.0) | ||
# shutdown_at :datetime | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# fiber_peer_id :integer | ||
# | ||
# Indexes | ||
# | ||
# index_fiber_channels_on_fiber_peer_id (fiber_peer_id) | ||
# index_fiber_channels_on_peer_id_and_channel_id (peer_id,channel_id) UNIQUE | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
class FiberGraphChannel < ApplicationRecord | ||
MAX_PAGINATES_PER = 100 | ||
DEFAULT_PAGINATES_PER = 10 | ||
paginates_per DEFAULT_PAGINATES_PER | ||
max_paginates_per MAX_PAGINATES_PER | ||
|
||
belongs_to :udt, optional: true | ||
belongs_to :open_transaction, class_name: "CkbTransaction" | ||
belongs_to :closed_transaction, class_name: "CkbTransaction", optional: true | ||
|
||
validates :open_transaction_id, presence: true | ||
|
||
scope :open_channels, -> { where(closed_transaction_id: nil) } | ||
|
||
def open_transaction_info | ||
open_transaction.as_json(only: %i[tx_hash block_number block_timestamp]).merge( | ||
{ | ||
capacity: funding_cell.capacity, | ||
udt_amount: funding_cell.udt_amount, | ||
address: funding_cell.address_hash, | ||
}, | ||
) | ||
end | ||
|
||
def closed_transaction_info | ||
return Hash.new unless closed_transaction | ||
|
||
closed_transaction.as_json(only: %i[tx_hash block_number block_timestamp]).merge( | ||
close_accounts: closed_transaction.outputs.map do |cell| | ||
{ | ||
capacity: cell.capacity, | ||
udt_amount: cell.udt_amount, | ||
address: cell.address_hash, | ||
} | ||
end, | ||
) | ||
end | ||
|
||
def udt_info | ||
udt&.as_json(only: %i[full_name symbol decimal icon_file]) | ||
end | ||
|
||
def funding_cell | ||
open_transaction.outputs.includes(:lock_script).find_by( | ||
lock_scripts: { code_hash: Settings.fiber_funding_code_hash }, | ||
) | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: fiber_graph_channels | ||
# | ||
# id :bigint not null, primary key | ||
# channel_outpoint :string | ||
# funding_tx_block_number :bigint | ||
# funding_tx_index :integer | ||
# node1 :string | ||
# node2 :string | ||
# last_updated_timestamp :bigint | ||
# created_timestamp :bigint | ||
# node1_to_node2_fee_rate :decimal(30, ) default(0) | ||
# node2_to_node1_fee_rate :decimal(30, ) default(0) | ||
# capacity :decimal(64, 2) default(0.0) | ||
# chain_hash :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# udt_id :bigint | ||
# open_transaction_id :bigint | ||
# closed_transaction_id :bigint | ||
# | ||
# Indexes | ||
# | ||
# index_fiber_graph_channels_on_channel_outpoint (channel_outpoint) UNIQUE | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class FiberGraphNode < ApplicationRecord | ||
MAX_PAGINATES_PER = 100 | ||
DEFAULT_PAGINATES_PER = 10 | ||
paginates_per DEFAULT_PAGINATES_PER | ||
max_paginates_per MAX_PAGINATES_PER | ||
|
||
has_many :fiber_udt_cfg_infos, dependent: :delete_all | ||
|
||
def channel_links | ||
FiberGraphChannel.where(node1: node_id).or(FiberGraphChannel.where(node2: node_id)). | ||
where(closed_transaction_id: nil) | ||
end | ||
|
||
def udt_cfg_infos | ||
fiber_udt_cfg_infos.map(&:udt_info) | ||
end | ||
|
||
def total_capacity | ||
channel_links.sum(&:capacity) | ||
end | ||
|
||
def connected_node_ids | ||
node_ids = channel_links.pluck(:node1, :node2).flatten | ||
node_ids.uniq - [node_id] | ||
end | ||
|
||
def open_channels_count | ||
channel_links.count | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: fiber_graph_nodes | ||
# | ||
# id :bigint not null, primary key | ||
# alias :string | ||
# node_id :string | ||
# addresses :string default([]), is an Array | ||
# timestamp :bigint | ||
# chain_hash :string | ||
# auto_accept_min_ckb_funding_amount :decimal(30, ) | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# peer_id :string | ||
# | ||
# Indexes | ||
# | ||
# index_fiber_graph_nodes_on_node_id (node_id) UNIQUE | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class FiberPeer < ApplicationRecord | ||
MAX_PAGINATES_PER = 100 | ||
DEFAULT_PAGINATES_PER = 10 | ||
paginates_per DEFAULT_PAGINATES_PER | ||
max_paginates_per MAX_PAGINATES_PER | ||
|
||
has_many :fiber_channels, dependent: :destroy | ||
|
||
validates :peer_id, presence: true, uniqueness: true | ||
|
||
def total_local_balance | ||
fiber_channels.where(state_name: "CHANNEL_READY").sum(:local_balance) | ||
end | ||
|
||
def channels_count | ||
fiber_channels.where(state_name: "CHANNEL_READY").count | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: fiber_peers | ||
# | ||
# id :bigint not null, primary key | ||
# name :string | ||
# peer_id :string | ||
# rpc_listening_addr :string default([]), is an Array | ||
# first_channel_opened_at :datetime | ||
# last_channel_updated_at :datetime | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# node_id :string | ||
# chain_hash :string | ||
# | ||
# Indexes | ||
# | ||
# index_fiber_peers_on_peer_id (peer_id) UNIQUE | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class FiberTransaction < ApplicationRecord | ||
belongs_to :fiber_channel | ||
belongs_to :fiber_peer | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: fiber_transactions | ||
# | ||
# id :bigint not null, primary key | ||
# fiber_peer_id :integer | ||
# fiber_channel_id :integer | ||
# ckb_transaction_id :integer | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# |
Oops, something went wrong.