本文整理匯總了Golang中github.com/hyperledger/fabric/core/util.ToChaincodeArgs函數的典型用法代碼示例。如果您正苦於以下問題:Golang ToChaincodeArgs函數的具體用法?Golang ToChaincodeArgs怎麽用?Golang ToChaincodeArgs使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ToChaincodeArgs函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Query
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function != "query" {
return nil, errors.New("Invalid query function name. Expecting \"query\"")
}
var sum string // Sum entity
var Aval, Bval, sumVal int // value of sum entity - to be computed
var err error
// Can query another chaincode within query, but cannot put state or invoke another chaincode (in transaction context)
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
chaincodeURL := args[0]
sum = args[1]
// Query chaincode_example02
f := "query"
queryArgs := util.ToChaincodeArgs(f, "a")
response, err := stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Aval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
queryArgs = util.ToChaincodeArgs(f, "b")
response, err = stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Bval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
// Compute sum
sumVal = Aval + Bval
fmt.Printf("Query chaincode successful. Got sum %d\n", sumVal)
jsonResp := "{\"Name\":\"" + sum + "\",\"Value\":\"" + strconv.Itoa(sumVal) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return []byte(strconv.Itoa(sumVal)), nil
}
示例2: invoke
// Invoke queries another chaincode and updates its own state
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var sum string // Sum entity
var Aval, Bval, sumVal int // value of sum entity - to be computed
var err error
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
chaincodeURL := args[0] // Expecting "github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02"
sum = args[1]
// Query chaincode_example02
f := "query"
queryArgs := util.ToChaincodeArgs(f, "a")
response, err := stub.InvokeChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Aval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
queryArgs = util.ToChaincodeArgs(f, "b")
response, err = stub.InvokeChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Bval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
// Compute sum
sumVal = Aval + Bval
// Write sumVal back to the ledger
err = stub.PutState(sum, []byte(strconv.Itoa(sumVal)))
if err != nil {
return nil, err
}
fmt.Printf("Invoke chaincode successful. Got sum %d\n", sumVal)
return []byte(strconv.Itoa(sumVal)), nil
}
示例3: iq
//helper
func (p *PassthruChaincode) iq(invoke bool, stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function == "" {
return nil, errors.New("Chaincode ID not provided")
}
chaincodeID := function
if invoke {
return stub.InvokeChaincode(chaincodeID, util.ToChaincodeArgs(args...))
}
return stub.QueryChaincode(chaincodeID, util.ToChaincodeArgs(args...))
}
示例4: TestDeployAndUpgrade
// TestUpgradeAndInvoke deploys chaincode_example01, upgrade it with chaincode_example02, then invoke it
func TestDeployAndUpgrade(t *testing.T) {
chainID := util.GetTestChainID()
var ctxt = context.Background()
url1 := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example01"
url2 := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
chaincodeID1 := &pb.ChaincodeID{Path: url1, Name: "upgradeex01"}
chaincodeID2 := &pb.ChaincodeID{Path: url2, Name: "upgradeex01"}
f := "init"
argsDeploy := util.ToChaincodeArgs(f, "a", "100", "b", "200")
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: chaincodeID1, CtorMsg: &pb.ChaincodeInput{Args: argsDeploy}}
cccid1 := chaincode.NewCCContext(chainID, "upgradeex01", "", "", false, nil)
cccid2 := chaincode.NewCCContext(chainID, "upgradeex02", "", "", false, nil)
resp, prop, err := deploy(endorserServer, chainID, spec, nil)
chaincodeName := spec.ChaincodeID.Name
if err != nil {
t.Fail()
chaincode.GetChain().Stop(ctxt, cccid1, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID1}})
chaincode.GetChain().Stop(ctxt, cccid2, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID2}})
t.Logf("Error deploying <%s>: %s", chaincodeName, err)
return
}
err = endorserServer.(*Endorser).commitTxSimulation(prop, chainID, signer, resp)
if err != nil {
t.Fail()
chaincode.GetChain().Stop(ctxt, cccid1, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID1}})
chaincode.GetChain().Stop(ctxt, cccid2, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID2}})
t.Logf("Error committing <%s>: %s", chaincodeName, err)
return
}
argsUpgrade := util.ToChaincodeArgs(f, "a", "150", "b", "300")
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: chaincodeID2, CtorMsg: &pb.ChaincodeInput{Args: argsUpgrade}}
resp, prop, err = upgrade(endorserServer, chainID, spec, nil)
if err != nil {
t.Fail()
chaincode.GetChain().Stop(ctxt, cccid1, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID1}})
chaincode.GetChain().Stop(ctxt, cccid2, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID2}})
t.Logf("Error upgrading <%s>: %s", chaincodeName, err)
return
}
fmt.Printf("Upgrade test passed\n")
t.Logf("Upgrade test passed")
chaincode.GetChain().Stop(ctxt, cccid1, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID1}})
chaincode.GetChain().Stop(ctxt, cccid2, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID2}})
}
示例5: TestRangeQuery
// Test the invocation of a transaction.
func TestRangeQuery(t *testing.T) {
//TODO enable after ledger enables RangeQuery
t.Skip()
chainID := util.GetTestChainID()
lis, err := initPeer(chainID)
if err != nil {
t.Fail()
t.Logf("Error creating peer: %s", err)
}
defer finitPeer(lis, chainID)
var ctxt = context.Background()
url := "github.com/hyperledger/fabric/examples/chaincode/go/map"
cID := &pb.ChaincodeID{Name: "tmap", Path: url}
f := "init"
args := util.ToChaincodeArgs(f)
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: args}}
cccid := NewCCContext(chainID, "tmap", "", "", false, nil)
_, err = deploy(ctxt, cccid, spec)
chaincodeID := spec.ChaincodeID.Name
if err != nil {
t.Fail()
t.Logf("Error initializing chaincode %s(%s)", chaincodeID, err)
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
return
}
// Invoke second chaincode, which will inturn invoke the first chaincode
f = "keys"
args = util.ToChaincodeArgs(f)
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: args}}
_, _, _, err = invoke(ctxt, chainID, spec)
if err != nil {
t.Fail()
t.Logf("Error invoking <%s>: %s", chaincodeID, err)
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
return
}
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
}
示例6: invokeExample02Transaction
// Invoke chaincode_example02
func invokeExample02Transaction(ctxt context.Context, cccid *CCContext, cID *pb.ChaincodeID, args []string, destroyImage bool) error {
f := "init"
argsDeploy := util.ToChaincodeArgs(f, "a", "100", "b", "200")
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: argsDeploy}}
_, err := deploy(ctxt, cccid, spec)
chaincodeID := spec.ChaincodeID.Name
if err != nil {
return fmt.Errorf("Error deploying <%s>: %s", chaincodeID, err)
}
time.Sleep(time.Second)
if destroyImage {
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
dir := container.DestroyImageReq{CCID: ccintf.CCID{ChaincodeSpec: spec, NetworkID: theChaincodeSupport.peerNetworkID, PeerID: theChaincodeSupport.peerID, ChainID: cccid.ChainID}, Force: true, NoPrune: true}
_, err = container.VMCProcess(ctxt, container.DOCKER, dir)
if err != nil {
err = fmt.Errorf("Error destroying image: %s", err)
return err
}
}
f = "invoke"
invokeArgs := append([]string{f}, args...)
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(invokeArgs...)}}
_, uuid, _, err := invoke(ctxt, cccid.ChainID, spec)
if err != nil {
return fmt.Errorf("Error invoking <%s>: %s", cccid.Name, err)
}
cccid.TxID = uuid
err = checkFinalState(cccid)
if err != nil {
return fmt.Errorf("Incorrect final state after transaction for <%s>: %s", chaincodeID, err)
}
// Test for delete state
f = "delete"
delArgs := util.ToChaincodeArgs(f, "a")
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: delArgs}}
_, uuid, _, err = invoke(ctxt, cccid.ChainID, spec)
if err != nil {
return fmt.Errorf("Error deleting state in <%s>: %s", cccid.Name, err)
}
return nil
}
示例7: deploy
func deploy(admCert crypto.CertificateHandler) error {
// Prepare the spec. The metadata includes the identity of the administrator
spec := &pb.ChaincodeSpec{
Type: 1,
ChaincodeID: &pb.ChaincodeID{Name: "mycc"},
CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("init")},
Metadata: admCert.GetCertificate(),
ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC,
}
// First build and get the deployment spec
var ctx = context.Background()
chaincodeDeploymentSpec, err := getDeploymentSpec(ctx, spec)
if err != nil {
return err
}
tid := chaincodeDeploymentSpec.ChaincodeSpec.ChaincodeID.Name
// Now create the Transactions message and send to Peer.
transaction, err := administrator.NewChaincodeDeployTransaction(chaincodeDeploymentSpec, tid)
if err != nil {
return fmt.Errorf("Error deploying chaincode: %s ", err)
}
ledger, err := ledger.GetLedger()
ledger.BeginTxBatch("1")
_, _, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
if err != nil {
return fmt.Errorf("Error deploying chaincode: %s", err)
}
ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil)
return err
}
示例8: exec
// Execute multiple transactions and queries.
func exec(ctxt context.Context, chainID string, chaincodeID string, numTrans int, numQueries int) []error {
var wg sync.WaitGroup
errs := make([]error, numTrans+numQueries)
e := func(qnum int) {
defer wg.Done()
var spec *pb.ChaincodeSpec
args := util.ToChaincodeArgs("invoke", "a", "b", "10")
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: &pb.ChaincodeID{Name: chaincodeID}, CtorMsg: &pb.ChaincodeInput{Args: args}}
_, _, _, err := invoke(ctxt, chainID, spec)
if err != nil {
errs[qnum] = fmt.Errorf("Error executing <%s>: %s", chaincodeID, err)
return
}
}
wg.Add(numTrans + numQueries)
//execute transactions sequentially..
go func() {
for i := 0; i < numTrans; i++ {
e(i)
}
}()
wg.Wait()
return errs
}
示例9: Invoke
// Invoke invokes another chaincode - chaincode_example02, upon receipt of an event and changes event state
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
var event string // Event entity
var eventVal string // State of event
var err error
var amount int
event = args[0]
eventVal = args[1]
chainCodeToCall := args[2] // Get the chaincode to call from the ledger
if eventVal == "middleSchool" {
amount = 2000
} else {
amount = 5000
}
// 轉賬操作
invokeArgs := util.ToChaincodeArgs("transferAcc", args[3], args[4], strconv.Itoa(amount))
response, err := stub.InvokeChaincode(chainCodeToCall, invokeArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to invoke chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
fmt.Printf("Invoke chaincode successful. Got response %s", string(response))
// Write the event state back to the ledger
err = stub.PutState(event, []byte(args[1]))
if err != nil {
return nil, err
}
return nil, nil
}
示例10: deployInternal
func deployInternal(deployer crypto.Client, adminCert crypto.CertificateHandler) (resp *pb.Response, err error) {
// Prepare the spec. The metadata includes the identity of the administrator
spec := &pb.ChaincodeSpec{
Type: 1,
ChaincodeID: &pb.ChaincodeID{Path: "github.com/hyperledger/fabric/examples/chaincode/go/asset_management"},
//ChaincodeID: &pb.ChaincodeID{Name: chaincodeName},
CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("init")},
Metadata: adminCert.GetCertificate(),
ConfidentialityLevel: confidentialityLevel,
}
// First build the deployment spec
cds, err := getChaincodeBytes(spec)
if err != nil {
return nil, fmt.Errorf("Error getting deployment spec: %s ", err)
}
// Now create the Transactions message and send to Peer.
transaction, err := deployer.NewChaincodeDeployTransaction(cds, cds.ChaincodeSpec.ChaincodeID.Name)
if err != nil {
return nil, fmt.Errorf("Error deploying chaincode: %s ", err)
}
resp, err = processTransaction(transaction)
appLogger.Debugf("resp [%s]", resp.String())
chaincodeName = cds.ChaincodeSpec.ChaincodeID.Name
appLogger.Debugf("ChaincodeName [%s]", chaincodeName)
return
}
示例11: whoIsTheOwner
func whoIsTheOwner(asset string) ([]byte, error) {
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs("query", asset)}
// Prepare spec and submit
spec := &pb.ChaincodeSpec{
Type: 1,
ChaincodeID: &pb.ChaincodeID{Name: "mycc"},
CtorMsg: chaincodeInput,
ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC,
}
var ctx = context.Background()
chaincodeInvocationSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
tid := chaincodeInvocationSpec.ChaincodeSpec.ChaincodeID.Name
// Now create the Transactions message and send to Peer.
transaction, err := administrator.NewChaincodeQuery(chaincodeInvocationSpec, tid)
if err != nil {
return nil, fmt.Errorf("Error deploying chaincode: %s ", err)
}
ledger, err := ledger.GetLedger()
ledger.BeginTxBatch("1")
result, _, err := chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
if err != nil {
return nil, fmt.Errorf("Error deploying chaincode: %s", err)
}
ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil)
return result, err
}
示例12: executeDeployTransaction
func executeDeployTransaction(t *testing.T, chainID string, name string, url string) {
lis, err := initPeer(chainID)
if err != nil {
t.Fail()
t.Logf("Error creating peer: %s", err)
}
defer finitPeer(lis, chainID)
var ctxt = context.Background()
f := "init"
args := util.ToChaincodeArgs(f, "a", "100", "b", "200")
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: &pb.ChaincodeID{Name: name, Path: url}, CtorMsg: &pb.ChaincodeInput{Args: args}}
cccid := NewCCContext(chainID, name, "", "", false, nil)
_, err = deploy(ctxt, cccid, spec)
chaincodeID := spec.ChaincodeID.Name
if err != nil {
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
t.Fail()
t.Logf("Error deploying <%s>: %s", chaincodeID, err)
return
}
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
}
示例13: InitTestStub
func InitTestStub(funargs ...string) *ChaincodeStub {
stub := ChaincodeStub{}
allargs := util.ToChaincodeArgs(funargs...)
newCI := &pb.ChaincodeInput{Args: allargs}
stub.init(&Handler{}, "TEST-txid", newCI)
return &stub
}
示例14: exec
// Execute multiple transactions and queries.
func exec(ctxt context.Context, chaincodeID string, numTrans int, numQueries int) []error {
var wg sync.WaitGroup
errs := make([]error, numTrans+numQueries)
e := func(qnum int, typ pb.Transaction_Type) {
defer wg.Done()
var spec *pb.ChaincodeSpec
if typ == pb.Transaction_CHAINCODE_INVOKE {
f := "invoke"
args := util.ToChaincodeArgs(f, "a", "b", "10")
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: &pb.ChaincodeID{Name: chaincodeID}, CtorMsg: &pb.ChaincodeInput{Args: args}}
} else {
f := "query"
args := util.ToChaincodeArgs(f, "a")
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: &pb.ChaincodeID{Name: chaincodeID}, CtorMsg: &pb.ChaincodeInput{Args: args}}
}
_, _, _, err := invoke(ctxt, spec, typ)
if err != nil {
errs[qnum] = fmt.Errorf("Error executing <%s>: %s", chaincodeID, err)
return
}
}
wg.Add(numTrans + numQueries)
//execute transactions sequentially..
go func() {
for i := 0; i < numTrans; i++ {
e(i, pb.Transaction_CHAINCODE_INVOKE)
}
}()
//...but queries in parallel
for i := numTrans; i < numTrans+numQueries; i++ {
go e(i, pb.Transaction_CHAINCODE_QUERY)
}
wg.Wait()
return errs
}
示例15: transferOwnership
func transferOwnership(owner crypto.Client, ownerCert crypto.CertificateHandler, fromAttributes string,
newOwnerCert crypto.CertificateHandler, toAttributes string, amount string) error {
// Get a transaction handler to be used to submit the execute transaction
// and bind the chaincode access control logic using the binding
submittingCertHandler, err := owner.GetTCertificateHandlerNext("role")
if err != nil {
return err
}
txHandler, err := submittingCertHandler.GetTransactionHandler()
if err != nil {
return err
}
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs(
"transferOwnership",
base64.StdEncoding.EncodeToString(ownerCert.GetCertificate()),
fromAttributes,
base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate()),
toAttributes,
amount)}
// Prepare spec and submit
spec := &pb.ChaincodeSpec{
Type: 1,
ChaincodeID: &pb.ChaincodeID{Name: "mycc"},
CtorMsg: chaincodeInput,
ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC,
}
var ctx = context.Background()
chaincodeInvocationSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
tid := chaincodeInvocationSpec.ChaincodeSpec.ChaincodeID.Name
// Now create the Transactions message and send to Peer.
transaction, err := txHandler.NewChaincodeExecute(chaincodeInvocationSpec, tid)
if err != nil {
return fmt.Errorf("Error deploying chaincode: %s ", err)
}
ledger, err := ledger.GetLedger()
ledger.BeginTxBatch("1")
_, _, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
if err != nil {
return fmt.Errorf("Error deploying chaincode: %s", err)
}
ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil)
return err
}