本文整理汇总了Golang中github.com/openblockchain/obc-peer/openchain/chaincode/shim.ChaincodeStub.GetState方法的典型用法代码示例。如果您正苦于以下问题:Golang ChaincodeStub.GetState方法的具体用法?Golang ChaincodeStub.GetState怎么用?Golang ChaincodeStub.GetState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/openblockchain/obc-peer/openchain/chaincode/shim.ChaincodeStub
的用法示例。
在下文中一共展示了ChaincodeStub.GetState方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getstring
/**
* Should allow us to query the value of the protected strings in the ledger
* Args:
* 0 strkey: The key under which the protected string is stored
*/
func (t *SimpleChaincode) getstring(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
var strkey string // The key associated with the string we want to find
var strvalBytes []byte // The actual value of the string
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
// Which string do we want?
strkey = args[0]
// Get the value of the string from the ledger
strvalBytes, err := stub.GetState(strkey)
if err != nil {
jsonResp := "{\"Error\": \"Failed to get state for " + strkey + "\"}"
return nil, errors.New(jsonResp)
}
if strvalBytes == nil {
jsonResp := "{\"Error\":\"Nil amount for " + strkey + "\"}"
return nil, errors.New(jsonResp)
}
jsonResp := "{\"StringKey\":\"" + strkey + "\", \"Value\":\"" + string(strvalBytes) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return strvalBytes, nil
}
示例2: GetAllCPs
func GetAllCPs(stub *shim.ChaincodeStub) ([]CP, error) {
var allCPs []CP
// Get list of all the keys
keysBytes, err := stub.GetState("PaperKeys")
if err != nil {
fmt.Println("Error retrieving paper keys")
return nil, errors.New("Error retrieving paper keys")
}
var keys []string
err = json.Unmarshal(keysBytes, &keys)
if err != nil {
fmt.Println("Error unmarshalling paper keys")
return nil, errors.New("Error unmarshalling paper keys")
}
// Get all the cps
for _, value := range keys {
cpBytes, err := stub.GetState(value)
var cp CP
err = json.Unmarshal(cpBytes, &cp)
if err != nil {
fmt.Println("Error retrieving cp " + value)
return nil, errors.New("Error retrieving cp " + value)
}
fmt.Println("Appending CP" + value)
allCPs = append(allCPs, cp)
}
return allCPs, nil
}
示例3: 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
}
示例4: Query
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, 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
}
示例5: findMarble4Trade
// ============================================================================================================================
// findMarble4Trade - look for a matching marble that this user owns and return it
// ============================================================================================================================
func findMarble4Trade(stub *shim.ChaincodeStub, 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")
}
示例6: 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
}
示例7: 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
}
示例8: 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
}
示例9: 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
}
示例10: getstring
/**
* Should allow us to query the value of the protected strings in the ledger
* Args:
* stub: The blockchain that holds the string we're want.
* strkey: The key under which the string is stored.
*/
func (t *SimpleChaincode) getstring(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
var strkey string
var strvalBytes []byte // The actual value of the string
if(len(args) != 1) {
return nil, errors.New("Incorrect number of arguments. Expecting 1");
}
// Which string do we want?
strkey = args[0];
// Get the value of the string from the ledger
strvalBytes, err := stub.GetState(strkey)
if err != nil {
jsonResp := "{\"Error\": \"Failed to get state for " + strkey + "\"}"
return nil, errors.New(jsonResp)
}
if strvalBytes == nil {
jsonResp := "{\"Error\":\"Nil amount for " + strkey + "\"}"
return nil, errors.New(jsonResp)
}
// Add quotes to the string or it will cause an invalid json to be created.
strvalBytes = []byte(fmt.Sprintf("\"%s\"", string(strvalBytes)))
jsonResp := "{\"StringKey\":\"" + strkey + "\",\"Value\":\"" + string(strvalBytes) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return strvalBytes, nil
}
示例11: 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
}
示例12: Query
// Query callback representing the query of a chaincode
func (t *systemChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
if function != "query" {
return nil, errors.New("Invalid query function name. Expecting \"query\"")
}
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
key := args[0]
if key != system_validity_period_key {
return nil, errors.New("Incorrect key. Expecting " + system_validity_period_key)
}
// Get the state from the ledger
vp, err := stub.GetState(key)
if err != nil {
jsonResp := "{\"Error\":\"Failed to get state for " + key + "\"}"
return nil, errors.New(jsonResp)
}
if vp == nil {
jsonResp := "{\"Error\":\"Nil value for " + key + "\"}"
return nil, errors.New(jsonResp)
}
jsonResp := "{\"Name\":\"" + key + "\",\"Value\":\"" + string(vp) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return []byte(jsonResp), nil
}
示例13: get_attribute
func (t *Vehicle) get_attribute(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
state, err := stub.GetState(args[0])
if err != nil {
return nil, errors.New("Failed to get state")
}
jsonResp := "{\"" + args[0] + "\":\"" + string(state) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return []byte(jsonResp), nil
}
示例14: init_marble
// ============================================================================================================================
// Init Marble - create a new marble, store into chaincode state
// ============================================================================================================================
func (t *SimpleChaincode) init_marble(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
var err error
// 0 1 2 3
// "asdf", "blue", "35", "bob"
if len(args) != 4 {
return nil, errors.New("Incorrect number of arguments. Expecting 4")
}
fmt.Println("- start init marble")
if len(args[0]) <= 0 {
return nil, errors.New("1st argument must be a non-empty string")
}
if len(args[1]) <= 0 {
return nil, errors.New("2nd argument must be a non-empty string")
}
if len(args[2]) <= 0 {
return nil, errors.New("3rd argument must be a non-empty string")
}
if len(args[3]) <= 0 {
return nil, errors.New("4th argument must be a non-empty string")
}
size, err := strconv.Atoi(args[2])
if err != nil {
return nil, errors.New("3rd argument must be a numeric string")
}
color := strings.ToLower(args[1])
user := strings.ToLower(args[3])
str := `{"name": "` + args[0] + `", "color": "` + color + `", "size": ` + strconv.Itoa(size) + `, "user": "` + user + `"}`
err = stub.PutState(args[0], []byte(str)) //store marble with id as key
if err != nil {
return nil, err
}
//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()
//append
marbleIndex = append(marbleIndex, args[0]) //add marble name to index list
fmt.Println("! marble index: ", marbleIndex)
jsonAsBytes, _ := json.Marshal(marbleIndex)
err = stub.PutState(marbleIndexStr, jsonAsBytes) //store name of marble
fmt.Println("- end init marble")
return nil, nil
}
示例15: invoke
// Transaction makes payment of X units from A to B
func (t *SimpleChaincode) invoke(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
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 Avalbytes == nil {
return nil, errors.New("Entity not found")
}
Bval, _ = strconv.Atoi(string(Bvalbytes))
// Perform the execution
X, err = strconv.Atoi(args[2])
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 nil, nil
}