hyperledger-fabric-sdk 0.1.7 → 0.2.0

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: 61082f9951ee384979aabdca9eb446bd756be84ec5aec0dbd7420b66b0d0cfb5
4
- data.tar.gz: d227227f253e076ac60d93b86a0b58e3d3ced29952a24cab323e73e44bc2fe22
3
+ metadata.gz: 1adb2476b322a3f58f95a51a5faef139b215c1b1ad0c87573def1ac041cf94b3
4
+ data.tar.gz: efe5b6c77982cadeaaa54e0c02f0ae6c68e7c0b7b699dd35af98f624db7d5cc0
5
5
  SHA512:
6
- metadata.gz: b2af6d41c93b39c2bb9af417b956dc2aaa6ef1740d88b39307149bc230030f283ff49badf7df405bc1da2ee2c928330691cd0aedadde97901b3bf4769ead3fb4
7
- data.tar.gz: 1b77714ef415d6a187f4ecdd74170bdfee5728541f0147ea13286b109c0af53761140c5b14fce51d6dbf8bcaf78e5a5b79c21924623e2ed63bf884735519cdf5
6
+ metadata.gz: a2830cee92fc0691272e115e2b61063e6e843c5f634cc82d55b2420e830263929ea76765c9d6aafc78c3baea525ce1c89ee3617860d7720ecc8d9241472a7f5c
7
+ data.tar.gz: e7b2e165c001a71114d58c9ffb65cabdfa5fd69b881afb8df51f579c38d698c20ba5aed45059c9beb8deb27d53d9218d899b05f193f3011c75c778577986d113
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Bryan Padron
3
+ Copyright (c) 2019 Alexandr Kirshin, Bryan Padron
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/lib/fabric.rb CHANGED
@@ -12,6 +12,7 @@ require_relative 'fabric/version'
12
12
  require_relative 'fabric/configuration'
13
13
  require_relative 'fabric/constants'
14
14
  require_relative 'fabric/client'
15
+ require_relative 'fabric/client_stub'
15
16
  require_relative 'fabric/peer'
16
17
  require_relative 'fabric/orderer'
17
18
  require_relative 'fabric/enumerator_queue'
@@ -22,7 +23,6 @@ require_relative 'fabric/transaction'
22
23
  require_relative 'fabric/error'
23
24
  require_relative 'fabric/fabric_logger'
24
25
  require_relative 'fabric/chaincode_response'
25
- require_relative 'fabric/channel'
26
26
  require_relative 'fabric/transaction_info'
27
27
  require_relative 'fabric/helper'
28
28
  require_relative 'fabric/block_decoder'
@@ -30,42 +30,23 @@ require_relative 'fabric/event_hub'
30
30
 
31
31
  module Fabric
32
32
  extend Configuration
33
- @orderers = []
34
- @peers = []
35
33
 
36
34
  def self.new(config)
37
- @orderers = config[:orderers]
38
- @peers = config[:peers]
35
+ assign(config)
36
+
39
37
  self
40
38
  end
41
39
 
42
40
  def self.client(opts = {})
43
41
  client = Fabric::Client.new opts
44
42
 
45
- @orderers.each { |url| client.register_orderer url, opts }
46
- @peers.each { |url| client.register_peer url, opts }
43
+ orderers.each { |config| client.register_orderer config }
44
+ peers.each { |config| client.register_peer config }
45
+ event_hubs.each { |config| client.register_event_hub config }
47
46
 
48
47
  client
49
48
  end
50
49
 
51
- def self.channel(opts = {})
52
- channel = Fabric::Channel.new opts
53
-
54
- @orderers.each { |url| channel.register_orderer url, opts }
55
- @peers.each { |url| channel.register_peer url, opts }
56
-
57
- channel
58
- end
59
-
60
- def self.event_hub(opts = {})
61
- options = Fabric.options.merge opts
62
-
63
- options[:crypto_suite] ||= Fabric.crypto_suite
64
- options[:url] = options[:event_hub_url]
65
-
66
- Fabric::EventHub.new options
67
- end
68
-
69
50
  def self.crypto_suite(opts = {})
