Class: Peer

Peer

The Peer class represents a peer in the target blockchain network to which HFC sends endorsement proposals, transaction ordering or query requests. The Peer class represents the remote Peer node and its network membership materials, aka the ECert used to verify signatures. Peer membership represents organizations, unlike User membership which represents individuals. When constructed, a Peer instance can be designated as an event source, in which case a “eventSourceUrl” attribute should be configured. This allows the SDK to automatically attach transaction event listeners to the event stream. It should be noted that Peer event streams function at the Peer level and not at the chain and chaincode levels.

new Peer(url, opts)

Constructs a Peer given its endpoint configuration settings.
Parameters:
Name Type Description
url string The URL with format of "grpcs://host:port".
opts Object The options for the connection to the peer.
- request-timeout {string} A integer value in milliseconds to be used as node.js based timeout. This will break the request operation if the grpc request has not responded within this timeout period. note: other options will be passed to the grpc connection

Methods


addListener(eventType, eventTypeData, eventCallback)

For a Peer that is connected to eventSource, the addListener registers an EventCallBack for a set of event types. addListener can be invoked multiple times to support differing EventCallBack functions receiving different types of events. Note that the parameters below are optional in certain languages, like Java, that constructs an instance of a listener interface, and pass in that instance as the parameter.
Parameters:
Name Type Description
eventType string : ie. Block, Chaincode, Transaction
eventTypeData object : Object Specific for event type as necessary, currently needed for “Chaincode” event type, specifying a matching pattern to the event name set in the chaincode(s) being executed on the target Peer, and for “Transaction” event type, specifying the transaction ID
eventCallback class Client Application class registering for the callback.
Returns:
An ID reference to the event listener.
Type
string

connectEventSource()

Since practically all Peers are event producers, when constructing a Peer instance, an application can designate it as the event source for the application. Typically only one of the Peers on a Chain needs to be the event source, because all Peers on the Chain produce the same events. This method tells the SDK which Peer(s) to use as the event source for the client application. It is the responsibility of the SDK to manage the connection lifecycle to the Peer’s EventHub. It is the responsibility of the Client Application to understand and inform the selected Peer as to which event types it wants to receive and the call back functions to use.
Returns:
This gives the app a handle to attach “success” and “error” listeners
Type
Promise

getEnrollmentCertificate()

Returns the Peer's enrollment certificate.
Returns:
Certificate in PEM format signed by the trusted CA
Type
object

getName()

Get the Peer name. Required property for the instance objects.
Returns:
The name of the Peer
Type
string

getRoles()

Get the user’s roles the Peer participates in. It’s an array of possible values in “client”, and “auditor”. The member service defines two more roles reserved for peer membership: “peer” and “validator”, which are not exposed to the applications.
Returns:
The roles for this user.
Type
Array.<string>

isEventListened(eventName, chain)

A network call that discovers if at least one listener has been connected to the target Peer for a given event. This helps application instance to decide whether it needs to connect to the event source in a crash recovery or multiple instance instantiation.
Parameters:
Name Type Description
eventName string required
chain Chain optional

removeListener(eventListenerRef)

Unregisters a listener.
Parameters:
Name Type Description
eventListenerRef string Reference returned by SDK for event listener.
Returns:
Success / Failure status
Type
boolean

sendProposal(proposal)

Send an endorsement proposal to an endorser.
Parameters:
Name Type Description
proposal Proposal A proposal of type Proposal
See:
  • /protos/peer/fabric_proposal.proto
Returns:
Promise for a ProposalResponse

setEnrollmentCertificate(enrollment)

Set the Peer’s enrollment certificate.
Parameters:
Name Type Description
enrollment object Certificate in PEM format signed by the trusted CA

setName(name)

Set the Peer name / id.
Parameters:
Name Type Description
name string

setRoles(roles)

Set the user’s roles the Peer participates in. See getRoles() for legitimate values.
Parameters:
Name Type Description
roles Array.<string> The list of roles for the user.

toString()

return a printable representation of this object