Skip to content

Commit

Permalink
Define Glueby::AR::SystemInformation.utxo_provider_default_value and …
Browse files Browse the repository at this point in the history
…Glueby::AR::SystemInformation.utxo_provider_pool_size
  • Loading branch information
Yamaguchi authored and azuchi committed Jan 28, 2022
1 parent 0a8e62b commit e5cf5ef
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
16 changes: 14 additions & 2 deletions lib/glueby/active_record/system_information.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ module AR
class SystemInformation < ::ActiveRecord::Base

def self.synced_block_height
SystemInformation.find_by(info_key: "synced_block_number")
find_by(info_key: "synced_block_number")
end

# Return if wallet allows to use only finalized utxo.
# @return [Boolean] true if wallet allows to use only finalized utxo, otherwise false.
def self.use_only_finalized_utxo?
SystemInformation.find_by(info_key: "use_only_finalized_utxo")&.int_value != 0
find_by(info_key: "use_only_finalized_utxo")&.int_value != 0
end

# Return default value of the utxo provider
# @return [Integer] default value of utxo provider
def self.utxo_provider_default_value
find_by(info_key: "utxo_provider_default_value")&.int_value
end

# Return pool size of the utxo provider
# @return [Integer] pool size of utxo provider
def self.utxo_provider_pool_size
find_by(info_key: "utxo_provider_pool_size")&.int_value
end

def int_value
Expand Down
24 changes: 12 additions & 12 deletions lib/glueby/utxo_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ def get_utxo(script_pubkey, value = DEFAULT_VALUE)
end

def default_value
info_value = Glueby::AR::SystemInformation.find_by(info_key: 'utxo_provider_default_value')
@default_value = if info_value
info_value.int_value
else
(UtxoProvider.config && UtxoProvider.config[:default_value]) || DEFAULT_VALUE
end
@default_value ||=
(
Glueby::AR::SystemInformation.utxo_provider_default_value ||
(UtxoProvider.config && UtxoProvider.config[:default_value]) ||
DEFAULT_VALUE
)
end

def utxo_pool_size
info_value = Glueby::AR::SystemInformation.find_by(info_key: 'utxo_provider_pool_size')
@utxo_pool_size ||= if info_value
info_value.int_value
else
(UtxoProvider.config && UtxoProvider.config[:utxo_pool_size]) || DEFAULT_UTXO_POOL_SIZE
end
@utxo_pool_size ||=
(
Glueby::AR::SystemInformation.utxo_provider_pool_size ||
(UtxoProvider.config && UtxoProvider.config[:utxo_pool_size]) ||
DEFAULT_UTXO_POOL_SIZE
)
end

private
Expand Down
15 changes: 15 additions & 0 deletions spec/glueby/utxo_provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,16 @@
info_key: 'utxo_provider_default_value',
info_value: '3000'
)

end
it { expect(subject).to eq 3_000 }

it 'cache value' do
allow(Glueby::AR::SystemInformation).to receive(:utxo_provider_default_value).and_return(3_000)
expect(subject).to eq 3_000
expect(subject).to eq 3_000
expect(Glueby::AR::SystemInformation).to have_received(:utxo_provider_default_value).once
end
end

context 'from config setting' do
Expand All @@ -109,6 +117,13 @@
end

it { expect(subject).to eq 300 }

it 'cache value' do
allow(Glueby::AR::SystemInformation).to receive(:utxo_provider_pool_size).and_return(300)
expect(subject).to eq 300
expect(subject).to eq 300
expect(Glueby::AR::SystemInformation).to have_received(:utxo_provider_pool_size).once
end
end

context 'from config setting' do
Expand Down

0 comments on commit e5cf5ef

Please sign in to comment.