예제 #1
0
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {

	switch function {

	case "getTran":

		if len(args) < 1 {
			return nil, errors.New("queryBTC operation must include single argument, the TX hash hex")
		}

		utxo := util.MakeUTXO(MakeChaincodeStore(stub))
		tx, err := utxo.Query(args[0])
		if err != nil {
			return nil, fmt.Errorf("Error querying for transaction:  %s", err)
		}
		if tx == nil {
			var data []byte
			return data, nil
		}
		return tx, nil

	default:
		return nil, errors.New("Unsupported operation")
	}

}
예제 #2
0
// Invoke callback representing the invocation of a chaincode
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
	switch function {

	case "execute":

		if len(args) < 1 {
			return nil, errors.New("execute operation must include single argument, the base64 encoded form of a bitcoin transaction")
		}
		txDataBase64 := args[0]
		txData, err := base64.StdEncoding.DecodeString(txDataBase64)
		if err != nil {
			return nil, fmt.Errorf("Error decoding TX as base64:  %s", err)
		}

		utxo := util.MakeUTXO(MakeChaincodeStore(stub))
		execResult, err := utxo.Execute(txData)
		if err != nil {
			return nil, fmt.Errorf("Error executing TX:  %s", err)
		}

		fmt.Printf("\nExecResult: Coinbase: %t, SumInputs %d, SumOutputs %d\n\n", execResult.IsCoinbase, execResult.SumPriorOutputs, execResult.SumCurrentOutputs)

		if execResult.IsCoinbase == false {
			if execResult.SumCurrentOutputs > execResult.SumPriorOutputs {
				return nil, fmt.Errorf("sumOfCurrentOutputs > sumOfPriorOutputs: sumOfCurrentOutputs = %d, sumOfPriorOutputs = %d", execResult.SumCurrentOutputs, execResult.SumPriorOutputs)
			}
		}

		return nil, nil

	default:
		return nil, errors.New("Unsupported operation")
	}

}