CBDH CBDH Smart Contract Development 2 — Questions and Answers
Question 1: What Fabric feature allows you to query a range of keys in the world state?
- stub.GetHistoryForKey()
- stub.GetStateByRange() (Correct answer)
- stub.QueryLedger()
- stub.ScanKeys()
Correct answer: stub.GetStateByRange()
GetStateByRange returns an iterator over all key-value pairs between a specified start and end key in the world state.
Question 2: Which Fabric chaincode API enables rich queries against a CouchDB state database?
- stub.GetStateByRange()
- stub.QueryStateDB()
- stub.GetQueryResult() (Correct answer)
- stub.RichQuery()
Correct answer: stub.GetQueryResult()
GetQueryResult executes a rich query against the CouchDB state database using a JSON selector query string.
Question 3: What does the chaincode lifecycle command `peer lifecycle chaincode package` do?
- Installs chaincode on all peers
- Creates a deployable .tar.gz package from chaincode source (Correct answer)
- Commits chaincode to the channel
- Approves chaincode for an organization
Correct answer: Creates a deployable .tar.gz package from chaincode source
The package command bundles chaincode source, metadata, and dependencies into a .tar.gz file ready for installation.
Question 4: In Hyperledger Fabric 2.x, how many organizations must approve a chaincode definition before it can be committed?
- All organizations in the channel
- Only the ordering organization
- The number required by the channel's LifecycleEndorsement policy (Correct answer)
- At least one organization
Correct answer: The number required by the channel's LifecycleEndorsement policy
The LifecycleEndorsement policy (default: majority of channel orgs) determines how many organizations must approve before committing.
Question 5: What is the role of `shim.Start(cc Chaincode)` in a Fabric chaincode program?
- Registers the chaincode with the ordering service
- Initializes CouchDB connections
- Starts the chaincode process and connects it to the peer (Correct answer)
- Creates the channel ledger
Correct answer: Starts the chaincode process and connects it to the peer
shim.Start() launches the chaincode process and establishes the gRPC connection between the chaincode and its peer.
Question 6: Which method retrieves the history of state changes for a specific key in Fabric chaincode?
- stub.GetStateByRange()
- stub.GetHistoryForKey() (Correct answer)
- stub.GetTransactionHistory()
- stub.GetKeyVersions()
Correct answer: stub.GetHistoryForKey()
GetHistoryForKey returns an iterator of all historical values and their associated transaction IDs for a given key.
What Fabric feature allows you to query a range of keys in the world state?