shim

package
v1.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2017 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package shim provides APIs for the chaincode to access its state variables, transaction context and call other chaincodes.

Package shim provides APIs for the chaincode to access its state variables, transaction context and call other chaincodes.

Index

Constants

View Source
const (
	LogDebug    = LoggingLevel(logging.DEBUG)
	LogInfo     = LoggingLevel(logging.INFO)
	LogNotice   = LoggingLevel(logging.NOTICE)
	LogWarning  = LoggingLevel(logging.WARNING)
	LogError    = LoggingLevel(logging.ERROR)
	LogCritical = LoggingLevel(logging.CRITICAL)
)

These constants comprise the LoggingLevel enumeration

View Source
const (
	// If status code less than 500, endorser will endorse it.
	// OK means init or invoke successfully.
	OK = 200

	// Code that greater than or equal to 500 will be considered an error and rejected by endorser.
	ERROR = 500
)

Variables

This section is empty.

Functions

func Error

func Error(msg string) pb.Response

func IsEnabledForLogLevel

func IsEnabledForLogLevel(logLevel string) bool

IsEnabledForLogLevel checks to see if the chaincodeLogger is enabled for a specific logging level used primarily for testing

func SetLoggingLevel

func SetLoggingLevel(level LoggingLevel)

SetLoggingLevel allows a Go language chaincode to set the logging level of its shim.

func SetupChaincodeLogging

func SetupChaincodeLogging()

SetupChaincodeLogging sets the chaincode logging format and the level to the values of CORE_CHAINCODE_LOGFORMAT and CORE_CHAINCODE_LOGLEVEL set from core.yaml by chaincode_support.go

func Start

func Start(cc Chaincode) error

Start is the entry point for chaincodes bootstrap. It is not an API for chaincodes.

func StartInProc

func StartInProc(env []string, args []string, cc Chaincode, recv <-chan *pb.ChaincodeMessage, send chan<- *pb.ChaincodeMessage) error

StartInProc is an entry point for system chaincodes bootstrap. It is not an API for chaincodes.

func Success

func Success(payload []byte) pb.Response

Types

type Chaincode

type Chaincode interface {
	// Init is called during Deploy transaction after the container has been
	// established, allowing the chaincode to initialize its internal data
	Init(stub ChaincodeStubInterface) pb.Response
	// Invoke is called for every Invoke transactions. The chaincode may change
	// its state variables
	Invoke(stub ChaincodeStubInterface) pb.Response
}

Chaincode interface must be implemented by all chaincodes. The fabric runs the transactions by calling these functions as specified.

type ChaincodeLogger

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

ChaincodeLogger is an abstraction of a logging object for use by chaincodes. These objects are created by the NewLogger API.

func NewLogger

func NewLogger(name string) *ChaincodeLogger

NewLogger allows a Go language chaincode to create one or more logging objects whose logs will be formatted consistently with, and temporally interleaved with the logs created by the shim interface. The logs created by this object can be distinguished from shim logs by the name provided, which will appear in the logs.

func (*ChaincodeLogger) Critical

func (c *ChaincodeLogger) Critical(args ...interface{})

Critical logs always appear; They can not be disabled.

func (*ChaincodeLogger) Criticalf

func (c *ChaincodeLogger) Criticalf(format string, args ...interface{})

Criticalf logs always appear; They can not be disabled.

func (*ChaincodeLogger) Debug

func (c *ChaincodeLogger) Debug(args ...interface{})

Debug logs will only appear if the ChaincodeLogger LoggingLevel is set to LogDebug.

func (*ChaincodeLogger) Debugf

func (c *ChaincodeLogger) Debugf(format string, args ...interface{})

Debugf logs will only appear if the ChaincodeLogger LoggingLevel is set to LogDebug.

func (*ChaincodeLogger) Error

func (c *ChaincodeLogger) Error(args ...interface{})

Error logs will appear if the ChaincodeLogger LoggingLevel is set to LogError, LogWarning, LogNotice, LogInfo or LogDebug.

