CBDH CBDH Smart Contract Development 1 — Questions and Answers
Question 1: In Hyperledger Fabric, what language is primarily used to write chaincode (smart contracts)?
- Python
- Go (Correct answer)
- Ruby
- Rust
Correct answer: Go
Hyperledger Fabric supports chaincode written in Go, JavaScript (Node.js), and Java, with Go being the most commonly used language.
Question 2: Which interface must a Fabric chaincode implement to be valid?
- ChaincodeInterface
- SmartContract
- Chaincode (Correct answer)
- LedgerContract
Correct answer: Chaincode
All Hyperledger Fabric chaincode must implement the Chaincode interface, which requires Init and Invoke methods.
Question 3: What does the `stub.PutState(key, value)` function do in Fabric chaincode?
- Reads a value from the ledger
- Writes or updates a key-value pair on the ledger (Correct answer)
- Deletes a record from the ledger
- Queries historical data
Correct answer: Writes or updates a key-value pair on the ledger
PutState writes or updates a key-value pair in the world state ledger during chaincode execution.
Question 4: Which chaincode function is invoked when the chaincode is first instantiated or upgraded?
- Start()
- Deploy()
- Init() (Correct answer)
- Invoke()
Correct answer: Init()
The Init() function is called during chaincode instantiation or upgrade, allowing initialization of ledger state.
Question 5: What is the purpose of `stub.GetState(key)` in Hyperledger Fabric chaincode?
- Deletes a key from the world state
- Returns the peer's current block height
- Retrieves the value associated with a key from the world state (Correct answer)
- Lists all keys in the ledger
Correct answer: Retrieves the value associated with a key from the world state
GetState retrieves the byte-array value associated with the specified key from the channel's world state.
Question 6: In Fabric chaincode, what does `stub.GetTxID()` return?
- The peer's node ID
- The channel name
- The unique transaction ID of the current invocation (Correct answer)
- The chaincode version number
Correct answer: The unique transaction ID of the current invocation
GetTxID returns the transaction ID that uniquely identifies the current transaction proposal being processed.
In Hyperledger Fabric, what language is primarily used to write chaincode (smart contracts)?