1. 程式人生 > >彈珠資產管理鏈碼實現

彈珠資產管理鏈碼實現

package main

import (
	"github.com/hyperledger/fabric/core/chaincode/shim"
	"github.com/hyperledger/fabric/protos/peer"
	"strconv"
	"encoding/json"
	"bytes"
	"fmt"
	"time"
)

type MarbleChaincode struct {
}

type Marble struct {
	Name string
	Size int
	Owner string
}

func (m *MarbleChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {
	return shim.Success(nil)
}

func (m *MarbleChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
	fn,args:=stub.GetFunctionAndParameters()
	if fn=="initMarble" {
		return m.initMarble(stub,args)
	} else if fn=="readMarble" {
		return m.readMarble(stub,args)
	} else if fn=="transferOwner" {
		return m.transferOwner(stub,args)
	} else if fn=="deleteMarble" {
		return m.deleteMarble(stub,args)
	} else if fn=="queryMarbleByRange" {
		return m.queryMarbleByRange(stub,args)
	} else if fn=="queryMarbleByOwner" {
		return m.queryMarbleByOwner(stub,args)
	} else if fn=="queryMarbleHistory" {
		return m.queryMarbleHistory(stub,args)
	}
	return shim.Error("")
}

func (m *MarbleChaincode) initMarble(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	name:=args[0]
	mb,err:=stub.GetState(name)
	if err!=nil {
		return shim.Error(err.Error())
	}
	if mb!=nil {
		return shim.Error("marble exist")
	}
	size,err:=strconv.Atoi(args[1])
	if err!=nil {
		return shim.Error(err.Error())
	}
	owner:=args[2]
	marble:=Marble{name,size,owner}
	mb,err=json.Marshal(marble)
	if err!=nil {
		return shim.Error(err.Error())
	}
	err=stub.PutState(name,mb)
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(nil)
}

func (m *MarbleChaincode) readMarble(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	name:=args[0]
	mb,err:=stub.GetState(name)
	if err!=nil {
		return shim.Error(err.Error())
	}
	if mb==nil {
		return shim.Error("marble not exist")
	}
	return shim.Success(mb)
}

func (m *MarbleChaincode) transferOwner(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	name:=args[0]
	mb,err:=stub.GetState(name)
	if err!=nil {
		return shim.Error(err.Error())
	}
	if mb==nil {
		return shim.Error("marble not exist")
	}
	marble:=Marble{}
	err=json.Unmarshal(mb,&marble)
	if err!=nil {
		return shim.Error(err.Error())
	}
	newowner:=args[1]
	marble.Owner=newowner
	mb,err=json.Marshal(marble)
	if err!=nil {
		return shim.Error(err.Error())
	}
	err=stub.PutState(name,mb)
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(nil)
}

func (m *MarbleChaincode) deleteMarble(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	name:=args[0]
	mb,err:=stub.GetState(name)
	if err!=nil {
		return shim.Error(err.Error())
	}
	if mb==nil {
		return shim.Error("marble not exist")
	}
	err=stub.DelState(name)
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(nil)
}

func (m *MarbleChaincode) queryMarbleByRange(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	startKey:=args[0]
	endKey:=args[1]
	iter,err:=stub.GetStateByRange(startKey,endKey)
	if err!=nil {
		return shim.Error(err.Error())
	}
	defer iter.Close()
	var buffer bytes.Buffer
	buffer.WriteString("[")
	n:=false
	for iter.HasNext() {
		item,err:=iter.Next()
		if err!=nil {
			return shim.Error(err.Error())
		}
		if n {
			buffer.WriteString(",")
		}
		n=true
		buffer.WriteString(string(item.Value))
	}
	buffer.WriteString("]")
	return shim.Success(buffer.Bytes())
}

func (m *MarbleChaincode) queryMarbleByOwner(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	name:=args[0]
	iter,err:=stub.GetQueryResult(fmt.Sprintf("{\"selector\":{\"Owner\":\"%s\"}}",name))
	if err!=nil {
		return shim.Error(err.Error())
	}
	defer iter.Close()
	var buffer bytes.Buffer
	buffer.WriteString("[")
	n:=false
	for iter.HasNext() {
		item,err:=iter.Next()
		if err!=nil {
			return shim.Error(err.Error())
		}
		if n {
			buffer.WriteString(",")
		}
		n=true
		buffer.WriteString(string(item.Value))
	}
	buffer.WriteString("]")
	return shim.Success(buffer.Bytes())
}

func (m *MarbleChaincode) queryMarbleHistory(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	name:=args[0]
	iter,err:=stub.GetHistoryForKey(name)
	if err!=nil {
		return shim.Error(err.Error())
	}
	defer iter.Close()
	var buffer bytes.Buffer
	buffer.WriteString("[")
	n:=false
	for iter.HasNext() {
		item,err:=iter.Next()
		if err!=nil {
			return shim.Error(err.Error())
		}
		defer iter.Close()
		if n {
			buffer.WriteString(",")
		}
		n=true
		buffer.WriteString("{TxId:\""+item.TxId+",")
		buffer.WriteString("Time:"+time.Unix(item.Timestamp.Seconds,int64(item.Timestamp.Nanos)).String()+",")
		buffer.WriteString("Value:"+string(item.Value)+",")
		buffer.WriteString("IsDelete:"+strconv.FormatBool(item.IsDelete)+"}")
	}
	buffer.WriteString("]")
	return shim.Success(buffer.Bytes())
}

func main()  {
	shim.Start(new(MarbleChaincode))
}