Fabric v2.4 AppDev
Fabric v2.4 AppDev
Ledger
block
emits
txn txn txn !
event
World state Blockchain
Peer
• Client app needs only make single (gRPC) connection to its (trusted) gateway peer.
– Gateway peer interacts with other peers to gather endorsements.
– Gateway peer also connects to ordering service nodes to submit transactions.
– Built in support for load balancing and retry logic.
• Each transaction proposal from the client will invoke that async redeem(ctx, assetId) {
named transaction function. // destroy asset
...
}
• Interacts with the ledger via the ‘stub API’. async description(ctx, assetId) {
// lookup asset description
– Encapsulated in the first argument (context). ...
return description;
– E.g., ctx.stub.getState(key) }
}
– E.g., ctx.stub.putState(key, value)
– Forms the read-write set (RW-set) module.exports = AssetManager;
• Network
– Represents the set of peers associated with a network channel.
• Contract
– Represents a smart contract instance on a network channel.
– evaluateTransaction()
• Invokes a transaction function to read from the ledger (query).
– submitTransaction()
• Invokes a transaction function to modify the ledger.
• Events
– Enables applications to process emitted contract and block events.
}
Reader certReader = Files.newBufferedReader(certificatePath);
X509Certificate certificate = Identities.readX509Certificate(certReader);
Identity identity = new X509Identity("Org1MSP", certificate);
Identity
Reader keyReader = Files.newBufferedReader(privateKeyPath);
PrivateKey privateKey = Identities.readPrivateKey(keyReader);
Signer signer = Signers.newPrivateKeySigner(privateKey);
} Signer
ManagedChannel grpcChannel = ManagedChannelBuilder.forAddress("peer0.org1.example.org", 7051)
.usePlaintext()
.build();
} gRPC to Peer
}
.identity(identity)
.signer(signer)
.connection(grpcChannel);
Connect Gateway
}
try (Gateway gateway = builder.connect()) {
Network network = gateway.getNetwork("channelName");
Contract contract = network.getContract("chaincodeName");
Target contract
contract.submitTransaction("CreateAsset", "asset001", "user001", "description");
contract.submitTransaction("TransferAsset", "asset001", "user002");
} Transact
} finally {
grpcChannel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
}
Node Go
© 2022 IBM Corporation 11
Managing a gRPC connection
• The new client API doesn’t create or manage ManagedChannel grpcChannel = NettyChannelBuilder
.forTarget("peer0.org1.example.org:7051")
gRPC connections for you. .sslContext(...)
.build();
Java
Node
Node
(RW-set). Node
– Be sent to the ordering service for inclusion in block,
and subsequent validation and commit.
network := gateway.GetNetwork(channelName)
contract := network.GetContract(chaincodeName)
• Using the Gateway API, this can all be achieved in a single _, err := contract.SubmitTransaction(
line of code. "create", "asset001", "owner1", "my asset",
)
– submitTransaction()
Go
1 Endorse
2 Submit
Org2 Org3 3 Broadcast block
Peer Peer
1 3
Ordering
Org 1 Service
Client Org1
submitTransaction() Peer
2
that return ledger state, then querying can’t be done. try (Gateway gw = builder.connect()) {
Network nw = gw.getNetwork("chnlName");
Contract contract = nw.getContract("ccName");
• A transaction function that doesn’t modify the ledger state byte[] desc = contract.evaluateTransaction(
does not need to be endorsed or sent for ordering. "description", "asset001");
} finally {
– The endorsement policy is only enforced when peers ...
}
attempt to commit a change to the ledger.
Java
– To maximise the chance of returning the most up-to- const network = gateway.getNetwork(channelName);
const contract = network.getContract(ccName);
date value.
const desc = await contract.evaluateTransaction(
‘description’, ‘asset001');
• The usual application pattern is to send this data to the smart contract using transient data.
• The gateway will take a more guarded approach to which orgs are selected for endorsement.
– Will only select orgs which own a copy of the PDC being written to.
– Could result in gateway not collecting sufficient endorsements.
• Application developer can override which orgs should endorse.
• In order to maintain security of any private data used in transactions, the application should
connect to a gateway peer belonging to the same organization as the client identity.
• If the client identity’s organization does not host any peers, then a peer in another trusted
organization should be used.
• However, some transactions might have to satisfy multiple policies at the same time.
– Chaincode to chaincode calls must satisfy the policies of both chaincodes.
– Transactions that write to private data collections will also need to satisfy the collection policy.
– Transactions that read private data will be restricted to orgs that own copies of that PDC.
– Any changes to states that have state-based endorsement (SBE) policies also need to be satisfied.
• Extra options can be associated with the transaction before it is evaluated or submitted to the ledger.
– E.g., pass transient data.
– E.g., target specific endorsing organizations.
Go Node
tx.submit();
• These steps can be invoked individually in your Java
application.
– Allows more options to be assigned to each step.
– Allows results of each step to examined.
– Allows for alternative flow logic, if required.
been sent to the ordering service. // Update UI or reply to REST request before waiting
for commit status
– This returns the result of the transaction function,
– and a ‘commit’ object which can be used to wait Status status = commit.getStatus();
if (!status.isSuccessful()) {
for the commit notification. // Commit failure
}
Java
• Can be used to implement responsive, asynchronous
patterns in the client application. For example,
– Update a UI with the result.
– Return a REST response.
• The gateway implements retry logic when interacting with other peers
and ordering nodes.
• Getting help.
– Fabric mailing list: https://lists.hyperledger.org/g/fabric/topics
– Hyperledger Discord server: https://discord.com/invite/hyperledger
– Stack Overflow: https://stackoverflow.com/questions/tagged/fabric