func (*ChaincodeLogger) Errorf

func (c *ChaincodeLogger) Errorf(format string, args ...interface{})

Errorf logs will appear if the ChaincodeLogger LoggingLevel is set to LogError, LogWarning, LogNotice, LogInfo or LogDebug.

func (*ChaincodeLogger) Info

func (c *ChaincodeLogger) Info(args ...interface{})

Info logs will appear if the ChaincodeLogger LoggingLevel is set to LogInfo or LogDebug.

func (*ChaincodeLogger) Infof

func (c *ChaincodeLogger) Infof(format string, args ...interface{})

Infof logs will appear if the ChaincodeLogger LoggingLevel is set to LogInfo or LogDebug.

func (*ChaincodeLogger) IsEnabledFor

func (c *ChaincodeLogger) IsEnabledFor(level LoggingLevel) bool

IsEnabledFor returns true if the logger is enabled to creates logs at the given logging level.

func (*ChaincodeLogger) Notice

func (c *ChaincodeLogger) Notice(args ...interface{})

Notice logs will appear if the ChaincodeLogger LoggingLevel is set to LogNotice, LogInfo or LogDebug.

func (*ChaincodeLogger) Noticef

func (c *ChaincodeLogger) Noticef(format string, args ...interface{})

Noticef logs will appear if the ChaincodeLogger LoggingLevel is set to LogNotice, LogInfo or LogDebug.

func (*ChaincodeLogger) SetLevel

func (c *ChaincodeLogger) SetLevel(level LoggingLevel)

SetLevel sets the logging level for a chaincode logger. Note that currently the levels are actually controlled by the name given when the logger is created, so loggers should be given unique names other than "shim".

func (*ChaincodeLogger) Warning

func (c *ChaincodeLogger) Warning(args ...interface{})

Warning logs will appear if the ChaincodeLogger LoggingLevel is set to LogWarning, LogNotice, LogInfo or LogDebug.

func (*ChaincodeLogger) Warningf

func (c *ChaincodeLogger) Warningf(format string, args ...interface{})

Warningf logs will appear if the ChaincodeLogger LoggingLevel is set to LogWarning, LogNotice, LogInfo or LogDebug.

type ChaincodeStub

type ChaincodeStub struct {
	TxID string
	// contains filtered or unexported fields
}

ChaincodeStub is an object passed to chaincode for shim side handling of APIs.

func (*ChaincodeStub) CreateCompositeKey

func (stub *ChaincodeStub) CreateCompositeKey(objectType string, attributes []string) (string, error)

CreateCompositeKey combines the given attributes to form a composite key.

func (*ChaincodeStub) DelState

func (stub *ChaincodeStub) DelState(key string) error

DelState removes the specified `key` and its value from the ledger.

func (*ChaincodeStub) GetArgs

func (stub *ChaincodeStub) GetArgs() [][]byte

GetArgs returns the argument list

func (*ChaincodeStub) GetArgsSlice

func (stub *ChaincodeStub) GetArgsSlice() ([]byte, error)

GetArgsSlice returns the arguments to the stub call as a byte array

func (*ChaincodeStub) GetBinding

func (stub *ChaincodeStub) GetBinding() ([]byte, error)

GetBinding returns the transaction binding

func (*ChaincodeStub) GetCreator

func (stub *ChaincodeStub) GetCreator() ([]byte, error)

GetCreator returns SignatureHeader.Creator of the proposal this Stub refers to.

func (*ChaincodeStub) GetFunctionAndParameters

func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params []string)

GetFunctionAndParameters returns the first arg as the function and the rest as argument string array

func (*ChaincodeStub) GetHistoryForKey

func (stub *ChaincodeStub) GetHistoryForKey(key string) (StateQueryIteratorInterface, error)

GetHistoryForKey function can be invoked by a chaincode to return a history of key values across time. GetHistoryForKey is intended to be used for read-only queries.

