istox 0.1.44 → 0.1.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d284e11ec21a267732521aa16f60cefc5d7d1efa69cf3b96bed1cc7ced004014
4
- data.tar.gz: 236be580fa06fc556dab64853fd8e48dc1b05c988b75d00a535c2be3a077fd37
3
+ metadata.gz: 559c9df3e5df4437bfd5d374aedf3967cdaa8beb01157db1facf3d722bfb3f0e
4
+ data.tar.gz: ae3165eed01d37392f02eb2c4bd0b6c5d55dd4fe17253a39db04a0785519124d
5
5
  SHA512:
6
- metadata.gz: 6ad5ad56023548b15ee8172ec7c03982f60d15c74ecc71c616e04a7ed6d46e642306d1761c6f8be2ea099c7be82d3f9d4ffd3b8ad65f9cac4560ffd757177423
7
- data.tar.gz: 417cf3903c36b166abce744be293dff76ad32c18293fc508e7c0bf42003e5d3b8f2050706387912d1ff68bfa174a7e9e5e851abf0f5af7c142734992747dd3f5
6
+ metadata.gz: c7243f21712865a80d720479b611a124084cba968985dc755e2189110dfc3308d85a2a07a946cf30049bf2947b6862da8a618d903818e0e04d9f44c579602922
7
+ data.tar.gz: 5e0ca0625bd42bcb4bbafe5fe7a77a7520bbc179deceaf9afc840cbdc978686eaaa8d3237949c1c3458afe8223437094c8af07e6f3e69e5f3f12ee89b32e5677
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- istox (0.1.43.9)
4
+ istox (0.1.44)
5
5
  bunny (>= 2.12.0)
6
6
  graphlient
7
7
  gruf
@@ -18,50 +18,41 @@ module Istox
18
18
  transactions.each do |transaction|
19
19
  receipt = ::Istox::BlockchainReceipt.where(txhash: transaction.id).first
20
20
 
21
- find_resource(transaction) do |receipt, resource_name, resource_id, resource_action, sid|
22
- resource = begin
23
- class_eval("::#{resource_name}").find(resource_id)
24
- rescue => e
25
- puts e.inspect
26
- next
27
- end
28
- receipt_list = get_receipt_list(resource, resource_name)
29
-
30
- next if !allowed_statuses?(transaction)
31
- next if receipt_handled?(receipt_list)
32
-
33
- if confirmed?(transaction)
34
- next if resource.outcome == 'pending'
35
-
36
- if resource.outcome == 'confirmed'
37
- resource.handle_confirm(resource_action)
38
- publish_to_frontend(resource, true, receipt, sid, receipt_list) do
39
- update_receipts(receipt_list)
40
- end
41
- end
42
- end
21
+ next if !(%w[failed confirmed pending].include?(transaction.status))
22
+ receipt.update!(status: transaction.status)
23
+
24
+ resource = begin
25
+ class_eval("::#{receipt.resource_name}").find(receipt.resource_id)
26
+ rescue => e
27
+ puts e.inspect
28
+ next
29
+ end
30
+
31
+ next if resource_handled?(resource)
32
+
33
+ if transaction.status == 'confirmed'
34
+ next if resource.outcome == 'pending'
43
35
 
44
- if failed?(transaction)
45
- resource.handle_fail(resource_action)
46
- publish_to_frontend(resource, false, receipt, sid, receipt_list) { update_receipts(receipt_list) }
36
+ if resource.outcome == 'confirmed'
37
+ resource.handle_confirm(resource_action)
38
+
39
+ mark_resource_handled(resource)
40
+ publish_to_frontend(resource, true, receipt, sid, receipt_list)
47
41
  end
48
42
  end
49
- end
50
- end
51
43
 
52
- private
44
+ if transaction.status == 'failed'
45
+ resource.handle_fail(resource_action)
53
46
 
54
- def allowed_statuses?(transaction)
55
- return %w[failed confirmed].include?(transaction.status)
56
- end
47
+ mark_resource_handled(resource)
48
+ publish_to_frontend(resource, false, receipt, sid, receipt_list)
49
+ end
57
50
 
58
- def get_receipt_list(resource, resource_name)
59
- ::Istox::BlockchainReceipt.where(
60
- resource_name: resource_name,
61
- resource_id: resource.id
62
- )
51
+ end
63
52
  end
64
53
 
54
+ private
55
+
65
56
  def publish_to_frontend(model, success, receipt, sid, receipt_list)
66
57
  yield if block_given?
67
58
 
@@ -85,34 +76,29 @@ module Istox
85
76
  )
86
77
  end
87
78
 
88
- def failed?(transaction)
89
- transaction.status == 'failed' || transaction.status == 'reverted'
90
- end
79
+ def get_receipts_tx_hashes(receipt_list)
80
+ tx_hashes = receipt_list.map(&:txhash).compact
91
81
 
92
- def confirmed?(transaction)
93
- transaction.status == 'confirmed'
82
+ tx_hashes.join(', ')
94
83
  end
95
84
 
96
- def update_receipts(receipt_list)
85
+ def mark_resource_handled(resource)
86
+ receipt_list = get_receipt_list(resource)
97
87
  receipt_list.each do |receipt|
98
88
  receipt.update(resource_handled: true)
99
89
  end
100
90
  end
101
91
 
