Scala hyperledger fabric client with akka actors
Akka + Hlf - A marriage made in heaven
Happyledger :)
This is the second part of my hyperledger fabric blog series - Happyledger
blog series :). In this post I’m gonna cover how to write scala
based hyperldger fabric client application. Most of the hyperledger fabric client applications writs with node sdk
. It’s worth to try it with jvm based language as well. So I have written scala
based client to interact with fabric network. Full source code of my application available at gitlab repository in here. Please clone it and follow the steps below.
1. Start fabric network
In my pervious post I have shown how to setup hyperledger fabric network with multiple orderers and kafka. In this post I’m gonna use it to setup the network. If you follow the previous post and up the network you will ended up with following running services
- One
ca
container - Three
orderer
containers - Three
peer
containers - Three
zookeeper
containers - Four
kafka
containers - One
cli
container
2. Add fabric dependency
In order to scala client to interact with hlf network build.sbt
need to have hyperledger-fabric-java-sdk
dependency. Following is the build.sbt
file dependencies(it has some other dependencies as well).
3. Implement fabric user
In order to interact with hlf network our client need to have fabric users(admin
and normal
users). So in our application we need to define a user with implementing org.hyperledger.fabric.sdk.User
interface. So I have created a PeezUser
with a case class
.
Now we can create instances of this user and interact with the hlf network peers.
4. Create CA client
All the users in hlf network need to have their own certificate materials. Since we have ca
service running we can use it to generate crypto materials for our users. That is the advantage of having ca service. All the user crypto materials can be generated via communicating with ca service.
In order to interact with ca service we need to have a ca client. Fabric java-sdk
provide HFCAClient
which we can use instantiate a ca client in our scala application. This function takes the ca service uri
as an argument. In our case we are running ca service on 7054
port on local machine.
5. Enroll users
Now our ca client is created, so we can enroll users to the network by generating crypto materials.
There are two types of users in hlf, admin
and normal user
. In here I have two functions to enroll the different user types. enrollAdmin
enrolls the admin users. When enrolling admin users, it user admin user credentials which defines in docker-compose ca service command
section. Function enrollUser
enrolls normal users(note that to enroll normal users we need to have admin user).
When enrolling a user, ca client returns the enrollment object
which contains private key and the certificate of the user. In here I’m using java object serialization to save that enrollment object in local file system. So when enrolling a user it first checks weather the given user already enrolled by UserStore.get(userId)
function. If user already enrolled we take enrolled user(UserStore
manages saving and loading users from file system).
6. Create fabric client
Next thing is to create hyperledger fabric client which comes in HFClient
. This client do all the transaction executions, channel discoveries etc with the hlf network.
7. Connect to channel
Now we can connect to channel by using previously created client. When connecting to channel, we need to define the peer
, orderer
and channel name
. All the peers and orders in hlf exposes GRPC
endpoints (peers and orders have their own GRPC endpoints to talk). We need to define GRPC end points of peer and orderer like below and connect to the channel.
8. Execute query transactions
There are two types of transactions which can execute in fabric chaincodes, query
and invoke
. Following is the function to execute query
transaction on mycc
chaincode which installed on our fabric network.
There is a builder which comes with ChaincodeID
. We can set chaincode name
, function
and arguments
with this builder and execute the queries.
9. Execute invoke transactions
Following is the function to execute invoke
transaction on mycc
chaincode which installed on our fabric network.
In here we need to send TransactionProposal
to the network initially. Then it will response back the ProposalResponse
list. If all the responses are correct, we can send the transaction to the network with sendTransaction
.
The sendTransaction
function returns CompletableFuture
with TransactionEvent
. We can handle the future and obtained the TransactionEvent
to decide weather the transaction is successful or not( weather transaction included to a block or not).
9. Akka actor to communicate
Finally I have created akka actor
to interact with the functions defined in above section. It consumes Init
and Exec
messages. When Init
message comes actor setup the ca client
, hlf client
and channel
. When ExecQuery
message comes it execute query transactions to the ledger. For ExecInvoke
message it executes invoke transactions to the ledger.
References
- https://medium.com/@itseranga/hyperledger-fabric-setup-with-multiple-peers-and-orderers-with-kafka-542023787a6d
- https://medium.com/@lkolisko/hyperledger-fabric-sdk-java-basics-tutorial-a67b2b898410
- https://codeburst.io/a-concise-tutorial-on-working-with-hyperledger-fabric-java-sdk-a6f11d8bb5b0