Skip to content

Commit

Permalink
Add comments to ledgernext example client application
Browse files Browse the repository at this point in the history
Change-Id: I34edfd399ebfe05045477f7a46e0b741c18dc64c
Signed-off-by: denyeart <enyeart@us.ibm.com>
  • Loading branch information
denyeart committed Sep 27, 2016
1 parent 9617a6e commit a069514
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/ledgernext/kvledger/example/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (app *App) Init(initialBalances map[string]int) (*protos.Transaction2, erro

// TransferFunds simulates a transaction for transferring fund from fromAccount to toAccount
func (app *App) TransferFunds(fromAccount string, toAccount string, transferAmt int) (*protos.Transaction2, 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 {
Expand Down Expand Up @@ -83,6 +85,9 @@ func (app *App) TransferFunds(fromAccount string, toAccount string, transferAmt
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
}
Expand Down
27 changes: 27 additions & 0 deletions core/ledgernext/kvledger/example/main/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ var consenter *example.Consenter
var accounts = []string{"account1", "account2", "account3", "account4"}

func init() {

// Initialization will get a handle to the ledger at the specified path
// Note, if subledgers are supported in the future,
// the various ledgers could be created/managed at this level
os.RemoveAll(ledgerPath)
ledgerConf := kvledger.NewConf(ledgerPath, 0)
var err error
Expand All @@ -52,12 +56,31 @@ func init() {

func main() {
defer finalLedger.Close()

// Each of the functions here will emulate endorser, orderer,
// and committer by calling ledger APIs to similate the proposal,
// get simulation results, create a transaction, add it to a block,
// and then commit the block.

// Initialize account balances by setting each account to 100
initApp()

printBalances()

// Transfer money between accounts. Exercises happy path.
transferFunds()

printBalances()

// Attempt to transfer more money than account balance
// Exercises simulation failure
tryInvalidTransfer()

// Attempt two transactions, the first one will have sufficient funds,
// the second one should fail since the account balance was updated
// (by the first tran) since simulation time. This exercises the MVCC check.
tryDoubleSpend()

printBalances()
}

Expand All @@ -79,7 +102,11 @@ func transferFunds() {
handleError(err, true)
tx2, err := app.TransferFunds("account3", "account4", 50)
handleError(err, true)

// act as ordering service (consenter) to create a Raw Block from the Transaction
rawBlock := consenter.ConstructBlock(tx1, tx2)

// act as committing peer to commit the Raw Block
finalBlock, invalidTx, err := committer.CommitBlock(rawBlock)
handleError(err, true)
printBlocksInfo(rawBlock, finalBlock, invalidTx)
Expand Down

0 comments on commit a069514

Please sign in to comment.