Introduction
Since joining HelloGold I have been promising to develop some course material to train new ethereum developers.
We have, of course, been so insanely busy delivering a product then running a token sale that the teaching has had to be put on hold with the exception of running a couple of conferences but with 2018 looming it is time to get ready to deliver.
My teaching style
I teach at a number of levels but I believe that if you understand what is going on under the hood, you will understand the rest a lot better. That being said, I only cover enough of the details to de-mystify and make things understandable. And I never teach it if I do not understand it. So let's start with the basics.
Don't worry. We will be writing smart contracts soon enough.
What is a transaction in a blockchain context?
A blockchain should be an open, immutable, repeatable record of the interactions that led us from a previous genesis state to a current state. Each interaction is a transaction.
Transactions
A transactions is the fundamental record of a blockchain. In early blockchains which represented a store of values, a transactions depicted a transfer of that value. In later blockchains the data carried in a transaction may be more important than any value.
example :
{
From : Dave
To : Alice
Value : 100
Data : "Happy Birthday Alice. Buy yourself something nice"
}
This theoretical instruction would send 100 coins from me to my dear friend Alice - but there is not a lot of proof that I read it. And what is to stop Alice, at a later date from changing the 100 to 1000?
We need a two part solution
Enter the Hash Function (part 1)
The hash function is well known way to guarantee the integrity of data. Most programmers will know of them as a way to prove that a password is valid without actually storing the password.
A hash function reduces a piece of data to a number, but a number that changes dramatically for a small change in the data.
Using the Keccak 256 function we reduce any number to a 256 bit number.
The transaction above converts to :
7487306b743291719ee02a6ee15330a1f5a9fcd4a1702da1f8d51aa3f2003bc3
but if Alice were to change the 100 to 1000 we get 8b43bdfb7ece3f543c5435089150c07f75471d3f9aac1b1c59f2772963ee8f2f
Encrypting it (part 2)
All this means is that Alice would need to generate a new hash value if she wants to change the data but public key encryption provides the second part of the solution. We are not going to touch the transaction itself - we need that to be public. We encrypt the hash value so that only I can create it but anybody can verify it.
We generate a private key
using a random number function. From this we drive a public key
such that data encrypted with the private key can only be read using the public key.
Data encrypted with the private key can be read using the public key - but the only the specific private key can do the encryption.
So I have to communicate my public key. How?
Both the private and public keys are unique. This means that I can use my public key as my ID so that the transaction would now read
{
From : 0x97202b21c749b1cbf2b7bbbf23a6c60205b29e1f
To : 0x31EFd75bc0b5fbafc6015Bd50590f4fDab6a3F22
Value : 100
Data : "Happy Birthday Alice. Buy yourself something nice"
Signature : 3ccbef9b961398f44b3cc3d3539105a9f988361ed7e0b050007ff49cb930acc2
}
I am going to admit that I faked the signature - (just used the hash) but in a real system a user would decrypt the signature and compare it to the hash that they get themselves with the data that they see.
The combination of public key encryption and hashes creates a digital signature that proves that a piece of electronic data is genuine so we have an un-fakeable transaction record. Alice gets 100 coins. Lucky Alice.
A load of transactions grouped together is a block
The record of me giving Alice 100 coins would not be very credible if Alice just printed it and stuck it on her fridge.
I submit that transaction to the blockchain network so everybody knows about it.
Without going into the whole mining thing, people all over the network are assembling available transactions into blocks
that link to previous blocks. To ensure that these blocks do not get altered, each block (including a link to the preceding block) is also signed by the person who assembles it. They then distribute that block to the rest of the network. The network votes on whether to accept it. The first valid block usually wins.
So each person on the network end up with a linked list of signed blocks made up of signed transactions. Each person's list is identical.
Which is pretty much a distributed, audited, set of accounts - also known as a distributed ledger.
To find out your balance, you can simply apply each interaction in turn and watch your changing fortunes over time until you finally see your current net worth.
Coming up next : What makes ethereum so special ?