本文整理匯總了Golang中github.com/hyperledger/fabric/core/ledger.TxSimulator.GetTxSimulationResults方法的典型用法代碼示例。如果您正苦於以下問題:Golang TxSimulator.GetTxSimulationResults方法的具體用法?Golang TxSimulator.GetTxSimulationResults怎麽用?Golang TxSimulator.GetTxSimulationResults使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hyperledger/fabric/core/ledger.TxSimulator
的用法示例。
在下文中一共展示了TxSimulator.GetTxSimulationResults方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: simulateProposal
//simulate the proposal by calling the chaincode
func (e *Endorser) simulateProposal(ctx context.Context, chainID string, txid string, prop *pb.Proposal, cid *pb.ChaincodeID, txsim ledger.TxSimulator) ([]byte, []byte, *pb.ChaincodeEvent, error) {
//we do expect the payload to be a ChaincodeInvocationSpec
//if we are supporting other payloads in future, this be glaringly point
//as something that should change
cis, err := putils.GetChaincodeInvocationSpec(prop)
if err != nil {
return nil, nil, nil, err
}
//---1. check ACL
if err = e.checkACL(prop); err != nil {
return nil, nil, nil, err
}
//---2. check ESCC and VSCC for the chaincode
if err = e.checkEsccAndVscc(prop); err != nil {
return nil, nil, nil, err
}
//---3. execute the proposal and get simulation results
var simResult []byte
var resp []byte
var ccevent *pb.ChaincodeEvent
resp, ccevent, err = e.callChaincode(ctx, chainID, txid, prop, cis, cid, txsim)
if err != nil {
return nil, nil, nil, err
}
if txsim != nil {
if simResult, err = txsim.GetTxSimulationResults(); err != nil {
return nil, nil, nil, err
}
}
return resp, simResult, ccevent, nil
}
示例2: TransferFunds
// TransferFunds simulates a transaction for transferring fund from fromAccount to toAccount
func (app *App) TransferFunds(fromAccount string, toAccount string, transferAmt int) (*common.Envelope, error) {
// act as endorsing peer shim code to simulate a transaction on behalf of chaincode
var txSimulator ledger.TxSimulator
var err error
if txSimulator, err = app.ledger.NewTxSimulator(); err != nil {
return nil, err
}
defer txSimulator.Done()
var balFromBytes []byte
if balFromBytes, err = txSimulator.GetState(app.name, fromAccount); err != nil {
return nil, err
}
balFrom := toInt(balFromBytes)
if balFrom-transferAmt < 0 {
return nil, fmt.Errorf("Not enough balance in account [%s]. Balance = [%d], transfer request = [%d]",
fromAccount, balFrom, transferAmt)
}
var balToBytes []byte
if balToBytes, err = txSimulator.GetState(app.name, toAccount); err != nil {
return nil, err
}
balTo := toInt(balToBytes)
txSimulator.SetState(app.name, fromAccount, toBytes(balFrom-transferAmt))
txSimulator.SetState(app.name, toAccount, toBytes(balTo+transferAmt))
var txSimulationResults []byte
if txSimulationResults, err = txSimulator.GetTxSimulationResults(); err != nil {
return nil, err
}
// act as endorsing peer to create an Action with the SimulationResults
// then act as SDK to create a Transaction with the EndorsedAction
tx := constructTransaction(txSimulationResults)
return tx, nil
}
示例3: CreateMarble
// CreateMarble simulates init transaction
func (marbleApp *MarbleApp) CreateMarble(args []string) (*common.Envelope, error) {
// 0 1 2 3
// "asdf", "blue", "35", "bob"
logger.Debugf("===COUCHDB=== Entering ----------CreateMarble()----------")
marbleName := args[0]
marbleJsonBytes, err := init_marble(args)
if err != nil {
return nil, err
}
var txSimulator ledger.TxSimulator
if txSimulator, err = marbleApp.ledger.NewTxSimulator(); err != nil {
return nil, err
}
defer txSimulator.Done()
txSimulator.SetState(marbleApp.name, marbleName, marbleJsonBytes)
var txSimulationResults []byte
if txSimulationResults, err = txSimulator.GetTxSimulationResults(); err != nil {
return nil, err
}
logger.Debugf("===COUCHDB=== CreateMarble() simulation done, packaging into a transaction...")
tx := constructTransaction(txSimulationResults)
logger.Debugf("===COUCHDB=== Exiting CreateMarble()")
return tx, nil
}
示例4: TransferMarble
// TransferMarble simulates transfer transaction
func (marbleApp *MarbleApp) TransferMarble(args []string) (*common.Envelope, error) {
// 0 1
// "name", "bob"
if len(args) < 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
marbleName := args[0]
marbleNewOwner := args[1]
logger.Debugf("===COUCHDB=== Entering ----------TransferMarble----------")
var txSimulator ledger.TxSimulator
var err error
if txSimulator, err = marbleApp.ledger.NewTxSimulator(); err != nil {
return nil, err
}
defer txSimulator.Done()
marbleBytes, err := txSimulator.GetState(marbleApp.name, marbleName)
logger.Debugf("===COUCHDB=== marbleBytes is: %v", marbleBytes)
if marbleBytes != nil {
jsonString := string(marbleBytes[:])
logger.Debugf("===COUCHDB=== TransferMarble() Retrieved jsonString: \n %s", jsonString)
}
theMarble := Marble{}
json.Unmarshal(marbleBytes, &theMarble) //Unmarshal JSON bytes into a Marble struct
logger.Debugf("===COUCHDB=== theMarble after unmarshal: %v", theMarble)
logger.Debugf("===COUCHDB=== Setting the owner to: %s", marbleNewOwner)
theMarble.User = marbleNewOwner //change the user
theMarble.Txid = "tx000000000000002" // COUCHDB hardcode a txid for now for demo purpose
updatedMarbleBytes, _ := json.Marshal(theMarble)
if updatedMarbleBytes != nil {
updatedJsonString := string(updatedMarbleBytes[:])
logger.Debugf("===COUCHDB=== updatedJsonString:\n %s", updatedJsonString)
}
err = txSimulator.SetState(marbleApp.name, marbleName, updatedMarbleBytes)
if err != nil {
return nil, err
}
var txSimulationResults []byte
if txSimulationResults, err = txSimulator.GetTxSimulationResults(); err != nil {
return nil, err
}
logger.Debugf("===COUCHDB=== TransferMarble() simulation done, packaging into a transaction...")
tx := constructTransaction(txSimulationResults)
return tx, nil
}
示例5: Init
// Init simulates init transaction
func (app *App) Init(initialBalances map[string]int) (*common.Envelope, error) {
var txSimulator ledger.TxSimulator
var err error
if txSimulator, err = app.ledger.NewTxSimulator(); err != nil {
return nil, err
}
defer txSimulator.Done()
for accountID, bal := range initialBalances {
txSimulator.SetState(app.name, accountID, toBytes(bal))
}
var txSimulationResults []byte
if txSimulationResults, err = txSimulator.GetTxSimulationResults(); err != nil {
return nil, err
}
tx := constructTransaction(txSimulationResults)
return tx, nil
}
示例6: endTxSimulation
func endTxSimulation(chainID string, txsim ledger.TxSimulator, payload []byte, commit bool, prop *pb.Proposal) error {
txsim.Done()
if lgr := peer.GetLedger(chainID); lgr != nil {
if commit {
var txSimulationResults []byte
var err error
//get simulation results
if txSimulationResults, err = txsim.GetTxSimulationResults(); err != nil {
return err
}
// assemble a (signed) proposal response message
resp, err := putils.CreateProposalResponse(prop.Header, prop.Payload, txSimulationResults, nil, nil, signer)
if err != nil {
return err
}
// get the envelope
env, err := putils.CreateSignedTx(prop, signer, resp)
if err != nil {
return err
}
envBytes, err := putils.GetBytesEnvelope(env)
if err != nil {
return err
}
//create the block with 1 transaction
block := common.NewBlock(1, []byte{})
block.Data.Data = [][]byte{envBytes}
//commit the block
if err := lgr.Commit(block); err != nil {
return err
}
}
}
return nil
}