本文整理匯總了Golang中github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStub類的典型用法代碼示例。如果您正苦於以下問題:Golang ChaincodeStub類的具體用法?Golang ChaincodeStub怎麽用?Golang ChaincodeStub使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ChaincodeStub類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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 != systemValidityPeriodKey {
return nil, errors.New("Incorrect key. Expecting " + systemValidityPeriodKey)
}
// 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
}
示例2: 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 // 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
}
示例3: Query
// Query callback representing the query of a chaincode
func (t *SampleSysCC) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
if function != "getval" {
return nil, errors.New("Invalid query function name. Expecting \"getval\"")
}
var key string // Entities
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
}
示例4: get_ecert
//==============================================================================================================================
// General Functions
//==============================================================================================================================
// get_ecert - Takes the name passed and calls out to the REST API for HyperLedger to retrieve the ecert
// for that user. Returns the ecert as retrived including html encoding.
//==============================================================================================================================
func (t *SimpleChaincode) get_ecert(stub *shim.ChaincodeStub, name string) ([]byte, error) {
var cert ECertResponse
peer_address, err := stub.GetState("Peer_Address")
if err != nil {
return nil, errors.New("Error retrieving peer address")
}
response, err := http.Get("http://" + string(peer_address) + "/registrar/" + name + "/ecert") // Calls out to the HyperLedger REST API to get the ecert of the user with that name
if err != nil {
return nil, errors.New("Error calling ecert API")
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body) // Read the response from the http callout into the variable contents
if err != nil {
return nil, errors.New("Could not read body")
}
err = json.Unmarshal(contents, &cert)
if err != nil {
return nil, errors.New("Could not retrieve ecert for user: " + name)
}
return []byte(string(cert.OK)), nil
}
示例5: createTableThree
func createTableThree(stub *shim.ChaincodeStub) error {
var columnDefsTableThree []*shim.ColumnDefinition
columnOneTableThreeDef := shim.ColumnDefinition{Name: "colOneTableThree",
Type: shim.ColumnDefinition_STRING, Key: true}
columnTwoTableThreeDef := shim.ColumnDefinition{Name: "colTwoTableThree",
Type: shim.ColumnDefinition_INT32, Key: false}
columnThreeTableThreeDef := shim.ColumnDefinition{Name: "colThreeTableThree",
Type: shim.ColumnDefinition_INT64, Key: false}
columnFourTableThreeDef := shim.ColumnDefinition{Name: "colFourTableFour",
Type: shim.ColumnDefinition_UINT32, Key: false}
columnFiveTableThreeDef := shim.ColumnDefinition{Name: "colFourTableFive",
Type: shim.ColumnDefinition_UINT64, Key: false}
columnSixTableThreeDef := shim.ColumnDefinition{Name: "colFourTableSix",
Type: shim.ColumnDefinition_BYTES, Key: false}
columnSevenTableThreeDef := shim.ColumnDefinition{Name: "colFourTableSeven",
Type: shim.ColumnDefinition_BOOL, Key: false}
columnDefsTableThree = append(columnDefsTableThree, &columnOneTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnTwoTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnThreeTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnFourTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnFiveTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnSixTableThreeDef)
columnDefsTableThree = append(columnDefsTableThree, &columnSevenTableThreeDef)
return stub.CreateTable("tableThree", columnDefsTableThree)
}
示例6: createTableFour
func createTableFour(stub *shim.ChaincodeStub) error {
var columnDefsTableFour []*shim.ColumnDefinition
columnOneTableFourDef := shim.ColumnDefinition{Name: "colOneTableFour",
Type: shim.ColumnDefinition_STRING, Key: true}
columnDefsTableFour = append(columnDefsTableFour, &columnOneTableFourDef)
return stub.CreateTable("tableFour", columnDefsTableFour)
}
示例7: 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.ChaincodeStub, 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")
}
}
示例8: 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.ChaincodeStub, function string, args []string) ([]byte, error) {
myLogger.Debugf("Query [%s]", function)
if function != "query" {
return nil, errors.New("Invalid query function name. Expecting \"query\"")
}
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
}
示例9: append_id
// "create": true -> create new ID, false -> append the id
func append_id(stub *shim.ChaincodeStub, indexStr string, id string, create bool) ([]byte, error) {
indexAsBytes, err := stub.GetState(indexStr)
if err != nil {
return nil, errors.New("Failed to get " + indexStr)
}
fmt.Println(indexStr + " retrieved")
// Unmarshal the index
var tmpIndex []string
json.Unmarshal(indexAsBytes, &tmpIndex)
fmt.Println(indexStr + " unmarshalled")
// Create new id
var newId = id
if create {
newId += strconv.Itoa(len(tmpIndex) + 1)
}
// append the new id to the index
tmpIndex = append(tmpIndex, newId)
jsonAsBytes, _ := json.Marshal(tmpIndex)
err = stub.PutState(indexStr, jsonAsBytes)
if err != nil {
return nil, errors.New("Error storing new " + indexStr + " into ledger")
}
return []byte(newId), nil
}
示例10: get_all_devices
func (t *SimpleChaincode) get_all_devices(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
devicesIndexBytes, err := stub.GetState(deviceIndexStr)
if err != nil {
return nil, errors.New("Failed to get devices index")
}
var deviceIndex []string
err = json.Unmarshal(devicesIndexBytes, &deviceIndex)
if err != nil {
return nil, errors.New("Could not marshal device indexes")
}
var devices []Device
for _, deviceId := range deviceIndex {
bytes, err := stub.GetState(deviceId)
if err != nil {
return nil, errors.New("Not able to get device")
}
var d Device
err = json.Unmarshal(bytes, &d)
devices = append(devices, d)
}
devicesJson, err := json.Marshal(devices)
if err != nil {
return nil, errors.New("Failed to marshal devices to JSON")
}
return devicesJson, nil
}
示例11: get_all_bids
func (t *SimpleChaincode) get_all_bids(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
bidIndexBytes, err := stub.GetState(bidIndexStr)
if err != nil {
return nil, errors.New("Failed to get bids index")
}
var bidIndex []string
err = json.Unmarshal(bidIndexBytes, &bidIndex)
if err != nil {
return nil, errors.New("Could not marshal bid indexes")
}
var bids []Bid
for _, bidId := range bidIndex {
bytes, err := stub.GetState(bidId)
if err != nil {
return nil, errors.New("Not able to get bid")
}
var b Bid
err = json.Unmarshal(bytes, &b)
bids = append(bids, b)
}
bidsJson, err := json.Marshal(bids)
if err != nil {
return nil, errors.New("Failed to marshal bids to JSON")
}
return bidsJson, nil
}
示例12: 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
}
示例13: Init
func (t *SimpleChaincode) Init(stub *shim.ChaincodeStub, function string, 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, err = strconv.Atoi(args[1])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
B = args[2]
Bval, err = strconv.Atoi(args[3])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
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
}
示例14: 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.ChaincodeStub, function string, 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
}
示例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.ChaincodeStub, function string, args []string) ([]byte, error) {
var key, val string // Entities
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))
if err != nil {
return nil, err
}
return nil, nil
}