Pluggable transaction endorsement and validation

Motivation

When a transaction is validated at time of commit, the peer performs various checks before applying the state changes that come with the transaction itself:

  • Validating the identities that signed the transaction.

  • Verifying the signatures of the endorsers on the transaction.

  • Ensuring the transaction satisfies the endorsement policies of the namespaces of the corresponding chaincodes.

There are use cases which demand custom transaction validation rules different from the default Fabric validation rules, such as:

  • UTXO (Unspent Transaction Output): When the validation takes into account whether the transaction doesn’t double spend its inputs.

  • Anonymous transactions: When the endorsement doesn’t contain the identity of the peer, but a signature and a public key are shared that can’t be linked to the peer’s identity.

Pluggable endorsement and validation logic

Fabric allows for the implementation and deployment of custom endorsement and validation logic into the peer to be associated with chaincode handling. This logic can be compiled into the peer or built with the peer and deployed alongside it as a Go plugin.

Note

Go plugins have a number of practical restrictions that require them to be compiled and linked in the same build environment as the peer. Differences in Go package versions, compiler versions, tags, and even GOPATH values will result in runtime failures when loading or executing the plugin logic.

By default, A chaincode will use the built in endorsement and validation logic. However, users have the option of selecting custom endorsement and validation plugins as part of the chaincode definition. An administrator can extend the endorsement/validation logic available to the peer by customizing the peer’s local configuration.

Configuration

Each peer has a local configuration (core.yaml) that declares a mapping between the endorsement/validation logic name and the implementation that is to be run.

The default logic are called ESCC (with the “E” standing for endorsement) and VSCC (validation), and they can be found in the peer local configuration in the handlers section:

  1. handlers:
  2. endorsers:
  3. escc:
  4. name: DefaultEndorsement
  5. validators:
  6. vscc:
  7. name: DefaultValidation

When the endorsement or validation implementation is compiled into the peer, the name property represents the initialization function that is to be run in order to obtain the factory that creates instances of the endorsement/validation logic.

The function is an instance method of the HandlerLibrary construct under core/handlers/library/library.go and in order for custom endorsement or validation logic to be added, this construct needs to be extended with any additional methods.

If the custom code is built as a Go plugin, the library property must be provided and set to the location of the shared library.

For example, if we have custom endorsement and validation logic which is implemented as a plugin, we would have the following entries in the configuration in core.yaml:

  1. handlers:
  2. endorsers:
  3. escc:
  4. name: DefaultEndorsement
  5. custom:
  6. name: customEndorsement
  7. library: /etc/hyperledger/fabric/plugins/customEndorsement.so
  8. validators:
  9. vscc:
  10. name: DefaultValidation
  11. custom:
  12. name: customValidation
  13. library: /etc/hyperledger/fabric/plugins/customValidation.so

And we’d have to place the .so plugin files in the peer’s local file system.

The name of the custom plugin needs to be referenced by the chaincode definition to be used by the chaincode. If you are using the peer CLI to approve the chaincode definition, use the --escc and --vscc flag to select the name of the custom endorsement or validation library. If you are using the Fabric SDK for Node.js, visit How to install and start your chaincode. For more information, see Fabric chaincode lifecycle.

Note

Hereafter, custom endorsement or validation logic implementation is going to be referred to as “plugins”, even if they are compiled into the peer.

Endorsement plugin implementation

To implement an endorsement plugin, one must implement the Plugin interface found in core/handlers/endorsement/api/endorsement.go:

  1. // Plugin endorses a proposal response
  2. type Plugin interface {
  3. // Endorse signs the given payload(ProposalResponsePayload bytes), and optionally mutates it.
  4. // Returns:
  5. // The Endorsement: A signature over the payload, and an identity that is used to verify the signature
  6. // The payload that was given as input (could be modified within this function)
  7. // Or error on failure
  8. Endorse(payload []byte, sp *peer.SignedProposal) (*peer.Endorsement, []byte, error)
  9. // Init injects dependencies into the instance of the Plugin
  10. Init(dependencies ...Dependency) error
  11. }

An endorsement plugin instance of a given plugin type (identified either by the method name as an instance method of the HandlerLibrary or by the plugin .so file path) is created for each channel by having the peer invoke the New method in the PluginFactory interface which is also expected to be implemented by the plugin developer:

  1. // PluginFactory creates a new instance of a Plugin
  2. type PluginFactory interface {
  3. New() Plugin
  4. }

The Init method is expected to receive as input all the dependencies declared under core/handlers/endorsement/api/, identified as embedding the Dependency interface.

After the creation of the Plugin instance, the Init method is invoked on it by the peer with the dependencies passed as parameters.

Currently Fabric comes with the following dependencies for endorsement plugins:

  • SigningIdentityFetcher: Returns an instance of SigningIdentity based on a given signed proposal:
  1. // SigningIdentity signs messages and serializes its public identity to bytes
  2. type SigningIdentity interface {
  3. // Serialize returns a byte representation of this identity which is used to verify
  4. // messages signed by this SigningIdentity
  5. Serialize() ([]byte, error)
  6. // Sign signs the given payload and returns a signature
  7. Sign([]byte) ([]byte, error)
  8. }
  • StateFetcher: Fetches a State object which interacts with the world state:
  1. // State defines interaction with the world state
  2. type State interface {
  3. // GetPrivateDataMultipleKeys gets the values for the multiple private data items in a single call
  4. GetPrivateDataMultipleKeys(namespace, collection string, keys []string) ([][]byte, error)
  5. // GetStateMultipleKeys gets the values for multiple keys in a single call
  6. GetStateMultipleKeys(namespace string, keys []string) ([][]byte, error)
  7. // GetTransientByTXID gets the values private data associated with the given txID
  8. GetTransientByTXID(txID string) ([]*rwset.TxPvtReadWriteSet, error)
  9. // Done releases resources occupied by the State
  10. Done()
  11. }

