Skip to content

Commit a069514

Browse files
committed
Add comments to ledgernext example client application
Change-Id: I34edfd399ebfe05045477f7a46e0b741c18dc64c Signed-off-by: denyeart <[email protected]>
1 parent 9617a6e commit a069514

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

core/ledgernext/kvledger/example/app.go

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func (app *App) Init(initialBalances map[string]int) (*protos.Transaction2, erro
5656

5757
// TransferFunds simulates a transaction for transferring fund from fromAccount to toAccount
5858
func (app *App) TransferFunds(fromAccount string, toAccount string, transferAmt int) (*protos.Transaction2, error) {
59+
60+
// act as endorsing peer shim code to simulate a transaction on behalf of chaincode
5961
var txSimulator ledger.TxSimulator
6062
var err error
6163
if txSimulator, err = app.ledger.NewTxSimulator(); err != nil {
@@ -83,6 +85,9 @@ func (app *App) TransferFunds(fromAccount string, toAccount string, transferAmt
8385
if txSimulationResults, err = txSimulator.GetTxSimulationResults(); err != nil {
8486
return nil, err
8587
}
88+
89+
// act as endorsing peer to create an Action with the SimulationResults
90+
// then act as SDK to create a Transaction with the EndorsedAction
8691
tx := constructTransaction(txSimulationResults)
8792
return tx, nil
8893
}

core/ledgernext/kvledger/example/main/example.go

+27
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ var consenter *example.Consenter
3838
var accounts = []string{"account1", "account2", "account3", "account4"}
3939

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

5357
func main() {
5458
defer finalLedger.Close()
59+
60+
// Each of the functions here will emulate endorser, orderer,
61+
// and committer by calling ledger APIs to similate the proposal,
62+
// get simulation results, create a transaction, add it to a block,
63+
// and then commit the block.
64+
65+
// Initialize account balances by setting each account to 100
5566
initApp()
67+
5668
printBalances()
69+
70+
// Transfer money between accounts. Exercises happy path.
5771
transferFunds()
72+
5873
printBalances()
74+
75+
// Attempt to transfer more money than account balance
76+
// Exercises simulation failure
5977
tryInvalidTransfer()
78+
79+
// Attempt two transactions, the first one will have sufficient funds,
80+
// the second one should fail since the account balance was updated
81+
// (by the first tran) since simulation time. This exercises the MVCC check.
6082
tryDoubleSpend()
83+
6184
printBalances()
6285
}
6386

@@ -79,7 +102,11 @@ func transferFunds() {
79102
handleError(err, true)
80103
tx2, err := app.TransferFunds("account3", "account4", 50)
81104
handleError(err, true)
105+
106+
// act as ordering service (consenter) to create a Raw Block from the Transaction
82107
rawBlock := consenter.ConstructBlock(tx1, tx2)
108+
109+
// act as committing peer to commit the Raw Block
83110
finalBlock, invalidTx, err := committer.CommitBlock(rawBlock)
84111
handleError(err, true)
85112
printBlocksInfo(rawBlock, finalBlock, invalidTx)

0 commit comments

Comments
 (0)