glueby 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9a8e99610f76e6d765a52a645ead25eb8cf71343ba259ed25e5ec2fbdb0d5e5
4
- data.tar.gz: 0ee19c244501e854cbe59f6957e5e0c8a25bca403a9fa58d0367ad95de3f0a38
3
+ metadata.gz: 9490bd9983bf524a8b937c6b495ecb4260b65d8f84f590bffd4d06bcf8db3c60
4
+ data.tar.gz: 187ab2c69864a94245dbb94c3c79abb1cfce1317138dcc9a9f6aea10b91864d6
5
5
  SHA512:
6
- metadata.gz: 47dfd21bdd71bf99379f448816a58b3cef5fd093352d0f7a53806a85db5b2509765fa4f2f4813621a36fe0edc98c7491aca162f445ab9bb0708e3b3ca68756cc
7
- data.tar.gz: a8cd80a311e74759b6bf1351974888e787ddeb084a435dd6a13aadc4c1fb2a65d0be5d993ee0303e6e08d67bfecd7e09cd0763ecb92d0a19146838544479ee85
6
+ metadata.gz: 71285a57ead31c478d4880ae64b1ab9e29091a22027f489fc92f30005d8cbf1eecf5a5e79f0bc81bbb8b99723399e4b6c1f856c8c2e9eb21ecadef64e591c593
7
+ data.tar.gz: 7609a05ffd89aa3eb2dad0c75b9201a356bdcc83ff7f76522a8888b16f80071edb4d809093fb7e6e17322653aed701c1561d25c760073b75c21a00723996f632
@@ -13,6 +13,7 @@ class CreateTimestamp < ActiveRecord::Migration<%= migration_version %>
13
13
  t.string :payment_base
14
14
  t.bigint :prev_id
15
15
  t.boolean :latest, null: false, default: true
16
+ t.boolean :hex, null: false, default: false
16
17
  end
17
18
 
18
19
  add_index :glueby_timestamps, [:prev_id], unique: true
@@ -4,5 +4,22 @@ require 'active_record'
4
4
  module Glueby
5
5
  module AR
6
6
  autoload :SystemInformation, 'glueby/active_record/system_information'
7
+
8
+ def transaction(isolation: nil)
9
+ options = { joinable: false, requires_new: true }
10
+
11
+ adapter_type = ActiveRecord::Base.connection.adapter_name.downcase
12
+
13
+ # SQLite3 does not support transaction isolation level READ COMMITTED.
14
+ if isolation == :read_committed && adapter_type == 'mysql2'
15
+ options[:isolation] = :read_committed
16
+ end
17
+
18
+ ActiveRecord::Base.transaction(**options) do
19
+ yield
20
+ end
21
+ end
22
+
23
+ module_function :transaction
7
24
  end
8
25
  end
@@ -14,11 +14,13 @@ module Glueby
14
14
  validate :validate_prev
15
15
 
16
16
  class << self
17
- def digest_content(content, digest)
17
+ def digest_content(content, digest, hex = false)
18
18
  case digest&.downcase
19
19
  when :sha256
20
+ content = hex ? content.htb : content
20
21
  Tapyrus.sha256(content).bth
21
22
  when :double_sha256
23
+ content = hex ? content.htb : content
22
24
  Tapyrus.double_sha256(content).bth
23
25
  when :none
24
26
  content
@@ -33,17 +35,20 @@ module Glueby
33
35
  # - content
34
36
  # - prefix(optional)
35
37
  # - timestamp_type(optional)
38
+ # - hex(optional) [Boolean] If true, the strings set in prefix and content are treated as hex strings.
36
39
  # @raise [Glueby::ArgumentError] If the timestamp_type is not in :simple or :trackable
37
40
  def initialize(attributes = nil)
38
41
  # Set content_hash from :content attribute
39
- content_hash = Timestamp.digest_content(attributes[:content], attributes[:digest] || :sha256)
42
+ hex = attributes[:hex] || false
43
+ content_hash = Timestamp.digest_content(attributes[:content], attributes[:digest] || :sha256, hex)
40
44
  super(
41
45
  wallet_id: attributes[:wallet_id],
42
46
  content_hash: content_hash,
43
- prefix: attributes[:prefix] ? attributes[:prefix] : '',
47
+ prefix: attributes[:prefix] || '',
44
48
  status: :init,
45
49
  timestamp_type: attributes[:timestamp_type] || :simple,
46
- prev_id: attributes[:prev_id]
50
+ prev_id: attributes[:prev_id],
51
+ hex: hex
47
52
  )
