Smart Contract Database Traversal Study Guide
Glossary
Term DefinitionsSmart ContractA computer program stored on a blockchain that automatically executes when predefined conditions are met.BlockchainA distributed database used to record transactions securely and transparently.Merkle Patricia Tree (MPT)A data structure used to efficiently store and retrieve key-value pairs in blockchain platforms such as Ethereum.A key-value pair (KVP) consists of two associated data elements: a unique identifier (key) and the information associated with that identifier (value).Logical MapA data structure used to map KVPs in an MPT for easy traversal and manipulation.IndexA list of numbers in a logical map, each number corresponding to a KVP in the MPT.Hash ValueA unique code generated by an algorithm that converts data of arbitrary length into a fixed-length string.
Short Answer Questions
What is a smart contract and how is it different from a traditional contract?
What role does a Merkle Patricia Tree (MPT) play in a blockchain?
Explain the purpose of KVPs in an MPT.
Why is a logical map needed to traverse an MPT?
What does the "size" attribute in a logical map represent?
How do indexes in the logical map help traversing the MPT?
Explain the role of "delete markers" when deleting a KVP in the logical map.
Describe the steps to retrieve a specific value from the MPT using the logical map.
Why are keys and values in the logical map usually stored as hash values?
How does the logical map improve the efficiency of the smart contract database?
Short answer questions
A smart contract is a self-executing contract stored on the blockchain, whose terms are written directly into the code. Unlike traditional contracts, smart contracts do not require a third-party intermediary to execute, and their execution process will proceed automatically once the preset conditions are met.
A Merkle Patricia Tree (MPT) is a data structure used to efficiently store and retrieve KVPs in a blockchain. It can be thought of as an optimized database that tracks the status of all accounts on a blockchain platform and their related information.
KVPs are used to store actual data in the MPT. Each KVP consists of a unique key and a value associated with it. For example, the key can be a user's address, and the value can be the user's balance.
Due to the structural characteristics of the MPT, it is impossible to directly traverse all its KVPs unless the key is known. Logical mapping solves this problem by creating a mapping relationship between KVPs, allowing a specific KVP in the MPT to be traversed and operated on in order.
The "size" attribute in the logical map indicates the number of KVPs stored in the logical map, including KVPs that have been deleted but still occupy index space.
The index in the logical map is a list of numbers, each number corresponding to a KVP in the MPT. By traversing the index, the mapped KVPs can be accessed in order without knowing their exact keys.
When a KVP is deleted from the logical map, a "deletion marker" is used to mark the index position corresponding to the KVP as deleted, but the actual data may not have been removed from the MPT. This allows the index position to be reused in the future, thereby optimizing storage space.
First, determine the corresponding number of the target value in the logical map index. Then, retrieve the corresponding key in the logical map based on the number. Finally, use the key to retrieve the target value from the MPT.
To ensure data integrity and security, keys and values in logical maps are usually stored as hash values. Hash values are a unique representation of data, and even minor changes to the original data will cause significant changes in the hash value.
Logical mapping improves the efficiency of the smart contract database by providing an efficient way to traverse, read, delete, and manipulate KVPs in MPT. This reduces the time and resources required to search and retrieve data, enabling smart contracts to execute faster and more efficiently.