func (*ChaincodeStub) GetQueryResult

func (stub *ChaincodeStub) GetQueryResult(query string) (StateQueryIteratorInterface, error)

GetQueryResult function can be invoked by a chaincode to perform a rich query against state database. Only supported by state database implementations that support rich query. The query string is in the syntax of the underlying state database. An iterator is returned which can be used to iterate (next) over the query result set

func (*ChaincodeStub) GetState

func (stub *ChaincodeStub) GetState(key string) ([]byte, error)

GetState returns the byte array value specified by the `key`.

func (*ChaincodeStub) GetStateByPartialCompositeKey

func (stub *ChaincodeStub) GetStateByPartialCompositeKey(objectType string, attributes []string) (StateQueryIteratorInterface, error)

GetStateByPartialCompositeKey function can be invoked by a chaincode to query the state based on a given partial composite key. This function returns an iterator which can be used to iterate over all composite keys whose prefix matches the given partial composite key. This function should be used only for a partial composite key. For a full composite key, an iter with empty response would be returned.

func (*ChaincodeStub) GetStateByRange

func (stub *ChaincodeStub) GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)

GetStateByRange function can be invoked by a chaincode to query of a range of keys in the state. Assuming the startKey and endKey are in lexical order, an iterator will be returned that can be used to iterate over all keys between the startKey and endKey, inclusive. The order in which keys are returned by the iterator is random.

func (*ChaincodeStub) GetStringArgs

func (stub *ChaincodeStub) GetStringArgs() []string

GetStringArgs returns the arguments as array of strings

func (*ChaincodeStub) GetTransient

func (stub *ChaincodeStub) GetTransient() (map[string][]byte, error)

GetTransient returns the ChaincodeProposalPayload.transient field. It is a map that contains data (e.g. cryptographic material) that might be used to implement some form of application-level confidentiality. The contents of this field, as prescribed by ChaincodeProposalPayload, are supposed to always be omitted from the transaction and excluded from the ledger.

func (*ChaincodeStub) GetTxID

func (stub *ChaincodeStub) GetTxID() string

GetTxID returns the transaction ID

func (*ChaincodeStub) GetTxTimestamp

func (stub *ChaincodeStub) GetTxTimestamp() (*timestamp.Timestamp, error)

GetTxTimestamp returns transaction created timestamp, which is currently taken from the peer receiving the transaction. Note that this timestamp may not be the same with the other peers' time.

func (*ChaincodeStub) InvokeChaincode

