本文整理汇总了Golang中github.com/openblockchain/obc-peer/openchain/chaincode/shim.ChaincodeStub.PutState方法的典型用法代码示例。如果您正苦于以下问题:Golang ChaincodeStub.PutState方法的具体用法?Golang ChaincodeStub.PutState怎么用?Golang ChaincodeStub.PutState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/openblockchain/obc-peer/openchain/chaincode/shim.ChaincodeStub
的用法示例。
在下文中一共展示了ChaincodeStub.PutState方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: transfer
func (t *SimpleChaincode) transfer(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
var v Vehicle
var err error
var bytes []byte
bytes, err = stub.GetState(args[0])
if err != nil {
return nil, errors.New("Error retrieving vehicle with v5cID = " + args[0])
}
err = json.Unmarshal(bytes, &v)
if err != nil {
return nil, errors.New("Corrupt vehicle record")
}
v.Owner = args[1]
bytes, err = json.Marshal(v)
if err != nil {
return nil, errors.New("Error creating vehicle record")
}
err = stub.PutState(v.V5cID, bytes)
if err != nil {
return nil, err
}
return nil, nil
}
示例2: init
// ============================================================================================================================
// Init - reset all the things
// ============================================================================================================================
func (t *SimpleChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
var Aval int
var err error
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
// Initialize the chaincode
Aval, err = strconv.Atoi(args[0])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
// Write the state to the ledger
err = stub.PutState("abc", []byte(strconv.Itoa(Aval))) //making a test var "abc", I find it handy to read/write to it right away to test the network
if err != nil {
return nil, err
}
var empty []string
jsonAsBytes, _ := json.Marshal(empty) //marshal an emtpy array of strings to clear the index
err = stub.PutState(marbleIndexStr, jsonAsBytes)
if err != nil {
return nil, err
}
return nil, nil
}
示例3: init
func (t *SimpleChaincode) init(stub *shim.ChaincodeStub, 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")
}
// 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
}
示例4: update_registration
func (t *Vehicle) update_registration(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
regBytes, err := stub.GetState("Reg")
reg := string(regBytes)
if err != nil {
return nil, errors.New("Failed to get state")
}
statusBytes, err := stub.GetState("Status")
status, err := strconv.Atoi(string(statusBytes))
if err != nil {
return nil, errors.New("Failed to get state")
}
if status == 1 || status == 2 {
reg = args[0]
} else {
return nil, errors.New("Permission denied")
}
err = stub.PutState("Reg", []byte(reg))
return nil, nil
}
示例5: initRand
// Initialize entities to random variables in order to test ledger status consensus
func (t *SimpleChaincode) initRand(stub *shim.ChaincodeStub, 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 = rand.Intn(100)
B = args[1]
Bval = rand.Intn(100)
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
}
示例6: Delete
// ============================================================================================================================
// Delete - remove a key/value pair from state
// ============================================================================================================================
func (t *SimpleChaincode) Delete(stub *shim.ChaincodeStub, 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
}
示例7: update_model
func (t *Vehicle) update_model(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
modelBytes, err := stub.GetState("Model")
model := string(modelBytes)
if err != nil {
return nil, errors.New("Failed to get state")
}
statusBytes, err := stub.GetState("Status")
status, err := strconv.Atoi(string(statusBytes))
if err != nil {
return nil, errors.New("Failed to get state")
}
ownerBytes, err := stub.GetState("Owner")
owner := string(ownerBytes)
if err != nil {
return nil, errors.New("Failed to get state")
}
if status == 1 && owner == "Toyota" {
model = args[0]
} else {
return nil, errors.New("Permission denied")
}
err = stub.PutState("Model", []byte(model))
return nil, nil
}
示例8: init
func (t *SimpleChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
var event string // Indicates whether event has happened. Initially 0
var eventVal int // State of event
var err error
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
// Initialize the chaincode
event = args[0]
eventVal, err = strconv.Atoi(args[1])
if err != nil {
return nil, errors.New("Expecting integer value for event status")
}
fmt.Printf("eventVal = %d\n", eventVal)
// Write the state to the ledger
err = stub.PutState(event, []byte(strconv.Itoa(eventVal)))
if err != nil {
return nil, err
}
return nil, nil
}
示例9: Query
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
if function == "getstring" {
return t.getstring(stub, args)
} else if function != "query" {
return nil, errors.New("Invalid query function name. Expecting \"query\"")
}
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
}
示例10: set_user
// ============================================================================================================================
// Set User Permission on Marble
// ============================================================================================================================
func (t *SimpleChaincode) set_user(stub *shim.ChaincodeStub, 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
}
示例11: init
func (t *AssetManagementChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
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{"Asset", shim.ColumnDefinition_STRING, true},
&shim.ColumnDefinition{"Owner", shim.ColumnDefinition_BYTES, false},
})
if err != nil {
return nil, errors.New("Failed creating AssetsOnwership table.")
}
// Set the admin
// The metadata will contain the certificate of the administrator
adminCert, err := stub.GetCallerMetadata()
if err != nil {
return nil, errors.New("Failed getting metadata.")
}
if len(adminCert) == 0 {
return nil, errors.New("Invalid admin certificate. Empty.")
}
stub.PutState("admin", adminCert)
return nil, nil
}
示例12: init
func (t *SimpleChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
var sum string // Sum of asset holdings across accounts. Initially 0
var sumVal int // Sum of holdings
var err error
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
}
示例13: init_car
// ============================================================================================================================
// Init Car
// ============================================================================================================================
func (t *SimpleChaincode) init_car(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
var err error
if len(args) != 5 {
return nil, errors.New("Incorrect number of arguments. Expecting 5")
}
str := `{
"data": {
"vin": "` + args[0] + `",
"year": "` + args[1] + `"
"make": "` + args[2] + `",
"model": "` + args[3] + `",
"license": "-"
},
"users": [{
"userid": "` + args[4] + `",
"permissions":["owner"]
}]
}`
// Write the state back to the ledger
err = stub.PutState(args[0], []byte(str)) //store car with vin# as key
if err != nil {
return nil, err
}
t.remember_me(stub, args[0])
return nil, nil
}
示例14: set_user_perms
// ============================================================================================================================
// Set User Permissions
// ============================================================================================================================
func (t *SimpleChaincode) set_user_perms(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
var err error
carAsBytes, err := stub.GetState(args[0])
if err != nil {
return nil, errors.New("Failed to get car profile")
}
res := Car{}
json.Unmarshal(carAsBytes, &res)
fmt.Println(res)
for i, perm := range res.Users {
if perm.UserId == args[1] { //find the correct user
res.Users[i].Permissions[0] = args[2] //set new perm, dsh - to do make this input as array of all perms
fmt.Println(res.Users[i].Permissions)
break
}
}
// Write the state back to the ledger
jsonAsBytes, _ := json.Marshal(res)
err = stub.PutState(args[0], jsonAsBytes)
if err != nil {
return nil, err
}
return nil, nil
}
示例15: update_colour
func (t *Vehicle) update_colour(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
colourBytes, err := stub.GetState("Colour")
colour := string(colourBytes)
if err != nil {
return nil, errors.New("Failed to get state")
}
statusBytes, err := stub.GetState("Status")
status, err := strconv.Atoi(string(statusBytes))
if err != nil {
return nil, errors.New("Failed to get state")
}
if status == 1 || status == 2 {
colour = args[0]
} else {
return nil, errors.New("Permission denied")
}
err = stub.PutState("Colour", []byte(colour))
return nil, nil
}