Reader small image

You're reading from  Mastering Blockchain - Fourth Edition

Product typeBook
Published inMar 2023
PublisherPackt
ISBN-139781803241067
Edition4th Edition
Concepts
Right arrow
Author (1)
Imran Bashir
Imran Bashir
author image
Imran Bashir

Imran Bashir has an M.Sc. in Information Security from Royal Holloway, University of London, and has a background in software development, solution architecture, infrastructure management, and IT service management. He is also a member of the Institute of Electrical and Electronics Engineers (IEEE) and the British Computer Society (BCS). Imran has extensive experience in both the public and financial sectors, having worked on large-scale IT projects in the public sector before moving to the financial services industry. Since then, he has worked in various technical roles for different financial companies in Europe's financial capital, London.
Read more about Imran Bashir

Right arrow

Experimenting further with bitcoin-cli

As we've seen so far, bitcoin-cli is a powerful and feature-rich command-line interface available with the Bitcoin Core client and can be used to perform various functions using the RPC interface provided by the Bitcoin Core client.

We will now see how to send Bitcoin to an address using the command line. For this, we will use the Bitcoin command-line interface on the Bitcoin regtest:

  1. Generate a new address using the following command:
$ bitcoin-cli -regtest getnewaddress
2NC31WFFRwRkwd3S4TpyjN5GGDY7E63GSVd
  1. Send 20 BTC to the newly generated address:
$ bitcoin-cli -regtest sendtoaddress \ 2NC31WFFRwRkwd3S4TpyjN5GGDY7E63GSVd 20.00
  1. The output of this command will show the transaction ID, which is:
a83ff460a32f29387d531f19e7092a5dcf6ce52d20931227447c0b9b7a5f2980
  1. We can now generate a few more blocks to get some confirmation for this:
$ bitcoin-cli -regtest generatetoaddress 7 $(bitcoin-cli -regtest getnewaddress)
  1. We can also query...

Bitcoin programming

Bitcoin programming is a very rich field. The Bitcoin Core client exposes various JSON-RPC commands that can be used to construct raw transactions and perform other functions via custom scripts or programs. Also, the command-line tool bitcoin-cli is available, which makes use of the JSON-RPC interface and provides a rich toolset to work with Bitcoin.