func (stub *ChaincodeStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response

InvokeChaincode locally calls the specified chaincode `Invoke` using the same transaction context; that is, chaincode calling chaincode doesn't create a new transaction message.

func (*ChaincodeStub) PutState

func (stub *ChaincodeStub) PutState(key string, value []byte) error

PutState writes the specified `value` and `key` into the ledger.

func (*ChaincodeStub) SetEvent

func (stub *ChaincodeStub) SetEvent(name string, payload []byte) error

SetEvent saves the event to be sent when a transaction is made part of a block

func (*ChaincodeStub) SplitCompositeKey

func (stub *ChaincodeStub) SplitCompositeKey(compositeKey string) (string, []string, error)

SplitCompositeKey splits the key into attributes on which the composite key was formed.

type ChaincodeStubInterface

type ChaincodeStubInterface interface {
	// Get the arguments to the stub call as a 2D byte array
	GetArgs() [][]byte

	// Get the arguments to the stub call as a string array
	GetStringArgs() []string

	// Get the function which is the first argument and the rest of the arguments
	// as parameters
	GetFunctionAndParameters() (string, []string)

	// Get the transaction ID
	GetTxID() string

	// InvokeChaincode locally calls the specified chaincode `Invoke` using the
	// same transaction context; that is, chaincode calling chaincode doesn't
	// create a new transaction message. If the called chaincode is on a different
	// channel, only the Response is returned to the caller; any PutState calls
	// will not have any effect on the ledger of the channel; effectively it is
	// a `Query`. If `channel` is empty, the caller's channel is assumed.
	InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response

	// GetState returns the byte array value specified by the `key`.
	GetState(key string) ([]byte, error)

	// PutState writes the specified `value` and `key` into the ledger.
	PutState(key string, value []byte) error

	// DelState removes the specified `key` and its value from the ledger.
	DelState(key string) error

	// GetStateByRange function can be invoked by a chaincode to query of a range
	// of keys in the state. Assuming the startKey and endKey are in lexical
	// an iterator will be returned that can be used to iterate over all keys
	// between the startKey (inclusive) and endKey (exclusive). The order in which keys are
	// returned by the iterator is random.
	GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)

	// GetStateByPartialCompositeKey function can be invoked by a chaincode to query the
	// state based on a given partial composite key. This function returns an
	// iterator which can be used to iterate over all composite keys whose prefix
	// matches the given partial composite key. This function should be used only for
	// a partial composite key. For a full composite key, an iter with empty response
	// would be returned. The objectType and attributes are expected to have only
	// valid utf8 strings and should not contain U+0000 (nil byte) and U+10FFFF (biggest and unallocated code point)
	GetStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error)

	// Given a list of attributes, CreateCompositeKey function combines these attributes
	// to form a composite key. The objectType and attributes are expected to have only
	// valid utf8 strings and should not contain U+0000 (nil byte) and U+10FFFF (biggest and unallocated code point)
	CreateCompositeKey(objectType string, attributes []string) (string, error)

	// Given a composite key, SplitCompositeKey function splits the key into attributes
	// on which the composite key was formed.
	SplitCompositeKey(compositeKey string) (string, []string, error)

	// GetQueryResult function can be invoked by a chaincode to perform a
	// rich query against state database.  Only supported by state database implementations
	// that support rich query.  The query string is in the syntax of the underlying
	// state database. An iterator is returned which can be used to iterate (next) over
	// the query result set
	GetQueryResult(query string) (StateQueryIteratorInterface, error)

	// GetHistoryForKey function can be invoked by a chaincode to return a history of
	// key values across time. GetHistoryForKey is intended to be used for read-only queries.
	GetHistoryForKey(key string) (StateQueryIteratorInterface, error)

	// GetCreator returns SignatureHeader.Creator of the proposal
	// this Stub refers to.
	GetCreator() ([]byte, error)

	// GetTransient returns the ChaincodeProposalPayload.transient field.
	// It is a map that contains data (e.g. cryptographic material)
	// that might be used to implement some form of application-level confidentiality. The contents
	// of this field, as prescribed by ChaincodeProposalPayload, are supposed to always
	// be omitted from the transaction and excluded from the ledger.
	GetTransient() (map[string][]byte, error)

	// GetBinding returns the transaction binding
	GetBinding() ([]byte, error)

	// GetArgsSlice returns the arguments to the stub call as a byte array
	GetArgsSlice() ([]byte, error)

	// GetTxTimestamp returns transaction created timestamp, which is currently
	// taken from the peer receiving the transaction. Note that this timestamp
	// may not be the same with the other peers' time.
	GetTxTimestamp() (*timestamp.Timestamp, error)

	// SetEvent saves the event to be sent when a transaction is made part of a block
	SetEvent(name string, payload []byte) error
}

ChaincodeStubInterface is used by deployable chaincode apps to access and modify their ledgers

type Handler

type Handler struct {
	sync.RWMutex

	To         string
	ChatStream PeerChaincodeStream
	FSM        *fsm.FSM
	// contains filtered or unexported fields
}

Handler handler implementation for shim side of chaincode.

type LoggingLevel

type LoggingLevel logging.Level

LoggingLevel is an enumerated type of severity levels that control chaincode logging.

func LogLevel

func LogLevel(levelString string) (LoggingLevel, error)

LogLevel converts a case-insensitive string chosen from CRITICAL, ERROR, WARNING, NOTICE, INFO or DEBUG into an element of the LoggingLevel type. In the event of errors the level returned is LogError.

