Class: Fabric::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fabric/client.rb

Overview

Gateway Client represents the connection to a Hyperledger Fabric Gateway.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grpc_client: client, default_call_options: {}, **client_opts) ⇒ Client #initialize(host: host, creds: creds, default_call_options: {}, **client_opts) ⇒ Client

Initializes a client

Overloads:

  • #initialize(grpc_client: client, default_call_options: {}, **client_opts) ⇒ Client

    Initializes a client from a gRPC Gateway client stub

    Parameters:

    • grpc_client (Gateway::Gateway::Stub) (defaults to: client)

      grpc gateway client stub

    • default_call_options (Hash) (defaults to: {})

      call options to use by default for different operations

    • **client_opts (Hash)

      client initialization options

    Options Hash (default_call_options:):

    • :endorse_options (Hash)

      default options for endorse call

    • :evaluate_options (Hash)

      default options for evaluate call

    • :submit_options (Hash)

      default options for submit call

    • :commit_status_options (Hash)

      default options for commit_status call

    • :chaincode_events_options (Hash)

      default options for chaincode_events call

    See Also:

  • #initialize(host: host, creds: creds, default_call_options: {}, **client_opts) ⇒ Client

    Instantiates a new gRPC Gateway client stub from the parameters

    Parameters:

    • host (string) (defaults to: host)

      hostname and port of the gateway

    • creds (GRPC::Core::ChannelCredentials|GRPC::Core::XdsChannelCredentials|Symbol) (defaults to: creds)

      channel credentials (usually the CA certificate)

    • **client_opts (Hash)

      client initialization options

    Options Hash (default_call_options:):

    • :endorse_options (Hash)

      default options for endorse call

    • :evaluate_options (Hash)

      default options for evaluate call

    • :submit_options (Hash)

      default options for submit call

    • :commit_status_options (Hash)

      default options for commit_status call

    • :chaincode_events_options (Hash)

      default options for chaincode_events call

    See Also:



42
43
44
45
46
47
48
49
50
51
# File 'lib/fabric/client.rb', line 42

def initialize(grpc_client: nil, host: nil, creds: nil, default_call_options: {}, **client_opts)
  if grpc_client
    init_stub grpc_client
  elsif host && creds
    init_grpc_args(host, creds, **client_opts)
  else
    raise InvalidArgument, 'Must pass a Gateway::Gateway::Stub or <host>, <creds>, <client_opts>'
  end
  init_call_options(default_call_options)
end

Instance Attribute Details

#default_call_optionsObject (readonly)

Returns the value of attribute default_call_options.



8
9
10
# File 'lib/fabric/client.rb', line 8

def default_call_options
  @default_call_options
end

#grpc_clientObject (readonly)

Returns the value of attribute grpc_client.



8
9
10
# File 'lib/fabric/client.rb', line 8

def grpc_client
  @grpc_client
end

Instance Method Details

#chaincode_events(chaincode_events_request) ⇒ Enumerator #chaincode_events(chaincode_events_request) {|event| ... } ⇒ nil #chaincode_events(chaincode_events_request, {return_op: true}) ⇒ GRPC::ActiveCall::Operation #chaincode_events(chaincode_events_request, {return_op: true}) {|event| ... } ⇒ GRPC::ActiveCall::Operation

Subscribe to chaincode events

