Class: Fabric::Contract

Inherits:
Object
  • Object
show all
Includes:
Accessors::Network
Defined in:
lib/fabric/contract.rb

Overview

Contract represents a smart contract, and allows applications to:

  • Evaluate transactions that query state from the ledger using the EvaluateTransaction() method.

  • Submit transactions that store state to the ledger using the SubmitTransaction() method.

For more complex transaction invocations, such as including transient data, transactions can be evaluated or submitted using the Evaluate() or Submit() methods respectively. The result of a submitted transaction can be accessed prior to its commit to the ledger using SubmitAsync().

By default, proposal, transaction and commit status messages will be signed using the signing implementation specified when connecting the Gateway. In cases where an external client holds the signing credentials, a signing implementation can be omitted when connecting the Gateway and off-line signing can be carried out by:

  1. Returning the serialized proposal, transaction or commit status along with its digest to the client for them to generate a signature.

  2. With the serialized message and signature received from the client to create a signed proposal, transaction or commit using the Gateway's NewSignedProposal(), NewSignedTransaction() or NewSignedCommit() methods respectively.

Instance Attribute Summary collapse

Attributes included from Accessors::Network

#gateway, #network_name

Attributes included from Accessors::Gateway

#client, #signer

Instance Method Summary collapse

Constructor Details

#initialize(network, chaincode_name, contract_name = '') ⇒ Contract

Returns a new instance of Contract.



32
33
34
35
36
# File 'lib/fabric/contract.rb', line 32

def initialize(network, chaincode_name, contract_name = '')
  @network = network
  @chaincode_name = chaincode_name
  @contract_name = contract_name
end

Instance Attribute Details

#chaincode_nameObject (readonly)

Returns the value of attribute chaincode_name.



26
27
28
# File 'lib/fabric/contract.rb', line 26

def chaincode_name
  @chaincode_name
end

#contract_nameObject (readonly)

Returns the value of attribute contract_name.



26
27
28
# File 'lib/fabric/contract.rb', line 26

def contract_name
  @contract_name
end

#networkObject (readonly)

Returns the value of attribute network.



26
27
28
# File 'lib/fabric/contract.rb', line 26

def network
  @network
end

Instance Method Details

#evaluate(transaction_name, proposal_options = {}) ⇒ String

Evaluate a transaction function and return its result. This method provides greater control over the transaction proposal content and the endorsing peers on which it is evaluated. This allows transaction functions to be evaluated where the proposal must include transient data, or that will access ledger data with key-based endorsement policies.

Parameters:

  • transaction_name (String)
  • proposal_options (Hash) (defaults to: {})

Options Hash (proposal_options):

  • :arguments (Array)

    array of arguments to pass to the transaction

  • :transient_data (Hash)

    Private data passed to the transaction function but not recorded on the ledger.

  • :endorsing_organizations (Array)

    Specifies the set of organizations that will attempt to endorse the proposal.

Returns:

  • (String)

    Raw evaluation response payload



83
84
85
# File 'lib/fabric/contract.rb', line 83

def evaluate(transaction_name, proposal_options = {})
  new_proposal(transaction_name, **proposal_options).evaluate
end

#evaluate_transaction(transaction_name, arguments = []) ⇒ String

Evaluate a transaction function and return its results. A transaction proposal will be evaluated on endorsing peers but the transaction will not be sent to the ordering service and so will not be committed to the ledger. This can be used for querying the world state.

Parameters:

  • transaction_name (String)
  • arguments (Array) (defaults to: [])

    array of arguments to pass to the transaction

Returns:

  • (String)

    raw payload of the transaction response



48
49
50
# File 'lib/fabric/contract.rb', line 48

def evaluate_transaction(transaction_name, arguments = [])
  evaluate(transaction_name, { arguments: arguments })
end

#new_proposal(transaction_name, arguments: [], transient_data: {}, endorsing_organizations: []) ⇒ Fabric::Proposal

Creates a transaction proposal that can be evaluated or endorsed. Supports off-line signing flow.

Parameters:

  • transaction_name (String)

    transaction name (first argument unshifted into the argument array)

  • arguments (Array<String>) (defaults to: [])

    array of arguments to pass to the transaction

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

    Private data passed to the transaction function but not recorded on the ledger.

  • endorsing_organizations (Array) (defaults to: [])

    Specifies the set of organizations that will attempt to endorse the proposal.

Returns:



131
132
133
134
135
136
137
138
139
140
# File 'lib/fabric/contract.rb', line 131

def new_proposal(transaction_name, arguments: [], transient_data: {}, endorsing_organizations: [])
  proposed_transaction = ProposedTransaction.new(
    self,
    qualified_transaction_name(transaction_name),
    arguments: arguments,
    transient_data: transient_data,
    endorsing_organizations: endorsing_organizations
  )
  Proposal.new(proposed_transaction)
end

#qualified_transaction_name(transaction_name) ⇒ string

Generates the qualified transaction name for the contract. (prepends the contract name to the transaction name if contract name is set)

Parameters:

  • transaction_name (string)

Returns:

  • (string)

    qualified transaction name



150
151
152
# File 'lib/fabric/contract.rb', line 150

def qualified_transaction_name(transaction_name)
  contract_name.nil? || contract_name.empty? ? transaction_name : "#{contract_name}:#{transaction_name}"
end

#submit(transaction_name, proposal_options = {}) ⇒ String

Submit a transaction to the ledger and return its result only after it is committed to the ledger. The transaction function will be evaluated on endorsing peers and then submitted to the ordering service to be committed to the ledger.

Parameters:

  • transaction_name (String)
  • proposal_options (Hash) (defaults to: {})

Options Hash (proposal_options):

  • :arguments (Array)

    array of arguments to pass to the transaction

  • :transient_data (Hash)

    Private data passed to the transaction function but not recorded on the ledger.

  • :endorsing_organizations (Array)

    Specifies the set of organizations that will attempt to endorse the proposal.

Returns:

  • (String)

    Raw evaluation response payload



102
103
104
105
106
107
# File 'lib/fabric/contract.rb', line 102

def submit(transaction_name, proposal_options = {})
  transaction = new_proposal(transaction_name, **proposal_options).endorse
  transaction.submit

  transaction.result
end

#submit_asyncObject

TODO:

unimplemented, not sure if this can be implemented because the official grpc ruby client does not support non-blocking async calls (https://github.com/grpc/grpc/issues/10973)

not 100% sure if grpc support is necessary for this.

Raises:



116
117
118
# File 'lib/fabric/contract.rb', line 116

def submit_async
  raise NotYetImplemented
end

#submit_transaction(transaction_name, arguments = []) ⇒ String

Submit a transaction to the ledger and return its result only after it is committed to the ledger. The transaction function will be evaluated on endorsing peers and then submitted to the ordering service to be committed to the ledger.

Parameters:

  • transaction_name (String)
  • arguments (Array) (defaults to: [])

    array of arguments to pass to the transaction

Returns:

  • (String)

    raw payload of the transaction response



63
64
65
# File 'lib/fabric/contract.rb', line 63

def submit_transaction(transaction_name, arguments = [])
  submit(transaction_name, { arguments: arguments })
end