Blockchain Workshop

Blockchain

Accounting professionals need to understand blockchain, distributed ledger technologies and cryptocurrencies in order to evaluate their impact on the accounting profession, business and accounting education. Given that accounting professionals do not necessarily want to become programmers, cryptographers or database experts, what is the essential technical knowledge that they need to have in order to evaluate and prepare for the effect of these disruptive technologies?

The objective of this hands-on interactive workshop is twofold:

  • First, provide attendees with the foundational knowledge needed to understand how a permissionless blockchain (like the one supporting bitcoin works).
  • Second, provide attendees with a basic understanding of permissioned blockchains in business using Hyperledger Fabric - an open source platform for developing blockchains and smart contracts (IBM and the Linux Foundation).

Advance Preparation

Part of the presentation is based on the following teaching notes:

Stratopoulos, T. C., & Calderon, J. (2019). Introduction to Blockchain, which is available for free for download from SSRN: https://papers.ssrn.com/abstract=3395619. Please download and review these notes before the workshop.

The hands-on component of the workshop will use the Hyperledger Composer.

Please check whether you are able to download the following.

Using your browser go to https://hyperledger.github.io/composer/latest/

If you would like to do further preparation, the material for the hands on component of this workshop follow:

Click: Try it online   

Click: Let’s Blockchain / Playground tutorial

Click: Introduction

Hyperledger Composer architecture (Playground tutorial /Introduction)

  • A template-driven blockchain development tool set
  • Model a business network by identifying the business assets you want to track on the blockchain, identifying the participants who are authorized to interact with and own the assets on the blockchain, and identifying the transactions that can be performed on the assets on the blockchain

Hyperledger is an open source blockchain development platform hosted by the Linux Foundation (www.hyperledger.org/about). A blockchain is a peer-to-peer distributed ledger/database with each party on the network having his/her own copy of the ledger; making for one shared version of the truth. It is a new way to conduct transactions between multiple parties with trust being built into the blockchain network technology instead of a third party like a bank. The way it works can be summarized as follows:

  • An asset (something of value) is registered on the network – a bike owner becomes an authorized user by downloading an app and registering his/her bicycle
  • An authorized user submits a transaction to change the status of his/her asset – like report a stolen bicycle
  • The transaction is verified by several nodes on the network agreeing that it is legitimate – referred to as a consensus protocol
  • The verified transaction is added to a block of transactions, connected digitally to the previous block of transactions, encrypted, and added as the next block on the chain
  • Other authorized users on the network, using their own apps, can access and view transactions on the blockchain
  • The blockchain represents a complete, unchangeable, history of all of the transactions related to an asset

Composer is a modeling front end that allows you to set yourself up as an administrator on the network, define your business network using an object-oriented modeling language, test it by passing data to actual objects, like an asset and a participant, and deploy it on a sample blockchain (Hyperledger Fabric).

Connecting & establishing our access credentials

The first thing that we have to do is set up a connection profile. Hyperledger Composer uses connection profiles to define the system to which to connect – a connection profile is a JSON file that becomes part of your business network card; which is your blockchain access credential (your blockchain wallet).

To create your connection profile – using IE or Edge go to https://hyperledger.github.io/composer/  

*At this point, what you see may vary depending on whether you have used Hyperledger Composer in the past. You may have to click  Admin / My Business Networks /

Deploy a new business network in order to set up a new blockchain network.

  • Click Try it online / Let’s Blockchain 
  • Click Deploy a new business network
    • Name yur network: bicycle-netwrk
    • Describe it: A blckchain for tracking my bicycle
    • Name yur network admin card: admin@bicycle-netwrk
    • Chose a Business Network Definition to start with: basic-sample-netwrk
    • Click Deply (at the bttom of the  CONNECTION PROFILE windw)

You should now have an admin@bicycle-network card in your My Business Networks window – also called your wallet. Your card has all of the information necessary for you to connect to your deployed blockchain bicycle-network; including an identity and permissions for a single (admin) user. This establishes you as a legitimate member of this new blockchain and allows you to connect; it also gives you administrative access privileges.

** If you are not able to establish your credentials, you will need to open your text editor and create your model and sample objects offline. This will allow you to building your sample network but will not allow you to submit test transactions. For a text editor, I suggest Notepad ++ for Windows users and Sublime for Mac users.

Defining our bicycle network

Click Connect now and you should see the Web bicycle-network Hyperledger window.

Notice that we are in the Define mode – which is where we define the files that make up the blockchain network’s definition – the Model, Script, and Access Control files. In the Model file, we will define the Assets, Participants, and Transactions that are allowed in our business blockchain network. In this tutorial, we are going to use the Basic Sample code from Hyperledger Composer for our Model, Script, and Access Control files to explain and demonstrate how to work with a blockchain.

Click Model File and you should see the following code in the Model file editor:

OR, if working offline, open the ExampleModel.txt file in your text editor.

/**

 * Sample business network definition.

 */

namespace org.example.basic

asset SampleAsset identified by assetId {

  o String assetId

  --> SampleParticipant owner

  o String value

}