70
51
  @crypto_suite ||= Fabric::CryptoSuite.new opts
71
52
  end
@@ -122,7 +122,7 @@ module Fabric
122
122
  def self.decode_payload_header_extension(extension_data)
123
123
  return unless extension_data
124
124
 
125
- Protos::ChaincodeHeaderExtension.decode(extension_data).to_hash
125
+ Protos::ChaincodeHeaderExtension.decode(extension_data)
126
126
  end
127
127
 
128
128
  def self.decode_signature_header(signature_header_data)
@@ -186,13 +186,13 @@ module Fabric
186
186
  end
187
187
 
188
188
  def self.decode_chaincode_proposal_payload_input(input_data)
189
- Protos::ChaincodeInvocationSpec.decode(input_data).to_hash
189
+ Protos::ChaincodeInvocationSpec.decode(input_data)
190
190
  end
191
191
 
192
192
  def self.decode_chaincode_input(input_data)
193
193
  proto_chaincode_input = Protos::ChaincodeInput.decode input_data
194
194
 
195
- proto_chaincode_input.to_hash
195
+ proto_chaincode_input
196
196
  end
197
197
 
198
198
  def self.decode_chaincode_endorsed_action(proto_action)
@@ -263,7 +263,7 @@ module Fabric
263
263
  end
264
264
 
265
265
  def self.decode_kv_rw_set(rwset_data)
266
- Kvrwset::KVRWSet.decode(rwset_data).to_hash
266
+ Kvrwset::KVRWSet.decode(rwset_data)
267
267
  end
268
268
 
269
269
  def self.decode_collection_hashed_rw_set(proto_collection_hashed_rwset)
@@ -277,23 +277,23 @@ module Fabric
277
277
  end
278
278
 
279
279
  def self.decode_hashed_rwset(hashed_rwset_data)
280
- Rwset::HashedRWSet.decode(hashed_rwset_data).to_hash
280
+ Rwset::HashedRWSet.decode(hashed_rwset_data)
281
281
  end
282
282
 
283
283
  def self.decode_chaincode_events(event_data)
284
- Protos::ChaincodeEvent.decode(event_data).to_hash
284
+ Protos::ChaincodeEvent.decode(event_data)
285
285
  end
286
286
 
287
287
  def self.decode_response(proposal_response)
288
288
  return unless proposal_response
289
289
 
290
- proposal_response.to_hash
290
+ proposal_response
291
291
  end
292
292
 
293
293
  def self.decode_chaincode_id(chaincode_id)
294
294
  return unless chaincode_id
295
295
 
296
- chaincode_id.to_hash
296
+ chaincode_id
297
297
  end
298
298
  end
299
299
  end
data/lib/fabric/client.rb CHANGED
@@ -1,35 +1,48 @@
1
1
  module Fabric
2
2
  class Client
3
- attr_reader :identity, :crypto_suite,
4
- :orderers, :peers, :logger
5
-
6
- MAX_ATTEMPTS_CHECK_TRANSACTION = 20
7
- DELAY_PERIOD_CHECK_TRANSACTION = 2
3
+ attr_reader :identity, :orderers, :peers, :event_hubs, :logger
8
4
 
9
5
  def initialize(opts = {})
10
6
  options = Fabric.options.merge opts
11
7
 
12
8
  @logger = FabricLogger.new options[:logger], options[:logger_filters]
13
9
  @identity = options[:identity]
14
- @crypto_suite = options[:crypto_suite]
15
10
  end
16
11
 
17
- def register_peer(url, opts = {})
12
+ def register_peer(options, extra_options = {})
18
13
  @peers ||= []
19
14
 
20
- @peers << Peer.new(url: url, opts: opts, logger: logger)
15
+ options = { host: options } if options.is_a?(String)
16
+ extra_options.merge!(options)
17
+ extra_options.merge!(logger: logger)
18
+
19
+ @peers << Peer.new(extra_options[:host], extra_options[:creds], extra_options)
21
20
  end
22
21
 
23
- def register_orderer(url, opts = {})
22
+ def register_orderer(options, extra_options = {})
24
23
  @orderers ||= []
