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
//https://www.youtube.com/channel/UCnxrdFPXJMeHru_b4Q_vTPQ

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

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

calcHash(){
return SHA256(this.index+this.previousHash+this.timestamp+JSON.stringify(this.data)+this.nonce).toString();
}

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

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

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

getLatestBlock(){
return this.chain[this.chain.length-1];
}

addBlock(newBlock){
newBlock.previousHash=this.getLatestBlock().hash;
newBlock.mineBlock(this.difficulty);
this.chain.push(newBlock);
}


isChainValidOne(){
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(JSON.stringify(educoin,null,4));
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 (http://remix.ethereum.org/) 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 (https://blockchainedupass.uniteideas.spigit.com/Page/Home) 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;
            Students[thisAddress].qual=_qual;
            studentsByAddress.push(thisAddress);
        }
}

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 https://www.iota.org/get-started/what-is-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