The main working principle of Ethereum is to accept transactions initiated by accounts, update their states, and maintain that state until another transaction updates them. On Ethereum, the entire process of accepting, executing, and writing transactions is divided into two phases. There is a decoupling between transaction acceptance and execution, and the writing to the ledger. This decoupling is essential for decentralized and distributed architectures to effectively support the normal operation of the system.
Blockchain mainly has the following three uses:
-
Trust: Blockchain can be used to create decentralized applications, allowing data to be collectively controlled by multiple people, where none of them has the power to change or delete previous records. Even if someone does manage to do so, the data they generate will not be accepted by other participants.
-
Autonomy: For applications on the blockchain, there is no single owner. Since there is no unique owner, no one can control it alone, but everyone can participate in the governance process through their actions, which helps establish a solution that cannot be manipulated or easily corrupted.
-
Disintermediation: Blockchain-based applications can eliminate intermediaries in existing processes. For example, in scenarios like vehicle registration and driver's license issuance, there is usually an intermediary role responsible for these tasks. If the process is designed based on blockchain, then this intermediary role becomes unnecessary, as once the data on the blockchain is confirmed, the driver's license will be automatically issued, and the vehicle will be automatically registered. Blockchain will usher in a new era where many businesses no longer require an authoritative institution for endorsement.
Cryptography is a science that transforms understandable and straightforward content into secret, hidden, and meaningless content; similarly, decryption is the reverse operation. Encryption technology helps in transmitting and storing data and cannot be easily decrypted.
In the field of computing, there are two types of encryption technologies: symmetric encryption and asymmetric encryption.
-
Symmetric encryption and decryption: Symmetric encryption refers to using the same key for both encryption and decryption. If this technology is used to exchange information, it means that many people will use the same key.
-
Asymmetric encryption and decryption: Two keys are used in the encryption and decryption process. Either key can be used for encryption and decryption. Information encrypted with a public key can be decrypted with a private key. Information encrypted with a private key can be decrypted with a public key. For example, Tom uses Alice's public key to encrypt a message and then sends it to Alice, who can then use her private key to decrypt it and extract the original information. Simply put, information encrypted with Alice's public key can only be decrypted by her unique private key, which is a common application scenario for asymmetric encryption. Asymmetric encryption also has another use, which will be introduced later: digital signatures.
Hashing is the process of converting input data into a fixed-length random string (hash value), but it is impossible to reverse-engineer or identify the original data from the result; therefore, hashing is also known as a data fingerprint. It is nearly impossible to derive input data based on its hash value, and even a slight change in the original data will produce a completely different hash value, ensuring that no one dares to tamper with the original data. Hashing also has another characteristic: although the input string data may vary in length, the resulting hash value length is fixed. For example, using the SHA256 hashing algorithm, regardless of the input data's length, it will always produce a 256-byte hash value. This is particularly useful when dealing with large amounts of data, as it consistently produces a 256-byte hash value that can be preserved as evidence. Ethereum uses hashing technology in many places; it hashes each transaction, re-hashes the hash values of two transactions, and ultimately generates a root hash value for each transaction within the same block.
Another important feature of hashing is that, mathematically, two different input data will not produce the same hash value. Similarly, it is computationally and mathematically impossible to reverse-engineer the input value from the hash value.
Ethereum uses Keccak256 as its hashing algorithm.
The following screenshot shows an example of hashing. The input "Ritesh Modi" produces a hash value, as shown below:
Even a slight change to the input data[1] will produce a completely different hash value, as shown below:
[1] Here, the space between the two words has been removed. — Translator's note
Asymmetric encryption has an important application in the creation and verification of digital signatures using asymmetric keys. A digital signature is similar to a handwritten signature on paper. Like a handwritten signature, a digital signature helps identify a person and ensures that information is not tampered with during transmission. Let's use an example to understand digital signatures.
Alice is preparing to send a message to Tom. The question arises: how can Tom ensure that the message he receives is from Alice and that it has not been tampered with during transmission? The solution is not to send the original message/transaction; Alice first needs to obtain the hash value of the message she intends to send, then encrypt that hash value with her private key, and finally, she attaches the newly generated digital signature to the hash value and sends it to Tom. Upon receiving the message, Tom uses Alice's public key to extract the digital signature and decrypt it to find the original hash value. At the same time, he extracts the hash value from the actual message received and compares the two hash values. If the two hash values match, it indicates that the message has not been tampered with during transmission.
Digital signatures are commonly used for asset or cryptocurrency (such as Ether) owners to confirm transactions.
Ether is the currency on Ethereum. Every activity on Ethereum requires the consumption of Ether as a fee, and miners who successfully produce blocks also receive Ether as a reward. Ether can be easily exchanged for fiat currency on trading platforms.
Ether uses a decimal measurement system, with its smallest unit being wei. Below are some measurement units, and more information can be found at https://github.com/ethereum/web3.js/blob/0.15.0/lib/utils/utils.js#L40.
Executing tasks on Ethereum requires the consumption of Ether. Since Ether is publicly traded on exchanges, its price fluctuates. When paying fees, if Ether is used directly, the cost for the same service may vary widely due to its unstable price. In such cases, people may choose to execute transactions when the price of Ether is low. This situation is not ideal for a platform[1]. The role of gas (fuel) is to alleviate this issue; it is Ethereum's current internal currency. By pricing in gas, users can pre-determine the execution cost of a transaction, known as gas cost. By adopting this method, when the price of Ether surges, the gas price can be appropriately lowered, and when the price of Ether plummets, the gas price can be appropriately raised. For example, using a function in a smart contract to modify a character will consume a certain amount of gas, and since the cost of gas has been predetermined, users can execute smart contracts in a regular manner.
[1] This creates a peak and valley effect; when prices are low, execution volume may be too high, causing the platform to operate under overload. — Translator's note
Blockchain is an architecture that contains multiple components, and what makes blockchain unique is the functionality and interaction of these components. Important components include: EVM (Ethereum Virtual Machine), miners, blocks, transactions, consensus algorithms, accounts, smart contracts, mining, Ether, and gas. In this chapter, we will introduce these components.
A blockchain network consists of numerous nodes, some of which are mining nodes belonging to miners, while others do not mine but help execute smart contracts and transactions. These nodes are collectively referred to as EVM. The various nodes on the network are interconnected, and they communicate with each other via P2P protocols, using port 30303 by default.
Each node maintains an instance (copy) of the ledger, containing all blocks on the chain. Due to the presence of numerous mining nodes on the network, these nodes continuously synchronize blocks to ensure consistency of ledger data.
In subsequent chapters, we will discuss the ledger, blocks, and transactions in more detail.
Smart contracts also run on the EVM. Smart contracts extend Ethereum's capabilities by writing personalized business functions. When smart contracts are executed, they are part of a transaction and follow the mining process mentioned earlier.
Users with accounts on the network can send messages to complete Ether transactions between accounts or send messages to call a function within a contract. For Ethereum, both methods are essentially transactions. When confirming transactions and changing account balances, account owners must digitally sign the transaction with their private key to verify the sender's identity. The structure of Ethereum is shown in the diagram below.
In blockchain and Ethereum, each block is connected to another block. The relationship between two blocks is one of parent and child, and it is a one-to-one relationship, forming a chain. In the later sections of this chapter, we will discuss blocks, and in the following diagram, we illustrate with three blocks (Block 1, Block 2, Block 3). Block 1 is the parent block of Block 2, and Block 2 is the parent block of Block 3. The hash value of the parent block is stored in the header of each block, establishing the parent-child relationship.
Block 2 stores the hash value of Block 1 in its header, and Block 3 stores the hash value of Block 2 in its header. So, the question arises: what is the parent block of the first block? Ethereum has the concept of a genesis block, which is the first block. This block is automatically created when the chain is first initiated. You can also think of the entire chain as starting with the genesis block (generated through the genesis.json file).
Knowing that blocks are interconnected, you might wonder how transactions are bound to blocks. In Ethereum, transactions are stored within blocks, and executing a transaction requires consuming a certain amount of gas. Since each block has a gas limit, the total gas consumed by pending transactions cannot exceed this limit, preventing all transactions from being stored in a single block. When the gas limit is reached, no further transactions can be written to this block, at which point nodes begin mining.
Once a transaction generates a hash value, it is stored in the block, and then the mining program selects the hash values of two transactions for re-hashing, producing a new hash value. Clearly, all transactions within a block ultimately produce a unique hash value, known as the Merkle root hash value, which is stored in the block header. If any transaction within the block changes, the hash value of that transaction will also change, ultimately leading to a change in the root transaction hash value. When the hash value of the block changes, since child blocks store the hash value of the parent block, the hash value of its child blocks must also change, triggering a chain reaction that requires the entire blockchain to change, thus making the immutability of the blockchain possible.
Nodes are computers that are interconnected via the P2P protocol, forming the Ethereum network.
There are two types of nodes in Ethereum:
- EVM
- Mining nodes
It is important to note that this classification is merely for conceptual differentiation; in most scenarios, there is no dedicated EVM, rather, all mining nodes perform the functions of the EVM.
The daily work of miners is quite similar to that of financial accountants, whose main job is to record data in financial ledgers. Similarly, the primary responsibility of miners is to write transaction data to Ethereum. The motivation for this work is the rewards they can earn; there are two types of rewards for miners: the first is the reward for writing blocks to the blockchain, and the second is the gas fees paid for transactions within the block. Generally, there are many miners on the blockchain competing with each other; however, ultimately only one miner can win and write the block to the ledger, while other miners are not authorized to write. The determination of which miner wins is based on solving a problem. During mining, each miner faces the same problem, and miners can only solve it using their machine's computational power. The first miner to solve the problem will write the block containing the transactions to their local ledger and then send the block and Nonce value[1] to other miners for confirmation. Other miners receive the block and verify whether the answer is correct; if so, the block will be written to their local ledger. In this process, the winning miner also receives a reward of 5 Ether. Each node maintains a copy of the Ethereum ledger, and the miners' responsibility is to synchronize data to keep their local ledgers up to date, ultimately achieving consistency among all miners' ledgers.
Mining nodes have three important functions:
- Mining or packaging transactions to produce new blocks and write them to the Ethereum ledger
- Sending the latest blocks to other miners and notifying them to receive
- Receiving blocks from other miners and updating their local ledgers
Mining nodes refer to those nodes belonging to miners, and they, along with EVM nodes, are part of the entire network. At a certain moment, miners will generate new blocks, then collect transactions from the transaction pool and package them into the newly created block. Ultimately, this block is added to the blockchain.
Next, we will explain some other concepts, such as consensus and solving problems.
[1] The answer to the problem being solved. — Translator's note
For all miners on the network, the mining program is the same; they need to continuously execute mining tasks according to the rules in the program. On one hand, miners are desperately calculating the program to mine new blocks, while on the other hand, they are constantly listening for new transactions in the transaction pool, receiving, verifying, and forwarding blocks from other miners. As mentioned earlier, miners first create a block, then collect transactions from the transaction pool and add them to the block. Before adding, they check whether the transaction has already been written to a block by other miners; if it has, they discard it.
To receive mining rewards, miners need to add their coinbase transaction (the first transaction in the block is the reward transaction for mining), then generate the block header and execute the following steps:
-
The miner grabs the hash values of two transactions, re-hashes them, and generates a new hash value. Once all transactions are hashed, a unique hash value is obtained, known as the root transaction hash value or Merkle root transaction hash value, which will be added to the block header.
-
The miner also needs to determine the hash value of the previous block, as it is the parent block of the current block, and the parent block's hash value must be stored in the current block header.
-
The miner calculates the state and receipts root hash values[1] of the transactions and writes them into the block header.
-
Nonce and timestamp are recorded in the block header.
-
The hash value for the entire block (including the block header and block body) is generated.
-
The mining process begins, with the miner continuously changing the Nonce value until they find a hash value that solves the problem. It is important to remember that the execution process is the same for miners on the network.
-
Clearly, a miner will eventually find the answer to the problem, and they will send the result to other miners on the network. Other miners will first verify whether the answer is correct; if it is, they will begin verifying each transaction, then accept the block and add it to their local ledger.
The entire mining process described above is referred to as proof of work (PoW) because the miner provides proof of the answer obtained through continuous calculations. Additionally, there are other algorithms, such as proof of stake (PoS) and proof of authority (PoA), but since Ethereum does not use these algorithms, this book will not delve into them. The block header and its components are shown in the diagram below:
[1] Here, state and receipts refer to two other Merkle trees. Ethereum has three Merkle trees: the transaction Merkle tree, state Merkle tree, and receipts Merkle tree.
Accounts are a major component of the Ethereum system. The process of saving transactions to the ledger on Ethereum is essentially the process of interaction between accounts. There are two types of accounts on Ethereum: external accounts and contract accounts. Each account has a default balance attribute, which shows the current balance of Ether in that account.
Users on Ethereum own accounts. In Ethereum, accounts cannot be called by name. When you create an external account on Ethereum, a pair of public and private keys is generated. The private key needs to be kept secure, while the public key serves as proof of ownership of the account. The public key is generally 256 characters long, but Ethereum only uses the first 160 characters to identify the identity.
For example, if Bob creates a new account on the Ethereum network (whether on a public or private network), he keeps the private key for himself, while the first 160 characters of the public key will become his identity identifier, allowing other accounts to send Ether or other Ethereum-based cryptocurrencies to this account.
External accounts can hold Ether but cannot execute any code. They can transact with other external accounts and can also execute transactions using functions within smart contracts.
A transaction is an agreement between a buyer and seller, supplier and consumer, or provider and consumer to exchange currency, cryptocurrency, or other assets for each other's assets, products, or services. Ethereum ensures the smooth execution of transactions, and the following are the three types of transactions supported on Ethereum:
- Sending Ether from one account to another: This account can be either an external account or a contract account. The following scenarios can occur:
- In a transaction, one external account sends Ether to another external account.
- In a transaction, one external account sends Ether to a contract account.
- In a transaction, one contract account sends Ether to another contract account.
- In a transaction, one contract account sends Ether to an external account.
-
Smart contract deployment: An external account deploys a contract on the EVM through a transaction.
-
Using or invoking functions within a contract: If you need to execute a function within a contract to change a state, a transaction is required; if the function does not change any state, a transaction is not needed.
Below are some important attributes related to transactions:
The "From" account attribute indicates that this account is the initiator of the transaction, sending gas or Ether. We have previously introduced the concepts of Ether and gas. The "From" account can be either an external account or a contract account.
The "To" account attribute refers to the account receiving Ether or other benefits; it can be either an external account or a contract account. If it is a contract deployment transaction, the "To" field will be empty.
The "Value" account attribute refers to the amount of Ether being transferred between accounts. The "Input" account attribute refers to the bytecode of the contract that has been deployed on the EVM after compilation. The input is also used to store information about function calls with parameters in smart contracts. The diagram below shows where the input field is used in a typical Ethereum transaction with a smart contract function; note that the input field contains a function call with parameters.
The "BlockHash" account attribute refers to the hash value of the block to which the transaction belongs.
The "BlockNumber" account attribute refers to the block number of the transaction.
The "Gas" account attribute refers to the amount of gas paid by the sender of the transaction.
The "GasPrice" account attribute refers to the gas price paid by the sender, measured in wei (the concept of wei was mentioned earlier in this chapter). Total gas consumption = gas amount * gas price.
The "Hash" account attribute refers to the hash value of the transaction.
The "Nonce" account attribute refers to the transaction number, which is generated by the sender before the current transaction.
The "TransactionIndex" account attribute refers to the serial number of the current transaction in the block.
The "Value" account attribute refers to the amount of Ether transferred, calculated in wei.
The "V", "r", and "s" attributes refer to the digital signature and transaction signature.
Below is a common transaction on Ethereum, where an external account sends Ether to another external account (note that the input field is not used here). The transaction screenshot shows that 2 Ether were transferred, with the value field measured in wei.
Transferring Ether between external accounts can also be implemented using the following code, based on the web3 JavaScript framework, which will be introduced later in this book.
The diagram below shows a transaction that deployed a smart contract, noting that the input field contains the bytecode compiled from the contract.
A block is an important concept in Ethereum. A block is a container for transactions, consisting of multiple transactions. Due to the gas limit of blocks (which will be introduced later) and block size restrictions, the number of transactions contained in each block varies. Blocks are connected together to form a blockchain, with the exception of the first block (also known as the genesis block), which has no parent block; all other blocks have a parent block, and the hash value of the parent block is stored in the block header.
The diagram below shows the structure of a block:
Blocks have many attributes; to facilitate understanding of key content, only some important parts are introduced below:
The "Difficulty" attribute refers to the computational difficulty miners face to mine this block.
The "GasLimit" attribute refers to the total gas limit allowed for the block. It determines how many transactions can be included in the block.
The "GasUsed" attribute refers to the actual amount of gas consumed by the transactions in the block.
The "Hash" attribute refers to the hash value of this block.
The "Nonce" attribute refers to a number that is the answer to the problem being solved.
The "Miner" attribute refers to the miner's account, which can be identified by the coinbase or etherbase address.
The "Number" attribute refers to the sequence number of the block in the blockchain.
The "ParentHash" attribute refers to the hash value of the parent block.
The "ReceiptsRoot", "stateRoot", and "TransactionsRoot" attributes refer to the Merkle trees mentioned in the previous mining process.
The "Transactions" attribute refers to an array of transactions that make up the block.
The "TotalDifficulty" attribute refers to the overall difficulty of the blockchain.
End-to-End Transaction#
Sam intends to send a digital asset (e.g., dollars) to Mark. First, Sam creates a transaction that includes fields such as from, to, and value, and then sends it to the Ethereum network. This transaction is not immediately written to the ledger but is temporarily stored in the transaction pool.
Mining nodes create a new block and then extract transactions from the transaction pool according to the gas limit standard (Sam's transaction will also be extracted) and add them to the block. All miners on the network are performing the same task.
Next, miners begin competing to solve the problem, and after a period (or a few seconds), the winner (the first to solve the problem) will issue a notification claiming they have found the answer and won the race, needing to write the block to the blockchain. Meanwhile, the winner adds the answer to the block and sends it to other miners. Upon receiving the notification, other miners first verify the answer; once confirmed as valid, they immediately stop their calculations, accept the block containing Sam's transaction, and add it to their local ledger. Thus, a new block is created on the chain, which will permanently exist across time and space. During this process, both parties' account balances will be updated, and finally, the block will be distributed and copied to all nodes on the network.
What is a Contract#
A contract is a legal document agreed upon by two or more parties to execute a transaction immediately or in the future. Because contracts are legal documents, they have enforceability and executability. There are many scenarios for contract applications, such as a person signing a contract with an insurance company to purchase health insurance, or a person buying a piece of land from another person, or a company selling equity to another company.
What is a Smart Contract#
A smart contract is code written according to user requirements and deployed and run on the Ethereum Virtual Machine. Smart contracts are digitalized, embedding the rules of transactions between accounts within the code. Smart contracts facilitate the transfer of digital assets through atomic transactions and can also be used to store important data, which can be used to record information, events, relationships, balances, and information that needs to be agreed upon in real-world contracts. Smart contracts are similar to object-oriented class definitions, so one contract can call another contract, just as we can call and instantiate between class objects. We can also think of smart contracts as small programs composed of functions. You can create a new contract and use the functions within it to view data on the blockchain and update data according to certain rules.
How to Write a Smart Contract#
Peter Todd, one of the core developers of Bitcoin, summarized the current state of smart contracts, stating that "the conclusion of the discussion on smart contracts is that no one understands what smart contracts really are. If we are to implement smart contracts, we would need oracles."
Indeed, clarifying the concept and essence of smart contracts is not an easy task.
We start with the origin of the smart contract concept. The term "smart contract" was proposed by computer scientist and cryptographer Nick Szabo around 1993. In 1994, he wrote the paper "Smart Contracts," which is considered the seminal work on smart contracts.
Nick Szabo begins his explanation of smart contracts with an example of a vending machine. We can think of the original ancestor of smart contracts as the unassuming vending machine. After potential assessments with limited losses, the vending machine ensures that the money in its cash box is far less than the cost incurred by a vandal. The vending machine charges coins based on the displayed product price, forming the initial computer design science through a simple mechanism, and automatically delivers changes and products based on the amount inserted. The vending machine is a transport contract: anyone holding coins can transact with the supplier. Locking the cash box and other security mechanisms protect the coins and goods stored in the vending machine from being vandalized, thus supporting the deployment of vending machines in various areas and generating profits.
Based on the vending machine concept, Nick Szabo provides the following definition of smart contracts:
"Smart contracts transcend the category of embedding various valuable attributes in vending machines, controlling contracts digitally. Smart contracts involve properties with dynamic and frequently proactive execution attributes, providing better observation and verification points, where proactive measures must be precise."
What Nick Szabo tells us is that the essential abstract concept of smart contracts is a recognized tool for forming relationships between individuals, institutions, and properties, a set of agreements that establish relationships and reach consensus. The terms of smart contracts (such as collateral, property division, etc.) can be embedded into processing hardware and software in such a way that the cost of breach is very high (even prohibitive). For example, a digital guarantee smart contract designed for a house continuously improves the house collateral agreement to better embed it into the processing of contract terms. According to the contract terms, these agreements will ensure that the encryption keys are entirely controlled by individuals with operational attributes, who will also legitimately own the property of the house. Simply put, to prevent theft, the user must complete the correct unlocking process; otherwise, the house will switch to an unusable state, such as door access failure and facility failure. In traditional methods, if the house is used for loan repayment, a headache for creditors is the difficulty of recovering the house from a defaulting debtor, requiring frequent communication to retrieve the house keys. To solve this problem, we can create a smart lien agreement: if the owner fails to pay, the smart contract invokes the lien agreement, transferring control of the house keys to the bank. This agreement may be cheaper and more effective than hiring a debt collector.
At the same time, Nick Szabo proposed three elements of smart contracts:
- A lock that allows the owner to simultaneously exclude illegal third parties;
- A backdoor that allows creditors secret access;
- The backdoor is only opened during a period of default and non-payment; after the final electronic payment is completed, the backdoor will be permanently closed.
Essentially, the working principle of these smart contracts is similar to the if-then statements of computer programs. Smart contracts interact with real-world properties in this way. When a pre-defined condition is triggered, the smart contract executes the corresponding contract terms. The reason why Nick Szabo's theory of smart contract work has not been realized for a long time is due to the lack of a digital system that can inherently support programmable contracts. If financial institutions still need to manually approve the transfer of assets, then the goal of smart contracts has not been achieved. Phil Rapoport, head of markets and trading at Ripple Labs, said, "One major obstacle to implementing smart contracts is that current computer programs cannot truly trigger payments." The emergence and widespread use of blockchain technology are changing the status quo that hinders the realization of smart contracts, thereby providing an opportunity for Nick Szabo's ideas to be realized. Smart contract technology is now built on the foundation of blockchain, as the blockchain itself is a computer program, and smart contracts can interact with it just as they can with other programs.
Based on the proposed concept of smart contracts and the continuous development of blockchain technology in recent years, we will attempt to provide a more specific and detailed explanation of smart contracts.
Smart contracts are a set of digitally defined commitments that control digital assets and contain the rights and obligations agreed upon by the participants in the contract, executed automatically by computer systems.
The commitment defines the essence and purpose of smart contracts. Taking a sales contract as an example: the seller commits to sending goods, and the buyer commits to paying a reasonable price. The digital form means that the contract needs to be written in executable code for computers; as long as the participants reach an agreement, the rights and obligations established by the smart contract will be executed by a computer or a network of computers.
Let’s illustrate smart contracts with a simple example:
If Event_X_Happened:
Send(Alice, 1000$)
Else:
Send(Bob, 1000$)
This means: if event X occurs, the contract sends $1000 to Alice; otherwise, it sends $1000 to Bob.
This is the simplest form of a contract.
The diagram below is a schematic model of a smart contract, with the definitions of its components as follows:
- Contract participants: Relevant participants executing the smart contract.
- Contract resource set: Resources involved in executing the smart contract, such as accounts of the parties involved, owned digital assets, etc.
- Automatic state machine: The key to the next step of the smart contract execution, including current resource state judgment, next contract transaction execution selection, etc.
- Contract transaction set: A collection of the next actions or behaviors of the smart contract, controlling the contract assets and responding to external information received.
There are many tools for writing smart contracts, such as Visual Studio. The simplest and fastest development method is to use browser-based development tools, such as Remix. You can directly use it by opening the website http://remix.ethereum.org. Remix is a new name; it was previously called browser-solidity. Remix provides a rich Solidity integrated development environment for creating, developing, deploying, and debugging smart contracts in the browser. Operations related to contract maintenance (such as creation, publishing, debugging) can all be completed in the same environment without switching to other windows or pages.
Some people may not be accustomed to using the online version of Remix. Since Remix is an open-source tool, it can be downloaded from https://github.com/ethereum/browser-solidity. After compiling, Remix can be used locally. Another advantage of using local tools is that it can connect to your own private network; otherwise, after writing code on the web, you would need to copy the files locally and compile them before publishing them to the private network. Let’s gradually understand Remix.
- Open the remix.ethereum.org website, and a smart contract will open by default in the browser.
If you don’t need it, you can delete it.
-
Create a new contract by selecting the + in the left menu bar.
-
Name this Solidity file with a .sol suffix. Enter the contract name HelloWorld, click "OK," and you will create a blank contract, as shown in the diagram below:
-
In the blank space in the production bar, enter the following code to create your first contract.
The details of the contract will be explained in Chapter 3. For now, simply understand that you can use the keyword contract to create a contract, declare global state variables and functions, and save the contract as a file with the .sol suffix. In the source code snippet below, when the GetHelloWorld function calls the HelloWorld contract, it will return the "Hello World" string.
To the right of Remix is the operation window, which has many tab pages: compile, run, settings, debug, analyze, and support. These operations are used to compile, publish, debug, and call the contract. The debug tab will compile the contract into bytecode (the code understood by Ethereum), and it will display warnings and error messages that occur during debugging. You should pay enough attention to these messages, as resolving them will enhance the robustness of the contract. The run page requires more time compared to the contract writing page. In the "Environment" option, since Remix binds to Ethereum's runtime environment in the browser, it allows you to use the JavaScript virtual machine to publish contracts. The Injected Web3 option is used to work with Mist and MetaMask tools (which will be mentioned in the next chapter), and the Web3 Provider option is used when connecting Remix to a private chain locally. In the examples in this chapter, the default JavaScript virtual machine option is used. The remaining options will be discussed when introducing Solidity in Chapter 3.
-
However, the most important operation for publishing a contract is to use the new button, as shown in the diagram below:
-
Clicking the new button will allow you to run Ethereum in the browser, and the functions within the contract will be displayed below the new button. Since there is only one function, GetHelloWorld, there is only this option. As shown in the diagram below:
-
Clicking the GetHelloWorld button will call and execute this function. The execution result will be displayed in the section below Remix, as shown in the diagram below:
Congratulations! Your first contract has been created, published, and executed successfully. If you do not like typing code directly, you can copy the HelloWorld contract code from this chapter and use it directly in Remix.
Remix simplifies the publishing process, but many programs are executed in the background. Understanding these steps helps us better control the publishing process.
-
Automation dimension. Smart contracts can automatically determine triggering conditions and select the corresponding next transaction; traditional contracts require manual judgment of triggering conditions, which is less accurate and timely than smart contracts.
-
Subjective and objective dimensions. Smart contracts are suitable for objective requests, while traditional contracts are suitable for subjective requests. The agreements, collateral, and penalties in smart contracts must be clearly defined in advance; however, subjective judgment indicators are difficult to incorporate into contract automata for judgment, making it challenging to guide the execution of contract transactions.
-
Cost dimension. The execution cost of smart contracts is lower than that of traditional contracts, as the rights and obligations of contract execution conditions are written into computer programs for automatic execution, providing cost advantages in state judgment, reward and punishment execution, and asset disposal.
-
Execution time dimension. Smart contracts belong to a pre-defined, preventive execution model; traditional contracts adopt a post-execution model, determining rewards and punishments based on state.
-
Breach penalty dimension. Smart contracts rely on collateral, deposits, and digital assets with digital attributes; once breached, participants' assets will suffer losses. In contrast, traditional contract breach penalties mainly rely on criminal penalties, and legal means can be used to protect rights in the event of a breach.
-
Applicability dimension. Smart contract technology can be adopted globally and is suitable for a global scope; traditional contracts are limited by specific jurisdictions, and different international regions' laws and cultural factors affect the execution process of traditional contracts.
How to Deploy a Contract#
The first step is to compile the contract using the Solidity compiler (in the next chapter, we will introduce how to download and use the Solidity compiler).
After compilation, there are mainly two outputs:
- ABI specification
- Contract bytecode
ABI (Application Binary Interface) is an interface composed of external functions with parameters and public functions. If a contract and other users are prepared to call functions within the contract, they can use the ABI to achieve this.
Bytecode is the representation of the contract that runs on Ethereum. When publishing, bytecode is required, while ABI is only used when calling functions within the contract. You can use the ABI to create a new contract instance.
The publication of a contract itself is a transaction. Therefore, to publish a contract, you need to create a new transaction. When publishing, you need to provide the bytecode and ABI. Since transactions consume gas during execution, this gas needs to be provided by the contract. Once the transaction is packaged and written to the blockchain, you can use the contract via its address, and the caller can also call functions within the contract through the new address.
A deep understanding of how blockchain and Ethereum work will help you write more robust, secure, and efficient smart contracts using Solidity. This chapter introduced the basic concepts of blockchain, explained what blockchain is, why it is important, and what value it has for building decentralized and distributed applications. This chapter also briefly introduced the architecture of blockchain and some important concepts, such as transactions, blocks, gas, Ether, accounts, cryptography, and mining. Additionally, this chapter touched on some aspects of smart contracts, such as writing smart contracts using Remix and how to run them. The explanations are relatively simple, as further detailed discussions will follow, at which point you will be able to develop smart contracts using Solidity.
You will notice that this chapter did not mention some tools related to Ethereum. The next chapter will introduce the installation of Ethereum and related tools. The Ethereum ecosystem is rich, with many tools available. We will select some key ones to introduce, such as web3.js, TestRPC, Geth, Mist, and MetaMask.
Smart Contracts and Blockchain#
Nick Szabo's theory of how smart contracts work has not been realized for a long time, primarily due to the lack of digital systems and technologies that can support programmable contracts. The emergence of blockchain technology has solved this problem, as it not only supports programmable contracts but also has advantages such as decentralization, immutability, and transparent traceability of processes, making it inherently suitable for smart contracts. Therefore, it can also be said that smart contracts are one of the features of blockchain technology.
If blockchain 1.0 is represented by Bitcoin, which solves the decentralization problem of currency and payment methods, then blockchain 2.0 is a more macro approach to decentralizing the entire market, using blockchain technology to convert many different digital assets, not just Bitcoin, to create value for different assets. The decentralized ledger function of blockchain technology can be used to create, confirm, and transfer various types of assets and contracts. Almost all types of financial transactions can be transformed for use on the blockchain, including stocks, private equity, crowdfunding, bonds, and other types of financial derivatives such as futures and options.
Smart contracts appear to be a segment of computer execution programs that can be accurately and automatically executed. So why is it difficult to achieve this with traditional technologies, necessitating new technologies like blockchain? Traditional technologies, even with software restrictions and performance optimizations, cannot simultaneously achieve the characteristics of blockchain: first, data cannot be deleted or modified, only added, ensuring the traceability of history, while the cost of malicious behavior will be high because such actions will be permanently recorded; second, decentralization avoids the influence of centralized factors.
Smart contracts based on blockchain technology can not only leverage the cost-efficiency advantages of smart contracts but also avoid malicious behavior interfering with the normal execution of contracts. By writing smart contracts in digital form into the blockchain, the characteristics of blockchain technology ensure that the entire process of storage, reading, and execution is transparent, traceable, and immutable. At the same time, the consensus algorithm built into the blockchain constructs a state machine system that allows smart contracts to run efficiently.
How Smart Contracts Work#
Blockchain-based smart contracts include mechanisms for transaction processing and storage, as well as a complete state machine for accepting and processing various smart contracts, with transaction storage and state processing completed on the blockchain. Transactions mainly contain the data to be sent, while events describe this data. After transaction and event information is passed to the smart contract, the resource states in the contract resource set will be updated, triggering the smart contract to perform state machine judgments. If the triggering conditions for one or more actions in the automatic state machine are met, the state machine will automatically execute the contract actions based on preset information.
The smart contract system automatically issues preset data resources and events containing triggering conditions when the triggering conditions are met, based on the event description information. The core of the entire smart contract system lies in the fact that the smart contract processes transactions and events in the form of transactions and events, outputting another set of transactions and events; the smart contract is merely a system composed of a transaction processing module and a state machine, which does not generate smart contracts nor modify them; its existence is solely to allow a set of complex, condition-triggered digital commitments to be executed correctly according to the will of the participants.
The construction and execution of blockchain-based smart contracts are divided into the following steps:
- Multiple users jointly participate in formulating a smart contract.
- The contract spreads through the P2P network and is stored on the blockchain.
- The smart contract constructed by the blockchain is executed automatically.
Step 1, "Multiple users jointly participate in formulating a smart contract," includes the following steps:
A. Users must first register as users of the blockchain, and the blockchain returns a public key and private key to the user; the public key serves as the user's account address on the blockchain, while the private key is the only key to operate that account.
B. Two or more users jointly agree on a commitment that includes the rights and obligations of both parties; these rights and obligations are programmed in machine language in electronic form; participants sign with their respective private keys to ensure the validity of the contract.
C. The signed smart contract will be transmitted to the blockchain network based on the content of the commitment.
Step 2, "The contract spreads through the P2P network and is stored on the blockchain," includes the following steps:
A. The contract spreads through P2P across the entire blockchain network, and each node receives a copy; the verification nodes in the blockchain will first save the received contract in memory, waiting for the next consensus time to trigger consensus and processing for that contract.
B. When the consensus time arrives, the verification nodes will package all contracts saved in memory during the recent period into a contract set and calculate the hash value of this contract set, finally assembling the hash value of this contract set into a block structure and spreading it across the network; other verification nodes receiving this block structure will extract the hash of the contract set contained within it and compare it with their saved contract set; at the same time, they will send a contract set they recognize to other verification nodes; through this multi-round sending and comparison, all verification nodes will ultimately reach consensus on the latest contract set within the specified time.
C. The latest agreed contract set will spread across the entire network in the form of a block, as shown in Figure 4-2. Each block contains the following information: the hash value of the current block, the hash value of the previous block, the timestamp at which consensus was reached, and other descriptive information; at the same time, the most important information in the blockchain is a set of contracts that have reached consensus; nodes receiving the contract set will verify each contract, and only those that pass verification will be written to the blockchain, with the verification content mainly focusing on whether the private key signatures of the contract participants match their accounts.
Step 3, "The smart contract constructed by the blockchain is executed automatically," includes the following steps:
A. The smart contract periodically checks the state of the automaton, traversing each contract's state machine, transactions, and triggering conditions; transactions that meet the conditions will be pushed to the queue for verification, while transactions that do not meet the triggering conditions will continue to be stored on the blockchain.
B. Transactions entering the latest round of verification will spread to each verification node, and like ordinary blockchain transactions or transactions, the verification nodes will first perform signature verification to ensure the validity of the transactions; verified transactions will enter the pending consensus set, and once a majority of verification nodes reach consensus, the transactions will be successfully executed and notify the users.
C. Once the transactions are successfully executed, the smart contract's built-in state machine will determine the status of the contract; when all transactions included in the contract have been executed in order, the state machine will mark the contract's status as completed and remove that contract from the latest block; otherwise, it will be marked as ongoing and continue to be stored in the latest block, waiting for the next round of processing until completed; the entire transaction and state processing is automatically completed by the smart contract system built into the underlying blockchain, with full transparency and immutability.
Assuming user Alice and user Bob need to construct a blockchain smart contract, the purpose is for Alice to lease her house to Bob for a rent of 1000 yuan per month, payable monthly, for a duration of one year. Assuming Alice's house lock can be controlled via the internet, with the unlocking key being Key (generated once a month), Alice's bank account is MA, and Bob's bank account is MB. The execution of the smart contract includes the following steps:
-
Alice and Bob submit a contract construction application to the smart contract server, which generates the contract and publishes it to the blockchain for it to take effect.
-
Alice provides the Key and MA to the smart contract server.
-
Bob pays 1000×12=12000 yuan to the smart contract server as a deposit through MB, or Bob pays a small amount to the smart contract server through a third-party institution's guarantee.
-
The contract begins execution; the smart contract server sends the Key to Bob and deducts 1000 yuan from Bob's deposit to send to Alice's account, generating participant records stored on the blockchain.
-
Each month, the smart contract will periodically check; if the contract has not expired, it will continue to deduct 1000 yuan from Bob's deposit to send to Alice's account and send the Key to Bob, generating participant records stored on the blockchain.
-
The entire process is monitored by a third-party institution, and all participants and third-party institutions can query the contract execution status through the blockchain.
-
After the lease term ends, the smart contract server generates a contract record indicating the termination of the contract and publishes it to the blockchain, thus terminating the contract execution.
The development of smart contracts may require a long road, but more smart contract mechanisms are being designed, and more talents from various fields are joining. So far, for automated contract execution from entirely different fields such as economics, cryptography, network science, and finance, jointly designing research contract criteria is a necessary path. Without cross-communication, whether due to a lack of technology or a lack of awareness of business use models, it will lead to inefficiencies in smart contracts.
Currently, platforms and related research dedicated to smart contracts include Orisi, Codius, Symboint, Hedgy, BitHalo, Mirror, Hyperledger, Eris Industries, Ethereum, Smart Contract, AntShares, Colored Coin, IBM, and others, and the application prospects of smart contracts are bright.
References
[1]http://www.fastcolabs.com/3035723/app-economy/smart-contracts-could-be-cryptocurrencys-killer-app
[2]https://medium.com/@heckerhut/whats-a-smart-contract-in-search-of-a-consensus-c268c830a8ad
[3] http://www.wtoutiao.com/p/14dyEMP.html
[4]http://www.coindesk.com/smart-contract-myths-blockchain/
[5]http://8btc.com/article-1921-1.html
[6]http://wangxiaoming.com/blog/2016/03/03/blockchain-2-0-he-yue/
Ethereum Network#
The Ethereum network is an open-source platform for creating and deploying distributed applications, supported by numerous computers (nodes) that interact with each other and store data in a distributed ledger. Here, the meaning of a distributed ledger is that each node on the network has a copy of the ledger. For developers, there are multiple network deployment methods to choose from, and when deploying solutions and smart contracts, they can select a suitable network based on actual needs and scenarios, so they do not have to pay real Ether or incur other expenses. Of course, there are also free networks available, as well as networks that require users to pay Ether or other currencies.
The Ethereum mainnet is a global public network that anyone can use, and access to the network can be gained through an account. For anyone, creating an account, deploying solutions, and contracts are free. The usage fees on the mainnet are measured in gas. Its code name is homestead (previously called Frontier). Through the internet, people can connect to the mainnet and view the data and transactions on it.
Test Networks#
The purpose of test networks is to help people quickly adapt to and use the Ethereum environment, and they are precisely replicated from the mainnet. On test networks, deploying and using contracts incurs no real costs because the Ether used for testing is generated arbitrarily and can only be used on the test network. At the time of writing this book, there are multiple test networks available, such as Ropsten, Kovan, and Rinkeby.
Ropsten is the first test network that produces blocks using the PoW consensus algorithm. It was previously called Morden. As mentioned above, you can use it free of charge to build and test contracts. By adding the --testnet parameter in Geth, you can enter the test network. The usage of Geth will be introduced later. Ropsten is currently the most popular test network.
Rinkeby is another Ethereum test network that uses the PoA consensus mechanism. The mechanisms for establishing consensus among miners differ between PoW and PoA. PoW is stronger in maintaining immutability and decentralization, but its drawback is that it cannot effectively control miners. PoA has the advantages of PoW while also having a certain level of control over miners.
The Kovan test network can only be used among a small number of users, so more information can be accessed at: https://kovan-testnet.github.io/website/.
Private Networks#
Private networks are established and run on networks owned by users, with control held by a single organization. For testing purposes, people do not want to place solutions, contracts, and scenarios on public networks, so they need to establish a development, testing, and production environment. Therefore, users should set up a private network to maintain full control.
Consortium Networks#
Consortium networks are also a type of private network, but with one difference: the nodes in a consortium network are managed by different organizations. In fact, no single organization can independently control the network and data on a consortium chain; instead, control is shared among all organizations and individuals within those organizations who have viewing and modification permissions. Consortium chains can be accessed via the internet or VPN networks.
Geth#
Currently, there are various Ethereum client nodes and tools written in multiple languages, including Go, C++, Python, JavaScript, Java, and Ruby. These functionalities are not limited by language, allowing developers to choose the one that best suits them. This book uses Go language, known as Geth, which can connect to public and test networks, and can also run mining and EVM (transaction nodes) on private networks.
Geth is a command-line tool written in Go language, which can be used to create nodes and miners on private networks. Geth can be installed in Windows, Linux, and Mac environments.
Installing Geth on Windows#
Before setting up a private network, you need to download and install the Geth (goethereum) tool.
The following are the steps for downloading and installing:
-
You can visit https://ethereum.github.io/go-ethereum/downloads/ to download, with both 32-bit and 64-bit versions available. In this book, the operating system used is Azure cloud's Windows Server 2016 version.
-
After downloading, execute the installation file, and then follow the default configuration step by step to complete the development environment setup.
-
Once the installation is complete, you can use it in CMD and PowerShell.
-
Open the command line and enter geth --help.
It is important to note that when you enter geth, it will connect to the mainnet and start synchronizing and downloading block and transaction data. Currently, the entire Ethereum blockchain is over 30GB[1]. After executing the help command, the screen will display the available commands for geth, including the version number.
Geth is based on the JSON RPC protocol. It defines a remote call specification using JSON format. Geth can connect using the following three JSON RPC protocols:
- Inter-Process Communication (IPC): Internal communication, usually used within a single computer.
- Remote Procedure Calls (RPC): Communication across computers, typically using TCP and HTTP protocols.
- WS, WebSockets: Using sockets to connect to Geth.
Configuring Geth involves many commands, switch parameters, and options, including:
- Configuring IPC, RPC, and WS protocols
- Configuring the type of network connection—private network, Ropsten, and Rinkeby
- Mining options
- Console and API
- Network
- Debugging and logging
Some options used when creating a private network will be mentioned in the next section.
Geth can connect to the public chain directly using Geth (without options). Homestead is the name of the current public chain. Its Network ID and Chain ID are both 1.
On different networks, the Chain ID is different. Among them:
- Chain ID 1 is the main public chain
- Chain ID 2 is the Morden network (open to a limited number of people)
- Chain ID 3 is the Ropsten network
- Chain ID 4 is the Rinkeby network
- Chain IDs greater than 4 are private networks.
Additionally, you can use Geth --testnet to connect to the Ropsten network, and --rinkeby to connect to the Rinkeby network. Geth can also be used in conjunction with Chain IDs.
Setting Up a Private Network#
Once Geth is installed, you can first configure it locally without needing to connect to the internet. Each network has a genesis block, which is the first block of the entire network and has no parent block. The genesis block is generated by the genesis.json file. The following code shows the content of genesis.json.
Following the steps below, we will begin setting up a private network.
-
During network initialization, Geth needs to use the genesis.json file and provide a directory for storing block data and account private keys (keystore).
-
Enter the geth init command, the genesis.json file, and the folder for storing block data and keystore to initialize.
The output result is as follows:
-
In the screenshot above, we can see that the genesis block has been generated, and the Geth node can be started. Geth defaults to using the IPC protocol upon startup. To ensure that the Geth node can be accessed via the RPC protocol, the command needs to include the RPC parameter.
-
To set up the environment for running nodes, execute the following command:
The execution result is as follows:
From the command execution, we can see that the command used the datadir parameter, enabled RPC, exposed modules and APIs to the outside, and the Network ID is 15, indicating that this is a private network[1]. From the execution result, we will find some valuable information. First, the etherbase or coinbase has not been set; it needs to be set before mining starts (the mining program can also be started via command line). The screen displays the location where block data is stored, as well as the Chain ID, and whether it is connected to the Homestead mainnet; a value of 0 indicates that it is not connected to the mainnet. The enode information on the screen is the node's identity in the network. If other nodes are preparing to join this network, they will need to provide their enode value.
Finally, the screen shows that the IPC and RPC protocols are running and accepting requests. RPC access can be done through http://127.0.0.1:8545 or http://localhost:8545, while IPC access can be done through \.\pipe\geth.ipc.
-
The above command will start the Ethereum node. Observant readers may notice that the command runs as a service, which prevents executing other commands. Therefore, to manage these running nodes, we can open another command line window on the local machine and enter Geth attach ipc:\.\pipe\geth.ipc to connect to the node, allowing us to execute other commands. The following diagram shows this:
-
To connect to the local private network, you can use the RPC method, entering the command Geth attach rpc:http://localhost:8545 or get attach rpc:http://127.0.0.1:8545. If the results you see differ from those shown above, it is because in my example, the coinbase account has already been set. The coinbase account will be introduced later.
-
The default RPC access port is 8545, which can be set in the command line using the -rpcport parameter. The IP address can also be set using the -rpcaddr parameter.
-
After connecting to the Geth node, you will need to set the coinbase or etherbase account. First, create a new account using the newAccount method of the personal object. When creating an account, you will need to enter a password. The following screen shows the account ID information after the account is created.
-
After the account is created, you need to determine whether to use the coinbase account or etherbase account. If you need to change the existing coinbase account address, you can use the setEtherBase function of the address.miner object to perform the operation. This operation will replace the existing coinbase account with the new account. The result of the change will be displayed as true or false.
-
Execute the following query command, and you will find that the address you just set has taken effect.
Once the coinbase address is successfully set, the Geth node starts running, and mining can be initiated. Since we only have one miner, it will receive all the mining rewards, and the Ether in the coinbase account will gradually increase.
- Execute the following code to start mining:
You can also use the command below:
The execution result of the above command is as follows:
The parameter in start represents the number of threads used for mining. This command will start the mining program, and you can also see the execution results on the screen.
- If you need to stop mining, you can execute the miner.stop() command in another window.
[1] As mentioned earlier, IDs greater than 4 indicate private networks. — Translator's note
ganache-cli#
In Ethereum, the process of writing transactions to the ledger is divided into two stages:
- Creating a transaction and placing it in the transaction pool.
- Regularly fetching transactions from the transaction pool and then starting mining. Mining means writing these transactions to the Ethereum database or ledger.
From this process, it can be seen that developing and testing solutions and smart contracts on Ethereum can be very time-consuming. Ganache-cli (formerly known as TestRPC) was created to alleviate this issue. Ganache-cli includes the transaction processing flow and mining functionality of Ethereum, but mining does not require competition; transactions are immediately written to the ledger after they are generated. For developers, using ganache-cli can serve as an Ethereum node, allowing transactions to be written to the ledger without mining.
Ganache-cli is developed based on Node.js, so before deploying ganache-cli, you need to install Node.js, which can be downloaded from the website https://nodejs.org/en/download/. You need to choose the appropriate 32-bit or 64-bit operating system version and click the link displayed on the screen to download the installation package:
Now, we will download the 64-bit Windows installation version, which can be used to install the node package manager (NPM) and Node.js.
Solidity Compiler#
Solidity is a language for writing smart contracts. The following chapters will detail smart contracts. After writing Solidity code, you need to use the Solidity compiler to compile it, which will generate bytecode and other outputs that will be used when deploying smart contracts. Previously, Solidity was part of the Geth installation package, but it has now been separated from the Geth installation process and needs to be installed independently. The Solidity compiler is called solc and can be installed using the npm command:
web3 JavaScript Library#
The web3 library is an open-source JavaScript library that can connect to local or remote Ethereum nodes using IPC or RPC protocols. The web3 library is client-facing and can be used with web pages to initiate query requests and transaction submission requests to Ethereum nodes. We can use node management tools to install it, just like the installation of the Solidity compiler. When writing this book, the latest version of web3 could not run and prompted that it was not installed correctly due to the missing BigNumber.js file. However, previous stable versions can connect to the backend of Ethereum nodes. The following steps show how to install the web3 JavaScript library.
- Install web3 using the command below:
After execution, the result is as follows:
-
After installing web3, it can be called using Node.js. In command line mode, as shown in the diagram below, executing the node command will enter node mode.
-
In node mode, entering the command below will connect to the Ethereum node. The Ethereum node can be a TestRPC or a private network based on Geth. The following example shows web3 using RPC protocol to connect to the Ethereum node:
The first line of the command loads the web3 module, and the second command creates a new HttpProvider instance to connect to the Ethereum node on local port 8545.
- To verify whether web3 is connected to the Ethereum node, you can execute the isConnected method. If the return value is true, it indicates that web3 has successfully connected.
Mist Wallet#
Ether runs on Ethereum, so a wallet is needed to send and receive Ether. Mist is such a wallet that allows users to send and receive Ether, and it also facilitates users to conduct transactions on the Ethereum network (including public and private networks). In Mist, users can create accounts, send and receive Ether, deploy and call smart contracts.
Mist can be downloaded from https://github.com/ethereum/mist/releases. Users need to choose a suitable installation package based on their situation (in this example, we are using Windows 2016 environment, so we downloaded Ethereum-Wallet-win64-0-9-2.zip), and after downloading, unzip it. The files after unzipping are shown in the diagram below; double-click the Ethereum Wallet icon.
Mist will start. Mist has a certain level of intelligence; if a private chain is running locally, it will recognize and connect to it. If there is no private network running locally, it will connect to the mainnet or Rinkeby test network.
In this book, a private network has already been set up, and the following diagram shows its connection status:
Once connected successfully, you can interact with the Ethereum network, send and receive Ether, deploy smart contracts, and call functions within smart contracts.
MetaMask#
MetaMask is a lightweight plugin for the Chrome browser that can interact with the Ethereum network. It is also a wallet for sending and receiving Ether. MetaMask can be downloaded from https://metamask.io. Since MetaMask runs in the browser, blockchain data cannot be downloaded locally; it can only be stored on remote servers, and users access it through the browser. Please see the following steps:
-
MetaMask can be added as a plugin, as shown in the diagram below:
-
After confirming the privacy notice and user agreement, a small icon will appear next to the go button. MetaMask can connect to multiple networks, and the diagram below shows the connection to the local network at Localhost 8545.
-
In MetaMask, to verify identity, when creating a key, you need to provide a protective password. This information is stored in the MetaMask server's key vault, as shown in the diagram below:
-
Click the Account icon and use the Import Account menu to import an existing account:
-
After creating an account, MetaMask can transfer Ether from one account to another through Ethereum transactions.
-
To send Ether to another account, first select a sending account, then click the Send button, enter the target account address and amount in the pop-up window, and then click the Next button:
-
Click the submit button to submit the transaction. At this point, the transaction will be stored in the transaction pool and will be in a pending state. The mining task will begin to write this transaction into permanent storage.
-
Start the mining task in the Geth console, and the transaction will be packaged, as shown in the diagram below:
After a while, the transaction will be written to the ledger, and the account balances of both parties in MetaMask will change.
Ethereum nodes provide JSON RPC connection methods, and we can connect using various methods such as WebSockets, IPC, and RPC. In this chapter, we introduced various blockchain networks: public networks, main networks, test networks, and private networks. This chapter also explained how to build a private network and how to set up a development environment, which will be used in the next chapter. This chapter focused on how to install various tools in a Windows environment. These tools have various uses. Clearly, there are overlaps in functionality among some tools. For example, both the private network based on Geth and the ganache-cli development environment can create Ethereum nodes, but there are still some differences. This chapter also covered how to install Geth, the Solidity compiler, ganache-cli, the web3 library, JavaScript framework, Mist, and MetaMask wallet.
Thus, Ethereum has established a programmable, Turing-complete blockchain. On this blockchain, you can produce various digital assets through simple programs, and you can also write programs to precisely control the state of blockchain assets circulating on Ethereum, such as whether this asset is pending payment, locked, or has quota restrictions, whether this account is on a blacklist or whitelist, and the automatic exchange between Ethereum and other digital assets, etc. At the same time, Ethereum serves as a programmable, Turing-complete blockchain network foundation, on which we can realize more functional products for non-blockchain assets. For example, I can use Ethereum to establish smart contracts applied in personal daily economic life and corporate economic activities; such applications can also be realized.
Ethereum is a brand new open blockchain platform built on the concepts of blockchain and blockchain assets. It allows anyone to establish and run decentralized applications on the platform using blockchain technology. In simple terms, Ethereum technology is blockchain technology plus smart contracts.
From its inception, Ethereum has planned a detailed development path and iteration versions, with a total of four planned iteration versions:
The first version Frontier
The second version Homestead
The third version Metropolis
The fourth version Serenity
Now, let’s recount some significant events in Ethereum's history:
At the end of 2013, founder Vitalik published the initial version of the white paper, launching the project.
In July 2014, Ethereum conducted the first batch of Ether pre-sales. This was one of the more well-known cases in early ICOs. However, at that time, the term ICO was not used; insiders referred to this token issuance as "coin crowdfunding." Through a 42-day pre-sale, the Ethereum team raised over 30,000 bitcoins, selling 60 million Ether.
In October 2014, Ethereum reduced the block production time from 60 seconds to 12 seconds, which is currently stabilized at around 15 seconds.
On July 30, 2015, Ethereum's first version, Frontier, was released. This was the initial version of Ethereum, which only had a command-line interface and no graphical interface, mainly suitable for developers.
On March 14, 2016, Pi Day, Ethereum released the second version, Homestead, which is the version currently in operation, greatly improving usability with a graphical interface, allowing ordinary users to experience Ethereum's functions and development.
In July 2016, Ethereum underwent a hard fork, splitting into Ethereum ETH and Ethereum Classic ETC. This will be discussed in detail later.
Recently, Ethereum aims to release the third version, Metropolis. In the third version, the Ethereum founding team will release a browser designed for non-technical users, called the Mist browser. You can think of it as a Chrome browser, which is very convenient to use and has a powerful and complete application store. The Mist browser will also include a decentralized application store and basic applications. If such a browser can be released, it will greatly benefit the participation and experience of decentralized applications for a wide range of internet users. According to the current progress announced by the Ethereum team, the third version is expected to be released by the end of 2017.
The release date for the final stage, Serenity, has not yet been determined. In the first three stages, Ethereum's consensus mechanism uses the proof of work (PoW) consensus mechanism, while the fourth stage will switch to a hybrid consensus mechanism.
The hybrid consensus mechanism combines the Bitcoin-style proof of work (PoW) with the proof of stake mechanism created by Vitalik, Casper, balancing the rights and interests of miners and token holders.
After discussing version iterations, I will list some terms related to Ethereum to help you understand the entire Ethereum system.
Ether
The first term is Ether. It is the token in the Ethereum system, abbreviated as ETH. Ether is the main fuel within Ethereum, providing the primary liquidity for running various digital asset transactions on this system, and is also used to pay for smart contract fees. It is the built-in blockchain asset of Ethereum used to pay for the operation of smart contracts. This concept is relatively simple to understand; we previously learned about Hash Cash and proof of work. I wonder if everyone remembers. We learned that to prevent the network from being flooded with spam emails, the sending computer must perform some work calculations. This is a huge burden for computers sending large amounts of spam. Similarly, in the Ethereum network, establishing and running smart contracts also requires a small threshold; this threshold is relatively low for those who genuinely want to develop, but it poses a significant burden for initiators of numerous spam projects or attackers. However, the threshold for Ethereum is not work but fuel, which we call "Gas," and Gas is exchanged for Ether.
Imagine if running a smart contract did not require any fees; there would be many garbage contracts or applications on this blockchain, and the blockchain would be attacked, rendering the entire network unusable. Therefore, Ethereum's blockchain requires a certain amount of Gas to be paid each time a smart contract runs, which can be paid in Ether, ensuring the stability and security of this blockchain.
Regarding the denomination of Ether, we are familiar with Bitcoin's smallest denomination being a satoshi, which is one hundred millionth of a Bitcoin. Ethereum also has its smallest denomination, named 1 "wei." How small is it? One Ether token can be divided to 18 decimal places, which is 1 wei.
Additionally, in July 2016, a hard fork occurred in the Ethereum blockchain, splitting Ethereum into two blockchains. The upgraded Ethereum, led by founder Vitalik, is called Ethereum, with the token code ETH. The chain that does not accept this upgrade is called "Ethereum Classic," with the original chain's Ether token code being ETC.
Ethereum Virtual Machine (EVM)
The second concept is the Ethereum Virtual Machine (EVM). Typically, there are some compilation and execution virtual machines that support a programming system. Just as Java has the JVM, Ethereum has its virtual machine that can execute any complex algorithm code. Developers can use existing friendly programming languages like JavaScript or Python to create the applications they want on Ethereum.
Smart Contracts
The third concept is smart contracts. The idea of smart contracts is not new; it was first proposed by cryptographer Nick Szabo in 1995, almost simultaneously with the internet, referring to commitments defined and automatically executed by computer programs. Although the concept has been around for a long time, it was not widely applied until the emergence of Ethereum. One important reason is the previous lack of a friendly, programmable foundational system.
With smart contracts, anyone can create the decentralized applications they want on Ethereum. Once created, smart contracts on Ethereum can execute automatically without the involvement of intermediaries, and no one can stop their operation. Smart contracts on Ethereum can control various digital assets on the blockchain and perform complex algorithms and operations.
For example, we often purchase flight delay insurance when flying, but when a delay actually occurs, you may still need to call customer service to understand the process, obtain proof offline, and contact the insurance company to complete your claim. If there were smart contracts, inputting conditions and linking flight data would ensure that the insurance company automatically pays you after the flight is delayed. The execution of the contract does not require third-party involvement and is automatically executed, greatly improving the efficiency of social and economic activities.
On the Ethereum blockchain, you can write code for assets, create new blockchain assets; simply put, you can issue your own blockchain tokens, and you decide what kind of issuance mechanism to use, what the token is called, how many to issue, and how to issue them. Sounds interesting, right? At the same time, you can also create functionalities for non-blockchain assets by writing smart contract code, such as voting, betting, conditional contracts, etc.
To support smart contracts, Ethereum has two types of account addresses: one is called an ordinary account, and the other is called a contract account. Ordinary accounts are similar to accounts in the Bitcoin network, while contract accounts are mainly used for smart contracts.
Now, let’s review this lesson. We introduced the second type of blockchain project, the smart contract platform, represented by Ethereum. I guided you through the origins of Ethereum, version iterations, and important terms.
Ethereum was created by Russian developer Vitalik in 2013, addressing the shortcomings of the Bitcoin blockchain system, such as the lack of Turing completeness. Ethereum established a programmable, Turing-complete blockchain that helps people more conveniently produce various digital assets and more precisely control the state of blockchain assets.
Ethereum's development has planned four stages: Frontier, Homestead, Metropolis, and Serenity.
"Frontier" is the initial version of Ethereum, which only has a command-line interface and is mainly used by developers;
The second version, "Homestead," added a graphical interface similar to Windows, allowing ordinary users to easily experience Ethereum's functions;
The third version, "Metropolis," will include a browser similar to Google Chrome, which is not only convenient to use but also has a powerful application store for installing plugins to achieve more functionalities. The third version is expected to be released by the end of 2017;
The fourth version, "Serenity," currently has no determined release date. It is expected to switch the consensus mechanism used in the first three versions from proof of work (PoW) to a hybrid consensus mechanism.
After discussing the four versions of Ethereum, I will also list some important terms related to Ethereum to help you understand the entire system.
Ether
The first term is Ether. It is the token in the Ethereum system, abbreviated as ETH. Ether is the main fuel within Ethereum, providing the primary liquidity for running various digital asset transactions on this system, and is also used to pay for smart contract fees. It is the built-in blockchain asset of Ethereum used to pay for the operation of smart contracts. This concept is relatively simple to understand; we previously learned about Hash Cash and proof of work. I wonder if everyone remembers. We learned that to prevent the network from being flooded with spam emails, the sending computer must perform some work calculations. This is a huge burden for computers sending large amounts of spam. Similarly, in the Ethereum network, establishing and running smart contracts also requires a small threshold; this threshold is relatively low for those who genuinely want to develop, but it poses a significant burden for initiators of numerous spam projects or attackers. However, the threshold for Ethereum is not work but fuel, which we call "Gas," and Gas is exchanged for Ether.
Imagine if running a smart contract did not require any fees; there would be many garbage contracts or applications on this blockchain, and the blockchain would be attacked, rendering the entire network unusable. Therefore, Ethereum's blockchain requires a certain amount of Gas to be paid each time a smart contract runs, which can be paid in Ether, ensuring the stability and security of this blockchain.
Regarding the denomination of Ether, we are familiar with Bitcoin's smallest denomination being a satoshi, which is one hundred millionth of a Bitcoin. Ethereum also has its smallest denomination, named 1 "wei." How small is it? One Ether token can be divided to 18 decimal places, which is 1 wei.
Additionally, in July 2016, a hard fork occurred in the Ethereum blockchain, splitting Ethereum into two blockchains. The upgraded Ethereum, led by founder Vitalik, is called Ethereum, with the token code ETH. The chain that does not accept this upgrade is called "Ethereum Classic," with the original chain's Ether token code being ETC.
Ethereum Virtual Machine (EVM)
The second concept is the Ethereum Virtual Machine (EVM). Typically, there are some compilation and execution virtual machines that support a programming system. Just as Java has the JVM, Ethereum has its virtual machine that can execute any complex algorithm code. Developers can use existing friendly programming