25
24
 
26
- @orderers << Orderer.new(url: url, opts: opts, logger: logger)
25
+ options = { host: options } if options.is_a?(String)
26
+ extra_options.merge!(options)
27
+ extra_options.merge!(logger: logger)
28
+
29
+ @orderers << Orderer.new(extra_options[:host], extra_options[:creds], extra_options)
30
+ end
31
+
32
+ def register_event_hub(options, extra_options = {})
33
+ @event_hubs ||= []
34
+
35
+ options = { host: options } if options.is_a?(String)
36
+ extra_options.merge!(options)
37
+ extra_options.merge!(logger: logger)
38
+
39
+ @event_hubs << EventHub.new(extra_options[:host], extra_options[:creds], extra_options)
27
40
  end
28
41
 
29
42
  def query(request = {})
30
43
  logging __method__, request
31
44
 
32
- proposal = Proposal.new crypto_suite, identity, request
45
+ proposal = Proposal.new identity, request
33
46
 
34
47
  send_query(proposal) { |response| parse_chaincode_response response.response }
35
48
  end
@@ -37,16 +50,17 @@ module Fabric
37
50
  def invoke(request = {})
38
51
  logging __method__, request
39
52
 
40
- proposal = Proposal.new crypto_suite, identity, request
53
+ proposal = Proposal.new identity, request
41
54
 
42
55
  responses = send_query(proposal) { |response| parse_peer_response response }
43
56
 
44
- transaction = Transaction.new crypto_suite, identity, proposal: proposal,
45
- responses: responses
57
+ transaction = Transaction.new identity, proposal: proposal, responses: responses
46
58
 
47
59
  send_transaction(transaction) { |response| parse_orderer_response response }
48
60
 
49
61
  responses.map { |response| parse_chaincode_response response.response }
62
+
63
+ transaction
50
64
  end
51
65
 
52
66
  private
@@ -67,36 +81,6 @@ module Fabric
67
81
  payload: payload.to_proto
68
82
 
69
83
  orderers.each { |orderer| orderer.send_broadcast envelope, &block }
70
-
71
- check_transaction transaction
72
- end
73
-
74
- def check_transaction(transaction)
75
- MAX_ATTEMPTS_CHECK_TRANSACTION.times do
76
- begin
77
- validation_code = get_transaction_validation_code transaction
78
-
79
- logging __method__, tx_id: transaction.tx_id, status: validation_code
80
-
81
- return validation_code if validation_code == :VALID
82
-
83
- raise Fabric::TransactionError, validation_code
84
- rescue UnknownError => ex
85
- sleep DELAY_PERIOD_CHECK_TRANSACTION
86
-
87
- logger.debug ex.message
88
- end
89
- end
90
- end
91
-
92
- def get_transaction_validation_code(transaction)
93
- channel_id = transaction.proposal.channel_id
94
- responses = query channel_id: channel_id,
95
- chaincode_id: 'qscc',
96
- args: ['GetTransactionByID', channel_id, transaction.tx_id]
97
- processed_transaction = Protos::ProcessedTransaction.decode responses.first
98
-
99
- Protos::TxValidationCode.lookup processed_transaction.validationCode
100
84
  end
101
85
 
102
86
  def parse_chaincode_response(response)
@@ -0,0 +1,25 @@
1
+ module Fabric
2
+ class ClientStub
3
+ attr_reader :host, :creds, :options, :logger
4
+
5
+ VALID_OPTIONS_KEYS = %i[channel_override timeout propagate_mask channel_args interceptors]
6
+
7
+ def initialize(host, creds, options = {})
8
+ @host = host
9
+ @logger = options[:logger]
10
+
11
+ @creds = GRPC::Core::ChannelCredentials.new(creds) if creds.is_a?(String)
12
+ @creds ||= :this_channel_is_insecure
13
+
14
+ @options = options.slice(*VALID_OPTIONS_KEYS)
15
+ end
16
+
17
+ private
18
+
19
+ def logging(section, message)
20
+ logger.debug section.to_s.upcase.colorize(:yellow),
21
+ host.colorize(:red),
22
+ message.to_s.colorize(:blue)
23
+ end
24
+ end
25
+ end
@@ -3,9 +3,13 @@ module Fabric
3
3
  DEFAULT_TIMEOUT = 30
