Skip to content

Commit 08a97b3

Browse files
authored
Merge pull request #368 from shaojunda/shaojunda-release-dao-withdraw-fee
[ᚬmaster] release dao withdraw fee
2 parents 1cb2721 + c8a399c commit 08a97b3

16 files changed

+540
-9
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ BLOCK_SAFETY_INTERVAL="10"
1313
AUTHENTICSYNC_LOOP_INTERVAL="10"
1414
INAUTHENTICSYNC_LOOP_INTERVAL="1"
1515
CODE_HASH="0x54811ce986d5c3e57eaafab22cdd080e32209e39590e204a99b32935f835a13c"
16+
DAO_CODE_HASH="0x0450e0eb0ad787135c1243e5730f72fa3bc8837e232e928c7f4b1efdd6692582"
1617
INITIAL_BLOCK_REWARD="5_000_000_000_000"
1718
HASH_RATE_STATISTICAL_INTERVAL="500"
1819
PROPOSAL_WINDOW = "10"

CHANGELOG.md

Lines changed: 375 additions & 0 deletions
Large diffs are not rendered by default.

app/models/address.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class Address < ApplicationRecord
77
has_many :ckb_transactions, through: :account_books
88
validates :balance, :cell_consumed, :ckb_transactions_count, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
99

10-
attribute :lock_hash, :ckb_hash
1110
after_commit :flush_cache
1211

1312
def lock_script

app/models/cell_output.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class CellOutput < ApplicationRecord
22
enum status: { live: 0, dead: 1 }
3+
enum cell_type: { normal: 0, dao: 1 }
34

45
belongs_to :ckb_transaction
56
belongs_to :generated_by, class_name: "CkbTransaction"
@@ -41,6 +42,7 @@ def node_output
4142
# cell_index :integer
4243
# generated_by_id :decimal(30, )
4344
# consumed_by_id :decimal(30, )
45+
# cell_type :integer default("normal")
4446
#
4547
# Indexes
4648
#

app/models/ckb_sync/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module CkbSync
22
class Api
33
include Singleton
44

5-
METHOD_NAMES = %w(system_script_out_point dry_run_transaction set_system_script_cell system_script_cell system_script_cell_hash genesis_block get_block_by_number genesis_block_hash get_block_hash get_block get_tip_header get_tip_block_number get_cells_by_lock_hash get_transaction get_live_cell local_node_info get_current_epoch get_epoch_by_number get_peers tx_pool_info get_blockchain_info get_peers_state compute_transaction_hash get_cellbase_output_capacity_details).freeze
5+
METHOD_NAMES = %w(system_script_out_point dry_run_transaction set_system_script_cell system_script_cell system_script_cell_hash genesis_block get_block_by_number genesis_block_hash get_block_hash get_block get_tip_header get_tip_block_number get_cells_by_lock_hash get_transaction get_live_cell local_node_info get_current_epoch get_epoch_by_number get_peers tx_pool_info get_blockchain_info get_peers_state compute_transaction_hash get_cellbase_output_capacity_details calculate_dao_maximum_withdraw).freeze
66

77
def initialize
88
@api = CKB::API.new(host: ENV["CKB_NODE_URL"])

app/models/ckb_sync/node_data_processor.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ def build_cell_outputs(node_outputs, ckb_transaction, addresses)
234234
end
235235
end
236236

237+
def cell_type(type_script)
238+
return "normal" if type_script.blank?
239+
240+
type_script.code_hash == ENV["DAO_CODE_HASH"] ? "dao" : "normal"
241+
end
242+
237243
def build_cell_output(ckb_transaction, output, address, cell_index)
238244
ckb_transaction.cell_outputs.build(
239245
capacity: output.capacity,
@@ -242,7 +248,8 @@ def build_cell_output(ckb_transaction, output, address, cell_index)
242248
block: ckb_transaction.block,
243249
tx_hash: ckb_transaction.tx_hash,
244250
cell_index: cell_index,
245-
generated_by: ckb_transaction
251+
generated_by: ckb_transaction,
252+
cell_type: cell_type(output.type)
246253
)
247254
end
248255

app/models/ckb_transaction.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ def display_outputs
6565
# block_timestamp :decimal(30, )
6666
# transaction_fee :decimal(30, )
6767
# version :integer
68-
# witnesses :string is an Array
6968
# created_at :datetime not null
7069
# updated_at :datetime not null
7170
# is_cellbase :boolean default(FALSE)
71+
# witnesses :jsonb
7272
#
7373
# Indexes
7474
#

app/utils/ckb_utils.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ def self.get_epoch_info(epoch)
8383
end
8484

8585
def self.ckb_transaction_fee(ckb_transaction)
86-
ckb_transaction.inputs.sum(:capacity) - ckb_transaction.outputs.sum(:capacity)
86+
if ckb_transaction.inputs.dao.present?
87+
dao_withdraw_tx_fee(ckb_transaction)
88+
else
89+
normal_tx_fee(ckb_transaction)
90+
end
8791
end
8892

8993
def self.get_unspent_cells(address_hash)
@@ -143,4 +147,25 @@ def self.update_target_block_miner_address_pending_rewards(current_block)
143147
miner_address = target_block.miner_address
144148
Address.decrement_counter(:pending_reward_blocks_count, miner_address.id, touch: true) if miner_address.present?
145149
end
150+
151+
def self.normal_tx_fee(ckb_transaction)
152+
ckb_transaction.inputs.sum(:capacity) - ckb_transaction.outputs.sum(:capacity)
153+
end
154+
155+
def self.dao_withdraw_tx_fee(ckb_transaction)
156+
dao_cells = ckb_transaction.inputs.dao
157+
witnesses = ckb_transaction.witnesses
158+
deps = ckb_transaction.deps
159+
interests =
160+
dao_cells.reduce(0) do |memo, dao_cell|
161+
witness = witnesses[dao_cell.cell_index]
162+
dep = deps[witness["data"].last.hex]
163+
out_point = CKB::Types::OutPoint.new(cell: CKB::Types::CellOutPoint.new(tx_hash: dao_cell.tx_hash, index: dao_cell.cell_index))
164+
memo + CkbSync::Api.instance.calculate_dao_maximum_withdraw(out_point, dep["block_hash"]).to_i - dao_cell.capacity.to_i
165+
end
166+
167+
ckb_transaction.inputs.sum(:capacity) + interests - ckb_transaction.outputs.sum(:capacity)
168+
rescue CKB::RPCError
169+
0
170+
end
146171
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddCellTypeToCellOutputs < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :cell_outputs, :cell_type, :integer, default: 0
4+
end
5+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ChangeWitnessesToJsonbInCkbTransactions < ActiveRecord::Migration[5.2]
2+
def change
3+
remove_column :ckb_transactions, :witnesses, :jsonb
4+
add_column :ckb_transactions, :witnesses, :jsonb
5+
end
6+
end

0 commit comments

Comments
 (0)