contract

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(contract Base, stub shim.ChaincodeStubInterface, rawCfg []byte) error

Configure sets up the contract configuration for the given ContractConfigurable instance.

This function attempts to perform the following steps: 1. If the ContractConfigurable instance implements the StubGetSetter interface, it sets the ChaincodeStub. 2. If the configuration bytes (cfgBytes) are nil, the function returns nil immediately. 3. It parses the configuration bytes into a ContractConfig instance. 4. If the ContractConfig instance has nil options, it initializes them. 5. It applies the parsed ContractConfig to the ContractConfigurable instance. 6. If the ContractConfigurable instance implements the TokenConfigurable interface, it parses and applies the TokenConfig. 7. If the ContractConfigurable instance implements the ExternalConfigurable interface, it applies the external configuration directly.

Parameters: - cc: The ContractConfigurable instance to be configured. - stub: The ChaincodeStubInterface instance used for the contract. - cfgBytes: A byte slice containing the configuration data.

Returns: - error: Returns an error if any step of the configuration process fails.

Example:

err := contract.Configure(myContract, myStub, configBytes)
if err != nil {
    log.Fatalf("Failed to configure contract: %v", err)
}

func ValidateConfig

func ValidateConfig(contract Base, rawCfg []byte) error

ValidateConfig validates the contract configuration for the given ContractConfigurable instance.

This function attempts to perform the following steps: 1. If the contract implements the Configurator interface, it validates the contract configuration. 2. If the contract implements the TokenConfigurator interface, it validates the token configuration. 3. If the contract implements the ExternalConfigurator interface, it validates the external configuration.

Parameters: - contract: The Base instance to be validated. - rawCfg: A byte slice containing the configuration data.

Returns: - error: Returns an error if any step of the validation process fails.

Example:

err := contract.ValidateConfig(myContract, configBytes)
if err != nil {
    log.Fatalf("Failed to validate contract: %v", err)
}

Types

type Base

type Base interface {
	ID() string // ID retrieves the unique identifier for the contract.

	Configurator
	StubGetSetter
}

Base is the minimal interface required for a contract to execute within the system.

type ConfigMapper

type ConfigMapper interface {
	// MapConfig maps the provided arguments to a proto.Config instance.
	MapConfig(args []string) (*proto.Config, error)
}

ConfigMapper defines a method for mapping Init arguments to a Config instance.

type ConfigMapperFunc

type ConfigMapperFunc func(args []string) (*proto.Config, error)

ConfigMapperFunc is a function type that implements the ConfigMapper interface.

func (ConfigMapperFunc) MapConfig

func (c ConfigMapperFunc) MapConfig(args []string) (*proto.Config, error)

MapConfig calls the underlying function to map the provided arguments to a proto.Config instance.

type Configurator

type Configurator interface {
	// ValidateConfig validates the provided contract configuration data.
	ValidateConfig(config []byte) error

	// ApplyContractConfig applies the provided contract configuration.
	ApplyContractConfig(config *proto.ContractConfig) error

	// ContractConfig retrieves the current contract configuration.
	ContractConfig() *proto.ContractConfig
}

Configurator defines methods for validating, applying, and retrieving contract configuration.

type ExternalConfigurator

type ExternalConfigurator interface {
	// ValidateExtConfig validates the provided external configuration data.
	ValidateExtConfig(cfgBytes []byte) error

	// ApplyExtConfig applies the provided external configuration to the chaincode.
	ApplyExtConfig(cfgBytes []byte) error
}

ExternalConfigurator defines methods for validating and applying external configuration.

type Function

type Function = string

Function represents the name of a chaincode function.

type Method

type Method struct {
	Type          MethodType // The type of the method.
	ChaincodeFunc Function   // The name of the chaincode function being called.
	MethodName    string     // The actual method name to be invoked.
	RequiresAuth  bool       // Indicates if the method requires authentication.
	ReturnsError  bool       // Indicates if the method returns an error.
	NumArgs       int        // Number of arguments the method takes (excluding the receiver).
	NumReturns    int        // Number of return values the method has.
}

Method represents an endpoint of a contract.

type MethodType

type MethodType int

MethodType represents the type of a method in the contract.

const (
	MethodTypeTransaction MethodType = iota // Tx-prefixed transaction when using reflectx.Router.
	MethodTypeInvoke                        // NBTx-prefixed transaction when using reflectx.Router.
	MethodTypeQuery                         // Query-prefixed transaction when using reflectx.Router.
)

Constants representing the different types of methods.

type Router

type Router interface {
	// Check validates the provided arguments for the specified method.
	// It returns an error if the validation fails.
	Check(method string, args ...string) error

	// Invoke calls the specified method with the provided arguments.
	// It returns a slice of return values and an error if the invocation fails.
	Invoke(method string, args ...string) ([]any, error)

	// Methods retrieves a map of all available methods, keyed by their chaincode function names.
	Methods() map[Function]Method
}

Router defines the interface for managing contract methods and routing calls.

type StubGetSetter

type StubGetSetter interface {
	// GetStub retrieves the current ChaincodeStubInterface.
	GetStub() shim.ChaincodeStubInterface

	// SetStub sets the provided ChaincodeStubInterface.
	SetStub(shim.ChaincodeStubInterface)
}

StubGetSetter defines methods for getting and setting the ChaincodeStubInterface.

type TokenConfigurator

type TokenConfigurator interface {
	// ValidateTokenConfig validates the provided token configuration data.
	ValidateTokenConfig(config []byte) error

	// ApplyTokenConfig applies the provided token configuration.
	ApplyTokenConfig(config *proto.TokenConfig) error

	// TokenConfig retrieves the current token configuration.
	TokenConfig() *proto.TokenConfig
}

TokenConfigurator defines methods for validating, applying, and retrieving token configuration.

Jump to

Keyboard shortcuts

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