4
4
 
5
5
  VALID_OPTIONS_KEYS = %i[
6
- crypto_suite identity orderers
7
- channel_id peers timeout logger
8
- logger_filters event_hub_url
6
+ orderers
7
+ peers
8
+ event_hubs
9
+ identity
10
+ timeout
11
+ logger
12
+ logger_filters
9
13
  ].freeze
10
14
 
11
15
  VALID_OPTIONS_KEYS.each { |attr| attr_accessor attr }
@@ -14,7 +18,7 @@ module Fabric
14
18
  base.reset
15
19
  end
16
20
 
17
- def self.configure
21
+ def configure
18
22
  yield self
19
23
  end
20
24
 
@@ -27,7 +31,14 @@ module Fabric
27
31
  def reset
28
32
  VALID_OPTIONS_KEYS.each { |key| send("#{key}=", nil) }
29
33
 
34
+ self.orderers = []
35
+ self.peers = []
36
+ self.event_hubs = []
30
37
  self.timeout = DEFAULT_TIMEOUT
31
38
  end
39
+
40
+ def assign(config)
41
+ VALID_OPTIONS_KEYS.each { |key| send("#{key}=", config[key]) if config.key?(key) }
42
+ end
32
43
  end
33
44
  end
@@ -1,25 +1,22 @@
1
1
  module Fabric
2
- class EventHub
3
- attr_reader :url, :identity, :logger, :crypto_suite, :channel_id
4
- attr_reader :channel, :connection, :queue
2
+ class EventHub < ClientStub
3
+ MAX_BLOCK_NUMBER = 4_611_686_018_427_387_903.freeze
5
4
 
6
- MAX_BLOCK_NUMBER = 1_000_000_000
5
+ def client
6
+ @client ||= ::Protos::Deliver::Stub.new host, creds, options
7
+ end
7
8
 
8
- def initialize(opts = {})
9
- @url = opts[:url]
10
- @identity = opts[:identity]
11
- @crypto_suite = opts[:crypto_suite]
12
- @logger = opts[:logger]
13
- @channel_id = opts[:channel_id]
9
+ def queue
10
+ @queue ||= Fabric::EnumeratorQueue.new client
11
+ end
14
12
 
15
- @channel = ::Protos::Deliver::Stub.new url, :this_channel_is_insecure
16
- @queue = Fabric::EnumeratorQueue.new channel
17
- @connection = channel.deliver(queue.each).each
13
+ def connection
14
+ @connection ||= client.deliver(queue.each).each
18
15
  end
19
16
 
20
- def observe(start_block = :newest, stop_block = MAX_BLOCK_NUMBER)
21
- tx_info = Fabric::TransactionInfo.new crypto_suite, identity
22
- seek_header = build_seek_header tx_info
17
+ def observe(channel_id, identity, start_block = :newest, stop_block = MAX_BLOCK_NUMBER)
18
+ tx_info = Fabric::TransactionInfo.new identity
19
+ seek_header = build_seek_header channel_id, tx_info
23
20
  seek_info = build_seek_info start_block, stop_block
24
21
  envelope = build_envelope tx_info, seek_header, seek_info
25
22
 
@@ -46,7 +43,7 @@ module Fabric
46
43
  payload: seek_payload.to_proto
47
44
  end
48
45
 
