Skip to content

Instantly share code, notes, and snippets.

View aschmidt75's full-sized avatar

Andreas Schmidt aschmidt75

View GitHub Profile
func trackingDataPointRegistry() registry {
return registry{
typeStr: "TrackingDataPoint",
typeRT: reflect.TypeOf(&TrackingDataPoint{}),
}
}
type registry struct {
typeStr string
typeRT reflect.Type
}
type registryInterface interface {
// key creates a composite key from an id
key(stub shim.ChaincodeStubInterface, id string) (string, error)
// creates a new data item by marshaling into JSON. Returns
// ID of newly created item.
create(stub shim.ChaincodeStubInterface, item interface{}) (string, error)
// get retrieves an item by its ID
get(stub shim.ChaincodeStubInterface, id string) (string, interface{}, error)
cc := &PreciousCargoChaincode{
// all functions as InvocationHandlers
handlers: map[string]reflect.Type{
"submitShipment": reflect.TypeOf((*submitShipmentInvocation)(nil)).Elem(),
"getShipment": reflect.TypeOf((*getShipmentInvocation)(nil)).Elem(),
"registerIndividualParticipant": reflect.TypeOf((*registerIndividualParticipantInvocation)(nil)).Elem(),
"getIndividualParticipant": reflect.TypeOf((*getIndividualParticipantInvocation)(nil)).Elem(),
"registerShipmentCo": reflect.TypeOf((*registerShipmentCoInvocation)(nil)).Elem(),
},
}
type PreciousCargoChaincode struct {
// map function names to function implementation types
handlers map[string]reflect.Type
}
type submitShipmentInvocation struct {
// input argument
arg submitShipmentArg
// intermediates
shipperKey, fromKey, toKey string
submittedAtParsed time.Time
// result
res submitShipmentResult
func (cci *PreciousCargoChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, _ := stub.GetFunctionAndParameters()
if invType, found := cci.handlers[function]; found {
// from invType as reflect.Type, create a new object and
// cast its interface to InvocationHandler.
inv := reflect.New(invType).Interface().(InvocationHandler)
// let it check its input
if err := inv.checkParseArguments(stub); err != nil {
return shim.Error(err.Error())
}
type InvocationHandler interface {
checkParseArguments(stub shim.ChaincodeStubInterface) error
process(stub shim.ChaincodeStubInterface) error
getResponse(stub shim.ChaincodeStubInterface) interface{}
}
func (cci *PreciousCargoChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
if function == "registerParticipant" {
if len(args) != 1 {
return shim.Error("expecting JSON input as first param")
}
// (1) parse input
arg := struct {
Name string `json:"name"`
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
...
}