trace_platform.go 3.12 KB
package main

import (
    "fmt"
    "github.com/zhigui/zigledger/core/chaincode/shim"
    pb "github.com/zhigui/zigledger/protos/peer"
    "math/big"
)

const (
    Transfer       = "transfer"
    AddUser        = "addUser"
    GetAccountInfo = "getAccountInfo"
    SetAccountInfo = "setAccountInfo"
    Suicide        = "suicide"
)

type TraceChaincode struct{}

func main() {
    err := shim.Start(new(TraceChaincode))
    if err != nil {
        fmt.Printf("Error starting assetChaincode: %s", err)
    }
}

func (t *TraceChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    return shim.Success([]byte("Init success."))
}

func (t *TraceChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    function, args := stub.GetFunctionAndParameters()

    switch function {
    case Transfer:
        if len(args) != 3 {
            return shim.Error("Incorrect number of arguments. Expecting 3.")
        }
        return t.transfer(stub, args)
    case AddUser:
        if len(args) != 2 {
            return shim.Error("Incorrect number of arguments. Expecting 2.")
        }
    case GetAccountInfo:
        if len(args) != 1 {
            return shim.Error("Incorrect number of arguments. Expecting 1.")
        }
        return t.getAccountInfo(stub, args)
    case SetAccountInfo:
        if len(args) != 2 {
            return shim.Error("Incorrect number of arguments. Expecting 2.")
        }
        return t.setAccountInfo(stub, args)
    case Suicide:
        return t.suicide(stub)
    }
    return shim.Error("Invalid invoke function name.")
}

func (t *TraceChaincode) transfer(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    amountType := args[0]
    transferAmount := new(big.Int)
    transferAmount.SetString(args[1], 10)
    transferTo := args[2]
    if amountType == "ZIG" {
        //if err := stub.Transfer(transferTo, transferType, transferAmount); err != nil {
        //    return shim.Error("Transfer error" + err.Error())
        //}
        fmt.Println("transfer zig ", transferTo, transferAmount)
    } else {
        argsByte := make([][]byte, len(args))
        for k, v := range args {
            argsByte[k] = []byte(v)
        }
        stub.InvokeChaincode(amountType, argsByte, "")
        fmt.Println("call other contract ", string(argsByte[0]), string(argsByte[1]), string(argsByte[2]))
    }
    return shim.Success([]byte("success"))
}

func (t *TraceChaincode) setAccountInfo(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    address := args[0]
    traceInfo := args[1]
    if err := stub.PutState("TRACE::" + address, []byte(traceInfo)); err != nil {
        shim.Error("PutState error" + err.Error())
    }
    return shim.Success([]byte("success"))
}

func (t *TraceChaincode) getAccountInfo(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    address := args[0]
    traceInfo, err := stub.GetState("TRACE::" + address)
    if err != nil {
        shim.Error("GetState error:" + err.Error())
    }
    return shim.Success([]byte(traceInfo))

}

func (t *TraceChaincode) suicide(stub shim.ChaincodeStubInterface) pb.Response {
    return shim.Success([]byte("success"))
}