contractapi

package
v0.0.0-...-6fe8828 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const SystemContractName = "org.hyperledger.fabric"

SystemContractName the name of the system smart contract

Variables

This section is empty.

Functions

This section is empty.

Types

type Contract

type Contract struct {
	Name                      string
	Info                      metadata.InfoMetadata
	UnknownTransaction        interface{}
	BeforeTransaction         interface{}
	AfterTransaction          interface{}
	TransactionContextHandler SettableTransactionContextInterface
}

Contract defines functions for setting and getting before, after and unknown transactions and name. Can be embedded in structs to quickly ensure their definition meets the ContractInterface.

func (*Contract) GetAfterTransaction

func (c *Contract) GetAfterTransaction() interface{}

GetAfterTransaction returns the current set afterTransaction, may be nil

func (*Contract) GetBeforeTransaction

func (c *Contract) GetBeforeTransaction() interface{}

GetBeforeTransaction returns the current set beforeTransaction, may be nil

func (*Contract) GetInfo

func (c *Contract) GetInfo() metadata.InfoMetadata

GetInfo returns the info about the contract for use in metadata

func (*Contract) GetName

func (c *Contract) GetName() string

GetName returns the name of the contract

func (*Contract) GetTransactionContextHandler

func (c *Contract) GetTransactionContextHandler() SettableTransactionContextInterface

GetTransactionContextHandler returns the current transaction context set for the contract. If none has been set then TransactionContext will be returned

func (*Contract) GetUnknownTransaction

func (c *Contract) GetUnknownTransaction() interface{}

GetUnknownTransaction returns the current set unknownTransaction, may be nil

type ContractChaincode

type ContractChaincode struct {
	DefaultContract string

	Info                  metadata.InfoMetadata
	TransactionSerializer serializer.TransactionSerializer
	// contains filtered or unexported fields
}

ContractChaincode a struct to meet the chaincode interface and provide routing of calls to contracts

func NewChaincode

func NewChaincode(contracts ...ContractInterface) (*ContractChaincode, error)

NewChaincode creates a new chaincode using contracts passed. The function parses each of the passed functions and stores details about their make-up to be used by the chaincode. Public functions of the contracts are stored and are made callable in the chaincode. The function will error if contracts are invalid e.g. public functions take in illegal types. A system contract is added to the chaincode which provides functionality for getting the metadata of the chaincode. The generated metadata is a JSON formatted MetadataContractChaincode containing each contract as a name and details of the public functions and types they take in/return. It also outlines version details for contracts and the chaincode. If these are blank strings this is set to latest. The names for parameters do not match those used in the functions, instead they are recorded as param0, param1, ..., paramN. If there exists a file contract-metadata/metadata.json then this will overwrite the generated metadata. The contents of this file must validate against the schema. The transaction serializer for the contract is set to be the JSONSerializer by default. This can be updated using by changing the TransactionSerializer property

func (*ContractChaincode) Init

Init is called during Instantiate transaction after the chaincode container has been established for the first time, passes off details of the request to Invoke for handling the request if a function name is passed, otherwise returns shim.Success

func (*ContractChaincode) Invoke

Invoke is called to update or query the ledger in a proposal transaction. Takes the args passed in the transaction and uses the first argument to identify the contract and function of that contract to be called. The remaining args are then used as parameters to that function. Args are converted from strings to the expected parameter types of the function before being passed using the set transaction serializer for the ContractChaincode. A transaction context is generated and is passed, if required, as the first parameter to the named function. Before and after functions are called before and after the named function passed if the contract defines such functions to exist. If the before function returns an error the named function is not called and its error is returned in shim.Error. If the after function returns an error then its value is returned to shim.Error otherwise the value returned from the named function is returned as shim.Success (formatted by the transaction serializer). If an unknown name is passed as part of the first arg a shim.Error is returned. If a valid name is passed but the function name is unknown then the contract with that name's unknown function is called and its value returned as success or error depending on its return. If no unknown function is defined for the contract then shim.Error is returned by Invoke. In the case of unknown function names being passed (and the unknown handler returns an error) or the named function returning an error then the after function if defined is not called. If the named function or unknown function handler returns a non-error type then then the after transaction is sent this value. The same transaction context is passed as a pointer to before, after, named and unknown functions on each Invoke. If no contract name is passed then the default contract is used.

func (*ContractChaincode) Start

func (cc *ContractChaincode) Start() error

Start starts the chaincode in the fabric shim

type ContractInterface

