Blockchain Data Analytics For Dummies. Michael G. Solomon

Blockchain Data Analytics For Dummies - Michael G. Solomon


Скачать книгу
all transactions that occurred between two specific accounts.

       Find all transactions that invoked a specific smart contract function.

      After you fetch the data you want, you can trace the transactions, calculating the value change (that is, keeping track of the Value and Transaction Fee fields) to find the information you’re looking for, such as a balance at a specific point.

      Logging events on the blockchain

      One of the more interesting aspects of blockchain data extends the information you can get from transactions. As mentioned, a transaction is the transfer of some value as a result of a smart contract function. Because the only way to create a transaction is to invoke a smart contract function, you can be sure that a transaction is the result of a function.

      The previous statement may sound redundant, but it's extremely important. Smart contract functions can be simple or complex. As smart contracts become more complex, just knowing the function a transaction invoked, along with its input parameter values, isn’t always enough information to describe what’s going on. You need a way to record what happens inside transactions.

Ethereum, and most popular blockchains second generation and beyond, support sophisticated smart contract languages. Ethereum’s EVM (Ethereum virtual machine) is a Turing complete machine, so with enough resources, an Ethereum smart contract can calculate anything. Of course, in the real world, transactions eventually run out of gas, but the point is that your smart contract functions can be as complex as you want.

Screenshot displaying the last two events for a current transaction in block 8976776 that occurred during a smart contract function.

      FIGURE 3-9: Ethereum events in Etherscan.

      Note that these events have names — LogTransfer() and LogOrderCancelled() — and parameters. Smart contract programmers use events to create messages that Ethereum logs and saves. Events make it easy to notify client applications that certain actions have taken place in a smart contract and also to store important information related to a transaction.

      

Smart contract programmers use events to record internal details of how smart contracts operate. The programmer defines events and the parameters passed when the events are called. Then, during runtime, the smart contract invokes the event when something notable happens in the code. For example, when using the popular language Solidity for writing smart contract code, the emit command invokes an event. Any time a programmer wants to send a message to the client or record an action, the emit statement invokes an event to do just that.

      Storing value with smart contracts

      The last main category of data associated with a blockchain is the state data. State data is the data that is most like traditional database data. Each smart contract can define one or more variables or structures to store data values. These values can include things such as highest order number (for an order entry contract) or a list of products (for a supply chain contract). State data make it possible to store data that contracts use each time one of their functions is invoked.

      Although transaction data is stored in blocks on the blockchain, Ethereum stores state data not in blockchain blocks but in an external (off-chain) database. Each block stores a hash value that points to the root of that block's state trie in the off-chain database, which stores the block’s state data. Storing data using a trie structure makes it possible to query the trie for a value, and validate the integrity of that value, without having to read the entire trie.

      

Refer to Figure 3-5, which shows the contents of a block header, including the state root hash that points to the root of the state values for that block (which is stored in the off-chain database).

      Unlike blockchain data, state data can change. Each time a function in a smart contract runs, it may change the value of one or more state data items. The transaction that caused the change is stored in a block on the blockchain, and log entries may be created by events, but state data changes are stored in the off-chain database.

      

Each Ethereum client can select its own database for storing state data. For example, the Geth client uses LevelDB, and the Parity client uses RocksDB. Each database uses different methods for access, so the blockchain client you use for analysis should support a database that is familiar.

      Now that you know about the basic categories of data a blockchain stores, you can start to dig into what each type of data you’ll find might mean. Few hard-and-fast rules for storing data exist. Each smart contract sets its own rules for defining and maintaining the data it needs to do its work.

      Exploring basic transaction data

      Every transaction contains basic information about crypto-asset ownership and transaction cost. The From, To, and Value fields record, respectively, the account that owns the value at the start of the transaction, the account to which the value is transferred, and the amount or cost of the asset that is transferred by the transaction.

      The Input Data field may contain additional information about the transaction. This data field is often very different from one transaction to another. When the To field of a transaction refers to a regular Ethereum account, the Input Data field may contain supporting or additional transaction details. In these cases, the transaction serves primarily to record a transfer of cryptocurrency from one account to another. If the To field contains the address of a smart contract, the Input Data field will contain information about the function that the transaction invokes and the data sent to the function.

      Part of the challenge in extracting blockchain data for analysis is classifying and making sense of the input data. The blockchain analytics process is more than just reading data and building models.

      Associating real-world meaning to events

      Although transaction data can reveal what a client requested and what value was transferred, it doesn’t always provide many details of how the transaction played out. In other words, transaction data doesn’t provide more than summary information. If you want to explore details of what happened during a transaction, you’ll have to look elsewhere.

      Because smart contract code can include complex calculations and data, it's often beneficial, and sometimes


Скачать книгу