102
- def find_resource(transaction)
103
- receipt = ::Istox::BlockchainReceipt.find_by_txhash(transaction.id)
104
-
105
- yield(receipt, receipt.resource_name, receipt.resource_id, receipt.resource_action, receipt.sid)
106
- end
107
-
108
- def get_receipts_tx_hashes(receipt_list)
109
- tx_hashes = receipt_list.map(&:txhash).compact
110
-
111
- tx_hashes.join(', ')
92
+ def resource_handled?(resource)
93
+ receipt_list = get_receipt_list(resource)
94
+ receipt_list.map(&:resource_handled).none? { |item| item.nil? || item == false }
112
95
  end
113
96
 
114
- def receipt_handled?(receipt_list)
115
- receipt_list.map(&:resource_handled).none? { |item| item.nil? || item == false }
97
+ def get_receipt_list(resource)
98
+ ::Istox::BlockchainReceipt.where(
99
+ resource_name: resource.class.name,
100
+ resource_id: resource.id
101
+ )
116
102
  end
117
103
 
118
104
  end
@@ -18,6 +18,7 @@ module Istox
18
18
  t.datetime :deleted_at, index: true
19
19
  t.timestamps
20
20
  t.string :sid
21
+ t.string :status, :default => "pending"
21
22
  t.integer :lock_version
22
23
  end
23
24
  end
@@ -1,33 +1,6 @@
1
1
  module Istox
2
2
  module BlockchainReceiptQuery
3
3
  extend ActiveSupport::Concern
4
-
5
- class_methods do
6
- def with_receipts(arr = nil)
7
- if !arr
8
- arr = self.all
9
- end
10
-
11
- hashes = arr.map do |model|
12
- blockchain_receipts = model.get_blockchain_receipts
13
-
14
- return blockchain_receipts.map do |blockchain_receipt|
15
- blockchain_receipt.txhash
16
- end
17
- end.flatten
18
-
19
- data = self.block_transactions(hashes)
20
-
21
- arr.map do |model|
22
- model.set_transactions_store(data)
23
- model
24
- end
25
- end
26
-
27
- def block_transactions(txhashes)
28
- Istox::Interfaces::Chainhub::Transaction.get_block_transactions(txhashes)
29
- end
30
- end
31
4
 
32
5
  #overrideable, when blockchain transaction has confirmed
33
6
  def handle_confirm(resource_action)
@@ -46,63 +19,23 @@ module Istox
46
19
 
47
20
  # outcome of the blockchain transaction, can be "pending" / "confirmed" / "failed"
48
21
  def outcome
49
- @blockchain_receipts = get_blockchain_receipts
22
+ blockchain_receipts = get_blockchain_receipts
50
23
 
51
- return "confirmed" if @blockchain_receipts.empty?
52
-
53
- txhashes = @blockchain_receipts.map do |blockchain_receipt|
54
- blockchain_receipt.txhash
55
- end
56
-
57
- # return pending state if there is any unknown hashes
58
- return "pending" if txhashes.any?{ |e| e.blank? }
59
-
60
- # if transactions store already exists, we should retrieve the state from
61
- prepare_block_transactions(txhashes)
62
- @blocks = []
24
+ return "confirmed" if blockchain_receipts.empty?
25
+
26
+ # return failed state if there is any failed hashes
27
+ return "failed" if blockchain_receipts.any?{ |r| r.status == 'failed' }
63
28
 
64
- @blockchain_receipts.map do |blockchain_receipt|
65
- transaction = @transactions_store.find { |t| t.txhash == blockchain_receipt.txhash}
66
-
67
- if transaction
68
- @blocks.push(transaction)
69
- else
70
- @blocks.push(OpenStruct.new({ txhash: blockchain_receipt.txhash, status: 'pending' }))
71
- end
72
- end
73
-
74
-
29
+ # return pending state if there is any pending hashes
30
+ return "pending" if blockchain_receipts.any?{ |r| r.status == 'pending' }
75
31
 
76
- return "failed" if blocks_failed?
77
- return "pending" if blocks_pending?
78
- return "confirmed" if blocks_confirmed?
32
+ # return confirmed otherwise
33
+ return "confirmed"
79
34
  end
80
35
 
81
36
  def get_blockchain_receipts
82
37
  ::Istox::BlockchainReceipt.where(resource_id: self.id, resource_name: self.class.name).all
83
38
  end
84
-
85
- def set_transactions_store(store)
86
- @transactions_store = store
87
- end
88
-
89
- def prepare_block_transactions(txhashes)
90
- @transactions_store ||= Istox::Interfaces::Chainhub::Transaction.get_block_transactions(txhashes)
91
- end
92
-
93
- private
94
-
95
- def blocks_failed?
96
- @blocks.any? { |obj| obj.state == 'failed' }
97
- end
98
-
99
- def blocks_pending?
100
- @blocks.empty? || @blocks.any? { |obj| obj.state == 'pending' }
101
- end
102
-
103
- def blocks_confirmed?
104
- !@blocks.any? { |obj| obj.state == 'pending' } && !@blocks.any? { |obj| obj.state == 'failed' }
105
- end
106
-
39
+
107
40
  end
108
41
  end
data/lib/istox/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Istox
2
- VERSION = "0.1.44"
2
+ VERSION = "0.1.45"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: istox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.44
4
+ version: 0.1.45
5
5
  platform: ruby
6
6
  authors:
7
7
  - Siong Leng
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-06 00:00:00.000000000 Z
11
+ date: 2019-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny