Class: Fabric::Transaction

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

Overview

Represents an endorsed transaction that can be submitted to the orderer for commit to the ledger, query the transaction results and its commit status.

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, prepared_transaction) ⇒ Transaction

Creates a new Transaction instance.

Parameters:

  • network (Fabric::Network)
  • prepared_transaction (Gateway::PreparedTransaction)


25
26
27
28
29
# File 'lib/fabric/entities/transaction.rb', line 25

def initialize(network, prepared_transaction)
  @network = network
  @prepared_transaction = prepared_transaction
  @envelope = Envelope.new(prepared_transaction.envelope)
end

Instance Attribute Details

#envelopeFabric::Envelope (readonly)

Returns:



17
18
19
# File 'lib/fabric/entities/transaction.rb', line 17

def envelope
  @envelope
end

#networkObject (readonly)

Returns the value of attribute network.



9
10
11
# File 'lib/fabric/entities/transaction.rb', line 9

def network
  @network
end

#prepared_transactionGateway::PreparedTransaction (readonly)

Returns Prepared Transaction.

Returns:

  • (Gateway::PreparedTransaction)

    Prepared Transaction



14
15
16
# File 'lib/fabric/entities/transaction.rb', line 14

def prepared_transaction
  @prepared_transaction
end

Instance Method Details

#result(check_status: true) ⇒ String

Get the transaction result. This is obtained during the endorsement process when the transaction proposal is run on endorsing peers.

Parameters:

  • check_status (boolean) (defaults to: true)

    set to true to raise exception if transaction has not yet been committed

Returns:

  • (String)

    Raw transaction result

Raises:



39
40
41
42
43
# File 'lib/fabric/entities/transaction.rb', line 39

def result(check_status: true)
  raise Fabric::CommitError, status if check_status && !status.successful

  envelope.result
end

#sign_status_requestFabric::Transaction

Sign the signed commit status request

Returns:



158
159
160
161
162
163
164
165
# File 'lib/fabric/entities/transaction.rb', line 158

def sign_status_request
  return if status_request_signed?

  signature = signer.sign(signed_commit_status_request.request)
  signed_commit_status_request.signature = signature

  self
end

#sign_submit_requestvoid

This method returns an undefined value.

Sign the transaction envelope.



74
75
76
77
78
79
# File 'lib/fabric/entities/transaction.rb', line 74

def sign_submit_request
  return if submit_request_signed?

  signature = signer.sign(envelope.payload_bytes)
  self.submit_request_signature = signature
end

#signed_commit_status_requestGateway::SignedCommitStatusRequest

Returns the current instance of the signed commit status request. Necessary so we can keep the state of the signature in the transaction object.

Returns:

  • (Gateway::SignedCommitStatusRequest)

    signed commit status request



173
174
175
# File 'lib/fabric/entities/transaction.rb', line 173

def signed_commit_status_request
  @signed_commit_status_request ||= new_signed_commit_status_request
end

#status(options = {}) ⇒ Fabric::Status

Get status of the committed transaction. If the transaction has not yet committed, this method blocks until the commit occurs. If status is already queried, this returns status from cache and does not make additional queries.

Parameters:

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

    gRPC call options

Returns:

See Also:



120
121
122
# File 'lib/fabric/entities/transaction.rb', line 120

def status(options = {})
  @status ||= query_status(options)
end

#status_request_digestString

Digest to be signed to support offline signing of the commit status request

Returns:

  • (String)

    digest of the commit status request



129
130
131
# File 'lib/fabric/entities/transaction.rb', line 129

def status_request_digest
  Fabric.crypto_suite.digest(signed_commit_status_request.request)
end

#status_request_signature=(signature) ⇒ void

This method returns an undefined value.

Sets the status request signature. This is used to support offline signing of the commit status request.

Parameters:

  • signature (String)


140
141
142
# File 'lib/fabric/entities/transaction.rb', line 140

def status_request_signature=(signature)
  signed_commit_status_request.signature = signature
end

#status_request_signed?Boolean

Returns true if the signed commit status request has been signed.

Returns:

  • (Boolean)

    true if signed; false otherwise



149
150
151
# File 'lib/fabric/entities/transaction.rb', line 149

def status_request_signed?
  !signed_commit_status_request.signature.empty?
end

#submit(options = {}) ⇒ Fabric::Transaction

Submit the transaction to the orderer to be committed to the ledger.

Parameters:

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

    gRPC call options

Returns:

See Also:



62
63
64
65
66
67
68
# File 'lib/fabric/entities/transaction.rb', line 62

def submit(options = {})
  sign_submit_request

  client.submit(new_submit_request, options)

  self
end

#submit_request_digestString

Digest to be signed to support offline signing of the submit request

Returns:

  • (String)

    digest of the submit request



95
96
97
# File 'lib/fabric/entities/transaction.rb', line 95

def submit_request_digest
  envelope.payload_digest
end

#submit_request_signature=(signature) ⇒ void

This method returns an undefined value.

Sets the submit request signature. This is used to support offline signing of the submit request.

Parameters:

  • signature (String)


106
107
108
# File 'lib/fabric/entities/transaction.rb', line 106

def submit_request_signature=(signature)
  envelope.signature = signature
end

#submit_request_signed?Boolean

Returns true if the transaction envelope has been signed.

Returns:

  • (Boolean)

    true if signed; false otherwise



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

def submit_request_signed?
  @envelope.signed?
end

#transaction_idString

Returns the transaction ID from the prepared transaction.

Returns:

  • (String)

    transaction_id



50
51
52
# File 'lib/fabric/entities/transaction.rb', line 50

def transaction_id
  prepared_transaction.transaction_id
end