Blockchain from scratch — Pt. 1

Leonardo Bork
CWI Software
Published in
12 min readMar 22, 2018

Introducing concepts

Explaining to a five year old

Imagine that there’s a big pot of candy, and for everything we produce we receive an amount of candy, but we don’t have a place to store our treats, so instead of we receiving the actual object, we receive a credit of candy. If you want to buy something, you just get from your credit. Suppose you want to buy a property of mine, so you and I decide to write a transaction record, where you write the amount of candy you’re giving me and we both would sign it, but it has to be secret, no one can know of our transaction. So we decide to sign it each with a code, that no one knows how to decipher, so we both copy the transaction record, and the next time I buy something, I can just use the transaction record as a representative of that candy.

There’s a big problem in our transaction system: what if I claim I never got paid and that property is still mine, and what if you claim that you never bought something and your still own that amount of candy. So we think about it and solve our problem: we call a friend. That friend would also receive a copy of the record, and if someone tries to corrupt a transaction, you and I would just call our friend that has all the transactions to check, it would be harder to corrupt. Now, wonder if we call more friends to validate our transactions, we would have more than 3 copies of our record, and even friends could check between them if the transaction record they have is valid between them, it would be very hard to corrupt.

By this system, we could maintain millions of our transactions and know how much candy we have, based on the record of values we have exchanged, and our friends would never know of anything, because of the codes.

This is a very simple introduction to the concept of blockchains: our friends are the blockchain, the candy is the currency, and the credit are the wallets.

It’s not that simple

In the blockchain, the place where these transaction records or ledgers are stored are called blocks. The blockchain is consisted of a chain of blocks that validate each other, each block contains a reference to the last block before them, thus being able to valid new transactions throughout the last block’s data, to change a block’s data or to create a new one people have to solve a very hard mathematical problem, this is called “mining”. New transactions are stored in a new block that has to be validated by mining.

In Bitcoin, a block is consisted by:

Source: http://dataconomy.com/2015/10/wtf-is-the-blockchain-a-guide-for-total-beginners/

Mining has a simple purpose: validate transactions and record them, the miners are basically the friends in our example, every single one of them runs a full node of the blockchain in their machine, so each one of them has a copy of each block. While running the blockchain, each machine competes to solve a difficult mathematical problem based on cryptographic functions, that when added to the block of transactions, causes this block to hash to a code with rare properties, this is called Proof-Of-Work, and it’ll will prove that the miner spent a lot of time trying to solve the problem while running a node, thus validating data on the machine.

In Ethereum, for example, Proof-Of-Work is made using Ethash, which is a new PoW(Proof-Of-Work) design to attack the problem of mining centralization, where a group of hardware or mining companies would acquire a large amount of power to impact or manipulate the network.

Source: http://www.bitlanders.com/blogs/the-concept-of-proof-of-work-in-the-bitcoin-ecosystem/255977

After the PoW is done, the block and the proof are sent back to the node’s blockchain, then it will update the broadcast to verify if the proof is right, other nodes will verify that the block’s answer is really the right one in their node too, if it is valid in a certain percentage of nodes (in Bitcoin, 51% of them) it will validate the transaction, this is called consensus. This process occurs every time a block is changed or new transactions are sent.

Under the hood, what happens is that, when changed or created, a block will have a new hash, and you’ll have to mine it to validate it again. As we already know, every single block has a reference to the hash of the block mined before him, now imagine I change a block that is 10 positions behind the last block on the blockchain, it will have to be mined again, thus it’s hash will change, and the next block will lose it’s reference, so it’ll also have to be mined again, and it’s hash would change… You get the story, I’d have to mine 10 blocks to have my blockchain valid, this takes a lot of time(really), and, let’s assume I do this. When I’d broadcast my node to the whole blockchain, it wouldn’t pass, because other people’s nodes are different, so there would be no consensus.

But, why? Why would someone run a node to maintain the blockchain’s integrity? The answer is simple, by running a node miners receive two types of rewards: a new amount of the currency or transaction fees.

In the world of cryptocurrencies, for example, currencies have a ballast, and they produce, and deliver, new coins to miners, as a reward for wasting time, and money validating transactions (or contracts, in case of Ethereum), as times goes by the reward keeps going down, creating value for the currency. The other type of reward is called “transactions fees”, it’s like a fee that is charged in the transaction, and it is sent to the miner. As times goes by, and more coins are mined, the greater the transaction cost is. In Ethereum, for example, this cost is called gas.

Proof-Of-Stake