Validation plugin implementation

To implement a validation plugin, one must implement the Plugin interface found in core/handlers/validation/api/validation.go:

  1. // Plugin validates transactions
  2. type Plugin interface {
  3. // Validate returns nil if the action at the given position inside the transaction
  4. // at the given position in the given block is valid, or an error if not.
  5. Validate(block *common.Block, namespace string, txPosition int, actionPosition int, contextData ...ContextDatum) error
  6. // Init injects dependencies into the instance of the Plugin
  7. Init(dependencies ...Dependency) error
  8. }

Each ContextDatum is additional runtime-derived metadata that is passed by the peer to the validation plugin. Currently, the only ContextDatum that is passed is one that represents the endorsement policy of the chaincode:

  1. // SerializedPolicy defines a serialized policy
  2. type SerializedPolicy interface {
  3. validation.ContextDatum
  4. // Bytes returns the bytes of the SerializedPolicy
  5. Bytes() []byte
  6. }

A validation plugin instance of a given plugin type (identified either by the method name as an instance method of the HandlerLibrary or by the plugin .so file path) is created for each channel by having the peer invoke the New method in the PluginFactory interface which is also expected to be implemented by the plugin developer:

  1. // PluginFactory creates a new instance of a Plugin
  2. type PluginFactory interface {
  3. New() Plugin
  4. }

The Init method is expected to receive as input all the dependencies declared under core/handlers/validation/api/, identified as embedding the Dependency interface.

After the creation of the Plugin instance, the Init method is invoked on it by the peer with the dependencies passed as parameters.

Currently Fabric comes with the following dependencies for validation plugins:

  • IdentityDeserializer: Converts byte representation of identities into Identity objects that can be used to verify signatures signed by them, be validated themselves against their corresponding MSP, and see whether they satisfy a given MSP Principal. The full specification can be found in core/handlers/validation/api/identities/identities.go.

  • PolicyEvaluator: Evaluates whether a given policy is satisfied:

  1. // PolicyEvaluator evaluates policies
  2. type PolicyEvaluator interface {
  3. validation.Dependency
  4. // Evaluate takes a set of SignedData and evaluates whether this set of signatures satisfies
  5. // the policy with the given bytes
  6. Evaluate(policyBytes []byte, signatureSet []*common.SignedData) error
  7. }
  • StateFetcher: Fetches a State object which interacts with the world state:
  1. // State defines interaction with the world state
  2. type State interface {
  3. // GetStateMultipleKeys gets the values for multiple keys in a single call
  4. GetStateMultipleKeys(namespace string, keys []string) ([][]byte, error)
  5. // GetStateRangeScanIterator returns an iterator that contains all the key-values between given key ranges.
  6. // startKey is included in the results and endKey is excluded. An empty startKey refers to the first available key
  7. // and an empty endKey refers to the last available key. For scanning all the keys, both the startKey and the endKey
  8. // can be supplied as empty strings. However, a full scan should be used judiciously for performance reasons.
  9. // The returned ResultsIterator contains results of type *KV which is defined in fabric-protos/ledger/queryresult.
  10. GetStateRangeScanIterator(namespace string, startKey string, endKey string) (ResultsIterator, error)
  11. // GetStateMetadata returns the metadata for given namespace and key
  12. GetStateMetadata(namespace, key string) (map[string][]byte, error)
  13. // GetPrivateDataMetadata gets the metadata of a private data item identified by a tuple <namespace, collection, key>
  14. GetPrivateDataMetadata(namespace, collection, key string) (map[string][]byte, error)
  15. // Done releases resources occupied by the State
  16. Done()
  17. }

Important notes

  • Validation plugin consistency across peers: In future releases, the Fabric channel infrastructure would guarantee that the same validation logic is used for a given chaincode by all peers in the channel at any given blockchain height in order to eliminate the chance of mis-configuration which would might lead to state divergence among peers that accidentally run different implementations. However, for now it is the sole responsibility of the system operators and administrators to ensure this doesn’t happen.

  • Validation plugin error handling: Whenever a validation plugin can’t determine whether a given transaction is valid or not, because of some transient execution problem like inability to access the database, it should return an error of type ExecutionFailureError that is defined in core/handlers/validation/api/validation.go. Any other error that is returned, is treated as an endorsement policy error and marks the transaction as invalidated by the validation logic. However, if an ExecutionFailureError is returned, the chain processing halts instead of marking the transaction as invalid. This is to prevent state divergence between different peers.

  • Error handling for private metadata retrieval: In case a plugin retrieves metadata for private data by making use of the StateFetcher interface, it is important that errors are handled as follows: CollConfigNotDefinedError and InvalidCollNameError, signalling that the specified collection does not exist, should be handled as deterministic errors and should not lead the plugin to return an ExecutionFailureError.

  • Importing Fabric code into the plugin: Importing code that belongs to Fabric other than protobufs as part of the plugin is highly discouraged, and can lead to issues when the Fabric code changes between releases, or can cause inoperability issues when running mixed peer versions. Ideally, the plugin code should only use the dependencies given to it, and should import the bare minimum other than protobufs.