• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang shim.ChaincodeStubInterface类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStubInterface的典型用法代码示例。如果您正苦于以下问题:Golang ChaincodeStubInterface类的具体用法?Golang ChaincodeStubInterface怎么用?Golang ChaincodeStubInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了ChaincodeStubInterface类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: findMarble4Trade

// ============================================================================================================================
// findMarble4Trade - look for a matching marble that this user owns and return it
// ============================================================================================================================
func findMarble4Trade(stub shim.ChaincodeStubInterface, user string, color string, size int) (m Marble, err error) {
	var fail Marble
	fmt.Println("- start find marble 4 trade")
	fmt.Println("looking for " + user + ", " + color + ", " + strconv.Itoa(size))

	//get the marble index
	marblesAsBytes, err := stub.GetState(marbleIndexStr)
	if err != nil {
		return fail, errors.New("Failed to get marble index")
	}
	var marbleIndex []string
	json.Unmarshal(marblesAsBytes, &marbleIndex) //un stringify it aka JSON.parse()

	for i := range marbleIndex { //iter through all the marbles
		//fmt.Println("looking @ marble name: " + marbleIndex[i]);

		marbleAsBytes, err := stub.GetState(marbleIndex[i]) //grab this marble
		if err != nil {
			return fail, errors.New("Failed to get marble")
		}
		res := Marble{}
		json.Unmarshal(marbleAsBytes, &res) //un stringify it aka JSON.parse()
		//fmt.Println("looking @ " + res.User + ", " + res.Color + ", " + strconv.Itoa(res.Size));

		//check for user && color && size
		if strings.ToLower(res.User) == strings.ToLower(user) && strings.ToLower(res.Color) == strings.ToLower(color) && res.Size == size {
			fmt.Println("found a marble: " + res.Name)
			fmt.Println("! end find marble 4 trade")
			return res, nil
		}
	}

	fmt.Println("- end find marble 4 trade - error")
	return fail, errors.New("Did not find marble to use in this trade")
}
开发者ID:IBM-Blockchain,项目名称:marbles,代码行数:38,代码来源:marbles_chaincode.go


示例2: query

func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
	var event string // Event entity
	var err error

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

	event = args[0]

	// Get the state from the ledger
	eventValbytes, err := stub.GetState(event)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get state for " + event + "\"}"
		return nil, errors.New(jsonResp)
	}

	if eventValbytes == nil {
		jsonResp := "{\"Error\":\"Nil value for " + event + "\"}"
		return nil, errors.New(jsonResp)
	}

	jsonResp := "{\"Name\":\"" + event + "\",\"Amount\":\"" + string(eventValbytes) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return []byte(jsonResp), nil
}
开发者ID:hyperledger,项目名称:fabric,代码行数:26,代码来源:chaincode_example04.go


示例3: query

func (t *AssetManagementChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
	var err error

	if len(args) != 1 {
		return nil, errors.New("Incorrect number of arguments. Expecting name of an asset to query")
	}

	// Who is the owner of the asset?
	asset := args[0]

	fmt.Printf("ASSET: %v", string(asset))

	var columns []shim.Column
	col1 := shim.Column{Value: &shim.Column_String_{String_: asset}}
	columns = append(columns, col1)

	row, err := stub.GetRow("AssetsOwnership", columns)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed retrieving asset " + asset + ". Error " + err.Error() + ". \"}"
		return nil, errors.New(jsonResp)
	}

	if len(row.Columns) == 0 {
		jsonResp := "{\"Error\":\"Failed retrieving owner for " + asset + ". \"}"
		return nil, errors.New(jsonResp)
	}

	jsonResp := "{\"Owner\":\"" + string(row.Columns[1].GetBytes()) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)

	return row.Columns[1].GetBytes(), nil
}
开发者ID:hyperledger,项目名称:fabric,代码行数:32,代码来源:asset_management_with_roles.go


示例4: createTableFour