In the long run PoW has a problem, we know that as times passes the release of coins shrink, so does the reward miners get for mining, and the computational cost for miners stays the same or increases. This causes a problem to the network, miners have less incentive to mine, and leave the network, with fewer miners transactions take more time to process, cost more and are more susceptible to being controlled. One example of this type of control is the 51% attack (in the case of Bitcoin), where a miner would have more than 51% computational power on the network and would be able to valid only his desired transactions, in a network with a lot of miners this is almost impossible.

PoS(Proof-Of-Stake) is the solution to that, it seeks to release mining power to the proportion of coins held by a miner. In this case, if a miner has 5% of all Bitcoins, he would only be able to mine 5% of all blocks. Also there would be no point in doing a 51% attack, because the miner would also have invested in the network, and if he attacks the coin or token value would drop, thus he would lose money. In Casper, Ethereum’s PoS project, the idea is that instead of using computational power to solve a puzzle, the steak-holder would “bet” on the block he thinks is gonna “pass” in the blockchain, if it passes he gets the reward (which is only transaction fees), this is the way blockchain is validated in Casper’s implementation of PoS. Utilizing this method of PoS, computational is much lower, therefore “mining” price will diminish.

Source: https://blog.ethereum.org/2015/12/28/understanding-serenity-part-2-casper/

In Casper, miners are called “validators” and there are staking pools for each block, if the stake-holders bet on non-valid blocks, they may lose tokens.

Addresses, keys and transactions

Having understood concepts like what is a block, mining and the basic cycle of a transaction, we may want to step right in some key concepts that make the blockchain even more interesting. In my opinion a good way to start is to understand what wallets are, what are they are constituted and what is their purpose.

In the real world, a wallet is the place where you keep your money, in the blockchain world it isn’t very different. Inside a blockchain wallet, you have your public addresses, private keys and your balance. A wallet can run a node of a blockchain, or can use a third-party node; these are the so called full-node wallets and light-wallets.

A private key is mainly the code you need to have access and control your cryptocurrency or token, you receive through a public key, but you control through private keys. Private keys can be generated using algorithms and after that transformed to a hash. As said, the public address is the place where people can send you money, it has a balance, just like in the candy example, the difference is that it is also is a hash.

In Bitcoin, for example, you can generate a PK(private key) by an ECDSA(Elliptic Curve Digital Signature Algorithm) secret exponent (the private key) using the SECP256k1 curve, and then convert the secret exponent/private key into base58check format. The PA(public address) is called P2PKH, the abbreviation of “Pay to Public Key Hash.”. The public key is generated through a mathematical algorithm that uses your private key as a base, although it’s impossible to discover your private key knowing only your PA. Every wallet software can generate a P2PKH, it’s not an impossible task, only a simple combination of cryptographic operations.

We all know that we have wallets to make and receive transactions (later you’ll see that transactions don’t necessarily need to involve currency, they can be a contract), and that is the main point of a blockchain. As briefly introduced before, blockchain transactions pass through a lot of stages to be validated:

Source: http://insurancefunda.in/bitcoin-cryptocurrency/

To submit a transaction you have to supply a certain number of information to the blockchain, in Ethereum, for example, this is the code we need to send to request a transaction:

var Web3 = require('web3');
web3 = new Web3.providers.HttpProvider('yourProviderHere'));
var Tx = require('ethereumjs-tx'); \\transaction lib
var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex') \\user private key

var rawTx = {
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

// console.log(serializedTx.toString('hex'));
// 0xf889808609184e72a00082271094000000000000000000000000000000000000000080a47f74657374320000000000000000000000000000000000000000000000000000006000571ca08a8bbf888cfa37bbf0bb965423625641fc956967b81d12e23709cead01446075a01ce999b56a8a88504be365442ea61239198e23d1fce7d00fcfc5cd3b44b7215f

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);

In this example I’m using Ethereum’s Web3, the JS library that deals with Ethereum’s blockchain, and I’m also using EthereumJS-TX, a module for handling and creating transactions.

Explaning the transaction object rawTxproperties:

  • nonce — Times the sending address has transacted.
  • gasPrice — In Ethereum, the sender of the transaction sets the price of gas, to which the miner can or not accept, this causes an emergence of a market around gas, normal is ‘0x14f46b0400’ or 90 GWei.
  • gasLimit — Limit to be used by the transaction, default is ‘0x55f0’ or 22000 GWei.
  • to — The receiver’s address.
  • value — The amount of Ether you want to send.
  • data — The containing the data of the function call on a contract, or in the case of a contract-creation transaction the initialization code. (Better explained later on the text) — This field is optional.

After sending that, the transaction will be broadcasted to the network and miners mine the transaction’s block with PoW, and this will be followed by sending the block and the PoW back to the local blockchain, and then it will broadcast to the network and check for consensus, once that is done, the new block will join the whole blockchain.

