Skip to content

Commit

Permalink
Fix for review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Dec 1, 2023
1 parent 213e661 commit 555608c
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 57 deletions.
4 changes: 2 additions & 2 deletions lib/glueby/contract/tx_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def create_multi_transfer_tx(color_id:, sender:, receivers:, fee_estimator: FeeE
tx = Tapyrus::Tx.new

amount = receivers.reduce(0) { |sum, r| sum + r[:amount].to_i }
utxos = sender.internal_wallet.list_unspent(color_id, only_finalized)
utxos = sender.internal_wallet.list_unspent(only_finalized, color_id: color_id)
sum_token, outputs = collect_colored_outputs(utxos, amount)
fill_input(tx, outputs)

Expand Down Expand Up @@ -247,7 +247,7 @@ def create_multi_transfer_tx(color_id:, sender:, receivers:, fee_estimator: FeeE
def create_burn_tx(color_id:, sender:, amount: 0, fee_estimator: FeeEstimator::Fixed.new, only_finalized: true)
tx = Tapyrus::Tx.new

utxos = sender.internal_wallet.list_unspent(color_id, only_finalized)
utxos = sender.internal_wallet.list_unspent(only_finalized, color_id: color_id)
sum_token, outputs = collect_colored_outputs(utxos, amount)
fill_input(tx, outputs)

Expand Down
2 changes: 1 addition & 1 deletion lib/glueby/fee_provider/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def collect_outputs

def current_utxo_pool_size
wallet
.list_unspent(nil, false)
.list_unspent(false)
.count { |o| !o[:color_id] && o[:amount] == fee_provider.fixed_fee }
end

Expand Down
23 changes: 16 additions & 7 deletions lib/glueby/internal/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,29 @@ def balance(only_finalized = true)
wallet_adapter.balance(id, only_finalized)
end

def list_unspent_with_count(color_id = nil, only_finalized = true, label = nil, page = 1, per = 25)
wallet_adapter.list_unspent_with_count(id, color_id, only_finalized, label, page, per)
end

# @param only_finalized [Boolean] The flag to get a UTXO with status only finalized
# @param label [String] This label is used to filtered the UTXOs with labeled if a key or Utxo is labeled.
# - If label is nil or :unlabeled, only unlabeled UTXOs will be returned.
# - If label=:all, all UTXOs will be returned.
# @param color_id [Tapyrus::Color::ColorIdentifier] The color identifier associated with UTXO.
# It will return only UTXOs with specified color_id. If color_id is nil, it will return all UTXOs.
# If Tapyrus::Color::ColorIdentifier.default is specified, it will return uncolored UTXOs(i.e. TPC)
# @param page [Integer] The page parameter is responsible for specifying the current page being viewed within the paginated results. default is 1.
# @param per [Integer] The per parameter is used to determine the number of items to display per page. default is 25.
def list_unspent_with_count(only_finalized = true, label = nil, color_id: nil, page: 1, per: 25)
wallet_adapter.list_unspent_with_count(id, only_finalized, label, color_id: color_id, page: page, per: per)
end

# @param only_finalized [Boolean] The flag to get a UTXO with status only finalized
# @param label [String] This label is used to filtered the UTXOs with labeled if a key or Utxo is labeled.
# - If label is nil or :unlabeled, only unlabeled UTXOs will be returned.
# - If label=:all, all UTXOs will be returned.
def list_unspent(color_id = nil, only_finalized = true, label = :unlabeled)
# @param color_id [Tapyrus::Color::ColorIdentifier] The color identifier associated with UTXO.
# It will return only UTXOs with specified color_id. If color_id is nil, it will return all UTXOs.
# If Tapyrus::Color::ColorIdentifier.default is specified, it will return uncolored UTXOs(i.e. TPC)
def list_unspent(only_finalized = true, label = :unlabeled, color_id: nil )
label = :unlabeled unless label
wallet_adapter.list_unspent(id, color_id, only_finalized, label)
wallet_adapter.list_unspent(id, only_finalized, label, color_id: color_id)
end

def lock_unspent(utxo)
Expand Down Expand Up @@ -302,7 +311,7 @@ def collect_utxos(
collect_all = amount.nil?

raise Glueby::ArgumentError, 'amount must be positive' unless collect_all || amount.positive?
utxos = list_unspent(color_id, only_finalized, label)
utxos = list_unspent(only_finalized, label, color_id: color_id)
utxos = utxos.shuffle if shuffle

r = utxos.inject([0, []]) do |(sum, outputs), output|
Expand Down
14 changes: 7 additions & 7 deletions lib/glueby/internal/wallet/abstract_wallet_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,30 @@ def balance(wallet_id, only_finalized = true)
# Returns all tokens with specified color_id
#
# @param [String] wallet_id - The wallet id that is offered by `create_wallet()` method.
# @param [Boolean] only_finalized - includes only finalized UTXO value if it
# is true. Default is true.
# @param [Tapyrus::Color::ColorIdentifier] color_id The color identifier associated with UTXO.
# It will return only UTXOs with specified color_id. If color_id is nil, it will return all UTXOs.
# If Tapyrus::Color::ColorIdentifier.default is specified, it will return uncolored UTXOs(i.e. TPC)
# @param [Boolean] only_finalized - includes only finalized UTXO value if it
# is true. Default is true.
# @param [Integer] page - The page parameter is responsible for specifying the current page being viewed within the paginated results. default is 1.
# @param [Integer] per - The per parameter is used to determine the number of items to display per page. default is 25.
# @return [Array<Utxo>] The array of the utxos with specified color_id
def list_unspent_with_count(wallet_id, color_id = nil, only_finalized = true, label = nil, page = 1, per = 25)
def list_unspent_with_count(wallet_id, only_finalized = true, label = nil, color_id: nil, page: 1, per: 25)
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

# Returns the UTXOs that the wallet has.
# If label is specified, return UTXOs filtered with label
#
# @param [String] wallet_id - The wallet id that is offered by `create_wallet()` method.
# @param [Tapyrus::Color::ColorIdentifier] color_id - The color identifier.
# It will return only UTXOs with specified color_id. If color_id is nil, it will return all UTXOs.
# If Tapyrus::Color::ColorIdentifier.default is specified, it will return uncolored UTXOs(i.e. TPC)
# @param [Boolean] only_finalized - The UTXOs includes only finalized UTXO value if it
# is true. Default is true.
# @param [String] label - Label for filtering UTXOs
# - If label is nil or :unlabeled, only unlabeled UTXOs will be returned.
# - If label=:all, it will return all utxos
# @param [Tapyrus::Color::ColorIdentifier] color_id - The color identifier.
# It will return only UTXOs with specified color_id. If color_id is nil, it will return all UTXOs.
# If Tapyrus::Color::ColorIdentifier.default is specified, it will return uncolored UTXOs(i.e. TPC)
# @return [Array of UTXO]
#
# ## The UTXO structure
Expand All @@ -102,7 +102,7 @@ def list_unspent_with_count(wallet_id, color_id = nil, only_finalized = true, la
# - finalized: [Boolean] Whether the UTXO is finalized
# - color_id: [String] Color id of the UTXO. If it is TPC UTXO, color_id is nil.
# - script_pubkey: [String] Script pubkey of the UTXO
def list_unspent(wallet_id, color_id = nil, only_finalized = true, label = nil)
def list_unspent(wallet_id, only_finalized = true, label = nil, color_id: nil)
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

Expand Down
4 changes: 2 additions & 2 deletions lib/glueby/internal/wallet/active_record_wallet_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def balance(wallet_id, only_finalized = true)
utxos.sum(&:value)
end

def list_unspent_with_count(wallet_id, color_id = nil, only_finalized = true, label = nil, page = 1, per = 25)
def list_unspent_with_count(wallet_id, only_finalized = true, label = nil, color_id: nil, page: 1, per: 25)
utxos = list_unspent_internal(wallet_id, color_id, only_finalized, label)
utxos = utxos.page(page).per(per) if per > 0
{
Expand All @@ -99,7 +99,7 @@ def list_unspent_with_count(wallet_id, color_id = nil, only_finalized = true, la
}
end

def list_unspent(wallet_id, color_id = nil, only_finalized = true, label = nil)
def list_unspent(wallet_id, only_finalized = true, label = nil, color_id: nil)
utxos = list_unspent_internal(wallet_id, color_id, only_finalized, label)
utxos_to_h(utxos)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/glueby/internal/wallet/tapyrus_core_wallet_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def balance(wallet_id, only_finalized = true)

# If label=nil, it will return unlabeled utxos to protect labeled utxos for specific purpose
# If label=:all, it will return all utxos
def list_unspent(wallet_id, color_id = nil, only_finalized = true, label = nil)
def list_unspent(wallet_id, only_finalized = true, label = nil, color_id: nil)
perform_as(wallet_id) do |client|
min_conf = only_finalized ? 1 : 0
res = client.listunspent(min_conf)
Expand Down
2 changes: 1 addition & 1 deletion lib/glueby/utxo_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def tpc_amount

def current_utxo_pool_size
wallet
.list_unspent(nil, false)
.list_unspent(false)
.count { |o| !o[:color_id] && o[:amount] == default_value }
end

Expand Down
4 changes: 2 additions & 2 deletions lib/glueby/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def id

# @return [HashMap] hash of balances which key is color_id or empty string, and value is amount
def balances(only_finalized = true)
utxos = @internal_wallet.list_unspent(nil, only_finalized)
utxos = @internal_wallet.list_unspent(only_finalized)
utxos.inject({}) do |balances, output|
key = output[:color_id] || ''
balances[key] ||= 0
Expand All @@ -47,7 +47,7 @@ def balances(only_finalized = true)
end

def token_utxos(color_id = nil, only_finalized = true, page = 1, per = 25)
@internal_wallet.list_unspent_with_count(color_id, only_finalized, nil, page, per)
@internal_wallet.list_unspent_with_count(only_finalized, nil, color_id: color_id, page: page, per: per)
end

private
Expand Down
16 changes: 8 additions & 8 deletions spec/functional/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
end.to raise_error(Glueby::Contract::Errors::InsufficientFunds)

# should not consume 'labeled' utxos
expect(sender.internal_wallet.list_unspent(nil, false, 'labeled').size).to eq(1)
expect(sender.internal_wallet.list_unspent(nil, false, 'labeled')[0][:amount]).to eq(5_000_000_000)
expect(sender.internal_wallet.list_unspent(false, 'labeled').size).to eq(1)
expect(sender.internal_wallet.list_unspent(false, 'labeled')[0][:amount]).to eq(5_000_000_000)

# create a usable utxo for payment
process_block(to_address: sender.internal_wallet.receive_address)
Expand All @@ -48,8 +48,8 @@
# receiver got the sent amount
expect(receiver.balances(false)['']).to eq(10_000)
# should not consume 'labeled' utxos
expect(sender.internal_wallet.list_unspent(nil, false, 'labeled').size).to eq(1)
expect(sender.internal_wallet.list_unspent(nil, false, 'labeled')[0][:amount]).to eq(5_000_000_000)
expect(sender.internal_wallet.list_unspent(false, 'labeled').size).to eq(1)
expect(sender.internal_wallet.list_unspent(false, 'labeled')[0][:amount]).to eq(5_000_000_000)
end
end

Expand All @@ -74,8 +74,8 @@
end.to raise_error(Glueby::Contract::Errors::InsufficientFunds)

# should not consume 'labeled' utxos
expect(sender.internal_wallet.list_unspent(nil, false, 'labeled').size).to eq(1)
expect(sender.internal_wallet.list_unspent(nil, false, 'labeled')[0][:amount]).to eq(5_000_000_000 + fixed_fee)
expect(sender.internal_wallet.list_unspent(false, 'labeled').size).to eq(1)
expect(sender.internal_wallet.list_unspent(false, 'labeled')[0][:amount]).to eq(5_000_000_000 + fixed_fee)

# create a usable utxo for payment
process_block(to_address: sender.internal_wallet.receive_address)
Expand All @@ -92,8 +92,8 @@
# receiver got the sent amount
expect(receiver.balances(false)['']).to eq(10_000)
# should not consume 'labeled' utxos
expect(sender.internal_wallet.list_unspent(nil, false, 'labeled').size).to eq(1)
expect(sender.internal_wallet.list_unspent(nil, false, 'labeled')[0][:amount]).to eq(5_000_000_000 + fixed_fee)
expect(sender.internal_wallet.list_unspent(false, 'labeled').size).to eq(1)
expect(sender.internal_wallet.list_unspent(false, 'labeled')[0][:amount]).to eq(5_000_000_000 + fixed_fee)

sighashtype = tx.inputs[-1].script_sig.chunks.first.pushed_data[-1].unpack1('C')
expect(sighashtype).to eq(Tapyrus::SIGHASH_TYPE[:all])
Expand Down
4 changes: 2 additions & 2 deletions spec/functional/token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@

expect(sender.balances(false)['']).to be_nil
expect(sender.balances(false)[token2.color_id.to_hex]).to eq(10_000)
expect(sender.internal_wallet.list_unspent(nil, false).select {|i| i[:color_id] == token2.color_id.to_hex}.size).to eq(100)
expect(sender.internal_wallet.list_unspent(false).select {|i| i[:color_id] == token2.color_id.to_hex}.size).to eq(100)

# Specify metadata option
token3, _txs = Glueby::Contract::Token.issue!(
Expand Down Expand Up @@ -457,7 +457,7 @@

expect(sender.balances(false)[token.color_id.to_hex]).to be_nil
expect(receiver.balances(false)[token.color_id.to_hex]).to eq(10_000)
expect(receiver.internal_wallet.list_unspent(nil, false).select {|i| i[:color_id] == token.color_id.to_hex}.size).to eq(100)
expect(receiver.internal_wallet.list_unspent(false).select {|i| i[:color_id] == token.color_id.to_hex}.size).to eq(100)
end

it 'doesn\'t raise insufficient error in too much split number' do
Expand Down
2 changes: 1 addition & 1 deletion spec/glueby/contract/timestamp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@

it 'the wallet never store the UTXO of the created trackable timestamp' do
subject
result = wallet.internal_wallet.list_unspent(nil, false, :all).find { |i| i[:txid] == contract.txid && i[:vout] == 0 }
result = wallet.internal_wallet.list_unspent(false, :all).find { |i| i[:txid] == contract.txid && i[:vout] == 0 }
expect(result).to be_nil
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/glueby/contract/token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@

before do
allow(internal_wallet).to receive(:list_unspent).and_return([])
allow(internal_wallet).to receive(:list_unspent).with(Tapyrus::Color::ColorIdentifier.default, true, nil).and_return(unspents.select{ |i| !i[:color_id] && i[:finalized] })
allow(internal_wallet).to receive(:list_unspent).with(Tapyrus::Color::ColorIdentifier.default, false, nil).and_return(unspents.select{ |i| !i[:color_id] })
allow(internal_wallet).to receive(:list_unspent).with(true, nil, color_id: Tapyrus::Color::ColorIdentifier.default).and_return(unspents.select{ |i| !i[:color_id] && i[:finalized] })
allow(internal_wallet).to receive(:list_unspent).with(false, nil, color_id: Tapyrus::Color::ColorIdentifier.default).and_return(unspents.select{ |i| !i[:color_id] })
[
"c3eb2b846463430b7be9962843a97ee522e3dc0994a0f5e2fc0aa82e20e67fe893",
"c2dbbebb191128de429084246fa3215f7ccc36d6abde62984eb5a42b1f2253a016",
"c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3",
].each do |color_id_hex|
color_id = Tapyrus::Color::ColorIdentifier.parse_from_payload(color_id_hex.htb)
allow(internal_wallet).to receive(:list_unspent).with(color_id, true, nil).and_return(unspents.select{ |i| i[:color_id] == color_id_hex && i[:finalized] })
allow(internal_wallet).to receive(:list_unspent).with(color_id, false, nil).and_return(unspents.select{ |i| i[:color_id] == color_id_hex })
allow(internal_wallet).to receive(:list_unspent).with(true, nil, color_id: color_id).and_return(unspents.select{ |i| i[:color_id] == color_id_hex && i[:finalized] })
allow(internal_wallet).to receive(:list_unspent).with(false, nil, color_id: color_id).and_return(unspents.select{ |i| i[:color_id] == color_id_hex })
end
allow(Glueby::Internal::RPC).to receive(:client).and_return(rpc)
allow(rpc).to receive(:sendrawtransaction).and_return('')
Expand Down
8 changes: 5 additions & 3 deletions spec/glueby/contract/tx_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,16 @@ class TxBuilderMock
let(:amount) { 100_001 }

before do
allow(internal_wallet).to receive(:list_unspent).with(color_id, true)
allow(internal_wallet).to receive(:list_unspent).with(true, color_id: color_id)
.and_return(unspents.select { |utxo| utxo[:color_id] == 'c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3' })
end
it { expect(subject.inputs.size).to eq 3 }
it { expect(subject.inputs[0].out_point.txid).to eq '100c4dc65ea4af8abb9e345b3d4cdcc548bb5e1cdb1cb3042c840e147da72fa2' }
it { expect(subject.inputs[0].out_point.index).to eq 0 }
it { expect(subject.inputs[1].out_point.txid).to eq 'a3f20bc94c8d77c35ba1770116d2b34375475a4194d15f76442636e9f77d50d9' }
it { expect(subject.inputs[1].out_point.index).to eq 2 }
it { expect(subject.inputs[2].out_point.txid).to eq '5c3d79041ff4974282b8ab72517d2ef15d8b6273cb80a01077145afb3d5e7cc5' }
it { expect(subject.inputs[2].out_point.index).to eq 0 }
it { expect(subject.outputs.size).to eq 3 }
it { expect(subject.outputs[0].value).to eq 100_001 }
it { expect(subject.outputs[0].colored?).to be_truthy }
Expand Down Expand Up @@ -426,7 +428,7 @@ class TxBuilderMock
end

before do
allow(internal_wallet).to receive(:list_unspent).with(color_id, true)
allow(internal_wallet).to receive(:list_unspent).with(true, color_id: color_id)
.and_return(unspents.select { |utxo| utxo[:color_id] == 'c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3' })
end

Expand Down Expand Up @@ -488,7 +490,7 @@ class TxBuilderMock


before do
allow(internal_wallet).to receive(:list_unspent).with(color_id, true)
allow(internal_wallet).to receive(:list_unspent).with(true, color_id: color_id)
.and_return(unspents.select { |utxo| utxo[:color_id] == 'c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3' })
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
it { expect(subject[1][:label]).to eq nil }
end

subject { adapter.list_unspent(wallet.wallet_id, color_id, only_finalized, label) }
subject { adapter.list_unspent(wallet.wallet_id, only_finalized, label, color_id: color_id) }

let(:other_wallet) { Glueby::Internal::Wallet::AR::Wallet.create(wallet_id: adapter.create_wallet) }
let(:only_finalized) { true }
Expand Down Expand Up @@ -525,7 +525,7 @@
end

describe '#list_unspent_with_count' do
subject { adapter.list_unspent_with_count(wallet_id, color_id, only_finalized, label, page, per) }
subject { adapter.list_unspent_with_count(wallet_id, only_finalized, label, color_id: color_id, page: page, per: per) }

let(:wallet_id) { wallet.wallet_id }
let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c185856a84c483fb108b1cdf79ff53aa7d54d1a137a5178684bd89ca31f906b2bd'.htb) }
Expand Down
Loading

0 comments on commit 555608c

Please sign in to comment.