Example #1
0
//==============================================================================================================================
//	 create_log - Invokes the function of event_code chaincode with the name 'chaincodeName' to log an
//					event.
//==============================================================================================================================
func (t *Chaincode) create_log(stub *shim.ChaincodeStub, args []string) ([]byte, error) {

	chaincode_name := "398f2e9a632d17bfc54f259f581818448a1f7b654901ce1648a66c36f0003b2ece440382e585d6b7dac8ee010f2b4fae29bda3135cf76d9c3895cacec1ffbeb4"
	chaincode_function := "create_log"
	chaincode_arguments := args

	_, err := stub.InvokeChaincode(chaincode_name, chaincode_function, chaincode_arguments)

	if err != nil {
		return nil, errors.New("Failed to invoke vehicle_log_code")
	}

	return nil, nil
}
Example #2
0
// Transaction invokes another chaincode and changes event state
func (t *SimpleChaincode) invoke(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var event string // Event entity
	var eventVal int // State of event
	var err error

	if len(args) != 2 {
		return nil, errors.New("Incorrect number of arguments. Expecting 2")
	}

	event = args[0]
	eventVal, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expected integer value for event state change")
	}

	if eventVal != 1 {
		fmt.Printf("Unexpected event. Doing nothing\n")
		return nil, nil
	}

	// Get the chaincode to call from the ledger
	chainCodeToCall, err := t.getChaincodeToCall(stub)
	if err != nil {
		return nil, err
	}

	f := "invoke"
	invokeArgs := []string{"a", "b", "10"}
	response, err := stub.InvokeChaincode(chainCodeToCall, f, invokeArgs)
	if err != nil {
		errStr := fmt.Sprintf("Failed to invoke chaincode. Got error: %s", err.Error())
		fmt.Printf(errStr)
		return nil, errors.New(errStr)
	}

	fmt.Printf("Invoke chaincode successful. Got response %s", string(response))

	// Write the event state back to the ledger
	err = stub.PutState(event, []byte(strconv.Itoa(eventVal)))
	if err != nil {
		return nil, err
	}

	return nil, nil
}
// Transaction invokes another chaincode and changes event state
func (t *SimpleChaincode) invoke(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var event string // Event entity
	var eventVal int // State of event
	var err error

	if len(args) != 2 {
		return nil, errors.New("Incorrect number of arguments. Expecting 2")
	}

	event = args[0]
	eventVal, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expected integer value for event state change")
	}

	if eventVal != 1 {
		fmt.Printf("Unexpected event. Doing nothing\n")
		return nil, nil
	}

	// Invoke chaincode_example02 - assuming its URL and vesion are known
	// Better coding will pass the chaincodeID's URL and version as arguments to this function (or to another function that stores it in the ledger state)
	chaincodeUrl := "github.com/openblockchain/obc-peer/openchain/example/chaincode/chaincode_example02"
	chaincodeVersion := "0.0.1"
	f := "invoke"
	invokeArgs := []string{"a", "b", "10"}
	response, err := stub.InvokeChaincode(chaincodeUrl, chaincodeVersion, f, invokeArgs)
	if err != nil {
		errStr := fmt.Sprintf("Failed to invoke chaincode. Got error: %s", err.Error())
		fmt.Printf(errStr)
		return nil, errors.New(errStr)
	}

	fmt.Printf("Invoke chaincode successful. Got response %s", string(response))

	// Write the event state back to the ledger
	err = stub.PutState(event, []byte(strconv.Itoa(eventVal)))
	if err != nil {
		return nil, err
	}

	return nil, nil
}