本文整理匯總了Golang中github.com/hyperledger/fabric/core/ledger.TxSimulator.SetState方法的典型用法代碼示例。如果您正苦於以下問題:Golang TxSimulator.SetState方法的具體用法?Golang TxSimulator.SetState怎麽用?Golang TxSimulator.SetState使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hyperledger/fabric/core/ledger.TxSimulator
的用法示例。
在下文中一共展示了TxSimulator.SetState方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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
}
示例2: 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
}
示例3: 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
}
示例4: 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
}