func createTableFour(stub shim.ChaincodeStubInterface) error {
	var columnDefsTableFour []*shim.ColumnDefinition
	columnOneTableFourDef := shim.ColumnDefinition{Name: "colOneTableFour",
		Type: shim.ColumnDefinition_STRING, Key: true}
	columnDefsTableFour = append(columnDefsTableFour, &columnOneTableFourDef)
	return stub.CreateTable("tableFour", columnDefsTableFour)
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:7,代码来源:table.go


示例5: query

func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
	var A string // Entity
	var Aval int // Asset holding
	var err error

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

	A = args[0]
	Aval, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expecting integer value for asset holding")
	}
	fmt.Printf("Aval = %d\n", Aval)

	// Write the state to the ledger - this put is illegal within Run
	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
	if err != nil {
		jsonResp := "{\"Error\":\"Cannot put state within chaincode query\"}"
		return nil, errors.New(jsonResp)
	}

	fmt.Printf("Something is wrong. This query should not have succeeded")
	return nil, nil
}
开发者ID:hyperledger,项目名称:fabric,代码行数:26,代码来源:chaincode_example03.go


示例6: Invoke

// Invoke invokes another chaincode - chaincode_example02, upon receipt of an event and changes event state
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
	var event string    // Event entity
	var eventVal string // State of event
	var err error
	var amount int

	event = args[0]
	eventVal = args[1]
	chainCodeToCall := args[2] // Get the chaincode to call from the ledger

	if eventVal == "middleSchool" {
		amount = 2000
	} else {
		amount = 5000
	}

	// 转账操作
	invokeArgs := util.ToChaincodeArgs("transferAcc", args[3], args[4], strconv.Itoa(amount))
	response, err := stub.InvokeChaincode(chainCodeToCall, 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(args[1]))
	if err != nil {
		return nil, err
	}

	return nil, nil
}
开发者ID:blzhang,项目名称:zhangbllib,代码行数:36,代码来源:donation.go


示例7: createTableThree

func createTableThree(stub shim.ChaincodeStubInterface) error {
	var columnDefsTableThree []*shim.ColumnDefinition
	columnOneTableThreeDef := shim.ColumnDefinition{Name: "colOneTableThree",
		Type: shim.ColumnDefinition_STRING, Key: true}
	columnTwoTableThreeDef := shim.ColumnDefinition{Name: "colTwoTableThree",
		Type: shim.ColumnDefinition_INT32, Key: false}
	columnThreeTableThreeDef := shim.ColumnDefinition{Name: "colThreeTableThree",
		Type: shim.ColumnDefinition_INT64, Key: false}
	columnFourTableThreeDef := shim.ColumnDefinition{Name: "colFourTableFour",
		Type: shim.ColumnDefinition_UINT32, Key: false}
	columnFiveTableThreeDef := shim.ColumnDefinition{Name: "colFourTableFive",
		Type: shim.ColumnDefinition_UINT64, Key: false}
	columnSixTableThreeDef := shim.ColumnDefinition{Name: "colFourTableSix",
		Type: shim.ColumnDefinition_BYTES, Key: false}
	columnSevenTableThreeDef := shim.ColumnDefinition{Name: "colFourTableSeven",
		Type: shim.ColumnDefinition_BOOL, Key: false}
	columnDefsTableThree = append(columnDefsTableThree, &columnOneTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnTwoTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnThreeTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnFourTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnFiveTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnSixTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnSevenTableThreeDef)
	return stub.CreateTable("tableThree", columnDefsTableThree)
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:25,代码来源:table.go


示例8: Query

// Query callback representing the query of a chaincode
func (t *Attributes2State) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
	if function != "read" {
		return nil, errors.New("Invalid query function name. Expecting \"read\"")
	}
	var attributeName string // Name of the attributeName to query.
	var err error

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

	attributeName = args[0]
	fmt.Printf("Reading attribute %v", attributeName)

	// Get the state from the ledger
	Avalbytes, err := stub.GetState(attributeName)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get state for " + attributeName + "\"}"
		fmt.Printf("Query Response:%s\n", jsonResp)
		return nil, errors.New(jsonResp)
	}

	if Avalbytes == nil {
		jsonResp := "{\"Error\":\"Nil amount for " + attributeName + "\"}"
		fmt.Printf("Query Response:%s\n", jsonResp)
		return nil, errors.New(jsonResp)
	}

	jsonResp := "{\"Name\":\"" + attributeName + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return []byte(jsonResp), nil
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:33,代码来源:attributes_to_state.go


示例9: MakePayment

