class Fabric::Transaction

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

Attributes

envelope[R]

@return [Fabric::Envelope]

network[R]
prepared_transaction[R]

@return [Gateway::PreparedTransaction] Prepared Transaction

Public Class Methods

new(network, prepared_transaction) click to toggle source

Creates a new Transaction instance.

@param [Fabric::Network] network @param [Gateway::PreparedTransaction] prepared_transaction

# 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

Public Instance Methods

result(check_status: true) click to toggle source

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

@param [boolean] check_status set to true to raise exception if transaction has not yet been committed

@return [String] Raw transaction result

# 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_request() click to toggle source

Sign the signed commit status request

@return [Fabric::Transaction] self

# 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_request() click to toggle source

Sign the transaction envelope.

@return [void]

# 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_request() click to toggle source

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

@return [Gateway::SignedCommitStatusRequest] signed commit status request

# 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 = {}) click to toggle source

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.

@see www.rubydoc.info/gems/grpc/GRPC%2FClientStub:request_response

@param [Hash] options gRPC call options

@return [Fabric::Status] status of the committed transaction

# File lib/fabric/entities/transaction.rb, line 120
def status(options = {})
  @status ||= query_status(options)
end
status_request_digest() click to toggle source

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

@return [String] digest of the commit status request

# 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) click to toggle source

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

@param [String] signature

@return [void]

# File lib/fabric/entities/transaction.rb, line 140
def status_request_signature=(signature)
  signed_commit_status_request.signature = signature
end
status_request_signed?() click to toggle source

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

@return [Boolean] true if signed; false otherwise

# File lib/fabric/entities/transaction.rb, line 149
def status_request_signed?
  !signed_commit_status_request.signature.empty?
end
submit(options = {}) click to toggle source

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

@see www.rubydoc.info/gems/grpc/GRPC%2FClientStub:request_response

@param [Hash] options gRPC call options

@return [Fabric::Transaction] self

# File lib/fabric/entities/transaction.rb, line 62
def submit(options = {})
  sign_submit_request

  client.submit(new_submit_request, options)

  self
end
submit_request_digest() click to toggle source

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

@return [String] digest of the submit request

# File lib/fabric/entities/transaction.rb, line 95
def submit_request_digest
  envelope.payload_digest
end
submit_request_signature=(signature) click to toggle source

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

@param [String] signature

@return [void]

# File lib/fabric/entities/transaction.rb, line 106
def submit_request_signature=(signature)
  envelope.signature = signature
end
submit_request_signed?() click to toggle source

Returns true if the transaction envelope has been signed.

@return [Boolean] true if signed; false otherwise

# File lib/fabric/entities/transaction.rb, line 86
def submit_request_signed?
  @envelope.signed?
end
transaction_id() click to toggle source

Returns the transaction ID from the prepared transaction.

@return [String] transaction_id

# File lib/fabric/entities/transaction.rb, line 50
def transaction_id
  prepared_transaction.transaction_id
end

Private Instance Methods

new_commit_status_request() click to toggle source

Generates a new commit status request

@return [Gateway::CommitStatusRequest] commit status request protobuf message

# File lib/fabric/entities/transaction.rb, line 211
def new_commit_status_request
  ::Gateway::CommitStatusRequest.new(
    channel_id: network_name,
    transaction_id: transaction_id,
    identity: signer.to_proto
  )
end
new_signed_commit_status_request() click to toggle source

Generates a new signed commit status request

@return [Gateway::SignedCommitStatusRequest] signed commit status request protobuf message

# File lib/fabric/entities/transaction.rb, line 200
def new_signed_commit_status_request
  ::Gateway::SignedCommitStatusRequest.new(
    request: new_commit_status_request.to_proto
  )
end
new_status(response) click to toggle source

New Status from CommitStatusResponse

@param [Gateway::CommitStatusResponse] response commit status response

@return [Fabric::Status] transaction status

# File lib/fabric/entities/transaction.rb, line 239
def new_status(response)
  Fabric::Status.new(
    transaction_id,
    response.block_number,
    Fabric::Status::TRANSACTION_STATUSES[response.result]
  )
end
new_submit_request() click to toggle source

Generates a new submit request.

@return [Gateway::SubmitRequest] submit request protobuf message

# File lib/fabric/entities/transaction.rb, line 224
def new_submit_request
  ::Gateway::SubmitRequest.new(
    transaction_id: transaction_id,
    channel_id: network_name,
    prepared_transaction: envelope.envelope
  )
end
query_status(options = {}) click to toggle source

Actual status query call used by status method.

@see www.rubydoc.info/gems/grpc/GRPC%2FClientStub:request_response

@param [Hash] options gRPC call options

@return [Fabric::Status] status of the committed transaction

# File lib/fabric/entities/transaction.rb, line 188
def query_status(options = {})
  sign_status_request

  commit_status_response = client.commit_status(signed_commit_status_request, options)
  new_status(commit_status_response)
end