These APIs are also available via many online service providers in the form of Bitcoin APIs, and they provide a simple HTTP REST interface. Bitcoin APIs, such as blockchain.info (https://blockchain.info/api), BitPay (https://bitpay.com/api), block.io (https://www.block.io), and many others, offer a myriad of options to develop Bitcoin-based solutions.

Various libraries are available for Bitcoin programming. A list is shown as follows. Those of you who are interested can explore the libraries further:

Summary

This chapter started with an introduction to Bitcoin installation, followed by some discussion on source code setup and how to set up Bitcoin clients for various networks. After this, we examined various command-line options available in Bitcoin clients. Lastly, we saw which APIs are available for Bitcoin programming and the main points to keep in mind while evaluating APIs for usage. In the next chapter, we will introduce alternative electronic cash blockchain projects, that is, the digital currency projects other than Bitcoin, such as Litecoin and Namecoin. We will also discuss various ideas that led to the development of alternative coin projects.

Keys and addresses

Keys and addresses are used in the Ethereum blockchain to represent ownership and transfer ether. The keys used are made up of pairs of private and public parts. The private key is generated randomly and is kept secret, whereas the public key is derived from the private key. Addresses are derived from public keys and are 20-byte codes used to identify accounts.

The process of key generation and address derivation is as follows:

  1. First, a private key is randomly chosen (a 256-bit positive integer) under the rules defined by the elliptic curve secp256k1 specification (in the range [1, secp256k1n − 1]).
  2. The public key is then derived from this private key using the Elliptic Curve Digital Signature Algorithm (ECDSA) recovery function. We will discuss this in the Transactions and messages section, in the context of digital signatures.
  3. An address is derived from the public key, specifically, from the rightmost 160 bits of the Keccak hash...

Accounts

An account is one of the main building blocks of the Ethereum blockchain. It is defined by pairs of private and public keys. Accounts are used by users to interact with the blockchain via transactions. A transaction is digitally signed by an account before submitting it to the network via a node. With Ethereum being a transaction-driven state machine, the state is created or updated as a result of the interaction between accounts and transaction executions. All accounts have a state that, when combined together, represents the state of the Ethereum network. With every new block, the state of the Ethereum network is updated. Operations performed between and on the accounts represent state transitions. The state transition is achieved using what’s called the Ethereum state transition function, which works as follows:

  1. Confirm the transaction validity by checking the syntax, signature validity, and nonce.
  2. The transaction fee is calculated, and the sending...

Transactions and messages

A transaction in Ethereum is a digitally signed (using a private key) data packet that contains the instructions that, when completed, either result in a message call or contract creation. Transactions are constructed by an actor external to the Ethereum blockchain or some external software tool. Smart contracts cannot send a transaction.

Initially, in Ethereum, there were simple transactions, message call transactions, and contract creation transactions. Later, as the system evolved, a need was felt to introduce more types and also to make it easier to introduce new features and future transaction types and be able to distinguish different types of transactions. New features such as access lists and EIP-1559 have been introduced while remaining compatible with legacy transactions. Further innovation resulted in the introduction of typed transactions, which were introduced in EIP-2718. They define a new transaction type, which is an envelope for future...

Ethereum virtual machine

The EVM is a simple stack-based execution machine that runs bytecode instructions to transform the system state from one state to another. The word size of the EVM is set to 256 bits. The stack size is limited to 1,024 elements and is based on the Last In, First Out (LIFO) queue. The EVM is a Turing-complete machine but is limited by the amount of gas that is required to run any instruction. This means that infinite loops that can result in denial-of-service attacks are not possible due to gas requirements.

The EVM also supports exception handling should exceptions occur, such as not having enough gas or providing invalid instructions, in which case the machine would immediately halt and return the error to the executing agent.

The EVM is an entirely isolated and sandboxed runtime environment. The code that runs on the EVM does not have access to any external resources such as a network or filesystem. This results in increased security, deterministic...

Blocks and blockchain

Blocks are the main building structure of a blockchain. Ethereum blocks consist of various elements, which are described as follows:

  • The list of headers of ommers or uncles
  • The block header
  • The transactions list

An uncle block is a block that is the child of a parent but does not have a child block. Ommers or uncles are valid but stale blocks that are not part of the main chain but contribute to the security of the chain. They also earn a reward for their participation but do not become part of the canonical truth.

Block header: Block headers are the most critical and detailed components of an Ethereum block. The header contains various elements, which are described in detail here:

  • Parent hash: This is the Keccak 256-bit hash of the parent (previous) block’s header.
  • Ommers hash: This is the Keccak 256-bit hash of the list of ommers (or uncle) blocks included in the block.
  • The beneficiary: The beneficiary...

Nodes and miners

The Ethereum network contains different nodes. Some nodes act only as wallets, some are light clients, and a few are full clients running the full blockchain. One of the most important types of nodes is mining nodes. We will see what constitutes mining in this section.

Transactions can be found in either transaction pools or blocks. In transaction pools, they wait for verification by a node, and in blocks, they are added after successful verification. When a mining node starts its operation of verifying blocks, it starts with the highest-paying transactions in the transaction pool and executes them one by one. When the gas limit is reached, or no more transactions are left to be processed in the transaction pool, the mining starts. As a result of the mining operation, currency (ether) is awarded to the nodes that perform mining operations as an incentive for them to validate and verify blocks made up of transactions. The mining process helps secure the network...

The Ethereum network

The Ethereum network is a peer-to-peer network where nodes participate in order to maintain the blockchain and contribute to the consensus mechanism. Networks can be divided into three types, based on the requirements and usage: the main net, test nets, and private nets.

Main net

The main net is the current live network of Ethereum. Its network ID is 1 and its chainID is also 1. The network and chainIDs are used to identify the network. A block explorer that shows detailed information about blocks and other relevant metrics is available at https://etherscan.io. This can be used to explore the Ethereum blockchain.

Test nets

There are two main networks available for Ethereum testing. The aim of these test blockchains is to provide a testing environment for smart contracts and DApps before being deployed to the production live blockchain. Moreover, being test networks, they also allow experimentation and research. The main test net is called Sepolia...

Precompiled smart contracts

We discussed smart contracts at length in Chapter 8, Smart Contracts. It is sufficient to say here that Ethereum supports the development of smart contracts that run on the EVM. There are also various contracts that are available in precompiled format in the Ethereum blockchain to support different functions. These contracts, known as precompiled contracts or native contracts, are described in the following subsection.

These are not strictly smart contracts in the sense of user-programmed Solidity smart contracts but are in fact functions that are available natively to support various computationally intensive tasks. They run on the local node and are coded within the Ethereum client; for example, Parity or Geth.

There are nine precompiled contracts or native contracts in the Ethereum Istanbul release. The following subsections outline these contracts and their details.

  • The elliptic curve public key recovery function: ECDSARECOVER (the...

Wallets and client software

As Ethereum is undergoing heavy development and evolution, there are many components, clients, and tools that have been developed and introduced over the last few years.

Wallets

A wallet is a generic program that can store private keys and, based on the addresses stored within it, it can compute the existing balance of ether associated with the addresses by querying the blockchain. It can also be used to deploy smart contracts. In Chapter 10, Ethereum in Practice, we will introduce the MetaMask wallet, which has become the tool of choice for developers.

Having discussed the role of wallets within Ethereum, let’s now discuss some common clients.

Geth

This is the official Go implementation of the Ethereum client.

The latest version is available at the following link: https://geth.ethereum.org/downloads/.

There are other implementations, including C++ implementation Eth, and many others. A list is available here: https:...

Supporting protocols

Various supporting protocols are available to assist with the complete decentralized ecosystem. In addition to the contracts layer, which is the core blockchain layer, there are additional layers that need to be decentralized to achieve a completely decentralized ecosystem. This includes decentralized messaging and decentralized storage. These are addressed by the Whisper and Swarm protocols, respectively.

Whisper

Whisper provides decentralized peer-to-peer messaging capabilities to the Ethereum network. In essence, Whisper is a communication protocol that DApps use to communicate with each other. The data and routing of messages are encrypted within Whisper communications. Whisper makes use of the DEVP2P wire protocol for exchanging messages between nodes on the network. Moreover, it is designed to be used for smaller data transfers and in scenarios where real-time communication is not required.

Whisper is also designed to provide a communication...

Summary

This chapter mainly covered the Ethereum architecture and ecosystem. We introduced the core concepts of the Ethereum blockchain, such as the state machine model, the world and machine states, accounts, and transactions. Moreover, a detailed introduction to the core components of the EVM was also presented. In addition, the Ethereum blockchain and network, wallets, software clients, and supporting protocols were also discussed.

In the next chapter, we will continue to explore Ethereum development. We will look at more concepts, such as programming languages and developing programs for Ethereum blockchain.

Join us on Discord!

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/ips2H

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Blockchain - Fourth Edition
Published in: Mar 2023Publisher: PacktISBN-13: 9781803241067
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Imran Bashir

Imran Bashir has an M.Sc. in Information Security from Royal Holloway, University of London, and has a background in software development, solution architecture, infrastructure management, and IT service management. He is also a member of the Institute of Electrical and Electronics Engineers (IEEE) and the British Computer Society (BCS). Imran has extensive experience in both the public and financial sectors, having worked on large-scale IT projects in the public sector before moving to the financial services industry. Since then, he has worked in various technical roles for different financial companies in Europe's financial capital, London.
Read more about Imran Bashir