type ContractInterface interface {
	// GetInfo returns the information stored for the contract. This information will be
	// used to build up the metadata. If version is left blank in this info then "latest"
	// will be used in the metadata. If title is blank then the contract's GetName will be
	// used, if that is blank then the contract struct name
	GetInfo() metadata.InfoMetadata

	// GetUnknownTransaction returns the unknown function to be used for a contract.
	// When the contract is used in creating a new chaincode this function is called
	// and the unknown transaction returned is stored. The unknown function is then
	// called in cases where an unknown function name is passed for a call to the
	// contract via Init/Invoke of the chaincode. If nil is returned the
	// chaincode uses its default handling for unknown function names
	GetUnknownTransaction() interface{}

	// GetBeforeTransaction returns the before function to be used for a contract.
	// When the contract is used in creating a new chaincode this function is called
	// and the before transaction returned is stored. The before function is then
	// called before the named function on each Init/Invoke of that contract via the
	// chaincode. When called the before function is passed no extra args, only the
	// the transaction context (if specified to take it). If nil is returned
	// then no before function is called on Init/Invoke.
	GetBeforeTransaction() interface{}

	// GetAfterTransaction returns the after function to be used for a contract.
	// When the contract is used in creating a new chaincode this function is called
	// and the after transaction returned is stored. The after function is then
	// called after the named function on each Init/Invoke of that contract via the
	// chaincode. When called the after function is passed the returned value of the
	// named function and the transaction context (if the function takes the transaction
	// context). If nil is returned then no after function is called on Init/
	// Invoke.
	GetAfterTransaction() interface{}

	// GetName returns the name of the contract. When the contract is used
	// in creating a new chaincode this function is called and the name returned
	// is then used to identify the contract within the chaincode on Init/Invoke calls.
	// This function can return a blank string but this is undefined behaviour.
	GetName() string

	// GetTransactionContextHandler returns the SettableTransactionContextInterface that is
	// used by the functions of the contract. When the contract is used in creating
	// a new chaincode this function is called and the transaction context returned
	// is stored. When the chaincode is called via Init/Invoke a transaction context
	// of the stored type is created and sent as a parameter to the named contract
	// function (and before/after and unknown functions) if the function requires the
	// context in its list of parameters. If functions taking the transaction context
	// take an interface as the context, the transaction context returned by this function
	// must meet that interface
	GetTransactionContextHandler() SettableTransactionContextInterface
}

ContractInterface defines functions a valid contract should have. Contracts to be used in chaincode must implement this interface.

type EvaluationContractInterface

type EvaluationContractInterface interface {
	// GetEvaluateTransactions returns a list of function names that should be tagged in the
	// metadata as "evaluate" to indicate to a user of the chaincode that they should query
	// rather than invoke these functions
	GetEvaluateTransactions() []string
}

EvaluationContractInterface extends ContractInterface and provides additional functionality that can be used to improve metadata

type IgnoreContractInterface

type IgnoreContractInterface interface {
	// GetIgnoredFunctions returns a list of function names for functions that should not
	// be included in the produced metadata or accessible by invoking/querying the chaincode.
	// Note these functions are still callable by the code just not directly by outside users.
	// Those that match functions in the ChaincodeInterface are ignored by default and do not
	// need to be included
	GetIgnoredFunctions() []string
}

IgnoreContractInterface extends ContractInterface and provides additional functionality that can be used to mark which functions should not be accessible by invoking/querying chaincode

type SettableTransactionContextInterface

type SettableTransactionContextInterface interface {
	// SetStub should provide a way to pass the stub from a chaincode transaction
	// call to the transaction context so that it can be used by contract functions.
	// This is called by Init/Invoke with the stub passed.
	SetStub(shim.ChaincodeStubInterface)
	// SetClientIdentity should provide a way to pass the client identity from a chaincode
	// transaction call to the transaction context so that it can be used by contract functions.
	// This is called by Init/Invoke with the stub passed.
	SetClientIdentity(ci cid.ClientIdentity)
}

SettableTransactionContextInterface defines functions a valid transaction context should have. Transaction context's set for contracts to be used in chaincode must implement this interface.

type SystemContract

type SystemContract struct {
	Contract
	// contains filtered or unexported fields
}

SystemContract contract added to all chaincode to provide access to metdata

func (*SystemContract) GetEvaluateTransactions

func (sc *SystemContract) GetEvaluateTransactions() []string

GetEvaluateTransactions returns the transactions that exist in system contract which should be marked as evaluate transaction in the metadata. I.e. should be called by query transaction

func (*SystemContract) GetMetadata

func (sc *SystemContract) GetMetadata() string

GetMetadata returns JSON formatted metadata of chaincode the system contract is part of. This metadata is composed of reflected metadata combined with the metadata file if used

type TransactionContext

type TransactionContext struct {
	// contains filtered or unexported fields
}

TransactionContext is a basic transaction context to be used in contracts, containing minimal required functionality use in contracts as part of chaincode. Provides access to the stub and clientIdentity of a transaction. If a contract implements the ContractInterface using the Contract struct then this is the default transaction context that will be used.

func (*TransactionContext) GetClientIdentity

func (ctx *TransactionContext) GetClientIdentity() cid.ClientIdentity

GetClientIdentity returns the current set client identity

func (*TransactionContext) GetStub

GetStub returns the current set stub

func (*TransactionContext) SetClientIdentity

func (ctx *TransactionContext) SetClientIdentity(ci cid.ClientIdentity)

SetClientIdentity stores the passed stub in the transaction context

func (*TransactionContext) SetStub

func (ctx *TransactionContext) SetStub(stub shim.ChaincodeStubInterface)

SetStub stores the passed stub in the transaction context

type TransactionContextInterface

type TransactionContextInterface interface {
	// GetStub should provide a way to access the stub set by Init/Invoke
	GetStub() shim.ChaincodeStubInterface
	// GetClientIdentity should provide a way to access the client identity set by Init/Invoke
	GetClientIdentity() cid.ClientIdentity
}

TransactionContextInterface defines the interface which TransactionContext meets. This can be taken by transacton functions on a contract which has not set a custom transaction context to allow transaction functions to take an interface to simplify unit testing.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL