當前位置: 首頁>>代碼示例>>Golang>>正文


Golang ChaincodeStubInterface.ReplaceRow方法代碼示例

本文整理匯總了Golang中github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStubInterface.ReplaceRow方法的典型用法代碼示例。如果您正苦於以下問題:Golang ChaincodeStubInterface.ReplaceRow方法的具體用法?Golang ChaincodeStubInterface.ReplaceRow怎麽用?Golang ChaincodeStubInterface.ReplaceRow使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStubInterface的用法示例。


在下文中一共展示了ChaincodeStubInterface.ReplaceRow方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: updateAccountBalance

// updateAccountBalance updates the balance amount of an account ID
// stub: chaincodestub
// accountID: account will be updated with the new balance
// contactInfo: contact information associated with the account owner (chaincode table does not allow me to perform updates on specific columns)
// amount: new amount to be udpated with
func (t *depositoryHandler) updateAccountBalance(stub shim.ChaincodeStubInterface,
	accountID string,
	contactInfo string,
	amount uint64) error {

	myLogger.Debugf("insert accountID= %v", accountID)

	//replace the old record row associated with the account ID with the new record row
	ok, err := stub.ReplaceRow(tableColumn, shim.Row{
		Columns: []*shim.Column{
			&shim.Column{Value: &shim.Column_String_{String_: accountID}},
			&shim.Column{Value: &shim.Column_String_{String_: contactInfo}},
			&shim.Column{Value: &shim.Column_Uint64{Uint64: amount}}},
	})

	if !ok && err == nil {
		myLogger.Errorf("system error %v", err)
		return errors.New("failed to replace row with account Id." + accountID)
	}
	return nil
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:26,代碼來源:depository_handler.go

示例2: addRole

func (t *RBACChaincode) addRole(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
	if len(args) != 2 {
		return nil, errors.New("Incorrect number of arguments. Expecting 2")
	}
	id, err := base64.StdEncoding.DecodeString(args[0])
	if err != nil {
		return nil, fmt.Errorf("Failed decoding tcert:  %s", err)
	}
	//id := []byte(args[0])
	role := args[1]

	myLogger.Debug("Add role [% x][%s]", id, role)

	// Verify that the invoker has the 'admin' role
	ok, _, err := t.hasInvokerRole(stub, "admin")
	if err != nil {
		return nil, fmt.Errorf("Failed checking role [%s]", err)
	}
	if !ok {
		return nil, errors.New("The invoker does not have the required roles.")
	}

	// Add role to id
	myLogger.Debug("Permission granted to the invoker")

	// Retrieve id's row
	var columns []shim.Column
	idCol := shim.Column{Value: &shim.Column_Bytes{Bytes: id}}
	columns = append(columns, idCol)
	row, err := stub.GetRow("RBAC", columns)
	if err != nil {
		return nil, fmt.Errorf("Failed retriving associated row [%s]", err)
	}
	if len(row.Columns) == 0 {
		// Insert row
		ok, err = stub.InsertRow("RBAC", shim.Row{
			Columns: []*shim.Column{
				&shim.Column{Value: &shim.Column_Bytes{Bytes: id}},
				&shim.Column{Value: &shim.Column_String_{String_: role}},
			},
		})
		if err != nil {
			return nil, fmt.Errorf("Failed inserting row [%s]", err)
		}
		if !ok {
			return nil, errors.New("Failed inserting row.")
		}

	} else {
		// Update row
		ok, err = stub.ReplaceRow("RBAC", shim.Row{
			Columns: []*shim.Column{
				&shim.Column{Value: &shim.Column_Bytes{Bytes: id}},
				&shim.Column{Value: &shim.Column_String_{String_: row.Columns[1].GetString_() + " " + role}},
			},
		})
		if err != nil {
			return nil, fmt.Errorf("Failed replacing row [%s]", err)
		}
		if !ok {
			return nil, errors.New("Failed replacing row.")
		}
	}

	return nil, err
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:66,代碼來源:rbac.go

示例3: Invoke


//.........這裏部分代碼省略.........
		columns = append(columns, &col6)
		columns = append(columns, &col7)

		row := shim.Row{Columns: columns}
		ok, err := stub.InsertRow("tableThree", row)
		if err != nil {
			return nil, fmt.Errorf("insertRowTableThree operation failed. %s", err)
		}
		if !ok {
			return nil, errors.New("insertRowTableThree operation failed. Row with given key already exists")
		}

	case "insertRowTableFour":
		if len(args) < 1 {
			return nil, errors.New("insertRowTableFour failed. Must include 1 column value1")
		}

		col1Val := args[0]

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

		row := shim.Row{Columns: columns}
		ok, err := stub.InsertRow("tableFour", row)
		if err != nil {
			return nil, fmt.Errorf("insertRowTableFour operation failed. %s", err)
		}
		if !ok {
			return nil, errors.New("insertRowTableFour operation failed. Row with given key already exists")
		}

	case "deleteRowTableOne":
		if len(args) < 1 {
			return nil, errors.New("deleteRowTableOne failed. Must include 1 key value")
		}

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

		err := stub.DeleteRow("tableOne", columns)
		if err != nil {
			return nil, fmt.Errorf("deleteRowTableOne operation failed. %s", err)
		}

	case "replaceRowTableOne":
		if len(args) < 3 {
			return nil, errors.New("replaceRowTableOne failed. Must include 3 column values")
		}

		col1Val := args[0]
		col2Int, err := strconv.ParseInt(args[1], 10, 32)
		if err != nil {
			return nil, errors.New("replaceRowTableOne failed. arg[1] must be convertable to int32")
		}
		col2Val := int32(col2Int)
		col3Int, err := strconv.ParseInt(args[2], 10, 32)
		if err != nil {
			return nil, errors.New("replaceRowTableOne failed. arg[2] must be convertable to int32")
		}
		col3Val := int32(col3Int)

		var columns []*shim.Column
		col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
		col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
		col3 := shim.Column{Value: &shim.Column_Int32{Int32: col3Val}}
		columns = append(columns, &col1)
		columns = append(columns, &col2)
		columns = append(columns, &col3)

		row := shim.Row{Columns: columns}
		ok, err := stub.ReplaceRow("tableOne", row)
		if err != nil {
			return nil, fmt.Errorf("replaceRowTableOne operation failed. %s", err)
		}
		if !ok {
			return nil, errors.New("replaceRowTableOne operation failed. Row with given key does not exist")
		}

	case "deleteAndRecreateTableOne":

		err := stub.DeleteTable("tableOne")
		if err != nil {
			return nil, fmt.Errorf("deleteAndRecreateTableOne operation failed. Error deleting table. %s", err)
		}

		err = createTableOne(stub)
		if err != nil {
			return nil, fmt.Errorf("deleteAndRecreateTableOne operation failed. Error creating table. %s", err)
		}

		return nil, nil

	default:
		return nil, errors.New("Unsupported operation")
	}
	return nil, nil
}
開發者ID:yoshiharay,項目名稱:fabric,代碼行數:101,代碼來源:table.go


注:本文中的github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStubInterface.ReplaceRow方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。