Saturday 28 April 2018

Teaching Blockchain

Recently started developing materials to teach and deliver some of the ideas around blockchain to a group of MSc Computing students within a module on internet programming. This post is really is reporting what I did, but I would be very interested to hear from others on their ways of delivering this topic.




1. First teaching approach

Goals I was hoping for, were:

  • For the students to consider what are blockchain and distributed ledger techniques;
  • Getting them to thinking like programmers (which they are) about the techniques rather than the hype - they may in the future be the one who has to persuade someone to either go with a distributed ledger solution or equally not.
  • For the students to see code that builds up a local blockchain (local to their machine) to see the basic principles.
  • To play with code for developing Smart Contracts (in this case Solidity) and think about how it could be expanded upon.

My decision was to initially go with a flipped approach of providing a set of materials on blockchain before the session, expecting the students to use them with some guidance and following it with the 'blockchain game' and user case example activity. The next session focused on introducing a bit of blockchain programming with some programming examples.

Separate to the two sessions focused on here, awareness of blockchain was 'sneaked in'/embedded in a few places within the module. 
  • Often as an example of current practice in internet programming. 
  • A further example, took the form of blockchain project (the #BlockchainEducationalPassport (https://blockchainedupass.uniteideas.spigit.com/Page/Home) project) was used as a case study as part of their assessed work. They didn't need to produce a blockchain but it was more along the lines of:
    • What data would you store for this case study?
    • Now you have the data what could you do with it?


2.The Blockchain Game


2.1 Rules You will be placed into groups by the tutor, please do not use the computer for anything else during the task part of the activity, but as a source of the rules of the activity. Each group will need

  • one sticky note/sheet of paper per group to store the 'blockchain'
  • paper and pen for each member of the group.
Please note groups are of different sizes on purpose - this is part of the exercise. 
Now your group's sticky note divide the note up in the same way as below.





We are now ready to start the game.


Rounds in the game
1. The tutor will provide a new 'block' it will be in this case a single number each round, this is the data. (Note: in an actual blockchain this will be a much more complex bit of data).


2. To slow the system down, increase security - the winner needs to do some work. This is the Proof of Work concept. Each member of the group needs to individually calculate the new Hash value using the following rule:
HASH = Prev + (Data*25)-1255
3. Each group decides on their answer and is trying to be the first to give the correct answer. Only one answer per group at a time. Before starting the game the group will decide on a mechanism for giving the answer - e.g. agree and answer or first to get the correct answer says it.
4. The tutor will decide who wins - the decision is final.


5. The winning team gets the win and every group now writes down the data and the new hash BUT only after the winner has been found. also start a new block with the hash just being calculate written in as the previous hash in the new block.


6. So a blockchain has a new item in the blockchain and every group's blockchain is the same. Now go back to step 1 the games carries on until the tutor ends it.


 



2.2 Reflection activities
Groups that have only one member during the task join together now to be a new group called the 'one and only'. Groups with two members only will join together to be a single larger group called 'two's companies' and the group(s) with four or more members will join together to form a single group called 'hydra'
Step 1 Individually - no discussion at this time do the following (15 minutes)
1. What I have learnt during this activity?
2. Do you think there is some benefit to larger group sizes for this problem?
3. What do you think the role of the hash and proof-of-work is?
4. What do you think would happen if someone tried to alter the data in the middle of the blockchain after other blocks have been added?
Step 2 Group. (15 minutes)
Using the same questions 1 to 4 come up with, after discussion, a single set of answers for the whole group.
Step 3 Sharing. Each group will present their findings. (10 minutes)
When not presenting the other two groups are expected to listen, take notes where appropriate and after the presenting group has finished be prepared to ask questions.
Step 4 (outside of the class): To consider individually or as a group.
(a) Think about your individual answers, group's answer, new insights you gained from the other groups and from sources external to the class; then possibly revise your answers. 
(b) Can you improve the game, for example change it so it might take several iterations to get the right hash (look up how bitcoin does this - no need in the game though for SHA256)



3. User Case Activity
In this activity, the three groups are given three different scenarios; sample examples include a social solution, cryptocurrency or supply chain activity. They were asked to consider a range of issues that a programmer or developer might have to consider for example
- What data would need to be stored within the blockchain for this scenario?
- Who would hold and mine a copy of the blockchain?
- Why would someone hold and mine the blockchain? What is their incentive?
- Why not use a centralised database for the scenario?


4. Reflection and where next
These initial activities were purposely designed to not involve coding. Though the choice of programming language is not always independent of the approach, I believe cutting through to the requirements is a central part of the programming the solution, and this is largely independent of the approach.

The previous section provided been an opportunity to think about blockchain and be critical of it. These students though have mostly come from through a technical computing route, and so some of the teaching programming examples used in the class and the logic behind them will be discussed. These are simple examples; only meant to get some basic ideas across that the students can then build on.



5. 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 - I recommend these as a starting point.






Visual Studio Code was used with 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 comfortable with; 
  • promotes the idea that blockchains are not language specific; 
  • the elements of the block and blockchain can be seen - without the language getting 'clever' and hiding how it is done.




6. 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 2) and then looking at waht is stored (figure 3)


Figure 2: Entering the record

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




7. Where next
The final part of the session was to look at Distributed Ledger alternatives to Blockchain - but not the programming them. Tangle used in IOTA https://www.iota.org/get-started/what-is-iota was discussed, to give a contrast with the serial nature of blockchain. As IOTA is mainly used in the Internet of Things (IoT), it also formed a nice link to the next part of the module thinking about IoT.

Update: On reflection, it made a good assessment vehicle - it is relatively new so there is a lot for Masters students to explore but there are a lot of great resources out there as well to help. 

I would love to hear how others are teaching this subject; please free to add your approaches in the comments section of the post.



All views and opinions are the author's and do not necessarily reflected those of any organisation they are associated with. Twitter: @scottturneruon

No comments:

Post a Comment