// Transaction makes payment of X units from A to B
func (t *ChaincodeExample) MakePayment(stub shim.ChaincodeStubInterface, param *example02.PaymentParams) error {

	var err error

	// Get the state from the ledger
	src, err := t.GetState(stub, param.PartySrc)
	if err != nil {
		return err
	}

	dst, err := t.GetState(stub, param.PartyDst)
	if err != nil {
		return err
	}

	// Perform the execution
	X := int(param.Amount)
	src = src - X
	dst = dst + X
	fmt.Printf("Aval = %d, Bval = %d\n", src, dst)

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

	err = stub.PutState(param.PartyDst, []byte(strconv.Itoa(dst)))
	if err != nil {
		return err
	}

	return nil
}
开发者ID:hyperledger,项目名称:fabric,代码行数:35,代码来源:chaincode_example02.go


示例10: set_user

// ============================================================================================================================
// Set User Permission on Marble
// ============================================================================================================================
func (t *SimpleChaincode) set_user(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
	var err error

	//   0       1
	// "name", "bob"
	if len(args) < 2 {
		return nil, errors.New("Incorrect number of arguments. Expecting 2")
	}

	fmt.Println("- start set user")
	fmt.Println(args[0] + " - " + args[1])
	marbleAsBytes, err := stub.GetState(args[0])
	if err != nil {
		return nil, errors.New("Failed to get thing")
	}
	res := Marble{}
	json.Unmarshal(marbleAsBytes, &res) //un stringify it aka JSON.parse()
	res.User = args[1]                  //change the user

	jsonAsBytes, _ := json.Marshal(res)
	err = stub.PutState(args[0], jsonAsBytes) //rewrite the marble with id as key
	if err != nil {
		return nil, err
	}

	fmt.Println("- end set user")
	return nil, nil
}
开发者ID:IBM-Blockchain,项目名称:marbles,代码行数:31,代码来源:marbles_chaincode.go


示例11: Init

func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
	var A, B string    // Entities
	var Aval, Bval int // Asset holdings
	var err error

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

	// Initialize the chaincode
	A = args[0]
	Aval, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expecting integer value for asset holding")
	}
	B = args[2]
	Bval, err = strconv.Atoi(args[3])
	if err != nil {
		return nil, errors.New("Expecting integer value for asset holding")
	}
	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

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

	err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
	if err != nil {
		return nil, err
	}

	return nil, nil
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:35,代码来源:chaincode_example02.go


示例12: Init

//Init func will return error if function has string "error" anywhere
func (p *PassthruChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, _ := stub.GetFunctionAndParameters()
	if strings.Index(function, "error") >= 0 {
		return nil, errors.New(function)
	}
	return []byte(function), nil
}
开发者ID:hyperledger,项目名称:fabric,代码行数:8,代码来源:passthru.go


示例13: Query

// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
	if function != "query" {
		return nil, errors.New("Invalid query function name. Expecting \"query\"")
	}
	var A string // Entities
	var err error

	if len(args) != 1 {
		return nil, errors.New("Incorrect number of arguments. Expecting name of the person to query")
	}

	A = args[0]

	// Get the state from the ledger
	Avalbytes, err := stub.GetState(A)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
		return nil, errors.New(jsonResp)
	}

	if Avalbytes == nil {
		jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
		return nil, errors.New(jsonResp)
	}

	jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return Avalbytes, nil
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:30,代码来源:chaincode_example02.go


示例14: Init

// Init takes two arguments, a string and int. The string will be a key with
// the int as a value.
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
	var sum string // Sum of asset holdings across accounts. Initially 0
	var sumVal int // Sum of holdings
	var err error
	_, args := stub.GetFunctionAndParameters()
	if len(args) != 2 {
		return nil, errors.New("Incorrect number of arguments. Expecting 2")
	}

	// Initialize the chaincode
	sum = args[0]
	sumVal, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expecting integer value for sum")
	}
	fmt.Printf("sumVal = %d\n", sumVal)

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

	return nil, nil
}
开发者ID:hyperledger,项目名称:fabric,代码行数:27,代码来源:chaincode_example05.go


示例15: Invoke

// Invoke gets the supplied key and if it exists, updates the key with the newly
// supplied value.
func (t *SampleSysCC) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
	var key, val string // Entities

	if len(args) != 2 {
		return nil, errors.New("need 2 args (key and a value)")
	}

	// Initialize the chaincode
	key = args[0]
	val = args[1]

	_, err := stub.GetState(key)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get val for " + key + "\"}"
		return nil, errors.New(jsonResp)
	}

	// Write the state to the ledger
	err = stub.PutState(key, []byte(val))
	if err != nil {
		return nil, err
	}

	return nil, nil
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:27,代码来源:samplesyscc.go