Ethereum, contracts and Dapps

There are concepts and functionalities that differ from blockchain to blockchain, one example is the Ethereum Project. It is also a public blockchain project like Bitcoin, but it has a different purpose, instead of just making transactions, Ethereum offers a new possibility, smart contracts.

Smart contracts are in a very simple way, a blockchain transaction that you can put code inside. You can set business rules to a transaction utilizing smart contracts, for example, if I want to divide money with 5 people, instead of sending individually the divided amount to each wallet, I can just create a smart contract that divides the amount of Ether that I send to it, into these 5 wallets, and this is just the simplest of the examples. These contracts are immutable and have instances that can call, extend, and even create new contracts; they work similarly to objects in OOP. An interesting part of Ethereum is that contracts don’t necessarily need to receive Ether, you can have a contracts that don’t even touch on a single Ether, and throughout that, people can use contracts to regulate things, such as promises, shipments and other things that need assurance, and that are often not regulated or regulated by people and the state.

There are projects for implementing blockchain in private institutions, Quorum, for example, is a fork of Ethereum focused on applying blockchain technology on private companies. So, companies and institutions can build applications that are run on the blockchain, are contract or ledger based, these are the so called Dapps (Decentralized Applications).

Dapps focus on utilizing the consistency and honesty that blockchain has, and tries to apply it in other areas. One example is the idea that people can have a social network that shows exactly the flow of your data. Being honest with it’s contracts and letting you know exactly what will happen to your information is a very useful appliance of Dapps. They’re are also great for decentralizing, organizing and clearing hierarchical structures, for example, you can run a whole company on a contract based software, every flow of data passes through contracts, and contracts are honest, and know exactly what to do with your data or tasks. This ensures that data will always have the same flow and that information will be available and hardly be corrupted. A lot of social positions that exist to ensure trust and honesty can now be substituted by the insurance that blockchain technology provides.

Dapp cases

Source: https://www-01.ibm.com/common/ssi/cgi-bin/ssialias?htmlfid=CPV03008USEN

IBM and Maersk: These companies are working together to make worldwide supply chain simpler and effective utilizing blockchain technology. Their plan is to implement a global trade platform that utilizes blockchain to ensure information across different environments, simplifying processes that are manual made and distribuiting information in a way that it is always there when it’s needed, trustability and simpleness are the key objectives.

Source: https://www.blocksg.com/single-post/2017/12/27/Coco-Framework-Whitepaper

JP Morgan’s Quorum: Quorum is a fork of Ethereum, it’s a project made for implementing blockchain on private ambients. It was designed to lay between private and public spheres, for working with derivatives and payments. It is focused to satisfy regulators who want to access to financial goings-on, while still protecting parties that don’t want their identity and their transactions revealed. You can read the docs here.

Conclusion

In this little article, I tried to introduce you to the very basics of blockchain technology, understanding the blockchain is not an easy task. It took me some time to digest these concepts and I still don’t understand a lot of the theory. Although in more or less one month studying this technology, I can already say that I am in love with it, it’s disruptive concepts are a game changer, with it can see the future in a very different way, and technology will make it better.

Blockchain technology is still starting, we still don’t know it’s full potential, but we already know that it is awesome. This is the first part of my series. The second part will bring more practical information on how you can interact code with a blockchain. Hope you liked it.

Sources

https://en.wikipedia.org/wiki/Proof-of-work_system

https://blockgeeks.com/guides/proof-of-work-vs-proof-of-stake/

https://blockgeeks.com/guides/blockchain-consensus/

https://www.investopedia.com/terms/1/51-attack.asp

https://www.investopedia.com/terms/b/blockchain.asp

https://www.investopedia.com/terms/p/private-key.asp

https://www.investopedia.com/terms/b/blockchain-wallet.asp

https://bitcoin.stackexchange.com/questions/1389/how-are-public-private-keys-in-an-address-created

https://bitcoin.stackexchange.com/questions/25024/how-do-you-get-a-bitcoin-public-key-from-a-private-key

https://github.com/ethereum/wiki/wiki/Ethash

https://github.com/ethereum/wiki/wiki/Mining

https://ethereum.stackexchange.com/questions/14/what-proof-of-work-function-does-ethereum-use

http://sammantics.com/blog/2016/3/6/how-transactions-are-validated-on-a-shared-ledger

https://bitcoin.com.au/page/popular-blockchain-consensus-mechanisms-work/

https://medium.com/@xbsoftware/smart-contracts-industry-examples-and-use-cases-for-business-df046dc097f2

https://blog.ethereum.org/2015/12/28/understanding-serenity-part-2-casper/

https://coinsutra.com/private-blockchain-public-blockchain/

--

--