shim

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chaincode

type Chaincode interface {
	// Invoke is called for every transactions.
	Invoke(stub ChaincodeStubInterface, function string, args [][]byte, readonly bool) ([]byte, error)
}

Chaincode interface purposed to be implemented by all chaincodes. The fabric runs the transactions by calling these functions as specified. We use the designation mixed with fabric 0.6 and 1.x (this interface is not so important like it was in the real fabric implement, just to provide a suitable interface in some tools)

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 transaction ID
	GetTxID() string

	// 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.
	GetTxTime() (time.Time, error)

	// 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

	// RangeQueryState 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 and endKey, inclusive. The order in which keys are
	// returned by the iterator is random.
	RangeQueryState(startKey, endKey string) (StateRangeQueryIteratorInterface, error)

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

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

	// obtain the original chaincodestub interface for more implement-spec code
	GetRawStub() interface{}
}

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

	// store when is called from another chaincode
	InvokedCCName string

	// Extended plugin
	EventHandler func(string, []byte) error
	// 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, Query or Invoke.

func NewMockStub

func NewMockStub(name string, cc Chaincode) *MockStub

Constructor to initialise the internal State map

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) GetBinding

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

Not implemented

func (*MockStub) GetCallerAttribute

func (stub *MockStub) GetCallerAttribute(attributeName string) ([]byte, error)

Not implemented

func (*MockStub) GetCallerCertificate

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

Not implemented

func (*MockStub) GetCallingChaincodeName

func (stub *MockStub) GetCallingChaincodeName() string

func (*MockStub) GetOriginalChaincodeName

func (stub *MockStub) GetOriginalChaincodeName() string

func (*MockStub) GetRawStub

func (stub *MockStub) GetRawStub() interface{}

func (*MockStub) GetState

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

GetState retrieves the value for a given key from the ledger

func (*MockStub) GetStringArgs

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

func (*MockStub) GetTxID

func (stub *MockStub) GetTxID() string

func (*MockStub) GetTxTime

func (stub *MockStub) GetTxTime() (time.Time, error)

Not implemented

func (*MockStub) InvokeChaincode

func (stub *MockStub) InvokeChaincode(chaincodeName string, function string, args [][]byte) ([]byte, error)

Invokes a peered chaincode. E.g. stub1.InvokeChaincode("stub2Hash", funcArgs) 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, function string, args [][]byte) ([]byte, error)

Initialise this chaincode, also starts and ends a transaction.

func (*MockStub) MockInvoke

func (stub *MockStub) MockInvoke(uuid string, function string, args [][]byte) ([]byte, error)

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) MockQuery

func (stub *MockStub) MockQuery(function string, args [][]byte) ([]byte, error)

Query this chaincode

func (*MockStub) MockTransactionEnd

func (stub *MockStub) MockTransactionEnd(uuid string, e error)

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) QueryChaincode

func (stub *MockStub) QueryChaincode(chaincodeName string, function string, args [][]byte) ([]byte, error)

func (*MockStub) RangeQueryState

func (stub *MockStub) RangeQueryState(startKey, endKey string) (StateRangeQueryIteratorInterface, error)

func (*MockStub) SetEvent

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

type StateRangeQueryIteratorInterface

type StateRangeQueryIteratorInterface 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
}

StateRangeQueryIteratorInterface allows a chaincode to iterate over a range 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