Friday, 27 April 2018

Teaching Blockchain and Distributed Ledger 2: Some Programming

In a previous post, I discussed examples of the work I have done on teaching introductory blockchain. In that post, a kind of 'unplugged' approach was being used; a paper-based game and exercises in starting to think through some issues.

Within this post some of the teaching programming examples used in the class and the logic behind them will be discussed. Health warning: These are simple examples; only meant to get some basic ideas across that the students can then build on.

1. Javascript version
This section looks at a producing a blockchain on a local machine - to build up the ideas gradually. Confession time, my starting point was the fantastic videos shown below from Simply Explained -Savjee - really nice introduction.

So as in the video Visual Studio Code was used to develop the solution; using JavaScript and running Node.js - a little bit of setting up is needed but certainly on a Mac or Linux machine it wasn't too difficult. So the code was built up and the following produced.
//adapted from the videos of Simple Explained -Savjee

const SHA256 =require('crypto-js/sha256');

class Block{
constructor (index,timestamp, data, previousHash = ''){
this.index = index;
this.timestamp = timestamp; = data;
this.previousHash = previousHash;
this.hash = this.calcHash();
this.nonce =0;

return SHA256(this.index+this.previousHash+this.timestamp+JSON.stringify(;

while(this.hash.substring(0,difficulty) != Array(difficulty+1).join("0")){
this.hash = this.calcHash();
console.log("Block mined hash: "+ this.hash);

class Blockchain{
this.chain = [this.createGenesisBlock()];
this.difficulty = 4;

return new Block("0","13/3/2017","Start","0");

return this.chain[this.chain.length-1];


for (let loop=1; loop<this.chain.length; loop++){
const current = this.chain[loop];
const prevOne = this.chain[loop-1];
if((current.hash != current.calcHash())||(current.previousHash != prevOne.hash)){
return false;
return true;

let educoin = new Blockchain();
educoin.addBlock(new Block(1,"10/7/2018", {name : "Ali", qual : "PhD", qual1 : "BSc"}));
educoin.addBlock(new Block(2,"10/7/2018", {name : "Scott", qual : "PhD"}));
educoin.addBlock(new Block(3,"10/4/2018", {name : "Scott", qual1 : "Swimming Certificate"}));

console.log("Is the chain valid ? "+educoin.isChainValidOne().toString());

The idea of going down this route was the students are familiar with JavaScript so it follows on from what they know; promotes the idea that blockchains are not language specific and lastly the elements of the block and blockchain can be seen - without the language getting 'clever' and hiding how it is done.

2. Solidity
The second stage was build up an example to go onto the Ethereum blockchain. So I decide to show Solidity, which is a programming language for doing this. After experimenting with various options I decided to use the free (always good) online integrated environment from Ethereum called Remix ( and stick with the online version. Remix meant that there wasn't any extra installation, it is pretty self-contained and it comes from Ethereum itself. 

So the code produced is inspired by the #BlockchainEducationalPassport ( project - but much simpler. The code sets up a record for a student (name and qualification) and adds it to a list/array of students.

pragma solidity ^0.4.0;

contract educoin1 {
        struct edRec {
            string name;
            string qual;
        address public student;
        mapping (address => edRec) public Students;
        address[] studentsByAddress;
        function add(string _student, string _qual) public {
            address thisAddress=msg.sender;
            Students[thisAddress].name = _student;

The figures below show a 'record' being added (figure 1) and then looking at waht is stored (figure 2)

Figure 1: Entering the record

Figure 2: Seeing the transactions
Following links to material on Solidity, the students were asked to alter the solution in ways that interest them.

3. Where next
The final section was to look at Distributed Ledger alternatives to Blockchain - but not the programming them so the Tangle used in IOTA was discussed.

All views are the authors, and may not reflect the views of any organisation the author is connected with in any way.

No comments:

Post a Comment