type MockStateRangeQueryIterator

type MockStateRangeQueryIterator struct {
	Closed   bool
	Stub     *MockStub
	StartKey string
	EndKey   string
	Current  *list.Element
}

func NewMockStateRangeQueryIterator

func NewMockStateRangeQueryIterator(stub *MockStub, startKey string, endKey string) *MockStateRangeQueryIterator

func (*MockStateRangeQueryIterator) Close

func (iter *MockStateRangeQueryIterator) Close() error

Close closes the range query iterator. This should be called when done reading from the iterator to free up resources.

func (*MockStateRangeQueryIterator) HasNext

func (iter *MockStateRangeQueryIterator) HasNext() bool

HasNext returns true if the range query iterator contains additional keys and values.

func (*MockStateRangeQueryIterator) Next

func (iter *MockStateRangeQueryIterator) Next() (string, []byte, error)

Next returns the next key and value in the range query iterator.

func (*MockStateRangeQueryIterator) Print

func (iter *MockStateRangeQueryIterator) Print()

type MockStub

type MockStub struct {

	// A nice name that can be used for logging
	Name string

	// State keeps name value pairs
	State map[string][]byte

	// Keys stores the list of mapped values in lexical order
	Keys *list.List

	// registered list of other MockStub chaincodes that can be called from this MockStub
	Invokables map[string]*MockStub

	// stores a transaction uuid while being Invoked / Deployed
	// TODO if a chaincode uses recursion this may need to be a stack of TxIDs or possibly a reference counting map
	TxID string
	// contains filtered or unexported fields
}

MockStub is an implementation of ChaincodeStubInterface for unit testing chaincode. Use this instead of ChaincodeStub in your chaincode's unit test calls to Init or Invoke.

func NewMockStub

func NewMockStub(name string, cc Chaincode) *MockStub

Constructor to initialise the internal State map

func (*MockStub) CreateCompositeKey

func (stub *MockStub) CreateCompositeKey(objectType string, attributes []string) (string, error)

CreateCompositeKey combines the list of attributes to form a composite key.

func (*MockStub) DelState

func (stub *MockStub) DelState(key string) error

DelState removes the specified `key` and its value from the ledger.

func (*MockStub) GetArgs

func (stub *MockStub) GetArgs() [][]byte

func (*MockStub) GetArgsSlice

func (stub *MockStub) GetArgsSlice() ([]byte, error)

Not implemented

func (*MockStub) GetBinding

func (stub *MockStub) GetBinding() ([]byte, error)

Not implemented

func (*MockStub) GetCreator

func (stub *MockStub) GetCreator() ([]byte, error)

Not implemented

func (*MockStub) GetFunctionAndParameters

func (stub *MockStub) GetFunctionAndParameters() (function string, params []string)

func (*MockStub) GetHistoryForKey

func (stub *MockStub) GetHistoryForKey(key string) (StateQueryIteratorInterface, error)

GetHistoryForKey function can be invoked by a chaincode to return a history of key values across time. GetHistoryForKey is intended to be used for read-only queries.

func (*MockStub) GetQueryResult

func (stub *MockStub) GetQueryResult(query string) (StateQueryIteratorInterface, error)

GetQueryResult function can be invoked by a chaincode to perform a rich query against state database. Only supported by state database implementations that support rich query. The query string is in the syntax of the underlying state database. An iterator is returned which can be used to iterate (next) over the query result set

func (*MockStub) GetState

func (stub *MockStub) GetState(key string) ([]byte, error)

GetState retrieves the value for a given key from the ledger

func (*MockStub) GetStateByPartialCompositeKey

func (stub *MockStub) GetStateByPartialCompositeKey(objectType string, attributes []string) (StateQueryIteratorInterface, error)

GetStateByPartialCompositeKey function can be invoked by a chaincode to query the state based on a given partial composite key. This function returns an iterator which can be used to iterate over all composite keys whose prefix matches the given partial composite key. This function should be used only for a partial composite key. For a full composite key, an iter with empty response would be returned.

func (*MockStub) GetStateByRange

func (stub *MockStub) GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)

func (*MockStub) GetStringArgs

func (stub *MockStub) GetStringArgs() []string

func (*MockStub) GetTransient

func (stub *MockStub) GetTransient() (map[string][]byte, error)

Not implemented

func (*MockStub) GetTxID

func (stub *MockStub) GetTxID() string

func (*MockStub) GetTxTimestamp

func (stub *MockStub) GetTxTimestamp() (*timestamp.Timestamp, error)

Not implemented

func (*MockStub) InvokeChaincode

func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response

InvokeChaincode calls a peered chaincode. E.g. stub1.InvokeChaincode("stub2Hash", funcArgs, channel) Before calling this make sure to create another MockStub stub2, call stub2.MockInit(uuid, func, args) and register it with stub1 by calling stub1.MockPeerChaincode("stub2Hash", stub2)

func (*MockStub) MockInit

func (stub *MockStub) MockInit(uuid string, args [][]byte) pb.Response

Initialise this chaincode, also starts and ends a transaction.

func (*MockStub) MockInvoke

func (stub *MockStub) MockInvoke(uuid string, args [][]byte) pb.Response

Invoke this chaincode, also starts and ends a transaction.

func (*MockStub) MockPeerChaincode

func (stub *MockStub) MockPeerChaincode(invokableChaincodeName string, otherStub *MockStub)

Register a peer chaincode with this MockStub invokableChaincodeName is the name or hash of the peer otherStub is a MockStub of the peer, already intialised

func (*MockStub) MockTransactionEnd

func (stub *MockStub) MockTransactionEnd(uuid string)

End a mocked transaction, clearing the UUID.

func (*MockStub) MockTransactionStart

func (stub *MockStub) MockTransactionStart(txid string)

Used to indicate to a chaincode that it is part of a transaction. This is important when chaincodes invoke each other. MockStub doesn't support concurrent transactions at present.

func (*MockStub) PutState

func (stub *MockStub) PutState(key string, value []byte) error

PutState writes the specified `value` and `key` into the ledger.

func (*MockStub) SetEvent

func (stub *MockStub) SetEvent(name string, payload []byte) error

Not implemented

func (*MockStub) SplitCompositeKey

func (stub *MockStub) SplitCompositeKey(compositeKey string) (string, []string, error)

SplitCompositeKey splits the composite key into attributes on which the composite key was formed.

type PeerChaincodeStream

type PeerChaincodeStream interface {
	Send(*pb.ChaincodeMessage) error
	Recv() (*pb.ChaincodeMessage, error)
	CloseSend() error
}

PeerChaincodeStream interface for stream between Peer and chaincode instance.

type StateQueryIterator

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

StateQueryIterator allows a chaincode to iterate over a set of key/value pairs in the state.

func (*StateQueryIterator) Close

func (iter *StateQueryIterator) Close() error

Close closes the range query iterator. This should be called when done reading from the iterator to free up resources.

func (*StateQueryIterator) HasNext

func (iter *StateQueryIterator) HasNext() bool

HasNext returns true if the range query iterator contains additional keys and values.

func (*StateQueryIterator) Next

func (iter *StateQueryIterator) Next() (string, []byte, error)

Next returns the next key and value in the range query iterator.

type StateQueryIteratorInterface

type StateQueryIteratorInterface interface {

	// HasNext returns true if the range query iterator contains additional keys
	// and values.
	HasNext() bool

	// Next returns the next key and value in the range query iterator.
	Next() (string, []byte, error)

	// Close closes the range query iterator. This should be called when done
	// reading from the iterator to free up resources.
	Close() error
}

StateQueryIteratorInterface allows a chaincode to iterate over a set of key/value pairs in the state.

Jump to

Keyboard shortcuts

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