49
- def build_seek_header(tx_info)
46
+ def build_seek_header(channel_id, tx_info)
50
47
  seek_info_header = Fabric::Helper.build_channel_header(
51
48
  type: ::Common::HeaderType::DELIVER_SEEK_INFO,
52
49
  channel_id: channel_id,
@@ -7,25 +7,25 @@ module Fabric
7
7
 
8
8
  def initialize(logger, filters = [])
9
9
  @logger = logger
10
- @filters = [filters]
10
+ @filters = [filters].compact
11
11
  end
12
12
 
13
13
  def error(*args)
14
14
  return unless logger
15
15
 
16
- logger.tagged(LOGGER_TAG) { |logger| logger.error filter_message(args.join('|')) }
16
+ logger.error filter_message(args.join('|'))
17
17
  end
18
18
 
19
19
  def info(*args)
20
20
  return unless logger
21
21
 
22
- logger.tagged(LOGGER_TAG) { |logger| logger.info filter_message(args.join('|')) }
22
+ logger.info filter_message(args.join('|'))
23
23
  end
24
24
 
25
25
  def debug(*args)
26
26
  return unless logger
27
27
 
28
- logger.tagged(LOGGER_TAG) { |logger| logger.debug filter_message(args.join('|')) }
28
+ logger.debug filter_message(args.join('|'))
29
29
  end
30
30
 
31
31
  private
data/lib/fabric/helper.rb CHANGED
@@ -19,7 +19,7 @@ module Fabric
19
19
  end
20
20
 
21
21
  def self.build_timestamp
22
- now = Time.current
22
+ now = Time.now
23
23
 
24
24
  Google::Protobuf::Timestamp.new seconds: now.to_i, nanos: now.nsec
25
25
  end
@@ -4,7 +4,8 @@ module Fabric
4
4
  class Identity
5
5
  attr_reader :private_key,
6
6
  :public_key,
7
- :address
7
+ :address,
8
+ :crypto_suite
8
9
 
9
10
  attr_accessor :certificate, :mspid
10
11
 
@@ -1,15 +1,9 @@
1
1
  require 'orderer/ab_services_pb.rb'
2
2
 
3
3
  module Fabric
4
- class Orderer
5
- attr_reader :url, :opts, :client, :logger
6
-
7
- def initialize(args)
8
- @url = args[:url]
9
- @opts = args[:opts]
10
- @logger = args[:logger]
11
-
12
- @client = ::Orderer::AtomicBroadcast::Stub.new url, :this_channel_is_insecure
4
+ class Orderer < ClientStub
5
+ def client
6
+ @client ||= ::Orderer::AtomicBroadcast::Stub.new host, creds, options
13
7
  end
14
8
 
15
9
  def send_broadcast(envelope)
@@ -23,13 +17,5 @@ module Fabric
23
17
  yield response if block_given?
24
18
  end
25
19
  end
26
-
27
- private
28
-
29
- def logging(section, message)
30
- logger.debug section.to_s.upcase,
31
- url,
32
- message.to_s
33
- end
34
20
  end
35
21
  end
data/lib/fabric/peer.rb CHANGED
@@ -1,15 +1,9 @@
1
1
  require 'peer/peer_services_pb'
2
2
 
3
3
  module Fabric
4
- class Peer
5
- attr_reader :url, :opts, :client, :logger
6
-
7
- def initialize(args)
8
- @url = args[:url]
9
- @opts = args[:opts]
10
- @logger = args[:logger]
11
-
12
- @client = Protos::Endorser::Stub.new url, :this_channel_is_insecure
4
+ class Peer < ClientStub
5
+ def client
6
+ @client ||= Protos::Endorser::Stub.new(host, creds, options)
13
7
  end
14
8
 
15
9
  def send_process_proposal(proposal)
@@ -21,17 +15,5 @@ module Fabric
21
15
 
22
16
  response
23
17
  end
24
-
25
- def create_event_hub
26
- EventHub.new url: url, opts: opts, logger: logger
27
- end
28
-
29
- private
30
-
31
- def logging(section, message)
32
- logger.debug section.to_s.upcase,
33
- url,
34
- message.to_s
35
- end
36
18
  end
37
19
  end
@@ -1,15 +1,18 @@
1
1
  module Fabric
2
2
  class Proposal
3
- attr_reader :crypto_suite, :identity, :request
3
+ attr_reader :identity, :request
4
4
 
5
- def initialize(crypto_suite, identity, request = {})
6
- @crypto_suite = crypto_suite
5
+ def initialize(identity, request = {})
7
6
  @identity = identity
8
7
  @request = request
9
8
 
10
9
  assign_tx request[:transaction_info] if request[:transaction_info]
11
10
  end
12
11
 
12
+ def crypto_suite
13
+ identity.crypto_suite
14
+ end
15
+
13
16
  def nonce
14
17
  @nonce ||= crypto_suite.generate_nonce
15
18
  end
@@ -70,7 +73,7 @@ module Fabric
70
73
  end
71
74
 
72
75
  def tx_timestamp
73
- now = Time.current
76
+ now = Time.now
74
77
 
75
78
  @tx_timestamp ||= Google::Protobuf::Timestamp.new seconds: now.to_i, nanos: now.nsec
76
79
  end
@@ -2,10 +2,9 @@ require 'peer/transaction_pb'
2
2
 
3
3
  module Fabric
4
4
  class Transaction
5
- attr_reader :crypto_suite, :identity, :request
5
+ attr_reader :identity, :request
6
6
 
7
- def initialize(crypto_suite, identity, request = {})
8
- @crypto_suite = crypto_suite
7
+ def initialize(identity, request = {})
9
8
  @identity = identity
10
9
  @request = request
11
10
  end
@@ -1,16 +1,15 @@
1
1
  module Fabric
2
2
  class TransactionInfo
3
- attr_reader :tx_id, :nonce, :crypto_suite, :identity
3
+ attr_reader :tx_id, :nonce, :identity
4
4
 
5
- def initialize(crypto_suite, identity)
5
+ def initialize(identity)
6
6
  @identity = identity
7
- @nonce = crypto_suite.generate_nonce
8
- @tx_id = crypto_suite.hexdigest(nonce + identity.serialize)
9
- @crypto_suite = crypto_suite
7
+ @nonce = identity.crypto_suite.generate_nonce
8
+ @tx_id = identity.crypto_suite.hexdigest(nonce + identity.serialize)
10
9
  end
11
10
 
12
11
  def nonce_hex
13
- crypto_suite.encode_hex nonce
12
+ identity.crypto_suite.encode_hex nonce
14
13
  end
15
14
 
16
15
  def signature_header
@@ -1,3 +1,3 @@
1
1
  module Fabric
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/fabric_ca.rb CHANGED
@@ -11,6 +11,12 @@ module FabricCA
11
11
  extend Configuration
12
12
 
13
13
  def self.new(options = {})
14
+ assign(options)
15
+
16
+ self
17
+ end
18
+
19
+ def self.client(options)
14
20
  FabricCA::Client.new(options)
15
21
  end
16
22
  end
@@ -23,5 +23,9 @@ module FabricCA
23
23
  def reset
24
24
  VALID_OPTIONS_KEYS.each { |key| send("#{key}=", nil) }
25
25
  end
26
+
27
+ def assign(config)
28
+ VALID_OPTIONS_KEYS.each { |key| send("#{key}=", config[key]) if config.key?(key) }
29
+ end
26
30
  end
27
31
  end
@@ -3,9 +3,6 @@ require 'faraday'
3
3
  module FabricCA
4
4
  module FaradayMiddleware
5
5
  class RaiseHttpException < Faraday::Middleware
6
- ## Variables
7
- LOGGER_TAG = 'FABRIC CA'.freeze
8
-
9
6
  ## Attributes
10
7
  attr_reader :app, :logger
11
8
 
@@ -40,7 +37,7 @@ module FabricCA
40
37
  def logging(message)
41
38
  return unless logger
42
39
 
43
- logger.tagged(LOGGER_TAG) { |logger| logger.error message }
40
+ logger.error message
44
41
  end
45
42
  end
46
43
  end
@@ -1,3 +1,3 @@
1
1
  module FabricCA
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperledger-fabric-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Kirshin(kirshin)
@@ -139,6 +139,7 @@ files:
139
139
  - lib/fabric/chaincode_response.rb
140
140
  - lib/fabric/channel.rb
141
141
  - lib/fabric/client.rb
142
+ - lib/fabric/client_stub.rb
142
143
  - lib/fabric/configuration.rb
143
144
  - lib/fabric/constants.rb
144
145
  - lib/fabric/crypto_suite.rb