participant SampleParticipant identified by participantId {

  o String participantId

  o String firstName

  o String lastName

}

transaction SampleTransaction {

  --> SampleAsset asset

  o String newValue

}

event SampleEvent {

  --> SampleAsset asset

  o String oldValue

  o String newValue

}

This is the object-oriented Modeling language used to define a blockchain business network model. The code is easy to understand:

  • The first 3 lines are documentation
  • The fourth line (namespace org.example.basic) defines the namespace that will be used to identify our blockchain business network. A namespace identifies a location on the Web (or any network) where computer resources are located.
  • The next line (asset SampleAsset identified by assetId {  ) defines a class of asset objects named SampleAsset that is identified by an attribute named assetId; and the object’s attributes are defined within the code brackets { … … }. An object is an identifiable thing (e.g., an asset) that we are going to track on this blockchain. In an object-oriented language, a class is a template to be used to instantiate actual objects representing real-world things (e.g. a bicycle).
  • The next 3 lines, all inside the code brackets { … } define the attributes of the asset class of objects named SampleAsset:
    • String assetId defines an attribute named assetId as having a data type f String (a string f characters, as pposed to a number r a date);
    • --> SampleParticipant wner defines an attribute named wner and assigns t it the participant class named SampleParticipant;
    • String value defines a string attribute named value representing an initial characteristic f this SampleAsset.
  • The next line (participant SampleParticipant identified by paticipantId { ) defines a class of participant objects named SampleParticipant that is identified by an attribute named paticipantId; and its attributes are defined within the code brackets { … … } as follows:
    • String paticipantId – a string f characters identifying each participant object;
    • String firstName and String lastName are self-explanatry.
  • The next line ( transaction SampleTransaction { ) defines a class of transaction objects named SampleTransaction. Within its code brackets, --> SampleAsset asset defines an attribute named asset and assigns to it the asset class named SampleAsset and String newValue  defines an attribute named newValue.
  • The next line ( event SampleEvent {  ) defines a class of event objects named SampleEvent. Within its code brackets, --> SampleAsset asset defines an attribute named asset and assigns to it the asset class named SampleAsset and String oldValue and String newValue define two attributes oldValue and newValue. An event is used to notify applications attached to the blockchain that a transaction has occurred affecting the values of an asset.

Remember, this is an object-oriented modeling language. All object-oriented languages define classes of objects that act as a template with which to create actual instances that represent objects in the real world. All real world objects have to have a class definition, like we see in this modeling language. We are actually using a proprietary object modeling language to create the blockchain network Model file defining the Assets, Participants, and Transactions classes that are allowed in our business network. It will be stored in our namespace (org.example.basic) as part of the package making up the Sample Business Network Archive blockchain. Next we will take a look at the Script file.

As mentioned previously, transactions in Hyperledger Composer are implemented with JavaScript functions in a Script file. In the Web basic-sample-network Hyperledger window, click Script File. You should now see the Script File editing window.

OR, if working offline, scroll down in the ExampleModel.txt file to the Beginning of JavaScript file.

/* global getAssetRegistry getFactory emit */

/**

 * Sample transaction processor function.

 * @param {org.example.basic.SampleTransaction} tx The sample transaction instance.

 * @transaction

 */

async function sampleTransaction(tx) {  // eslint-disable-line no-unused-vars

    // Save the old value of the asset.

    const oldValue = tx.asset.value;

    // Update the asset with the new value.

    tx.asset.value = tx.newValue;

    // Get the asset registry for the asset.

    const assetRegistry = await getAssetRegistry('org.example.basic.SampleAsset');

    // Update the asset in the asset registry.

    await assetRegistry.update(tx.asset);

    // Emit an event for the modified asset.

    let event = getFactory().newEvent('org.example.basic', 'SampleEvent');

    event.asset = tx.asset;

    event.oldValue = oldValue;

    event.newValue = tx.newValue;

    emit(event);

}

This is a JavaScript function (actually Node.js for network applications) that implements a transaction on our sample network. The code operates as follows:

  • The first few lines are documentation. Notice that the @param {org.example.basic.SampleTransaction(tx) {  documents that this Script file applies to a SampleTransaction in our namespace.
  • The next line of code (const oldValue = tx.asset.value; ) is a JavaScript statement using the constant keyword (const) (indicating that this value cannot be changed) that assigns the current value of the asset’s property named value to a new variable named oldValue; which saves it for future use.
  • The next line of code (tx.asset.value = tx.newValue ) is an assignment statement ( = ) that assigns the current value of the variable newValue to the asset’s property named value.  
  • The next two lines of code gets information from the assetRegistry that holds our blockchain code and updates the asset with its new value.
  • The last five lines of code passes the asset data to an event handler function to report it on the blockchain.

If you are familiar with JavaScript, this code is straight-forward. Note that the async function is necessary because the code is being run on a Node.js server. Similarly, the getAssetRegistry function is a reference to Hyperledger’s way of storing all of the files that make up our blockchain model in an asset registry in our namespace (org.example.basic).

The final file that we need to include in our business network definition is the Access Control file. If you click Access Control in your Web basic-sample-network window, you will see a Sample access control list containing a number of rules which define what network participants can do; including the following: all participants have READ access to the materials on the blockchain; all participants can CREATE transactions; and all participants that are asset owners have ALL access to their assets. We do not need to change these business Access Control rules. This file will also be stored in our namespace (org.example.basic) as part of the package making up the Web basic-sample-network blockchain.

Testing our bicycle network

At this point, we have established our Admin access credentials, defined our network based on Hyperledger Composer’s Sample Business Network using the SampleAsset, SampleParticipant, SampleTransaction, and SampleEvent classes, and deployed it on the blockchain. Since we have administrative access, we can now test it by instantiating objects based on the predefined sample classes (templates) with actual values. We will then run it to see if it works as defined.

To get into Test mode, click Test (on the menu bar) and you should see the Participant registry for org.example.basic.SampleParticipant.

OR, if working offline, scroll down in the ExampleModel.txt file to Our test participant object.

Test mode Participant registry

Click the + Create New Participant button and you should see a JSON Data Preview file based on the SampleParticipant class from our Model file.

{

  "$class": "org.example.basic.SampleParticipant",

  "participantId": "3010",

  "firstName": "",

  "lastName": ""

}

Make a note of your “participantId” : “value” (e.g., 3010) and then enter your first name as the value for the “firstName” variable (e.g., “firstName”: “your first name” ) and do the same for the “lastName” variable, and click the Create New button (at the bottom of the Create New Participant editing window). The end-result should be a new participant object in our Participant registry.

A participant object in our Participant registry – a JSON data file

This data is saved in a participantRegistrary file in our network namespace.

Next, click SampleAsset and then click the + Create New Asset button and you should see a JSON Data Preview file based on our SampleAsset class that we created in our Model file.

OR, if working offline, scroll down in the ExampleModel.txt file to Our test asset object.

Make a note of your “assetId” : “value” (e.g., 9663) and then on line 4 changeSampleParticipant#xxxx  to your participantId value that you noted when you created your SampleParticipant in the previous step

(e.g., “owner”: “resource:org.example.basic.SampleParticipant#xxxx” ) – this establishes you as the owner of this asset. Next, enter a value for the “value” variable (e.g., “value”: “1000”). Next, click the Create New button and the result should be a new asset object owned by you in our Asset registry.

An asset object in our Asset registry – a JSON data file

This data is saved in an assetRegistrary file in our network namespace.

Next click Submit Transaction and you should see a JSON Data Preview file based on the SampleTransaction that we are using from model file.

OR, if working offline, scroll down in the ExampleModel.txt file to Our test transaction object.

 On the “asset” line, changeSampleAsset#xxxx  to your assetId value that you noted when you created your SampleAsset in the previous step

(e.g., “asset”: “resource:org.example.basic.SampleAsset#xxxx” ) – this establishes this transaction as applying to the SampleAsset that you created. Next, enter a value for the “newValue” variable (e.g., “newValue”: “800”). Next, click the Submit button. You should see a popup window indicating that the transaction was successful.

To see the result of your transaction, click All Transactions and then click view record for the most recent transaction. The result should be similar to below:

OR, if working offline, scroll down in the ExampleModel.txt file to Our test transaction.

A transaction object in our Transaction registry – a JSON data file

Notice the transactionId – this is a unique ID on our blockchain network created by hashing all of the data in this transaction, including the timestamp.

An event object in our Transaction registry – a JSON data file

Remember we also defined an event in our JavaScript transaction file. An event is a special type of object created so that other users on the network, such as the Police and the insurance adjustor, can see the results of a transaction.

Summary

We have now created a business blockchain model using the Hyperledger Composer object-oriented modeling language and tested it on a sample Hyperledger Fabric blockchain. Whether you were able to work online or off, you played the role of a network administrator and used a text editor to write code to define and then test a blockchain. The following are several important things to realize when contemplating a business blockchain network:

  • To create a blockchain, we simply need an asset, participants, and transactions and we model each by defining a class of objects with attributes that describe them.
  • Blockchain objects are instantiated using JSON to assign values to the attributes defined in the respective object classes.
  • Blockchain transactions are simple in that they change the values of an asset’s attributes.
  • Business blockchains can have many different types of participants; for example, an asset owner and other interested parties that simply monitor events.
  • In terms of whether a blockchain is an appropriate solution for a business problem, you are only limited by your imagination – a bicycle tracking network involving owners, police, and insurance companies for example.

My intent in creating this tutorial is to introduce you to the mechanics of defining a business blockchain – one that is based on tracking changes to an asset as opposed to trading a cryptocurrency. At the beginning of writing this tutorial, Hyperledger Composer was a Web-based platform for building experimental blockchain applications. Today it has been deprecated in favor of development on the Hyperledger Fabric Unix-based platform. However, the tools and techniques introduced in this tutorial, including the object-oriented modeling language and the JSON data objects, are the foundation for the entire Hyperledger platform.