Overloads:

  • #chaincode_events(chaincode_events_request) ⇒ Enumerator

    Returns enumerator with Gateway::ChaincodeEventsResponse objects.

    Examples:

    Utilizing Blocking Enumerator

    call = client.chaincode_events(chaincode_events_request)
    call.each do |event|
      pp event
    end

    Parameters:

    • chaincode_events_request (Gateway::ChaincodeEventsRequest)
    • options (Hash)

      gRPC call options (merged with default options)

    Returns:

    • (Enumerator)

      enumerator with Gateway::ChaincodeEventsResponse objects

  • #chaincode_events(chaincode_events_request) {|event| ... } ⇒ nil

    Examples:

    Utilizing a blocking block

    client.chaincode_events(chaincode_events_request) do |event|
      pp event
    end

    Parameters:

    • chaincode_events_request (Gateway::ChaincodeEventsRequest)
    • options (Hash)

      gRPC call options (merged with default options)

    Yields:

    • (event)

      Blocking call that yields Gateway::ChaincodeEventsResponse objects when received from the server

    Yield Parameters:

    • event (Gateway::ChaincodeEventsResponse)

      chaincode event

    Returns:

    • (nil)
  • #chaincode_events(chaincode_events_request, {return_op: true}) ⇒ GRPC::ActiveCall::Operation

    Examples:

    Utilizing an operation control object and a enumerator

    op = client.chaincode_events(chaincode_events_request, {return_op: true})
    
    t = Thread.new do
      call = op.execute
      call.each do |event|
        pp event
      end
    end
    
    op.status
    op.cancelled?
    op.cancel

    Parameters:

    • chaincode_events_request (Gateway::ChaincodeEventsRequest)
    • options (Hash)

      gRPC call options (merged with default options)

    Returns:

    • (GRPC::ActiveCall::Operation)
  • #chaincode_events(chaincode_events_request, {return_op: true}) {|event| ... } ⇒ GRPC::ActiveCall::Operation

    Examples:

    Utilizing an operation control object and a block

    op = client.chaincode_events(chaincode_events_request, {return_op: true}) do |event|
      pp event
    end
    
    t = Thread.new do
      call = op.execute
    end
    
    op.status
    op.cancelled?
    op.cancel

    Parameters:

    • chaincode_events_request (Gateway::ChaincodeEventsRequest)
    • options (Hash)

      gRPC call options (merged with default options)

    Yields:

    • (event)

      Blocking call that yields Gateway::ChaincodeEventsResponse objects when received from the server

    Yield Parameters:

    • event (Gateway::ChaincodeEventsResponse)

      chaincode event

    Returns:

    • (GRPC::ActiveCall::Operation)

See Also:



165
166
167
168
# File 'lib/fabric/client.rb', line 165

def chaincode_events(chaincode_events_request, options = {}, &block)
  @grpc_client.chaincode_events(chaincode_events_request,
                                final_call_options(:chaincode_events, options), &block)
end

#commit_status(commit_status_request, options = {}) ⇒ Gateway::CommitStatusResponse

Submits an commit_status_request to the gateway to be evaluted.

Returns an enum or if you pass a block, use the block.

Parameters:

Returns:

  • (Gateway::CommitStatusResponse)

    commit_status_response



99
100
101
# File 'lib/fabric/client.rb', line 99

def commit_status(commit_status_request, options = {})
  @grpc_client.commit_status(commit_status_request, final_call_options(:commit_status, options))
end

#endorse(endorse_request, options = {}) ⇒ Gateway::EndorseResponse

Submits an endorse_request to the gateway to be evaluted.

Parameters:

Returns:

  • (Gateway::EndorseResponse)

    endorse_response



74
75
76
# File 'lib/fabric/client.rb', line 74

def endorse(endorse_request, options = {})
  @grpc_client.endorse(endorse_request, final_call_options(:endorse, options))
end

#evaluate(evaluate_request, options = {}) ⇒ Gateway::EvaluateResponse

Submits an evaluate_request to the gateway to be evaluted.

Parameters:

  • evaluate_request (Gateway::EvaluateRequest)
  • options (Hash) (defaults to: {})

    gRPC call options (merged with default_call_options from initializer)

Returns:

  • (Gateway::EvaluateResponse)

    evaluate_response

See Also:



62
63
64
# File 'lib/fabric/client.rb', line 62

def evaluate(evaluate_request, options = {})
  @grpc_client.evaluate(evaluate_request, final_call_options(:evaluate, options))
end

#submit(submit_request, options = {}) ⇒ Gateway::SubmitResponse

Submits an submit_request to the gateway to be evaluted.

Parameters:

Returns:

  • (Gateway::SubmitResponse)

    submit_response



86
87
88
# File 'lib/fabric/client.rb', line 86

def submit(submit_request, options = {})
  @grpc_client.submit(submit_request, final_call_options(:submit, options))
end