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.

Sunday 22 April 2018

Teaching Blockchain and Distributed Ledger approaches 1

Recently started developing materials to teach blockchain to a group of MSc Computing students within a module on internet programming. So what is the problem? 

Not really a problem, but:

  • deciding where to start;
  • wanting the students to consider what are blockchain and distributed ledger techniques;
  • thinking like programmers 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.
1. First teaching approach
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.


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.
Start of the blockchain game.


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. Summary
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.

In the next post in the series, I will look at the coding.


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

Saturday 21 April 2018

Teaching VR through the Web

This post documents my personal experience with teaching WebVR - Web based Virtual Reality within a module on Internet Programming. In this case, based on the wonderful A-Frame (https://aframe.io) and exploring it with MSc Computing students.

The three insights: 

  • It is great fun (which I did expect) to teach;
  • It is nowhere near as hard as I thought it was going to be when I first started;
  • Students seem to enjoy it


1. The approach
I needed a way of doing it that doesn't require a lot of setting up and can be done on a variety of machines. The approach used was using A-Frame (https://aframe.io) inside Thimble (https://thimble.mozilla.org ). Thimble was selected because it is an online editor,  simple to use, it is free and you see the preview immediately. One restriction to bear in mind is that the filesizes of images and videos have to be small though.



2. How easy is it?
You can treat it as if was HTML, after you have added the script file shown in bold.

    
   

The video below shows setting up and adding a box to the scene.



This next video takes this a little further by adding rotation to an object.

 

In this video below, mapping an image to an object and changing camera position is looked at.

 



3. Adding video
Actual in some ways it as easy to add video as adding an image, at it's simplest adding src="" with either the URL or relative filename in the speech marks can be used for both images and video. Alternatively using  combination with again the filename or URL between speech marks adds a block and pastes the video on top. The video below shows a worked example of these two approaches

 

4. 360 degree video.
A-Frame allows 360 degree to be incorporated into the scene using the  tag. The video below shows a worked example of this. The video below shows another worked example.

 

5. 3D objects and Assets
We can also add 3D models that others have developed into our scene. In the video below a Penguin, defined externally using .obj for the model and .mtl for the material, is loaded into the scene.

 



6. Overview
Teaching this was fun, and lends itself to a problem-focused approach. Small projects are set up and worked through but the visual nature I believe encourages experimentation. The students were very quickly able to develop their own small projects and were experimenting with a range of objects.

One interesting feature I found when looking around for supporting materials is there is a very limited range of books on A-Frame and WebVR but some fantastic resources online including the best place to start: https://aframe.io/docs/0.7.0/introduction/ 


All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon