istox 0.1.79.pre.test4 → 0.1.79
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 +4 -4
- data/Gemfile.lock +8 -6
- data/lib/istox/consumers/blockchain_hash_handler.rb +19 -0
- data/lib/istox/consumers/blockchain_status_handler.rb +107 -42
- data/lib/istox/helpers/blockchain_receipt_service.rb +71 -0
- data/lib/istox/helpers/graphql_client.rb +29 -29
- data/lib/istox/helpers/grpc_client.rb +54 -51
- data/lib/istox/models/concerns/blockchain_receipt_query.rb +24 -10
- data/lib/istox/version.rb +1 -1
- data/lib/istox.rb +22 -23
- metadata +8 -10
- data/.rubocop.yml +0 -105
- data/.solargraph.yml +0 -14
- data/lib/istox/helpers/blockchain_service.rb +0 -65
- data/lib/istox/helpers/common_helper.rb +0 -69
- data/lib/istox/helpers/my_open_struct.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58d997502994f1db5538c06204840b3342b4f5698fb1230889949df0da38d6a8
|
4
|
+
data.tar.gz: 7948ebc1e984766a30b9b10c9b23ec1c616b89c96d03921d6a703bcef77178a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 188fcf120fec87d968a93a141787ed7191df228d6625e839bb3ffedd4333b7fc2bbede0245f54eaa27520c1283e5bafc0190b818db45425c5082b309f6d99741
|
7
|
+
data.tar.gz: e4e46c2bc88fc9670d92ff0fd62be71eaab96aaf0d1fafe01b51462df6fab42c41426ecc8ca412d4e0dd5ab116e47c4ba82d24fcf1eb1675303f4d785557ad54
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
istox (0.1.
|
4
|
+
istox (0.1.77)
|
5
5
|
binding_of_caller
|
6
6
|
bunny (>= 2.12.0)
|
7
7
|
graphlient
|
@@ -68,6 +68,7 @@ GEM
|
|
68
68
|
uniform_notifier (~> 1.11.0)
|
69
69
|
bunny (2.14.2)
|
70
70
|
amq-protocol (~> 2.3, >= 2.3.0)
|
71
|
+
byebug (8.2.5)
|
71
72
|
concurrent-ruby (1.1.4)
|
72
73
|
crass (1.0.4)
|
73
74
|
database_cleaner (1.6.2)
|
@@ -88,21 +89,21 @@ GEM
|
|
88
89
|
ffi (1.11.1)
|
89
90
|
globalid (0.4.2)
|
90
91
|
activesupport (>= 4.2.0)
|
91
|
-
google-protobuf (3.9.1
|
92
|
+
google-protobuf (3.9.1)
|
92
93
|
googleapis-common-protos-types (1.0.4)
|
93
94
|
google-protobuf (~> 3.0)
|
94
95
|
graphlient (0.3.6)
|
95
96
|
faraday
|
96
97
|
faraday_middleware
|
97
98
|
graphql-client
|
98
|
-
graphql (1.9.
|
99
|
+
graphql (1.9.9)
|
99
100
|
graphql-client (0.15.0)
|
100
101
|
activesupport (>= 3.0)
|
101
102
|
graphql (~> 1.8)
|
102
|
-
grpc (1.
|
103
|
+
grpc (1.22.0)
|
103
104
|
google-protobuf (~> 3.8)
|
104
105
|
googleapis-common-protos-types (~> 1.0)
|
105
|
-
grpc-tools (1.
|
106
|
+
grpc-tools (1.22.0)
|
106
107
|
gruf (2.7.0)
|
107
108
|
activesupport (> 4)
|
108
109
|
concurrent-ruby (> 1)
|
@@ -216,6 +217,7 @@ PLATFORMS
|
|
216
217
|
DEPENDENCIES
|
217
218
|
bullet (~> 5.7.5)
|
218
219
|
bundler (~> 1.16)
|
220
|
+
byebug
|
219
221
|
database_cleaner (~> 1.6.0)
|
220
222
|
factory_bot_rails (~> 4.8.2)
|
221
223
|
faker (~> 1.7.3)
|
@@ -227,4 +229,4 @@ DEPENDENCIES
|
|
227
229
|
sqlite3 (~> 1.3.6)
|
228
230
|
|
229
231
|
BUNDLED WITH
|
230
|
-
1.17.
|
232
|
+
1.17.1
|
@@ -0,0 +1,19 @@
|
|
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,49 +1,105 @@
|
|
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
|
16
4
|
|
17
|
-
|
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)
|
18
33
|
|
19
|
-
|
34
|
+
next if resource.outcome == 'pending'
|
35
|
+
|
36
|
+
if resource.outcome == 'confirmed'
|
37
|
+
resource.handle_confirm(receipt.resource_action)
|
38
|
+
|
39
|
+
mark_resource_handled(receipt_list)
|
40
|
+
publish_to_frontend(resource, true, receipt, receipt.sid, receipt_list)
|
41
|
+
end
|
20
42
|
|
21
|
-
resource = begin
|
22
|
-
class_eval("::#{receipt.resource_name}", __FILE__, __LINE__).find(receipt.resource_id)
|
23
|
-
rescue StandardError
|
24
|
-
puts 'Class not found, skipping...'
|
25
|
-
return
|
26
43
|
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
|
27
72
|
|
28
|
-
|
73
|
+
receipt_list = get_receipt_list(resource)
|
29
74
|
|
30
|
-
|
31
|
-
resource.handle_confirm(receipt.resource_action)
|
75
|
+
next if resource_handled?(receipt_list)
|
32
76
|
|
33
|
-
|
34
|
-
|
77
|
+
# if transaction.status == 'confirmed'
|
78
|
+
# next if resource.outcome == 'pending'
|
35
79
|
|
36
|
-
|
37
|
-
resource.
|
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
|
38
94
|
|
39
|
-
mark_resource_handled(receipt)
|
40
|
-
publish_to_frontend(resource, false, receipt, receipt.sid)
|
41
95
|
end
|
42
96
|
end
|
43
97
|
|
44
98
|
private
|
45
99
|
|
46
|
-
def publish_to_frontend(model, success, receipt, sid)
|
100
|
+
def publish_to_frontend(model, success, receipt, sid, receipt_list)
|
101
|
+
yield if block_given?
|
102
|
+
|
47
103
|
::Istox::Publisher.publish(
|
48
104
|
'amq.topic',
|
49
105
|
model.class.name.gsub('::', '').underscore.to_s,
|
@@ -58,26 +114,35 @@ module Istox
|
|
58
114
|
sid,
|
59
115
|
data: {
|
60
116
|
success: success,
|
61
|
-
purpose: receipt.activity
|
117
|
+
purpose: receipt.activity,
|
118
|
+
txids: get_receipts_tx_hashes(receipt_list)
|
62
119
|
}
|
63
120
|
)
|
64
|
-
rescue StandardError => e
|
65
|
-
Rails.logger.error "Unable to publish to frontend for receipt #{receipt.inspect},
|
66
|
-
but will silently ignore the error"
|
67
|
-
Rails.logger.error e
|
68
121
|
end
|
69
122
|
|
70
|
-
def
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
but will silently ignore the error"
|
75
|
-
Rails.logger.error e
|
123
|
+
def get_receipts_tx_hashes(receipt_list)
|
124
|
+
tx_hashes = receipt_list.map(&:txhash).compact
|
125
|
+
|
126
|
+
tx_hashes.join(', ')
|
76
127
|
end
|
77
128
|
|
78
|
-
def
|
79
|
-
|
129
|
+
def mark_resource_handled(receipt_list)
|
130
|
+
receipt_list.each do |receipt|
|
131
|
+
receipt.update(resource_handled: true)
|
132
|
+
end
|
80
133
|
end
|
134
|
+
|
135
|
+
def resource_handled?(receipt_list)
|
136
|
+
receipt_list.map(&:resource_handled).none? { |item| item.nil? || item == false }
|
137
|
+
end
|
138
|
+
|
139
|
+
def get_receipt_list(resource)
|
140
|
+
::Istox::BlockchainReceipt.where(
|
141
|
+
resource_name: resource.class.name,
|
142
|
+
resource_id: resource.id
|
143
|
+
)
|
144
|
+
end
|
145
|
+
|
81
146
|
end
|
82
147
|
end
|
83
148
|
end
|
@@ -0,0 +1,71 @@
|
|
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
|
@@ -1,35 +1,35 @@
|
|
1
|
-
require
|
1
|
+
require "graphlient"
|
2
2
|
|
3
3
|
module Istox
|
4
4
|
class GraphqlClient
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
5
|
+
class << self
|
6
|
+
def add_host(host_type, url)
|
7
|
+
unless defined?(@@hosts)
|
8
|
+
@@hosts = {}
|
9
|
+
end
|
10
|
+
init_host(host_type, url)
|
11
|
+
end
|
12
|
+
|
13
|
+
def query(host_type, query, variables = {})
|
14
|
+
client = @@hosts[host_type]
|
15
|
+
|
16
|
+
raise "Please make sure you have initialised graphql cient for host #{host_type}" if !client
|
17
|
+
|
18
|
+
Rails.logger.info "Querying Graphql host: #{host_type}, query: #{query}, variables: #{variables.inspect}"
|
19
|
+
result = client.query(query, variables)
|
20
|
+
Rails.logger.info "Graphql result: #{result.inspect}"
|
21
|
+
|
22
|
+
return result
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def init_host(host_type, url)
|
28
|
+
client = Graphlient::Client.new(url, http: Graphlient::Adapters::HTTP::HTTPAdapter)
|
29
|
+
|
30
|
+
@@hosts[host_type] = client
|
31
|
+
end
|
30
32
|
|
31
|
-
@@hosts[host_type] = client
|
32
33
|
end
|
33
|
-
end
|
34
34
|
end
|
35
|
-
end
|
35
|
+
end
|
@@ -1,63 +1,66 @@
|
|
1
1
|
module Istox
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class GrpcClient
|
3
|
+
class << self
|
4
|
+
def add_host(host_type, url)
|
5
|
+
unless defined?(@@hosts)
|
6
|
+
@@hosts = {}
|
7
|
+
end
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
+
@@hosts[host_type] = url
|
10
|
+
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
def call(host_type, service, method, **keyword_args)
|
13
|
+
execute(host_type, service, method, **keyword_args)
|
14
|
+
# rescue => e
|
15
|
+
# reinitiate service and try again, second chance
|
16
|
+
# reinitiate_service(host_type, service)
|
17
|
+
# execute(host_type, service, method, **keyword_args)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
17
21
|
|
18
|
-
|
22
|
+
def execute(host_type, service, method, **keyword_args)
|
23
|
+
Rails.logger.info "Calling method in grpc method: #{method.inspect}, params: #{keyword_args.inspect}"
|
24
|
+
t1 = Time.now
|
25
|
+
|
26
|
+
result = get_host(host_type, service).call(method, keyword_args)
|
27
|
+
Rails.logger.info "Time taken for grpc execution: #{Time.now - t1} seconds"
|
28
|
+
Rails.logger.info "Result: #{result.message.inspect}"
|
29
|
+
result
|
30
|
+
end
|
19
31
|
|
20
|
-
|
21
|
-
|
22
|
-
|
32
|
+
def get_host(host_type, service)
|
33
|
+
unless defined?(@@services)
|
34
|
+
@@services = {}
|
35
|
+
end
|
23
36
|
|
24
|
-
|
25
|
-
Rails.logger.info "Time taken for grpc execution: #{Time.now - t1} seconds"
|
37
|
+
grpc_client = @@services[get_key(host_type, service)]
|
26
38
|
|
27
|
-
|
28
|
-
|
39
|
+
if grpc_client
|
40
|
+
return grpc_client
|
41
|
+
else
|
42
|
+
reinitiate_service(host_type, service)
|
43
|
+
return @@services[get_key(host_type, service)]
|
44
|
+
end
|
45
|
+
end
|
29
46
|
|
30
|
-
|
31
|
-
|
47
|
+
def reinitiate_service(host_type, service)
|
48
|
+
unless defined?(@@services)
|
49
|
+
@@services = {}
|
50
|
+
end
|
32
51
|
|
33
|
-
|
34
|
-
|
52
|
+
host_url = @@hosts[host_type]
|
53
|
+
raise StandardError, 'Unable to find host, have you forgotten to add host to grpc client?' if !host_url
|
54
|
+
|
55
|
+
Rails.logger.info 'Reinitiating to grpc host at ' + host_url
|
56
|
+
t1 = Time.now
|
57
|
+
@@services[get_key(host_type, service)] = ::Gruf::Client.new(service: service, options: { hostname: host_url })
|
58
|
+
Rails.logger.info "Time taken for reinitiating grpc host: #{Time.now - t1} seconds"
|
59
|
+
end
|
35
60
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
return grpc_client
|
40
|
-
else
|
41
|
-
reinitiate_service(host_type, service)
|
42
|
-
return @@services[get_key(host_type, service)]
|
61
|
+
def get_key(host_type, service)
|
62
|
+
host_type.to_s + service.to_s
|
63
|
+
end
|
43
64
|
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def reinitiate_service(host_type, service)
|
47
|
-
@@services = {} unless defined?(@@services)
|
48
|
-
|
49
|
-
host_url = @@hosts[host_type]
|
50
|
-
raise StandardError, 'Unable to find host, have you forgotten to add host to grpc client?' unless host_url
|
51
|
-
|
52
|
-
Rails.logger.info 'Reinitiating to grpc host at ' + host_url
|
53
|
-
t1 = Time.now
|
54
|
-
@@services[get_key(host_type, service)] = ::Gruf::Client.new(service: service, options: { hostname: host_url })
|
55
|
-
Rails.logger.info "Time taken for reinitiating grpc host: #{Time.now - t1} seconds"
|
56
|
-
end
|
57
|
-
|
58
|
-
def get_key(host_type, service)
|
59
|
-
host_type.to_s + service.to_s
|
60
|
-
end
|
61
65
|
end
|
62
|
-
|
63
|
-
end
|
66
|
+
end
|
@@ -1,31 +1,45 @@
|
|
1
1
|
module Istox
|
2
2
|
module BlockchainReceiptQuery
|
3
3
|
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
#
|
6
|
-
def handle_confirm(
|
4
|
+
|
5
|
+
#overrideable, when blockchain transaction has confirmed
|
6
|
+
def handle_confirm(resource_action)
|
7
7
|
self
|
8
8
|
end
|
9
9
|
|
10
|
-
#
|
11
|
-
def handle_fail(
|
10
|
+
#overrideable, when blockchain transaction has failed
|
11
|
+
def handle_fail(resource_action)
|
12
12
|
self
|
13
13
|
end
|
14
14
|
|
15
|
-
#
|
15
|
+
#overrideable, extra data that will be published back to frontend
|
16
16
|
def handle_extra
|
17
17
|
nil
|
18
18
|
end
|
19
19
|
|
20
20
|
# outcome of the blockchain transaction, can be "pending" / "confirmed" / "failed"
|
21
21
|
def outcome
|
22
|
-
|
22
|
+
blockchain_receipts = get_blockchain_receipts
|
23
|
+
|
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' }
|
23
28
|
|
24
|
-
return
|
29
|
+
# return pending state if there is any pending hashes
|
30
|
+
return "pending" if blockchain_receipts.any?{ |r| r.status == 'pending' }
|
25
31
|
|
26
|
-
return
|
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
|
38
|
+
end
|
27
39
|
|
28
|
-
|
40
|
+
def last_receipt
|
41
|
+
::Istox::BlockchainReceipt.where(resource_id: self.id, resource_name: self.class.name).last
|
29
42
|
end
|
43
|
+
|
30
44
|
end
|
31
45
|
end
|
data/lib/istox/version.rb
CHANGED
data/lib/istox.rb
CHANGED
@@ -1,34 +1,33 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require 'paranoia'
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "gruf"
|
4
|
+
require "listen"
|
5
|
+
require "istox/version"
|
6
6
|
|
7
7
|
module Istox
|
8
8
|
# Your code goes here...
|
9
9
|
class Test
|
10
10
|
def self.method1
|
11
|
-
|
11
|
+
"abc"
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
require
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
23
|
-
require
|
24
|
-
require
|
25
|
-
require
|
26
|
-
require
|
27
|
-
require
|
28
|
-
require
|
29
|
-
require
|
30
|
-
require
|
31
|
-
require
|
32
|
-
require
|
33
|
-
require 'istox/logging/log_formatter'
|
15
|
+
require "istox/interfaces/chainhub/transaction"
|
16
|
+
require "istox/helpers/publisher"
|
17
|
+
require "istox/helpers/bunny_boot"
|
18
|
+
require "istox/helpers/vault"
|
19
|
+
require "istox/helpers/order_book"
|
20
|
+
require "istox/helpers/grpc_client"
|
21
|
+
require "istox/helpers/graphql_client"
|
22
|
+
require "istox/helpers/gruf_listener_hook"
|
23
|
+
require "istox/helpers/message_service"
|
24
|
+
require "istox/helpers/blockchain_receipt_service"
|
25
|
+
require "istox/helpers/f_math"
|
26
|
+
require "istox/models/blockchain_receipt"
|
27
|
+
require "istox/models/concerns/blockchain_receipt_query"
|
28
|
+
require "istox/consumers/blockchain_status_handler"
|
29
|
+
require "istox/consumers/blockchain_hash_handler"
|
30
|
+
require "istox/migrations/create_blockchain_receipts"
|
31
|
+
require "istox/logging/hash_logging"
|
32
|
+
require "istox/logging/log_formatter"
|
34
33
|
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.79
|
4
|
+
version: 0.1.79
|
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-
|
11
|
+
date: 2019-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -278,8 +278,6 @@ files:
|
|
278
278
|
- ".idea/modules.xml"
|
279
279
|
- ".idea/vcs.xml"
|
280
280
|
- ".idea/workspace.xml"
|
281
|
-
- ".rubocop.yml"
|
282
|
-
- ".solargraph.yml"
|
283
281
|
- CODE_OF_CONDUCT.md
|
284
282
|
- Gemfile
|
285
283
|
- Gemfile.lock
|
@@ -289,16 +287,15 @@ files:
|
|
289
287
|
- bin/setup
|
290
288
|
- istox.gemspec
|
291
289
|
- lib/istox.rb
|
290
|
+
- lib/istox/consumers/blockchain_hash_handler.rb
|
292
291
|
- lib/istox/consumers/blockchain_status_handler.rb
|
293
|
-
- lib/istox/helpers/
|
292
|
+
- lib/istox/helpers/blockchain_receipt_service.rb
|
294
293
|
- lib/istox/helpers/bunny_boot.rb
|
295
|
-
- lib/istox/helpers/common_helper.rb
|
296
294
|
- lib/istox/helpers/f_math.rb
|
297
295
|
- lib/istox/helpers/graphql_client.rb
|
298
296
|
- lib/istox/helpers/grpc_client.rb
|
299
297
|
- lib/istox/helpers/gruf_listener_hook.rb
|
300
298
|
- lib/istox/helpers/message_service.rb
|
301
|
-
- lib/istox/helpers/my_open_struct.rb
|
302
299
|
- lib/istox/helpers/order_book.rb
|
303
300
|
- lib/istox/helpers/publisher.rb
|
304
301
|
- lib/istox/helpers/vault.rb
|
@@ -323,11 +320,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
323
320
|
version: '0'
|
324
321
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
325
322
|
requirements:
|
326
|
-
- - "
|
323
|
+
- - ">="
|
327
324
|
- !ruby/object:Gem::Version
|
328
|
-
version:
|
325
|
+
version: '0'
|
329
326
|
requirements: []
|
330
|
-
|
327
|
+
rubyforge_project:
|
328
|
+
rubygems_version: 2.7.8
|
331
329
|
signing_key:
|
332
330
|
specification_version: 4
|
333
331
|
summary: istox backend shared gem
|
data/.rubocop.yml
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
require: rubocop-rspec
|
2
|
-
|
3
|
-
AllCops:
|
4
|
-
TargetRubyVersion: 2.5
|
5
|
-
Exclude:
|
6
|
-
- .gems/**/*
|
7
|
-
- bin/**/*
|
8
|
-
- config/**/*
|
9
|
-
- db/**/*
|
10
|
-
- log/**/*
|
11
|
-
- public/**/*
|
12
|
-
- tmp/**/*
|
13
|
-
- vendor/**/*
|
14
|
-
- spec/rails_helper.rb
|
15
|
-
- spec/spec_helper.rb
|
16
|
-
- config.ru
|
17
|
-
- Guardfile
|
18
|
-
- Rakefile
|
19
|
-
|
20
|
-
Documentation:
|
21
|
-
Enabled: false
|
22
|
-
|
23
|
-
Layout/SpaceBeforeFirstArg:
|
24
|
-
Exclude:
|
25
|
-
- app/views/api/**/**/*
|
26
|
-
|
27
|
-
Lint/AmbiguousBlockAssociation:
|
28
|
-
Exclude:
|
29
|
-
- spec/**/*
|
30
|
-
|
31
|
-
Metrics/AbcSize:
|
32
|
-
# The ABC size is a calculated magnitude, so this number can be an Integer or
|
33
|
-
# a Float.
|
34
|
-
Max: 100
|
35
|
-
|
36
|
-
Metrics/BlockLength:
|
37
|
-
CountComments: false # count full line comments?
|
38
|
-
Max: 25
|
39
|
-
Exclude:
|
40
|
-
- config/**/*
|
41
|
-
- spec/**/*
|
42
|
-
- app/admin/**/*
|
43
|
-
|
44
|
-
Metrics/BlockNesting:
|
45
|
-
Max: 4
|
46
|
-
|
47
|
-
Metrics/ClassLength:
|
48
|
-
CountComments: false # count full line comments?
|
49
|
-
Max: 200
|
50
|
-
|
51
|
-
# Avoid complex methods.
|
52
|
-
Metrics/CyclomaticComplexity:
|
53
|
-
Max: 20
|
54
|
-
|
55
|
-
Metrics/MethodLength:
|
56
|
-
CountComments: false # count full line comments?
|
57
|
-
Max: 100
|
58
|
-
|
59
|
-
Metrics/ModuleLength:
|
60
|
-
CountComments: false # count full line comments?
|
61
|
-
Max: 200
|
62
|
-
|
63
|
-
Metrics/LineLength:
|
64
|
-
Max: 130
|
65
|
-
# To make it possible to copy or click on URIs in the code, we allow lines
|
66
|
-
# containing a URI to be longer than Max.
|
67
|
-
AllowURI: true
|
68
|
-
URISchemes:
|
69
|
-
- http
|
70
|
-
- https
|
71
|
-
|
72
|
-
Metrics/ParameterLists:
|
73
|
-
Max: 5
|
74
|
-
CountKeywordArgs: true
|
75
|
-
|
76
|
-
Metrics/PerceivedComplexity:
|
77
|
-
Max: 12
|
78
|
-
|
79
|
-
Style/FrozenStringLiteralComment:
|
80
|
-
Enabled: false
|
81
|
-
|
82
|
-
Style/ModuleFunction:
|
83
|
-
Enabled: false
|
84
|
-
|
85
|
-
RequireNotFound:
|
86
|
-
Enabled: false
|
87
|
-
|
88
|
-
RSpec/MultipleExpectations:
|
89
|
-
Max: 99
|
90
|
-
|
91
|
-
RSpec/ExampleLength:
|
92
|
-
Max: 100
|
93
|
-
|
94
|
-
Lint/BigDecimalNew:
|
95
|
-
Enabled: false
|
96
|
-
|
97
|
-
|
98
|
-
RSpec/MessageSpies:
|
99
|
-
Enabled: false
|
100
|
-
|
101
|
-
RSpec/AnyInstance:
|
102
|
-
Enabled: false
|
103
|
-
|
104
|
-
RSpec/VerifiedDoubles:
|
105
|
-
Enabled: false
|
data/.solargraph.yml
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
module Istox
|
2
|
-
class BlockchainService
|
3
|
-
def self.init(blockchain_receipt_klass)
|
4
|
-
@@blockchain_receipt_klass = blockchain_receipt_klass
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.get_blockchain_receipt_klass
|
8
|
-
@@blockchain_receipt_klass
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.request(sid:, action:, before:, execute:)
|
12
|
-
start(sid: sid, action: action, before: before, execute: execute, is_request: true)
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.run(sid:, action:, before:, execute:)
|
16
|
-
start(sid: sid, action: action, before: before, execute: execute)
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
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 = 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
|
-
execute.call(uuid)
|
47
|
-
end
|
48
|
-
rescue StandardError => e
|
49
|
-
@receipt&.destroy
|
50
|
-
raise e
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.blockchain_receipt_class
|
54
|
-
raise 'Have you forgetten to init blockchain receipt service?' if get_blockchain_receipt_klass.nil?
|
55
|
-
|
56
|
-
blockchain_receipt_klass = get_blockchain_receipt_klass
|
57
|
-
klass = class_eval("::#{blockchain_receipt_klass.name}")
|
58
|
-
unless klass <= ::Istox::BlockchainReceipt
|
59
|
-
raise "#{blockchain_receipt_klass.name} does not inherit istox blockchain receipt"
|
60
|
-
end
|
61
|
-
|
62
|
-
klass
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
module Istox
|
2
|
-
module CommonHelper
|
3
|
-
def self.to_open_struct(model)
|
4
|
-
return nil if model.blank?
|
5
|
-
|
6
|
-
if model.is_a?(Array)
|
7
|
-
model.map do |item|
|
8
|
-
hash = deep_to_h(item).deep_transform_keys { |key| key.to_s.underscore.to_sym }
|
9
|
-
process_hash(nil, hash)
|
10
|
-
to_recursive_ostruct(hash)
|
11
|
-
end
|
12
|
-
else
|
13
|
-
hash = deep_to_h(model).deep_transform_keys { |key| key.to_s.underscore.to_sym }
|
14
|
-
process_hash(nil, hash)
|
15
|
-
to_recursive_ostruct(hash)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.process_hash(_parent, my_hash)
|
20
|
-
my_hash.each do |key, value|
|
21
|
-
if value.is_a?(Hash)
|
22
|
-
process_hash(key, value)
|
23
|
-
elsif value.is_a?(Array)
|
24
|
-
my_hash[key] = value.map do |item|
|
25
|
-
process_hash(nil, item.to_h)
|
26
|
-
end
|
27
|
-
else
|
28
|
-
my_hash[key] = value
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# def self.add_methods(ostruct)
|
34
|
-
# hashmap = ostruct.to_h
|
35
|
-
|
36
|
-
# hashmap&.each do |k, _v|
|
37
|
-
# # add_method k.to_s do
|
38
|
-
# ostruct.send(k.to_s)
|
39
|
-
# # end
|
40
|
-
# end
|
41
|
-
# end
|
42
|
-
|
43
|
-
def self.to_recursive_ostruct(hash)
|
44
|
-
::Istox::MyOpenStruct.new(hash.each_with_object({}) do |(key, val), memo|
|
45
|
-
memo[key] = if val.is_a?(Hash)
|
46
|
-
to_open_struct(val)
|
47
|
-
elsif val.is_a?(Array)
|
48
|
-
val.map do |item|
|
49
|
-
to_open_struct(item)
|
50
|
-
end
|
51
|
-
else
|
52
|
-
val
|
53
|
-
end
|
54
|
-
end)
|
55
|
-
end
|
56
|
-
|
57
|
-
def add_method(name, &block)
|
58
|
-
(class << self; self; end).class_eval do
|
59
|
-
define_method name, &block
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.deep_to_h(obj)
|
64
|
-
obj.to_h.transform_values do |v|
|
65
|
-
v.is_a?(OpenStruct) ? deep_to_h(v) : v
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|