示例16: Query

// Query callback representing the query of a chaincode
func (t *SampleSysCC) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
	if function != "getval" {
		return nil, errors.New("Invalid query function name. Expecting \"getval\"")
	}
	var key string // Entities
	var err error

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

	key = args[0]

	// Get the state from the ledger
	valbytes, err := stub.GetState(key)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get state for " + key + "\"}"
		return nil, errors.New(jsonResp)
	}

	if valbytes == nil {
		jsonResp := "{\"Error\":\"Nil val for " + key + "\"}"
		return nil, errors.New(jsonResp)
	}

	return valbytes, nil
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:28,代码来源:samplesyscc.go


示例17: Init

// Init takes a string and int. These are stored as a key/value pair in the state
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
	var A string // Entity
	var Aval int // Asset holding
	var err error
	_, args := stub.GetFunctionAndParameters()
	if len(args) != 2 {
		return nil, errors.New("Incorrect number of arguments. Expecting 2")
	}

	// Initialize the chaincode
	A = args[0]
	Aval, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expecting integer value for asset holding")
	}
	fmt.Printf("Aval = %d\n", Aval)

	// Write the state to the ledger - this put is legal within Run
	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
	if err != nil {
		return nil, err
	}

	return nil, nil
}
开发者ID:hyperledger,项目名称:fabric,代码行数:26,代码来源:chaincode_example03.go


示例18: Query

// Query callback representing the query of a chaincode
// Supported functions are the following:
// "query(asset)": returns the owner of the asset.
// Anyone can invoke this function.
func (t *AssetManagementChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, args := stub.GetFunctionAndParameters()
	myLogger.Debugf("Query [%s]", function)

	if function != "query" {
		return nil, errors.New("Invalid query function name. Expecting 'query' but found '" + function + "'")
	}

	var err error

	if len(args) != 1 {
		myLogger.Debug("Incorrect number of arguments. Expecting name of an asset to query")
		return nil, errors.New("Incorrect number of arguments. Expecting name of an asset to query")
	}

	// Who is the owner of the asset?
	asset := args[0]

	myLogger.Debugf("Arg [%s]", string(asset))

	var columns []shim.Column
	col1 := shim.Column{Value: &shim.Column_String_{String_: asset}}
	columns = append(columns, col1)

	row, err := stub.GetRow("AssetsOwnership", columns)
	if err != nil {
		myLogger.Debugf("Failed retriving asset [%s]: [%s]", string(asset), err)
		return nil, fmt.Errorf("Failed retriving asset [%s]: [%s]", string(asset), err)
	}

	myLogger.Debugf("Query done [% x]", row.Columns[1].GetBytes())

	return row.Columns[1].GetBytes(), nil
}
开发者ID:hyperledger,项目名称:fabric,代码行数:38,代码来源:asset_management.go


示例19: Invoke

// Invoke has two functions
// put - takes two arguements, a key and value, and stores them in the state
// remove - takes one argument, a key, and removes if from the state
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {

	switch function {
	case "put":
		if len(args) < 2 {
			return nil, errors.New("put operation must include two arguments, a key and value")
		}
		key := args[0]
		value := args[1]

		err := stub.PutState(key, []byte(value))
		if err != nil {
			fmt.Printf("Error putting state %s", err)
			return nil, fmt.Errorf("put operation failed. Error updating state: %s", err)
		}
		return nil, nil

	case "remove":
		if len(args) < 1 {
			return nil, errors.New("remove operation must include one argument, a key")
		}
		key := args[0]

		err := stub.DelState(key)
		if err != nil {
			return nil, fmt.Errorf("remove operation failed. Error updating state: %s", err)
		}
		return nil, nil

	default:
		return nil, errors.New("Unsupported operation")
	}
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:36,代码来源:map.go


示例20: Query

// Query function
func (t *EventSender) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
	b, err := stub.GetState("noevents")
	if err != nil {
		return nil, errors.New("Failed to get state")
	}
	jsonResp := "{\"NoEvents\":\"" + string(b) + "\"}"
	return []byte(jsonResp), nil
}
开发者ID:yoshiharay,项目名称:fabric,代码行数:9,代码来源:eventsender.go



注:本文中的github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStubInterface类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang shim.MockStub类代码示例发布时间:2022-05-28
下一篇:
Golang shim.ChaincodeStub类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap