Blockchain Data Analytics For Dummies. Michael G. Solomon
Чтение книги онлайн.
Читать онлайн книгу Blockchain Data Analytics For Dummies - Michael G. Solomon страница 19
Recording transaction data
Regardless of the approach used to create new blocks, blocks generally contain transactions or smart contract code. Because blockchain technology was introduced to manage cryptocurrency, it stands to reason that transaction data focuses on transferring ownership from one address to another. In this section, you look at a block to see its header information and a list of transactions.
Etherscan is a popular website that allows you to examine the live Ethereum network, mainnet. Figure 3-1 shows a portion of Etherscan’s block header view. The block we will examine is block number 8976776. Note that this block contains 95 transactions.
FIGURE 3-1: Viewing block header information in Etherscan.
To find block 8976776 in Etherscan, go to https://etherscan.io/
and enter the block number in the All Filters field. Then click or tap the search icon (magnifying glass).
Etherscan does much more than provide a way to peek at data on Ethereum’s mainnet. You can examine and retrieve data from mainnet; popular testnets including Ropsten, Kovan, Rinkeby, and Goerli; and the Energy Web Foundation (EWF) chain. If you create an account and request a free API key, you can use the key to extract blockchain data.
To see a list of transactions in block 8976776, click or tap the 95 Transactions link. Figure 3-2 shows the first 5 transactions in block 8976776. You can see that each transaction has a From account, a To account, and an amount. In simplest terms, each transaction records an amount in the Value column being transferred from one Ethereum account to another.
FIGURE 3-2: Listing transactions in a block in Etherscan.
Click or tap the fourth transaction in Figure 3-2 to open the Etherscan transaction details page shown in Figure 3-3. This initial page shows general information about the Ethereum transaction. The To field shows that the target address is Contract, which means that this transaction is the result of a call to a smart contract.
FIGURE 3-3: Examining a transaction in Etherscan.
In Ethereum, the only way you can access data stored in the blockchain is through a smart contract. You use the smart contract’s address (where the smart contract code is stored in the blockchain) to run, or invoke, one of its functions. Smart contract functions contain the instructions for accessing blockchain data.Click or tap Click to See More, in the bottom left, to display the expanded transaction details page with additional information about the smart contract function call shown in Figure 3-4. You can see that this transaction is the result of invoking this smart contract’s cancelOrder()
function. You'll learn more about smart contracts and transaction details in later chapters, but for now, be aware that blockchain technology keeps a record of every change to blockchain data, which provides a great place to get analytics data.
FIGURE 3-4: Exploring additional transaction details in Etherscan.
Dissecting the parts of a block
Before you can start extracting data from a blockchain for analysis, you need to learn a little more about how the data you want is stored. Most of that data is stored in blocks, so that's what I discuss next.
Yes, that’s right! Most but not all data you’ll want for analytics is stored in blocks. Some blockchains, including Ethereum, store some data in an external, or off-chain, database. Don’t worry; I describe off-chain data too.
I describe only the basic Ethereum block and chain details. The authoritative reference for Ethereum internals is the Ethereum yellow paper, at
https://ethereum.github.io/yellowpaper/paper.pdf
. You can also find a good third-party detailed discussion of Ethereum block structure internals at https://ethereum.stackexchange.com/questions/268/ethereum-block-architecture
.
A block is a data structure that contains two main sections: a header and a body. Transactions are added to the body and then submitted to the blockchain network. Miners take the blocks and try to solve a mathematical puzzle to win a prize. Miners are just nodes, or pools of nodes, with enough computational power to calculate block hashes many times to solve the puzzle.
In Ethereum, the mining process uses the submitted block header and an arbitrary number called a nonce (number used once). The miner chooses a value for the nonce, which is part of the block header, and calculates a hash value using a hash function on the block header. The result has to match an agreed-upon pattern, which gets more difficult over time as miners get faster at mining blocks. If the first mining result doesn’t match the pattern, the miner chooses another nonce and calculates a hash on the new block header. This process continues until a miner finds a nonce that results in a hash that matches the pattern.
The miner that finds the solution broadcasts that solution to the rest of the network. That miner collects a reward, in ETH (ether), for doing the hard work to validate the block. Because many miners work on blocks at the same time, it's common for several miners to solve the hash puzzle at almost the same time. In other blockchains, these blocks are discarded as orphans. In Ethereum, these blocks are called uncles. An uncle block is any successfully mined block that arrives after that block has already been accepted. Ethereum accepts uncle blocks and even provides a reward to the miner, but one that's smaller than the accepted block.
Ethereum rewards miners that solve uncle blocks to reduce mining centralization and to increase the security of the blockchain. Uncle rewards provide an incentive for smaller miners to participate. Otherwise, mining would be profitable only for large pools that could eventually