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 :

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

 

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:
    • SampleSupplyChain.abi
    • SampleSupplyChain.bin
    • SampleSupplyChain.json

 

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
    • Update the "GetContractAbi" and "GetContractByteCode" methods to return the updated abi and bin values from the "SampleSupplyChain.abi" and "SampleSupplyChain.bin" files created during the contract compilation step.
  • 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'

 

Next Steps

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.

 

Additional Resources