istox 0.1.77.2 → 0.1.79.pre.test1

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: 7d324292bffb18d421b724dcd855d131e4a332b14f78bac836dba1ac8486472e
4
- data.tar.gz: 55515508117180d4444cfcbbd57b24909dff4362f938c4de701874288baafe21
3
+ metadata.gz: 8b9fe0e553280bf1aeb096f7a61a8e5fae7f6fa220e0bb6eb64cce5e0be685e2
4
+ data.tar.gz: adcc41c73750b9b3b09919a9b8fd20e7671eccd0dda34e730bd5cf59b00bb4bb
5
5
  SHA512:
6
- metadata.gz: c98f2b9fba7c8a7408202de9cdc7ae415e22f23af6da681847802107148263d9a71939a1955296660fa13ec8f767efd767992328562ebefec45c754f3a725951
7
- data.tar.gz: 6437e9b8fe2265ad406e4f137c106984c26a61245bdeec2c3b18e04b972e3c4da825ee70c8213195cc0a53a39ca3384d9c22a662119e44c17e1a3212c927ff7b
6
+ metadata.gz: 07e4b1fb124bb17eb60c7c38e3e0c7faeec9afc1e9802da2fae721ad8cbf5fe8afea128c691b907f97799bdb8aa1805ad9c16e0d7321512972e9b7a505da1876
7
+ data.tar.gz: 79870c307e990da68a18f17633b49300b9348443baf5fed0ac5a32cc7c7a23c46eb0e0a85036bdcce4a6ae54a36c1d847de88b252b189944158cb6bbf3ea7f40
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- istox (0.1.75)
4
+ istox (0.1.79)
5
5
  binding_of_caller
6
6
  bunny (>= 2.12.0)
7
7
  graphlient
@@ -227,4 +227,4 @@ DEPENDENCIES
227
227
  sqlite3 (~> 1.3.6)
228
228
 
229
229
  BUNDLED WITH
230
- 1.16.6
230
+ 1.17.1
data/lib/istox.rb CHANGED
@@ -21,12 +21,11 @@ module Istox
21
21
  require "istox/helpers/graphql_client"
22
22
  require "istox/helpers/gruf_listener_hook"
23
23
  require "istox/helpers/message_service"
24
- require "istox/helpers/blockchain_receipt_service"
24
+ require "istox/helpers/blockchain_service"
25
25
  require "istox/helpers/f_math"
26
26
  require "istox/models/blockchain_receipt"
27
27
  require "istox/models/concerns/blockchain_receipt_query"
28
28
  require "istox/consumers/blockchain_status_handler"
29
- require "istox/consumers/blockchain_hash_handler"
30
29
  require "istox/migrations/create_blockchain_receipts"
31
30
  require "istox/logging/hash_logging"
32
31
  require "istox/logging/log_formatter"
@@ -1,105 +1,49 @@
1
1
  module Istox
2
2
  class BlockchainStatusHandler
3
3
  class << self
4
+ # format of transaction
5
+ # {
6
+ # uuid: "XXXXXX"
7
+ # status: "confirmed/failed"
8
+ # }
9
+
10
+ def txn_status_changed(transaction)
11
+ receipt = ::Istox::BlockchainReceipt.where(txid: transaction.uuid).first
12
+ if receipt.blank?
13
+ puts 'Transaction doesn\'t belong to this backend, skipping...'
14
+ return
15
+ end
4
16
 
5
- def txn_data_confirmed(transactions)
6
- found_txns = transactions.map do |transaction|
7
- receipt = ::Istox::BlockchainReceipt.where(txid: transaction.uuid).first
8
- if !receipt.blank?
9
- handling_receipt = ::Istox::BlockchainReceipt.where(txhash: transaction.id, txid: transaction.uuid).first
10
- raise 'Unable to find receipt' if handling_receipt.blank?
11
- transaction
12
- else
13
- puts 'Transaction doesnt belong here, skipping...'
14
- nil
15
- end
16
- end.compact
17
-
18
- found_txns.each do |transaction|
19
- receipt = ::Istox::BlockchainReceipt.where(txhash: transaction.id, txid: transaction.uuid).first
20
-
21
- receipt.update!(status: "confirmed")
22
-
23
- resource = begin
24
- class_eval("::#{receipt.resource_name}").find(receipt.resource_id)
25
- rescue => e
26
- puts "Class not found, skipping..."
27
- next
28
- end
29
-
30
- receipt_list = get_receipt_list(resource)
31
-
32
- next if resource_handled?(receipt_list)
17
+ return if !(%w[failed confirmed].include?(transaction.status))
18
+ receipt.update!(status: transaction.status)
19
+
20
+ resource = begin
21
+ class_eval("::#{receipt.resource_name}").find(receipt.resource_id)
22
+ rescue => e
23
+ puts "Class not found, skipping..."
24
+ return
25
+ end
33
26
 
