Building Blockchain DApp's on Service Fabric
This post is first in its series to give high level overview of building an end-to-end DApp on Service Fabric. If you are new to Blockchain or Service Fabric here are some resources to get started :
- Blockchain as a Service : https://azure.microsoft.com/en-us/solutions/blockchain/
- Service Fabric Overview : /en-us/azure/service-fabric/service-fabric-overview
For this post I will start with a simple supply chain visibility scenario and a simple ASP.net Core web app. I will add more modules around IoT, Counterfeiting, Provenance in upcoming posts. Let's get started.
( Source Code for this post is available on Github @ https://github.com/jomit/sfblockchain )
Setup Development Environment
- Install Node.js v6+ : https://nodejs.org/en/
- Install Solidity Compiler : https://solidity.readthedocs.io/en/develop/installing-solidity.html
- You can also use truffle. I will cover that in next posts.
- Install Geth : https://geth.ethereum.org/downloads/
- Make sure Geth is added to the PATH
- Install Visual Studio 2017 : https://www.visualstudio.com/downloads/
- Install Service Fabric SDK : /en-us/azure/service-fabric/service-fabric-get-started
Setup Private Blockchain locally
We can use TestRPC to simulate an Ethereum client but I have chosen the Go Ethereum client - Geth to create a more real world blockchain environment.
- Clone the repo that I mentioned above and run the "testnet\startgeth.bat" file
- Type "eth.accounts" at the prompt. You should see the 2 default accounts.
- Type "exit" to stop the client.
Create and Compile Smart Contract
The contract is written in Solidity and I am using the solidity command line compiler to generate the .abi and .bin files necessary to deploy the contracts in blockchain. You can also use truffle or some other framework to do the contract compilation and deployment.
- Open "contracts\SampleSupplyChain.sol" to see the contract.
- It contains 2 simple functions , "placeOrder" and "manufacturingComplete" to perform state changes on the "orderStatus" variable.
- The function "getOrderStatus" returns the current value of the orderStatus variable. We will use this function to verify the contract invocation.
- Run "contracts\compile.bat" to compile the contracts. ( You may see some warnings, you can ignore them for now.)
- Once the compilation is complete you should see 3 new files in the folder:
Deploy Smart Contract and call its functions
- Open the "SFDApp\SFDApp.sln" solution in Visual Studio.
- The solution is a Service Fabric App with one ASP.net Core Stateless Service.
- The ASP.net Core Web App uses nethereum library to interact with the Go Ethereum client.
- Open the "ContractWebApp\Services\EthereumService.cs" file
- Now first start the blockchain client using "testnet\startgeth.bat" as shown above and keep it running.
- Then run the SF Application locally and browse to the Web App Home Page
- Make sure to use the full path : "https://localhost:8888/Home/Index"
- Step 1) Click on Deploy Contract
- This will deploy the smart contract in the blockchain and return the Contract Address
- See the logs in the blockchain client command line window
- Step 2) Click on Place Order
- This will call the "placeOrder" function of the Smart Contract
- Now click on "Check Order Status" button to see the order status. It should be '0'.
- This will call the "getOrderStatus" function of the Smart Contract
- Step 3) Click on Manufacturing Complete
- This will call the "manufacturingComplete" function of the Smart Contract
- Now click on "Check Order Status" button again, this time the status should be changed to '1'
As I mentioned in the introduction, this post is a very high level overview of creating a DApp. In the coming posts I will dig deeper into the details and other components including message ingestion, key signing, off chain storage & compute and details on setting up network infrastructure for private blockchain in Azure.
- Service Fabric Learning Path : https://azure.microsoft.com/en-us/documentation/learning-paths/service-fabric/
- Service Fabric Patterns and Practices : https://mva.microsoft.com/en-US/training-courses/service-fabric-patterns-and-practices-16925?l=N2KwbbSGD_6405167344
- Azure Blockchain Service: https://azuremarketplace.microsoft.com/en-us/marketplace/apps/microsoft-azure-blockchain.azure-blockchain-service
- Project "Bletchley" : https://github.com/Azure/azure-blockchain-projects/blob/master/bletchley/bletchley-whitepaper.md
- Azure blockchain PoC framework : https://azure.microsoft.com/en-us/blog/azure-introduces-a-new-blockchain-proof-of-concept-framework-for-developers/
- Enterprise Smart Contracts : https://azure.microsoft.com/en-us/blog/introducing-enterprise-smart-contracts/