Blockchain: How does Ethereum blockchain work?
In general, software engineering can be understood as many software development techniques combined together so that the software created can be built, tested, and deployed in a certain period of time.
Nowadays, Web 3 is compared by many specialists with the internet “boom” from the 90s, it is considered a movement with a lot of potential, and there are loads of possibilities when solving business problems using new Web 3 technologies. Particularly, in the case of financial solutions, where security is one very important pillar, blockchain gets a leading role spot.
Often, people think of technology when they want to create a solution for their business problems, or when they just want to create something new that can be profitable. With blockchain, many financial problems can be simplified. One of them is crowdfunding. Generally, when people want to join together to help someone or a cause, they create crowdfunding. It can be done by depositing assets straight into someone’s bank account, or a banking account is created specifically for that reason. Whatever the way it is done, the implementation and technologies used in the regular banking system are much more complex than it appears.
The blockchain subject is vast, it can be approached and explained from many perspectives such as finance, economics, cryptography, the theory of games, and distributed systems. For this article, I will examine the importance of blockchain technology in the finance branch. Specifically, how it implements a new solution for an old financial problem through blockchain software development.
Blockchain Spectrum
Economics
The essence of a blockchain can be found in its economic basis. Bansal affirms that “a blockchain is a distributed system using cryptography to secure an evolving consensus about an economically valuable token”, [1]. The idea of having finances happening in a blockchain is extraordinary: a network that manages tokens, transactions, values, and people’s behavior in a distributed and open way as all the transactions and activities that happen within the blockchain are public. However, not everything is as paradise as it appears to be. The mechanism only works well when the miners see value in it. In other words, the proof-of-work or proof-of-stake that is going to be explained later in this article, only happens if the miners see economic advantages on it. Otherwise, the blockchain becomes only a centralized and, consequently, a bad system. In [1], “a Blockchain without an economically valuable token reduces to a centralized system or database”.
Now that we understand somewhat that economic interest is at the core of blockchain technology, we should be able to venture into the cryptocurrency universe. Additionally, it is necessary to highlight the term “De-Fi” for discussion, as it is mentioned many times in the crypto and blockchain universe.
According to Harvey et al. [2, p.6], there are five key problems in the current financial system: centralized control, limited access, inefficiency, lack of interoperability, and opacity. The control is centralized on Central banks and big institutions, the access limitation can be explained due to the fact that to open an account or get a loan, a person must show various documents, and this person must have a certain status that the banks consider safe for them to have the person as a client. The current system is inefficient in terms of transactions when you consider that in every credit card transaction, 3% of the value is taken by the banks. Yet, it is necessary to wait two days to have the money transferred between the parts. Moreover, banks demand high loan interest rates compared to low deposit interest. This is the bank making money with their client’s money. The lack of interoperability occurs when the money takes three days to go from one bank to another, and opacity relates to how the current system is not transparent and clear in its operations. Banks are controlled and managed by people, and as we all know people can be corrupted, thus systems and measures controlled by humans can be bypassed.
“DeFi” can be described as a movement of financial applications towards decentralization. In other words, traditional financial services are provided by DApps (decentralized applications) deployed in a blockchain. “DeFi” can offer various solutions for the problems stated above. And the blockchain can be the “where” of these solutions. It is just a matter of “how” these solutions can be implemented.
Also, “DeFi” can improve the payment & clearance system, it can give more accessibility, and improve the centralization & transparency problem. At the moment, if a person wants to make an international transaction, it is necessary to wait at least three days for a transaction to be completed. Additionally, a minimum of 3 taxes are to be paid, the exchange rate from the bank, the international wire fee, and the international inbound fee, according to Lau et al. [3, p. 6]. Nowadays, the Ethereum blockchain is the main network that most of the DApps rely on to run their smart contracts, which are pieces of code written in a language called Solidity that are going to be introduced later in this article.
Regular web applications, Decentralised Applications, and Reasons for Decentralisation
A DApp gives the user two very hard assets to get on today’s internet: permissionlessness and censorship resistance, according to Harvey et al. [2, p. 27]. Often, financial services such as currency exchange, payments, and deposits, happen through a centralized system where a third party is responsible for giving credibility to the transaction. If two people are transferring money to each other online, who ensures that someone is not committing fraud? Moreover, on an online purchase, who ensures that the purchased item is not being sold twice for two different buyers? The regular applications have no issues as they are backed by those third parties whose systems are responsible for the consensus mechanisms. In DApps case, however, the consensus mechanism is assured by the blockchain.
Decentralization in software engineering means that not only one single machine has control over the whole process. According to Buterin [4], the decentralization can happen in 3 ways: architectural, political, and logical. When it comes to blockchain, decentralization is architectural: many computers create the system, and political: many individuals or organizations are in charge of the computers in the system. Additionally, the logic is centralized because the system works as it was run in a single machine, for example, the Ethereum blockchain runs the Ethereum Virtual Machine.
There are numerous reasons to explain why decentralization is important. First, fault tolerance, a system that relies on more than one and separated components has less chance to fail. Another good reason for decentralization is the resistance to attacks. It is harder for attackers to aim at a system that is not concentrated in only one central point. To conclude the importance of decentralization is the resistance to collusion. A system spread out in many nodes is not under the influence of only one company or one big investor and is less inclined to collusion. It is known that in a blockchain, the system would be in all its nodes. If we have few nodes, and the owners know each other, they could collude. For that, it is necessary that the blockchain has mining algorithms that have no risks of centralization. There are many other measures that can be taken in order to avoid a “corrupt” blockchain. The ones above are only a few of them.
In fact, regular applications are good, they also provide reliability, they are more common and the knowledge in the software engineering community is well set, it’s through them that the whole internet today is based. SQL and NoSQL databases will continue to exist. There is no major effort in building a regular application and deploying that to a cloud server. However, the comparison between a regular application and a DApp should be done with precautions. Firstly, not every application should be a DApp and there are some business and technical reasons for that.
There are some questions that need to be answered before a blockchain implementation. They are exemplified in the picture below:
Blockchain implementation Check-list
Regarding the blockchain types mentioned in the picture above:
Public blockchains: any person can use them, as well as participate in the consensus. Public blockchains are secured by economic incentives and cryptographic verification using proof of work or proof of stake mechanisms. Consortium blockchains: in consortium blockchains, the process of mining blocks is controlled by pre-selected nodes. The blockchain can be public, or restricted to participants, and this type of blockchain can be partially decentralized. Fully private blockchains: it is a blockchain where the permission is kept centralized to one node. Reading permission can be public or can be restricted. The blockchain contains applications that can be audited, internal to only one company, thus, public readability is not important.
Cryptography
Cryptography is the essence of a blockchain. If blockchain was a real chain, the cryptography would be its links. Cryptography is what makes the blockchain reliable and strong. Particularly, the cryptographic signature. Although this is a resource commonly used in many systems, when it comes to Ethereum and Bitcoin, there are some differences. Both systems mentioned use ECDSA or Elliptic Curve Digital Signature Algorithm. One of the characteristics of this algorithm is that it cannot be used for encryption. With this algorithm it is possible to derive a value from the private key, making an impossible way back. This approach is called Trapdoor. Here is the Trapdoor definition according to Zuidhoorn [5]: a function that is easy to compute in one direction, although difficult to compute in the opposite direction without special information.
Elliptic Curve
In Ethereum, all the transactions create a hash after the encryption and these transactions can be accessed in the Ethereum MainNet. Every block and every transaction is hashed.
Hashing Process
Addresses in Ethereum are also hashes of a public key, and each address is an account. The address is generated, then, from a public key and a private key after they are processed by the Elliptic Curve algorithm mentioned above.
New State Handling Concept and the Web 3
The internet has achieved great milestones in terms of state. Nowadays, there is a surplus of new frameworks and libraries that make state handling far more interesting than the first link-based Web pages of the past in HTML, known as Web 1. Additionally, the domination of frameworks and libraries in the frontend changed the paradigm of the Internet. State handling became more important in Web 2, applications that make use of React and Vue, for example.
In Decentralized applications, the state is a very important issue. The state is a set of variables that define how a system is at a particular time, Brennan [6]. Currently, the Internet leans towards the Web 3 concept with a lot of promises when it comes to state handling. The Web 3, also called The Stateful Internet, produces connection and data in a machine-readable way, and in decentralized apps deployed to a blockchain, creation, storage, data handling, and connection happen in a decentralized way. With this new concept on the internet, users are able to make financial exchanges instantaneously without the interference of a third party. It happens because of a global state secured by cryptography, and blockchain protocols. Furthermore, those protocols are decentralized, and distributed in a chain that is public, and anyone can have access to them.
The Metamask Wallet
Metamask is the extension on the browser that permits a user to interact with a decentralized application. According to Phillips and Hussey [7], Metamask is a browser extension designed to make access to Ethereum’s DApp ecosystem easier. It also serves as a wallet for holding tokens, or Digital Assets, allowing users to access services built on the network via the wallet.
Overall, Metamask is a useful tool, and a key part of any decentralized system implementation, as it handles directly the RPC calls to the smart contract via Ethereum Web3.js API. Some DApps that were already implemented in the market lost many users on their journey after creation as they did not have anything similar to Metamask. One example is the platform CryptoKitties [7].
One of the characteristics of a DApp is that it passes through the fact that the platform owner does not handle user logins or any user registration. The user, though, makes use of his own Metamask wallet to do all the transactions or interactions with the DApp. Under the hood, there are a lot of important processes happening so that Metamask can keep the user safe. But, that could easily be the theme for another thesis, so, for this matter, it is enough to know that Metamask is another Web 3 feature, and in the future, wallets like Metamask will be used to login into applications just as today’s Gmail is used.
Proof of Work and Proof of Stake
In any distributed or decentralized system, the most important thing is the consensus mechanism. Often, in blockchain, the consensus mostly used is the Proof of Work. The base of the Proof of Work consensus is a transaction, making use of cryptography as discussed before in this thesis. The transactions are atomic, which means that either they fully happen or they fully do not happen in the network. After that, the effects of the transaction change the state of the network. In a Proof of Work consensus, there are numerous roles: Miners, Full nodes, Light nodes, and Archive nodes.
Firstly, Miners are nodes responsible for transaction certifications. Basically, miners are those that are going to include valid transactions in blocks and consequently receive their recompense for mining the block. After the block is created, it propagates throughout the network, changing its network state.
Secondly, Full Nodes maintain the full network state, also responsible for checking the blocks to make sure they are valid. Light nodes are the ones that submit transactions to the network; they do not participate in the whole process of validation, though. Last but not least, the archive nodes act like full nodes maintaining the current state but also providing historical states.
Additionally, there are two more critical characteristics in the Proof of Work consensus: block production and block propagation. The block production happens after the miner receives the latest block and finishes at the moment that the miner solves the Proof of Work algorithm challenge. The Block Propagation is the period when the block is spreading through the blockchain network. Full nodes will only spread the blocks that are validated. It is essential to mention that individual validation is how the proof of work consensus occurs to maintain the state of the blockchain distributed network safe.
On the other hand, there is the Proof-of-Stake consensus that works in reducing the computational work needed to verify blocks and transactions in the blockchain, Frankenfield [8]. Proof-of-Stake relies on coin owners that offer their coins in order to be eligible to become block validators in the blockchain. After the validators are established, the system picks the validator to mine the blocks randomly. Thus, that is the difference from the Proof-of-work consensus based on the competition between the miners. Generally, to become a validator in the Ethereum network, the Staker needs to hold 32 ETH in his wallet.
Gas in Ethereum Network
When it comes to Ethereum Blockchain, a concept that has to be mentioned is Gas. For every operation that happens in the Ethereum Virtual Machine, gas is consumed. So, we can say that gas is the fuel that runs the Ethereum blockchain network. Gas is the metering unit for use of the Ethereum World Computer, Chow [9]. The miners get a fee for processing the blocks and transactions and the gas is responsible for determining how much the miners are going to receive.
Clarifying, in the Ethereum network, every transaction must show in advance that it contains enough gas to cover all the use of the EVM’s computation and storage power. Any transaction that has not enough gas is rolled back, although it is included in a block and the fee associated with that is still paid to the miner. The event when a transaction runs out of gas is called “Out of Gas Exception”. In an “Out of Gas Exception”, all the gas is consumed and the transaction is not completed. The originator pays the miner for all the work, but contracts, stored values, and any state changed in the network is reverted.
Smart Contracts, Oracles, and The Ethereum Virtual Machine (EVM)
A Smart Contract can be considered a program that is computed by the Ethereum Virtual Machine. Smart contracts contain methods, store data, and are located in a memory address in the Ethereum blockchain. Additionally, smart contracts have balances and are able to send transactions, as earlier mentioned in this article. In this context, users’ accounts are able to interact with smart contracts by submitting a transaction that is going to use a function in the smart contract to perform an operation. By definition, smart contracts can never be deleted, all the transactions performed in the blockchain making use of a smart contract are forever and irreversible.
Smart Contract Creation
A smart contract network should have some characteristics such as Trustlessness, Universal accessibility, Traceability, Immutable transactions, and Self- Executing transactions. Starting with trustless, two parties should be able to act on an agreement with no third party involved. The second property, Universally Accessible means that every user must have access to and use the system not relying on approval from the network. Another one is Traceability. Contract transactions should be able to be located and traced, this is important for dispute resolution as well as for compliance. Immutable transactions must be performed in smart contracts. Users should not be able to revoke decisions. Finally, Self-execution. Roughly, the idea around Self-Execution is that a smart contract must be away from manual intervention as far as possible.
Transaction Execution
Additionally, smart contracts deployed to Ethereum Virtual Machine can be written in two different programming languages: Solidity and Vyper. Although smart contracts are a great resource in blockchain development, they also have their limitations. They cannot get information in real-time from the real world as they are not able to use the HTTP protocols, because this could jeopardize the consensus. For that, a decentralized application should make use of oracles, and external on-chain APIs that can be connected to the real world in order to feed the smart contracts with important information such as fiat currency price, weather, or stock market.
Going further into the Ethereum Virtual Machine, it is possible to understand how the mechanism brings together transactions, and nodes functions in the figure, below:
Ethereum Virtual Machine Mechanism
The Ethereum Virtual Machine is responsible for converting the smart contract into bytecode and executing it. After the bytecode execution, there will be a new world state. Likewise, the EVM is a Turing-complete machine, so it can execute more advanced computing than the Bitcoin network, for example. The gas limit is important to avoid forever loops in the network, though. Below, the figure demonstrates how the state in the Ethereum network is changed after a transaction is executed:
Ethereum Virtual Machine State Changing Mechanism
Software Engineering Best Practices in Blockchain Development
As with any programming language, aspects of Software Engineering such as Design Patterns are also followed in Solidity and in blockchain development. The idea is to always be prepared for failure. It is not possible to know how secure the code is. However, the smart contracts can be architected in a way that in case of a failure, it will cause minimal damage.
Some of the techniques used in smart contracts are very common to the Solidity programming language, and some of them are already implemented in regular web application programming, although they have more importance when they are used in blockchain. For example Circuit Breakers. In a normal backend language such as Java, a circuit breaker would be used to stop a loop or a mapping in a method that the only damage to the system in case it does not work would be time wasted. In Solidity, if a Circuit breaker does not work properly, the transaction will run out of gas, which means that depending on how much gas is being used in the transaction, a lot of money will be lost in that transaction because of bad practice in code. Circuit breakers stop the execution of the smart contract when some conditions are met or when errors are discovered.
Generally speaking, smart contracts are implemented as a regular piece of code. Regarding tests, Test-Driven Development (TDD) is the only option. As a smart contract is developed to be deployed in a blockchain, the test coverage needs to be nearly 100%. Deployment is a very sensitive stage in blockchain development. Once a Smart Contract is deployed, there is no way back. For this reason, TDD must be followed strictly. Other measures related to this would be: “testnet” deployment, and beta “mainnet” deployment. These two are also best practices in blockchain development. It is essential that all the features of the smart contract are tested in a “testnet”. The “testnet” simulates gas expends and allows the developer to catch bugs that were not able to be found in the unit tests. After “testnet” implementation, it is also a good practice to deploy the project in a beta version in the mainnet. So, more features can be tested using high volume tests.”Generally speaking, smart contracts are implemented as a regular piece of code. Regarding tests, Test-Driven Development (TDD) is the only option. As a smart contract is developed to be deployed in a blockchain, the test coverage needs to be nearly 100%. Deployment is a very sensitive stage in blockchain development. Once a Smart Contract is deployed, there is no way back. For this reason, TDD must be followed strictly. Other measures related to this would be: “testnet” deployment, and beta “mainnet” deployment. These two are also best practices in blockchain development. It is essential that all the features of the smart contract are tested in a “testnet”. The “testnet” simulates gas expends and allows the developer to catch bugs that were not able to be found in the unit tests. After “testnet” implementation, it is also a good practice to deploy the project in a beta version in the “mainnet”. So, more features can be tested using high volume tests.
Scalability
Often, companies need security and permissioning when they look for blockchain implementation, Robinson [10]. This can be addressed with private smart contracts. This is one of the ways how Ethereum can scale in a manner that does not sound anarchist. J.P. Morgan implemented Quorum, a Golang Ethereum resource for that. In the same way, Hyperledger Fabric was developed by IBM, nowadays run by the Linux Foundation. Both permit scalability in a private way with Ethereum.
Apparently, the use of sidechains is the way to go in blockchain scalability. A sidechain is a separate blockchain that is parallel to the “mainnet”, although it is independently operated. Sidechains also have their own consensus protocol. Even though a sidechain is parallel to a “mainnet”, it works the same way as the mainnet works. In Ethereum, the sidechain also uses the Ethereum Virtual Machine. Sidechains can do the work that is not possible to be done in the “mainnet”, but can still do it in a blockchain.
Sidechains
The use of sidechains can be justified by the idea that an attacker could spend millions of dollars in a 51% attack on a financial blockchain and reverse a transaction, but would never spend the same millions to attack a microblog application deployed to the mainnet, Konstantopoulos [11]. This is why sidechains are necessary, it makes no sense to spend thousands of dollars with micro applications in the “mainnet”. That would be an over-engineering practice. DApps in sidechains are often cheaper, and consequently, less secure. At the end of the day, this has to be a business decision.
Another solution for scalability is through regular web applications. A regular application can be built in order to handle all the administrative states. For example, images, names, and other project data can be stored in regular databases, and accessed by regular APIs, and only important financial transactions could be made on-chain. This way, the application would have a hybrid composition that could be interesting in terms of not relying on scarce blockchain developers. Regular web developers would build all of the off-chain applications implementing regular software engineering patterns leaving the sensitive money-handling matters to the blockchain side. Again, a business decision.
References
I. Sommerville, Software Engineering. Boston: Addison-Wesley, 2011.
[1] D. Bansal, “Why it is Hard to Get Bitcoin”, Unchained Capital, December, 2017. Available: https://unchained.com/blog/blockchain-spectrum/. Accessed date: March 20, 2022.
[2] C. R. Harvey, A. Ramachandran, and J. Santoro, DeFi and the Future of Finance. New Jersey: Wiley, 2021.
[3]D. Lau, D. Lau, T. S. Jin, K. Kho, E. Azmi, T. Lee, and B. Ong, How To DeFi, 1st ed. Coin Gecko, 2020. [E-book]. Available: Amazon
[4] V. Buterin, “The Meaning of Decentralization”, Medium, February 6, 2017. Available: https://medium.com/@VitalikButerin/the-meaning-of-decentralization-a0c92b76a274. Accessed date: February 17, 2022.
[5] M. Zuidhoorn, “The Magic of Digital Signatures on Ethereum”, Medium, October 8, 2020. Available: https://medium.com/mycrypto/the-magic-of-digital-signatures-on-ethereum-98fe184dc9c7. Accessed date: February 17, 2022.
[6] C. Brennan, “Want to Really Understand Blockchain? You need to understand State”, ConsenSys Blog, March 20, 2020. Available: https://consensys.net/blog/blockchain-explained/want-to-really-understand-blockchain-you-need-to-understand-state/. Accessed date: January 15, 2022.
[7] M.Hussey and D.Phillips, “MetaMask: What It Is and How To Use It”t, Decrypt, September 18, 2020. Available: https://decrypt.co/resources/metamask. Accessed date: March 27, 2022.
[8] J. Frankenfield, “Proof Of Stake (PoS)”, Investopedia, December 17, 2021. Available: https://www.investopedia.com/terms/p/proof-stake-pos.asp. Accessed date: March 10, 2022.
[9] J. Chow, “Ethereum, Gas, Fuel, and Fees”, ConsenSys Media, June 23, 2016. Available: https://media.consensys.net/ethereum-gas-fuel-and-fees-3333e17fe1dc. Accessed date: January 10, 2022.
[10] P. Robinson, “Requirements for Ethereum Private Sidechains”, 2018. [Online]. Available: https://ui.adsabs.harvard.edu/abs/2018arXiv180609834R/abstract. Accessed date: March 2, 2022.[11] G. Konstantopoulos, “Million-User DApps on Ethereum: An Introduction to Application -Specific Sidechains”, December 18, 2017. Available: https://medium.com/loom-network/million-user-dapps-on-ethereum-an-introduction-to-application-specific-sidechains-c0fdc288c5e5. Accessed date: March 4, 2022.
Bruno
Amazing