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


Golang ChaincodeStubInterface.GetFunctionAndParameters方法代碼示例

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


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

示例1: Init

// Init initialization
func (t *AssetManagementChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
	_, args := stub.GetFunctionAndParameters()
	myLogger.Info("[AssetManagementChaincode] Init")
	if len(args) != 0 {
		return nil, errors.New("Incorrect number of arguments. Expecting 0")
	}

	// Create ownership table
	err := stub.CreateTable("AssetsOwnership", []*shim.ColumnDefinition{
		&shim.ColumnDefinition{Name: "Asset", Type: shim.ColumnDefinition_STRING, Key: true},
		&shim.ColumnDefinition{Name: "Owner", Type: shim.ColumnDefinition_BYTES, Key: false},
	})
	if err != nil {
		return nil, fmt.Errorf("Failed creating AssetsOnwership table, [%v]", err)
	}

	// Set the role of the users that are allowed to assign assets
	// The metadata will contain the role of the users that are allowed to assign assets
	assignerRole, err := stub.GetCallerMetadata()
	fmt.Printf("Assiger role is %v\n", string(assignerRole))

	if err != nil {
		return nil, fmt.Errorf("Failed getting metadata, [%v]", err)
	}

	if len(assignerRole) == 0 {
		return nil, errors.New("Invalid assigner role. Empty.")
	}

	stub.PutState("assignerRole", assignerRole)

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

示例2: 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

示例3: 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

示例4: 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

示例5: 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

示例6: Invoke

// Invoke is a no-op
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, args := stub.GetFunctionAndParameters()
	if function == "query" {
		return t.query(stub, args)
	}

	return nil, errors.New("Invalid invoke function name. Expecting \"query\"")
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:9,代碼來源:chaincode_example03.go

示例7: Init

// Init intializes the chaincode by reading the transaction attributes and storing
// the attrbute values in the state
func (t *Attributes2State) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
	_, args := stub.GetFunctionAndParameters()
	err := t.setStateToAttributes(stub, args)
	if err != nil {
		return nil, err
	}
	return nil, nil
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:10,代碼來源:attributes_to_state.go

示例8: Invoke

// Invoke transaction makes payment of X units from A to B
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
	_, args := stub.GetFunctionAndParameters()

	var A, B string    // Entities
	var Aval, Bval int // Asset holdings
	var X int          // Transaction value
	var err error

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

	A = args[0]
	B = args[1]

	// Get the state from the ledger
	// TODO: will be nice to have a GetAllState call to ledger
	Avalbytes, err := stub.GetState(A)
	if err != nil {
		return nil, errors.New("Failed to get state")
	}
	if Avalbytes == nil {
		return nil, errors.New("Entity not found")
	}
	Aval, _ = strconv.Atoi(string(Avalbytes))

	Bvalbytes, err := stub.GetState(B)
	if err != nil {
		return nil, errors.New("Failed to get state")
	}
	if Bvalbytes == nil {
		return nil, errors.New("Entity not found")
	}
	Bval, _ = strconv.Atoi(string(Bvalbytes))

	// Perform the execution
	X, err = strconv.Atoi(args[2])
	if err != nil {
		return nil, errors.New("Invalid transaction amount, expecting a integer value")
	}
	Aval = Aval - X
	Bval = Bval + X
	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

	// Write the state back 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 []byte(fmt.Sprintf("{%d,%d}", Aval, Bval)), nil
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:58,代碼來源:invokereturnsvalue.go

示例9: Invoke

// Invoke  method is the interceptor of all invocation transactions, its job is to direct
// invocation transactions to intended APIs
func (t *AuthorizableCounterChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, args := stub.GetFunctionAndParameters()

	//	 Handle different functions
	if function == "increment" {
		return t.increment(stub, args)
	} else if function == "read" {
		return t.read(stub, args)
	}
	return nil, errors.New("Received unknown function invocation, Expecting \"increment\" \"read\"")
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:13,代碼來源:authorizable_counter.go

示例10: Init

// Init initialization, this method will create asset despository in the chaincode state
func (t *AssetManagementChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, args := stub.GetFunctionAndParameters()
	myLogger.Debugf("********************************Init****************************************")

	myLogger.Info("[AssetManagementChaincode] Init")
	if len(args) != 0 {
		return nil, errors.New("Incorrect number of arguments. Expecting 0")
	}

	return nil, dHandler.createTable(stub)
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:12,代碼來源:asset_management02.go

示例11: Invoke

func (t *Attributes2State) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, args := stub.GetFunctionAndParameters()
	if function == "delete" {
		return nil, t.delete(stub, args)
	} else if function == "submit" {
		return nil, t.setStateToAttributes(stub, args)
	} else if function == "read" {
		return t.query(stub, args)
	}

	return nil, errors.New("Invalid invoke function name. Expecting either \"delete\" or \"submit\" or \"read\"")
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:12,代碼來源:attributes_to_state.go

示例12: Init

// Init method will be called during deployment
func (t *RBACChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {

	function, args := stub.GetFunctionAndParameters()
	// Init the crypto layer
	if err := crypto.Init(); err != nil {
		panic(fmt.Errorf("Failed initializing the crypto layer [%s]", err))
	}

	myLogger.Info("Init")
	// if len(args) != 0 {
	// 	return nil, errors.New("Incorrect number of arguments. Expecting 0")
	// }

	myLogger.Debug("Creating RBAC Table...")

	// Create RBAC table
	err := stub.CreateTable("RBAC", []*shim.ColumnDefinition{
		&shim.ColumnDefinition{Name: "ID", Type: shim.ColumnDefinition_BYTES, Key: true},
		&shim.ColumnDefinition{Name: "Roles", Type: shim.ColumnDefinition_STRING, Key: false},
	})
	if err != nil {
		return nil, errors.New("Failed creating RBAC table.")
	}

	myLogger.Debug("Assign 'admin' role...")

	// Give to the deployer the role 'admin'
	deployer, err := stub.GetCallerMetadata()
	if err != nil {
		return nil, errors.New("Failed getting metadata.")
	}
	if len(deployer) == 0 {
		return nil, errors.New("Invalid admin certificate. Empty.")
	}

	myLogger.Debug("Add admin [% x][%s]", deployer, "admin")
	ok, err := stub.InsertRow("RBAC", shim.Row{
		Columns: []*shim.Column{
			&shim.Column{Value: &shim.Column_Bytes{Bytes: deployer}},
			&shim.Column{Value: &shim.Column_String_{String_: "admin"}},
		},
	})
	if !ok && err == nil {
		return nil, fmt.Errorf("Failed initiliazing RBAC entries.")
	}
	if err != nil {
		return nil, fmt.Errorf("Failed initiliazing RBAC entries [%s]", err)
	}

	myLogger.Debug("Done.")

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

示例13: Invoke

// Invoke will be called for every transaction.
// Supported functions are the following:
// "assign(asset, owner)": to assign ownership of assets. An asset can be owned by a single entity.
// Only an administrator can call this function.
// "transfer(asset, newOwner)": to transfer the ownership of an asset. Only the owner of the specific
// asset can call this function.
// An asset is any string to identify it. An owner is representated by one of his ECert/TCert.
func (t *AssetManagementChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, args := stub.GetFunctionAndParameters()
	// Handle different functions
	if function == "assign" {
		// Assign ownership
		return t.assign(stub, args)
	} else if function == "transfer" {
		// Transfer ownership
		return t.transfer(stub, args)
	}

	return nil, errors.New("Received unknown function invocation")
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:20,代碼來源:asset_management.go

示例14: Invoke

// Invoke Run callback representing the invocation of a chaincode
func (t *RBACChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, args := stub.GetFunctionAndParameters()
	// Handle different functions
	switch function {
	case "addRole":
		return t.addRole(stub, args)
	case "write":
		return t.write(stub, args)
	case "read":
		return t.read(stub, args)
	}

	return nil, fmt.Errorf("Received unknown function invocation [%s]", function)
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:15,代碼來源:rbac.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) ([]byte, error) {
	f, args := stub.GetFunctionAndParameters()

	switch f {
	case "putval":
		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))
		return nil, err
	case "getval":
		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
	default:
		jsonResp := "{\"Error\":\"Unknown functon " + f + "\"}"
		return nil, errors.New(jsonResp)
	}
}
開發者ID:hyperledger,項目名稱:fabric,代碼行數:51,代碼來源:samplesyscc.go


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