34
- next if resource.outcome == 'pending'
27
+ return if resource_handled?(receipt)
35
28
 
36
- if resource.outcome == 'confirmed'
37
- resource.handle_confirm(receipt.resource_action)
29
+ if transaction.status == 'confirmed'
30
+ resource.handle_confirm(receipt.resource_action)
38
31
 
39
- mark_resource_handled(receipt_list)
40
- publish_to_frontend(resource, true, receipt, receipt.sid, receipt_list)
41
- end
42
-
32
+ mark_resource_handled(receipt)
33
+ publish_to_frontend(resource, true, receipt, receipt.sid)
43
34
  end
44
- end
45
-
46
- def txn_status_changed(transactions, payload_target)
47
- # pre check all transaction does exists
48
- found_txns = transactions.map do |transaction|
49
- receipt = ::Istox::BlockchainReceipt.where(txid: transaction.uuid).first
50
- if !receipt.blank?
51
- handling_receipt = ::Istox::BlockchainReceipt.where(txhash: transaction.id, txid: transaction.uuid).first
52
- raise 'Unable to find receipt' if handling_receipt.blank?
53
- transaction
54
- else
55
- puts 'Transaction doesnt belong here, skipping...'
56
- nil
57
- end
58
- end.compact
59
-
60
- found_txns.each do |transaction|
61
- receipt = ::Istox::BlockchainReceipt.where(txhash: transaction.id, txid: transaction.uuid).first
62
-
63
- next if !(%w[failed pending].include?(transaction.status))
64
- receipt.update!(status: transaction.status)
65
-
66
- resource = begin
67
- class_eval("::#{receipt.resource_name}").find(receipt.resource_id)
68
- rescue => e
69
- puts "Class not found, skipping..."
70
- next
71
- end
72
-
73
- receipt_list = get_receipt_list(resource)
74
35
 
75
- next if resource_handled?(receipt_list)
76
-
77
- # if transaction.status == 'confirmed'
78
- # next if resource.outcome == 'pending'
79
-
80
- # if resource.outcome == 'confirmed'
81
- # resource.handle_confirm(receipt.resource_action)
82
-
83
- # mark_resource_handled(receipt_list)
84
- # publish_to_frontend(resource, true, receipt, receipt.sid, receipt_list)
85
- # end
86
- # end
87
-
88
- if transaction.status == 'failed'
89
- resource.handle_fail(receipt.resource_action)
90
-
91
- mark_resource_handled(receipt_list)
92
- publish_to_frontend(resource, false, receipt, receipt.sid, receipt_list)
93
- end
36
+ if transaction.status == 'failed'
37
+ resource.handle_fail(receipt.resource_action)
94
38
 
39
+ mark_resource_handled(receipt)
40
+ publish_to_frontend(resource, false, receipt, receipt.sid)
95
41
  end
96
42
  end
97
43
 
98
44
  private
99
45
 
100
- def publish_to_frontend(model, success, receipt, sid, receipt_list)
101
- yield if block_given?
102
-
46
+ def publish_to_frontend(model, success, receipt, sid)
103
47
  ::Istox::Publisher.publish(
104
48
  'amq.topic',
105
49
  model.class.name.gsub('::', '').underscore.to_s,
@@ -114,33 +58,23 @@ module Istox
114
58
  sid,
115
59
  data: {
116
60
  success: success,
117
- purpose: receipt.activity,
118
- txids: get_receipts_tx_hashes(receipt_list)
61
+ purpose: receipt.activity
119
62
  }
120
63
  )
64
+ rescue => e
65
+ Rails.logger.error "Unable to publish to frontend for receipt #{receipt.inspect}, but will silently ignore the error"
66
+ Rails.logger.error e
121
67
  end
122
68
 
123
- def get_receipts_tx_hashes(receipt_list)
124
- tx_hashes = receipt_list.map(&:txhash).compact
125
-
126
- tx_hashes.join(', ')
127
- end
128
-
129
- def mark_resource_handled(receipt_list)
130
- receipt_list.each do |receipt|
131
- receipt.update(resource_handled: true)
132
- end
133
- end
134
-
135
- def resource_handled?(receipt_list)
136
- receipt_list.map(&:resource_handled).none? { |item| item.nil? || item == false }
69
+ def mark_resource_handled(receipt)
70
+ receipt.update(resource_handled: true)
71
+ rescue => e
72
+ Rails.logger.error "Unable to update resource_handled for receipt #{receipt.inspect}, but will silently ignore the error"
73
+ Rails.logger.error e
137
74
  end
138
75
 
139
- def get_receipt_list(resource)
140
- ::Istox::BlockchainReceipt.where(
141
- resource_name: resource.class.name,
142
- resource_id: resource.id
143
- )
76
+ def resource_handled?(receipt)
77
+ return receipt.resource_handled == true
144
78
  end
145
79
 
146
80
  end
@@ -0,0 +1,66 @@
1
+ module Istox
2
+ class BlockchainService
3
+
4
+ def self.init(blockchain_receipt_klass)
5
+ @@blockchain_receipt_klass = blockchain_receipt_klass
6
+ end
7
+
8
+ def self.get_blockchain_receipt_klass
9
+ @@blockchain_receipt_klass
10
+ end
11
+
12
+ def self.request(sid:, action:, before:, execute:)
13
+ self.start(sid: sid, action: action, before: before, execute: execute, is_request: true)
14
+ end
15
+
16
+ def self.run(sid:, action:, before:, execute:)
17
+ self.start(sid: sid, action: action, before: before, execute: execute)
18
+ end
19
+
20
+ private
21
+ # before is the proc that will be executed before, must return the main model blockchain receipt will bind to
22
+ def self.start(sid:, action:, before:, execute:, is_request: false)
23
+ # create blockchain receipt first
24
+ klass = self.blockchain_receipt_class
25
+ uuid = :: SecureRandom.uuid
26
+ @receipt = klass.create!({
27
+ txid: uuid,
28
+ sid: sid,
29
+ is_request: is_request,
30
+ resource_action: action
31
+ })
32
+
33
+ ::ActiveRecord::Base.transaction do
34
+ # execute before proc, and get the model that blockchain receipt should bind to
35
+ model = before.call(uuid)
36
+
37
+ puts model
38
+
39
+ # delete the previous existing blockchain receipts
40
+ klass.where(resource_name: model.class.name, resource_id: model.id).destroy_all
41
+
42
+ # then update receipt
43
+ @receipt.update!(resource_id: model.id, resource_name: model.class.name)
44
+
45
+ # execute the actual call to chainhub
46
+ result = execute.call(uuid)
47
+ end
48
+ rescue => e
49
+ @receipt.destroy if @receipt
50
+ raise e
51
+ end
52
+
53
+ def self.blockchain_receipt_class
54
+ raise "Have you forgetten to init blockchain receipt service?" if self.get_blockchain_receipt_klass == nil
55
+
56
+ blockchain_receipt_klass = self.get_blockchain_receipt_klass
57
+ klass = class_eval("::#{blockchain_receipt_klass.name}")
58
+ unless klass <= ::Istox::BlockchainReceipt
59
+ raise RuntimeError, "#{blockchain_receipt_klass.name} does not inherit istox blockchain receipt"
60
+ end
61
+
62
+ return klass
63
+ end
64
+
65
+ end
66
+ end
@@ -14,7 +14,7 @@ module Istox
14
14
  hash[:app] = progname
15
15
  end
16
16
 
17
- return JSON.pretty_generate(hash) + "\n"
17
+ return hash.to_json + "\n"
18
18
 
19
19
  # return "timestamp='#{time}' level=#{severity} progname='#{progname}' #{processed_message(msg)}\n" if progname.present?
20
20
  # "timestamp='#{time}' level=#{severity} #{processed_message(msg)}\n"
@@ -23,7 +23,7 @@ module Istox
23
23
  private
24
24
 
25
25
  def processed_message(msg)
26
- return msg.map { |k, v| "#{k}='#{v&.inspect.to_s.strip}'" }.join(' ') if msg.is_a?(Hash)
26
+ return msg.map { |k, v| "#{k}='#{v&.strip}'" }.join(' ') if msg.is_a?(Hash)
27
27
 
28
28
  msg
29
29
  end
@@ -19,27 +19,16 @@ module Istox
19
19
 
20
20
  # outcome of the blockchain transaction, can be "pending" / "confirmed" / "failed"
21
21
  def outcome
22
- blockchain_receipts = get_blockchain_receipts
22
+ blockchain_receipt = ::Istox::BlockchainReceipt.where(resource_id: self.id, resource_name: self.class.name).last
23
23
 
24
- return "confirmed" if blockchain_receipts.empty?
24
+ return "confirmed" if blockchain_receipt.empty?
25
25
 
26
- # return failed state if there is any failed hashes
27
- return "failed" if blockchain_receipts.any?{ |r| r.status == 'failed' }
28
-
29
- # return pending state if there is any pending hashes
30
- return "pending" if blockchain_receipts.any?{ |r| r.status == 'pending' }
31
-
32
- # return confirmed otherwise
33
- return "confirmed"
34
- end
35
-
36
- def get_blockchain_receipts
37
- ::Istox::BlockchainReceipt.where(resource_id: self.id, resource_name: self.class.name).all
26
+ return blockchain_receipt.status
38
27
  end
39
28
 
40
- def last_receipt
29
+ def get_blockchain_receipt
41
30
  ::Istox::BlockchainReceipt.where(resource_id: self.id, resource_name: self.class.name).last
42
31
  end
43
-
32
+
44
33
  end
45
34
  end
data/lib/istox/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Istox
2
- VERSION = "0.1.77.2"
2
+ VERSION = "0.1.79-test1"
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.77.2
4
+ version: 0.1.79.pre.test1
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-08-28 00:00:00.000000000 Z
11
+ date: 2019-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -287,9 +287,8 @@ files:
287
287
  - bin/setup
288
288
  - istox.gemspec
289
289
  - lib/istox.rb
290
- - lib/istox/consumers/blockchain_hash_handler.rb
291
290
  - lib/istox/consumers/blockchain_status_handler.rb
292
- - lib/istox/helpers/blockchain_receipt_service.rb
291
+ - lib/istox/helpers/blockchain_service.rb
293
292
  - lib/istox/helpers/bunny_boot.rb
294
293
  - lib/istox/helpers/f_math.rb
295
294
  - lib/istox/helpers/graphql_client.rb
@@ -320,12 +319,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
320
319
  version: '0'
321
320
  required_rubygems_version: !ruby/object:Gem::Requirement
322
321
  requirements:
323
- - - ">="
322
+ - - ">"
324
323
  - !ruby/object:Gem::Version
325
- version: '0'
324
+ version: 1.3.1
326
325
  requirements: []
327
326
  rubyforge_project:
328
- rubygems_version: 2.7.8
327
+ rubygems_version: 2.7.7
329
328
  signing_key:
330
329
  specification_version: 4
331
330
  summary: istox backend shared gem
@@ -1,19 +0,0 @@
1
- module Istox
2
- class BlockchainHashHandler
3
- class << self
4
- def hash_generated(data, payload_target)
5
- receipt = ::Istox::BlockchainReceipt.where(txid: data.uuid).first
6
- if receipt.nil?
7
- puts 'Transaction doesnt belong here, skipping...'
8
- return
9
- end
10
-
11
- # sid cannot be nil
12
- update_receipt = ::Istox::BlockchainReceipt.where(txid: data.uuid, txhash: nil).where.not(sid: nil).first
13
- raise "Unable to find receipt to update" if update_receipt.blank?
14
-
15
- update_receipt.update!(txhash: data.txnHash)
16
- end
17
- end
18
- end
19
- end
@@ -1,71 +0,0 @@
1
- module Istox
2
- class BlockchainReceiptService
3
-
4
- def self.init(blockchain_receipt_klass)
5
- @@blockchain_receipt_klass = blockchain_receipt_klass
6
- end
7
-
8
- def self.get_blockchain_receipt_klass
9
- @@blockchain_receipt_klass
10
- end
11
-
12
- def self.generate_uuid
13
- klass = ::Istox::BlockchainReceiptService.blockchain_receipt_class
14
- uuid = SecureRandom.uuid
15
-
16
- klass.create!({
17
- txid: uuid
18
- })
19
-
20
- uuid
21
- end
22
-
23
- def create!(tx_message, model, sid, action, is_request: false)
24
- klass = ::Istox::BlockchainReceiptService.blockchain_receipt_class
25
-
26
- # delete the previous existing blockchain receipts
27
- klass.where(resource_name: model.class.name,
28
- resource_id: model.id).destroy_all
29
-
30
- (0...tx_message.txnCount).each do |i|
31
- if i == 0
32
- blockchain_receipt = klass.find_by_txid(tx_message.uuid)
33
-
34
- raise "Unable to find blockchain receipt with uuid #{tx_message.uuid}, have you forgetten to generate uuid?" if blockchain_receipt.blank?
35
-
36
- blockchain_receipt.update!({
37
- sid: sid,
38
- txid: tx_message.uuid,
39
- resource_name: model.class.name,
40
- resource_id: model.id,
41
- resource_action: action,
42
- is_request: is_request
43
- })
44
- else
45
- blockchain_receipt = klass.new(
46
- sid: sid,
47
- txid: tx_message.uuid,
48
- resource_name: model.class.name,
49
- resource_id: model.id,
50
- resource_action: action,
51
- is_request: is_request
52
- )
53
- blockchain_receipt.save!
54
- end
55
- end
56
- end
57
-
58
- def self.blockchain_receipt_class
59
- raise "Have you forgetten to init blockchain receipt service?" if ::Istox::BlockchainReceiptService.get_blockchain_receipt_klass == nil
60
-
61
- blockchain_receipt_klass = ::Istox::BlockchainReceiptService.get_blockchain_receipt_klass
62
- klass = class_eval("::#{blockchain_receipt_klass.name}")
63
- unless klass <= ::Istox::BlockchainReceipt
64
- raise RuntimeError, "#{blockchain_receipt_klass.name} does not inherit istox blockchain receipt"
65
- end
66
-
67
- return klass
68
- end
69
-
70
- end
71
- end