github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/examples/chaincode/go/utxo/chaincode.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/base64"
    21  	"errors"
    22  	"fmt"
    23  
    24  	"github.com/hyperledger/fabric/core/chaincode/shim"
    25  	"github.com/hyperledger/fabric/examples/chaincode/go/utxo/util"
    26  )
    27  
    28  // The UTXO example chaincode contains a single invocation function named execute. This function accepts BASE64
    29  // encoded transactions from the Bitcoin network. This chaincode will parse the transactions and pass the transaction
    30  // components to the Bitcoin libconsensus C library for script verification. A table of UTXOs is maintained to ensure
    31  // each transaction is valid.
    32  // Documentation can be found at
    33  // https://github.com/hyperledger/fabric/blob/master/examples/chaincode/go/utxo/README.md
    34  
    35  // SimpleChaincode example simple Chaincode implementation
    36  type SimpleChaincode struct {
    37  }
    38  
    39  // Init does nothing in the UTXO chaincode
    40  func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    41  	return nil, nil
    42  }
    43  
    44  // Invoke callback representing the invocation of a chaincode
    45  func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    46  	function, args := stub.GetFunctionAndParameters()
    47  	switch function {
    48  
    49  	case "execute":
    50  
    51  		if len(args) < 1 {
    52  			return nil, errors.New("execute operation must include single argument, the base64 encoded form of a bitcoin transaction")
    53  		}
    54  		txDataBase64 := args[0]
    55  		txData, err := base64.StdEncoding.DecodeString(txDataBase64)
    56  		if err != nil {
    57  			return nil, fmt.Errorf("Error decoding TX as base64:  %s", err)
    58  		}
    59  
    60  		utxo := util.MakeUTXO(MakeChaincodeStore(stub))
    61  		execResult, err := utxo.Execute(txData)
    62  		if err != nil {
    63  			return nil, fmt.Errorf("Error executing TX:  %s", err)
    64  		}
    65  
    66  		fmt.Printf("\nExecResult: Coinbase: %t, SumInputs %d, SumOutputs %d\n\n", execResult.IsCoinbase, execResult.SumPriorOutputs, execResult.SumCurrentOutputs)
    67  
    68  		if execResult.IsCoinbase == false {
    69  			if execResult.SumCurrentOutputs > execResult.SumPriorOutputs {
    70  				return nil, fmt.Errorf("sumOfCurrentOutputs > sumOfPriorOutputs: sumOfCurrentOutputs = %d, sumOfPriorOutputs = %d", execResult.SumCurrentOutputs, execResult.SumPriorOutputs)
    71  			}
    72  		}
    73  
    74  		return nil, nil
    75  
    76  	case "getTran":
    77  
    78  		if len(args) < 1 {
    79  			return nil, errors.New("queryBTC operation must include single argument, the TX hash hex")
    80  		}
    81  
    82  		utxo := util.MakeUTXO(MakeChaincodeStore(stub))
    83  		tx, err := utxo.Query(args[0])
    84  		if err != nil {
    85  			return nil, fmt.Errorf("Error querying for transaction:  %s", err)
    86  		}
    87  		if tx == nil {
    88  			var data []byte
    89  			return data, nil
    90  		}
    91  		return tx, nil
    92  
    93  	default:
    94  		return nil, errors.New("Unsupported operation")
    95  	}
    96  
    97  }
    98  
    99  func main() {
   100  	err := shim.Start(new(SimpleChaincode))
   101  	if err != nil {
   102  		fmt.Printf("Error starting chaincode: %s", err)
   103  	}
   104  }