A Practical Introduction to Blockchain

Within a blockchain infrastructure, we must create a genesis record, and which defines the starting point of the blockchain. This will…

A Practical Introduction to Blockchain

Within a blockchain infrastructure, we must create a genesis record, and which defines the starting point of the blockchain. This will contain the initial accounts that own cryptocurrency. Initially we can create an Ethereum blockchain using Geth with [Linux version]:

C:\Program Files\Geth> geth --datadir=d:\eth6 init customg.json
INFO [06-26|21:42:43] Allocated cache and file handles database=d:\\eth6
\\geth\\chaindata cache=16 handles=16
INFO [06-26|21:42:43] Writing custom genesis block
INFO [06-26|21:42:43] Successfully wrote genesis state database=chaindat
a hash=10367b.67437b
INFO [06-26|21:42:43] Allocated cache and file handles database=d:\\eth6
\\geth\\lightchaindata cache=16 handles=16
Fatal: Failed to write genesis block: database already contains an incompatible
genesis block (have 0c5f429f24f7078a, new 10367b56f68be716)

The genesis block is created with:

{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty": "200000000",
"gasLimit": "0x3d0900",
"alloc": {
"228041751ddb7365cc4bc75c4985d14d5db2432f": { "balance": "30000000" },
"cdfc92d1b5dd1c9ee1c9e2368abc86a193ae35a5": { "balance": "40000000" },
"c9c425ae15a0e66500ecf5b7a1c10c6ed35600b9": { "balance": "0x400000000000000" }
    }
}

In this case we have given the account “0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9” a balance of 0x400000000000000. Next we will start Geth:

C:\Program Files\Geth>geth --datadir=d:\eth6
INFO [06-26|21:42:53] Starting peer-to-peer node instance=Geth/v1.
6.6-stable-10a45cb5/windows-amd64/go1.8.3
INFO [06-26|21:42:53] Allocated cache and file handles database=d:\\eth6
\\geth\\chaindata cache=128 handles=1024
WARN [06-26|21:42:53] Upgrading chain database to use sequential keys
INFO [06-26|21:42:53] Database conversion successful
INFO [06-26|21:42:53] Initialised chain configuration config="{ChainID:
15 Homestead: 0 DAO: DAOSupport: false EIP150: EIP155: 0 EIP158: 0
Metropolis: Engine: unknown}"
INFO [06-26|21:42:53] Disk storage enabled for ethash caches dir=d:\\eth6\\get
h\\ethash count=3
INFO [06-26|21:42:53] Disk storage enabled for ethash DAGs dir=C:\\Users\\Ad
ministrator\\AppData\\Ethash count=2
WARN [06-26|21:42:53] Upgrading db log bloom bins
INFO [06-26|21:42:53] Bloom-bin upgrade completed elapsed=10.000ms
INFO [06-26|21:42:53] Initialising Ethereum protocol versions="[63 62]
" network=1
INFO [06-26|21:42:53] Loaded most recent local header number=0 hash=103
67b.67437b td=200000000
INFO [06-26|21:42:53] Loaded most recent local full block number=0 hash=103
67b.67437b td=200000000
INFO [06-26|21:42:53] Loaded most recent local fast block number=0 hash=103
67b.67437b td=200000000
INFO [06-26|21:42:53] Starting P2P networking

Next we will connect to the Geth from another terminal and create a new account (and which has a public and a private key):

C:\Program Files\Geth>geth attach
Welcome to the Geth JavaScript console!
instance: Geth/v1.6.6-stable-10a45cb5/windows-amd64/go1.8.3
coinbase: 0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9
at block: 0 (Thu, 01 Jan 1970 00:00:00 GMT)
datadir: d:\eth6
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txp
ool:1.0 web3:1.0
> personal.newAccount("Qwerty")
"0xce1373ddfa2232dc9ca82d98420be7a2e11962b5"
> web3.eth.accounts
["0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9", "0xbb4fcfac2efd3dbc35117dc979ce5c
43ca5c615b", "0xce1373ddfa2232dc9ca82d98420be7a2e11962b5"]

We can then look at the initial balances in the accounts:

> eth.getBalance("0xce1373ddfa2232dc9ca82d98420be7a2e11962b5")
0
> eth.getBalance("0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9")
288230376151711744
> personal.unlockAccount('0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9','Qwerty')
true

Next we can transfer some currency from one account to another:

> eth.sendTransaction({from: '0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9', to: '
0xce1373ddfa2232dc9ca82d98420be7a2e11962b5',value:1000})
"0x4029e82ac13fd2a56078c2747f2ff55b42db12c8fa40dbde8c6350b128476243"

>
> eth.getTransaction('0x4029e82ac13fd2a56078c2747f2ff55b42db12c8fa40dbde8c6350b128476243')
{
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000
",
blockNumber: null,
from: "0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9",
gas: 90000,
gasPrice: 18000000000,
hash: "0x4029e82ac13fd2a56078c2747f2ff55b42db12c8fa40dbde8c6350b128476243",
input: "0x",
nonce: 0,
r: "0xedbbbe21778eab7a3b3f82198854e6354abff4348dc9668ec337a786749a4d3a",
s: "0x27228d637ac06acf1ffdcd93ff5a2dbd59f23353d196b97ff2ee7e2a14527595",
to: "0xce1373ddfa2232dc9ca82d98420be7a2e11962b5",
transactionIndex: 0,
v: "0x41",
value: 1000
}

If we look at the balances there has not been any transfers:

> eth.getBalance("0xce1373ddfa2232dc9ca82d98420be7a2e11962b5")
0
> eth.getBalance("0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9")
288230376151711744

We can now start the miner:

> miner.start()
null
> eth.getBalance("0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9")
288230376151711744
> eth.getBalance("0xce1373ddfa2232dc9ca82d98420be7a2e11962b5")
0

We can transfer again:

> eth.sendTransaction({from: '0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9', to: '
0xce1373ddfa2232dc9ca82d98420be7a2e11962b5',value:100000})

"0x2e25093e25cbf511c2892cb38b45a5c9f6f9b2785774cd5830cf5bd978839165"
> eth.getBalance("0xce1373ddfa2232dc9ca82d98420be7a2e11962b5")
0
> eth.getBalance("0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9")
288230376151711744

The mining process adds some credits to the inital account:

> eth.getBalance("0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9")
5288230376151711744
> eth.getBalance("0xce1373ddfa2232dc9ca82d98420be7a2e11962b5")
0

After 30 minutes we see:

> eth.getBalance("0xce1373ddfa2232dc9ca82d98420be7a2e11962b5")
200000

If we look at the blockchain we see there are two records:

> eth.blockNumber
2

Within Geth we see:

INFO [06-26|22:26:08] Commit new mining work                   number=2 txs=2 un
cles=0 elapsed=2.000ms
INFO [06-26|22:26:55] Successfully sealed new block number=2 hash=783
ace.91c41f
INFO [06-26|22:26:55] ?? mined potential block number=2 hash=78
3ace.91c41f
INFO [06-26|22:26:55] Commit new mining work number=3 txs=0 un
cles=0 elapsed=0s

Creating a contract

Now let’s create a contract. We do this by using the Solidity compiler (in order to create the trusted code that will run on the blockchain):

pragma solidity ^0.4.0;
contract test2{
uint a ;
function test2() {
a = 1;
}
function val() returns(uint){
return a;
}
}
contract test3 is test2{ 
uint b = a++;
function show() returns(uint){
return b;
}
}

We now open up [here] and paste our code.

Now we copy from Web Deploy and place in a JavaScript file, and then load it:

>loadScript(‘sayhello2.js’)

and next define the account to run the script:

> web3.eth.defaultAccount = '0x821eacc2a570c1aeb9b5aa64b5b915d4c1e1f3ee'

We can now start our miners:

> miner.start()
null
> null [object Object]
Contract mined! address: 0x8d487f4a719b5a1cf47c61cc83e757b8d269f877 transactionH
ash: 0xf4bb0fa6ddc1d9e1921a55d576d68acf5b715d00cd89cc7268ece3653c50de50
null [object Object]
Contract mined! address: 0xf3872dc9ced78283ad3a511e970891807dd38590 transactionH
ash: 0xab90aa5169f4ebfcbc139874208cabb29416feb3f12c296c93466d7d8090f805
null [object Object]
Contract mined! address: 0x7a74b5da4168f0a06a752301a3711c8991acaf88 transactionH
ash: 0x6ce2a63c59d124d5ecd4681a368243ba7de8aeacc735d41583f834789cba0b16

Finally we can view:

> test_sol_test2
{
abi: [{
constant: false,
inputs: [],
name: "val",
outputs: [{...}],
payable: false,
type: "function"
}, {
inputs: [],
payable: false,
type: "constructor"
}],
address: "0x7a74b5da4168f0a06a752301a3711c8991acaf88",
transactionHash: "0x6ce2a63c59d124d5ecd4681a368243ba7de8aeacc735d41583f834789c
ba0b16",
allEvents: function(),
val: function()
}
> test_sol_test3
{
abi: [{
constant: false,
inputs: [],
name: "val",
outputs: [{...}],
payable: false,
type: "function"
}, {
constant: false,
inputs: [],
name: "show",
outputs: [{...}],
payable: false,
type: "function"
}],
address: "0xbd570c2f87b8af945146177377276901fd82b12d",
transactionHash: "0xc028384b4d8ea0e283c9cd3a6a747ab3efff859bb591d55f710ca20b09
665808",
allEvents: function(),
show: function(),
val: function()
}

And then test:

> test_sol_test2.val()
"0xd69b536cd4055a45e209f3274d9b9370f33c88b474c0dca294b665efa2ac5d2d"
> test_sol_test3.val()
"0x4a5fa248e8f6c2223082518106c3e784d54e4ff70793c9d4f65c9ef931cd667c"

A bit of maths

Now we will create a contract to do a bit of maths. Let’s say we want to calculate the square root of a value:

pragma solidity ^0.4.0;
contract mymath {
function sqrt(uint x) constant returns (uint y) {
uint z = (x + 1) / 2;
y = x;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
}
}

When we create the JavaScript for the compiled version, and we load and run we get:

> personal.unlockAccount('0xc7552f45deb093cafb47286a0bc9415845ca3735','Qwerty')
true
> loadScript('mycontract.js')
null [object Object]
true
Contract mined! address: 0xc706a04b759a32dbec85702dd3864584e737aa77 transactionH
ash: 0xece670dcb578a78dec4d2338755ecade084a517310daacf37fd46fe336341563
null [object Object]
Contract mined! address: 0xfafb5f4d0db2c545592ac9134292162b03088295 transactionH
ash: 0x46204af57db69df078e1ae637b50fa76d8415ee1c1e3bd7e1c2990f328dc85ce
null [object Object]
Contract mined! address: 0x83e0bbb8abe2f0976fde9cf5db05333de067b0df transactionH
ash: 0xabea9606989bcc1bf93513213d298c84d47c7e8e1b397eaf536ebffb793d9304
> test_sol_mymath.sqrt(9)
3
> test_sol_mymath.sqrt(12)
3
> test_sol_mymath.sqrt(81)
9

Commands used

The following outlines the commands:

personal.newAccount("Qwerty")
eth.getBalance("0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9")
eth.getBalance("0xce1373ddfa2232dc9ca82d98420be7a2e11962b5")
personal.unlockAccount('0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9','Qwerty')
eth.sendTransaction({from: '0xc9c425ae15a0e66500ecf5b7a1c10c6ed35600b9', to: '0xce1373ddfa2232dc9ca82d98420be7a2e11962b5',value:100000})
eth.getTransaction('0x4029e82ac13fd2a56078c2747f2ff55b42db12c8fa40dbde8c6350b128476243')
web3.eth.accounts
miner.start()
miner.stop()
loadScript(‘sayhello2.js’)
web3.eth.defaultAccount = '0x821eacc2a570c1aeb9b5aa64b5b915d4c1e1f3ee'

Demo

Here is a tutorial using the Linux version of Geth [here].