48
53
  rescue ::ArgumentError => e
49
54
  raise Glueby::ArgumentError, e.message
@@ -142,6 +147,8 @@ module Glueby
142
147
  )
143
148
  end
144
149
 
150
+ prefix = hex ? self.prefix&.htb : self.prefix
151
+ content_hash = hex ? self.content_hash&.htb : self.content_hash
145
152
  tx = builder.set_data(prefix, content_hash)
146
153
  .set_inputs(utxo_provider)
147
154
  .build
@@ -29,6 +29,7 @@ module Glueby
29
29
  # - :simple
30
30
  # - :trackable
31
31
  # @param [Integer] prev_timestamp_id The id column of glueby_timestamps that will be updated by the timestamp that will be created
32
+ # @param [Boolean] hex If true, prefix and content are treated as hex strings
32
33
  # @raise [Glueby::Contract::Errors::UnsupportedDigestType] if digest is unsupported
33
34
  # @raise [Glueby::Contract::Errors::InvalidTimestampType] if timestamp_type is unsupported
34
35
  def initialize(
@@ -39,7 +40,8 @@ module Glueby
39
40
  digest: :sha256,
40
41
  utxo_provider: nil,
41
42
  timestamp_type: :simple,
42
- prev_timestamp_id: nil
43
+ prev_timestamp_id: nil,
44
+ hex: false
43
45
  )
44
46
  @wallet = wallet
45
47
  @content = content
@@ -51,6 +53,7 @@ module Glueby
51
53
  raise Glueby::Contract::Errors::InvalidTimestampType, "#{timestamp_type} is invalid type, supported types are :simple, and :trackable." unless [:simple, :trackable].include?(timestamp_type)
52
54
  @timestamp_type = timestamp_type
53
55
  @prev_timestamp_id = prev_timestamp_id
56
+ @hex = hex
54
57
  end
55
58
 
56
59
  # broadcast to Tapyrus Core
@@ -66,7 +69,8 @@ module Glueby
66
69
  content: @content,
67
70
  timestamp_type: @timestamp_type,
68
71
  digest: @digest,
69
- prev_id: @prev_timestamp_id
72
+ prev_id: @prev_timestamp_id,
73
+ hex: @hex
70
74
  )
71
75
  @ar.save_with_broadcast!(fee_estimator: @fee_estimator, utxo_provider: @utxo_provider)
72
76
  @ar.txid
@@ -79,7 +79,6 @@ module Glueby
79
79
  else
80
80
  raise Glueby::Contract::Errors::UnsupportedTokenType
81
81
  end
82
-
83
82
  [new(color_id: color_id), txs]
84
83
  end
85
84
 
@@ -135,7 +134,7 @@ module Glueby
135
134
  script_pubkey = funding_tx.outputs.first.script_pubkey
136
135
  color_id = Tapyrus::Color::ColorIdentifier.reissuable(script_pubkey)
137
136
 
138
- ActiveRecord::Base.transaction(joinable: false, requires_new: true) do
137
+ Glueby::AR.transaction(isolation: :read_committed) do
139
138
  # Store the script_pubkey for reissue the token.
140
139
  Glueby::Contract::AR::ReissuableToken.create!(
141
140
  color_id: color_id.to_hex,
@@ -226,7 +225,7 @@ module Glueby
226
225
  .build
227
226
  end
228
227
 
229
- ActiveRecord::Base.transaction(joinable: false, requires_new: true) do
228
+ Glueby::AR.transaction(isolation: :read_committed) do
230
229
  if metadata
231
230
  p2c_utxo = txb.p2c_utxos.first
232
231
  Glueby::Contract::AR::TokenMetadata.create!(
@@ -354,7 +353,10 @@ module Glueby
354
353
  txb.pay(r[:address], r[:amount].to_i, color_id)
355
354
  end
356
355
 
357
- tx = sender.internal_wallet.broadcast(txb.build)
356
+ tx = nil
357
+ Glueby::AR.transaction(isolation: :read_committed) do
358
+ tx = sender.internal_wallet.broadcast(txb.build)
359
+ end
358
360
  [color_id, tx]
359
361
  end
360
362
 
@@ -373,7 +375,7 @@ module Glueby
373
375
  raise Glueby::Contract::Errors::InsufficientTokens unless balance
374
376
  raise Glueby::Contract::Errors::InsufficientTokens if balance < amount
375
377
 
376
- tx = Internal::ContractBuilder
378
+ builder = Internal::ContractBuilder
377
379
  .new(
378
380
  sender_wallet: sender.internal_wallet,
379
381
  fee_estimator: fee_estimator,
@@ -382,9 +384,11 @@ module Glueby
382
384
  )
383
385
  .burn(amount, color_id)
384
386
  .change_address(sender.internal_wallet.receive_address, color_id)
385
- .build
386
387
 
387
- sender.internal_wallet.broadcast(tx)
388
+ Glueby::AR.transaction(isolation: :read_committed) do
389
+ tx = builder.build
390
+ sender.internal_wallet.broadcast(tx)
391
+ end
388
392
  end
389
393
 
390
394
  # Return balance of token in the specified wallet.
@@ -130,26 +130,26 @@ module Glueby
130
130
  )
131
131
  tx, index = nil
132
132
 
133
- if Glueby.configuration.use_utxo_provider? || utxo_provider
134
- utxo_provider ||= UtxoProvider.new
135
- script_pubkey = Tapyrus::Script.parse_from_addr(address)
136
- tx, index = utxo_provider.get_utxo(script_pubkey, amount)
137
- else
138
- fee_estimator ||= @fee_estimator
139
- txb = Tapyrus::TxBuilder.new
140
- fee = fee_estimator.fee(Contract::FeeEstimator.dummy_tx(txb.build))
141
- _sum, utxos = sender_wallet
142
- .collect_uncolored_outputs(fee + amount, nil, only_finalized)
143
- utxos.each { |utxo| txb.add_utxo(to_tapyrusrb_utxo_hash(utxo)) }
144
- tx = txb.pay(address, amount)
145
- .change_address(sender_wallet.change_address)
146
- .fee(fee)
147
- .build
148
- sender_wallet.sign_tx(tx)
149
- index = 0
150
- end
133
+ Glueby::AR.transaction(isolation: :read_committed) do
134
+ if Glueby.configuration.use_utxo_provider? || utxo_provider
135
+ utxo_provider ||= UtxoProvider.new
136
+ script_pubkey = Tapyrus::Script.parse_from_addr(address)
137
+ tx, index = utxo_provider.get_utxo(script_pubkey, amount)
138
+ else
139
+ fee_estimator ||= @fee_estimator
140
+ txb = Tapyrus::TxBuilder.new
141
+ fee = fee_estimator.fee(Contract::FeeEstimator.dummy_tx(txb.build))
142
+ _sum, utxos = sender_wallet
143
+ .collect_uncolored_outputs(fee + amount, nil, only_finalized)
144
+ utxos.each { |utxo| txb.add_utxo(to_tapyrusrb_utxo_hash(utxo)) }
145
+ tx = txb.pay(address, amount)
146
+ .change_address(sender_wallet.change_address)
147
+ .fee(fee)
148
+ .build
149
+ sender_wallet.sign_tx(tx)
150
+ index = 0
151
+ end
151
152
 
152
- ActiveRecord::Base.transaction(joinable: false, requires_new: true) do
153
153
  # Here needs to use the return tx from Internal::Wallet#broadcast because the txid
154
154
  # is changed if you enable FeeProvider.
155
155
  tx = sender_wallet.broadcast(tx)
@@ -120,7 +120,7 @@ module Glueby
120
120
  end
121
121
 
122
122
  def broadcast(wallet_id, tx, &block)
123
- ::ActiveRecord::Base.transaction do
123
+ ::ActiveRecord::Base.transaction(joinable: false, requires_new: true) do
124
124
  AR::Utxo.destroy_for_inputs(tx)
125
125
  AR::Utxo.create_or_update_for_outputs(tx, status: :broadcasted)
126
126
  block.call(tx) if block
@@ -1,3 +1,3 @@
1
1
  module Glueby
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glueby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-27 00:00:00.000000000 Z
11
+ date: 2023-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tapyrus