本文整理匯總了Golang中github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStubInterface.DelState方法的典型用法代碼示例。如果您正苦於以下問題:Golang ChaincodeStubInterface.DelState方法的具體用法?Golang ChaincodeStubInterface.DelState怎麽用?Golang ChaincodeStubInterface.DelState使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStubInterface
的用法示例。
在下文中一共展示了ChaincodeStubInterface.DelState方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Delete
// ============================================================================================================================
// Delete - remove a key/value pair from state
// ============================================================================================================================
func (t *SimpleChaincode) Delete(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
name := args[0]
err := stub.DelState(name) //remove the key from chaincode state
if err != nil {
return nil, errors.New("Failed to delete state")
}
//get the marble index
marblesAsBytes, err := stub.GetState(marbleIndexStr)
if err != nil {
return nil, errors.New("Failed to get marble index")
}
var marbleIndex []string
json.Unmarshal(marblesAsBytes, &marbleIndex) //un stringify it aka JSON.parse()
//remove marble from index
for i, val := range marbleIndex {
fmt.Println(strconv.Itoa(i) + " - looking at " + val + " for " + name)
if val == name { //find the correct marble
fmt.Println("found marble")
marbleIndex = append(marbleIndex[:i], marbleIndex[i+1:]...) //remove it
for x := range marbleIndex { //debug prints...
fmt.Println(string(x) + " - " + marbleIndex[x])
}
break
}
}
jsonAsBytes, _ := json.Marshal(marbleIndex) //save new index
err = stub.PutState(marbleIndexStr, jsonAsBytes)
return nil, nil
}
示例2: delete
// delete Deletes an entity from the state, returning error if the entity was not found in the state.
func (t *Attributes2State) delete(stub shim.ChaincodeStubInterface, args []string) error {
if len(args) != 1 {
return errors.New("Incorrect number of arguments. Expecting only 1 (attributeName)")
}
attributeName := args[0]
fmt.Printf("Deleting attribute %v", attributeName)
valBytes, err := stub.GetState(attributeName)
if err != nil {
return err
}
if valBytes == nil {
return errors.New("Attribute '" + attributeName + "' not found.")
}
isOk, err := stub.VerifyAttribute(attributeName, valBytes)
if err != nil {
return err
}
if isOk {
// Delete the key from the state in ledger
err = stub.DelState(attributeName)
if err != nil {
return errors.New("Failed to delete state")
}
}
return nil
}
示例3: 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")
}
}
示例4: DeleteAccount
// Deletes an entity from state
func (t *ChaincodeExample) DeleteAccount(stub shim.ChaincodeStubInterface, param *example02.Entity) error {
// Delete the key from the state in ledger
err := stub.DelState(param.Id)
if err != nil {
return errors.New("Failed to delete state")
}
return nil
}
示例5: delete
// Deletes an entity from state
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
A := args[0]
// Delete the key from the state in ledger
err := stub.DelState(A)
if err != nil {
return nil, errors.New("Failed to delete state")
}
return nil, nil
}
示例6: 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) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
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
case "get":
if len(args) < 1 {
return nil, errors.New("get operation must include one argument, a key")
}
key := args[0]
value, err := stub.GetState(key)
if err != nil {
return nil, fmt.Errorf("get operation failed. Error accessing state: %s", err)
}
return value, nil
case "keys":
keysIter, err := stub.RangeQueryState("", "")
if err != nil {
return nil, fmt.Errorf("keys operation failed. Error accessing state: %s", err)
}
defer keysIter.Close()
var keys []string
for keysIter.HasNext() {
key, _, iterErr := keysIter.Next()
if iterErr != nil {
return nil, fmt.Errorf("keys operation failed. Error accessing state: %s", err)
}
keys = append(keys, key)
}
jsonKeys, err := json.Marshal(keys)
if err != nil {
return nil, fmt.Errorf("keys operation failed. Error marshaling JSON: %s", err)
}
return jsonKeys, nil
default:
return nil, errors.New("Unsupported operation")
}
}