Vite in a nutshell


  • SBP

    Introduction

    As mentioned in our white paper, Vite uses DAG (directed acyclic graph) technology for its core ledger structure, which stores transaction information for every account.
    In order to improve the data security of the whole DAG ledger and make it tamper-resistant, Vite has created the concept of Snapshot Chain.
    Since the white paper is abstract, this article hopes to describe the core DAG ledger structure and snapshot chain with the help of 500 lines of code.

    Core Concept

    1. The DAG ledger structure of Vite
    2. The snapshot chain of Vite

    The DAG Ledger Structure of Vite

    af8c9752-3440-4bfe-87b8-d250e0e30925-image.png

    Vite's design made a trade-off between two aspects, reduction of false fork ratio and tamper resistance.

    The above figure shows the performances of false fork and tamper resistance in several blockchain ledger structures, also described in the white paper.

    Vite decided on the block-lattice of DAG. The following paragraphs describe the block-lattice of DAG structure in more detail.

    3ebf232d-adad-4640-8748-e33d8c1c5304-image.png

    In the figure above, S stands for the state of the world, and there are 3 initial accounts A, B, C. The status of these accounts are A0, B0, C0. Solid arrows point from the current status of a given account to the previous status of said account, and dotted arrows indicate any indirect impact (e.g. caused by transfers) between the statuses of different accounts. Assume that in the initial status, the balance of accounts is $100 each,

    1. Initial state of the world: S1={A0=100, B0=100, C0=100}
    2. After account B transferred $20 to account A, state of the world : S2={A1=120, B1=80, C0=100}
    3. After account A transferred $30 to account C, state of the world : S3={A2=90, B1=80, C1=130}

    In Vite, every account status is a block (such as A0, A1, etc in figure); reference relationship among blocks (arrows in figure) is the transfer process among accounts.

    The entire history of transactions among accounts is represented by a directed acyclic graph, or DAG.

    The Snapshot Chain Structure of Vite

    The DAG chain, which is composed of account blocks, can explicitly describe the change of world status. However, the frequency of transactions for a given account affects the length of the relevant chain, and an account chain with infrequent transactions is easy to be tampered with.

    To solve this problem, Vite introduces snapshot chain that grows continuously, thereby increasing the tamper-resistant level of the block-lattice structure.

    22f9a564-3baa-41f4-b654-0e3a090f3f5e-image.png

    The above describes the world status of 3 accounts: S1, S2, S3. The arrow direction shows the previous status of the current status. The above structure is basically the snapshot chain, where every block represents the state of the world (A set of all account statuses), and each block references the previous one. Of course, in order to reduce storage, each block only contains the change between successive states of the world, as shown below. Also, the blocks are generated on a fixed schedule; so not every transaction generates a snapshot block.

    f0adc170-2eed-4b5b-8f4d-eeee4512a298-image.png

    Implementation of Code

    The context above gave a brief introduction of DAG ledger and snapshot chain structure of Vite. The following sections implement these two data structures as well as the process of transfer and snapshot generation via code.

    Data Structure

    Transfers among ledgers are made through transactions, the code below defines the data structure of a transaction:

    7e43b3a0-7572-45b1-b13d-ad1cf3d70936-image.png

    There are two types in TxType, representing send and receive transactions separately. These are corresponding to the fact that in Vite, a transfer generates two transactions.

    Below is the data structure of account status, we can see that the account status references the transaction data Tx. This means that every change in account status is caused by a transaction.

    Send accounts reference TxType as send transaction and receive accounts reference TxType as receive transaction.

    e022c477-29ab-44fe-9e48-886e48793c67-image.png

    Account status will be snapshotted by snapshot chain periodically, the definition of snapshot block is as below, Height param means the height of snapshot block, and it increments by 1 each time. AccountsHash represents all the hashes of cached account status.

    7c4b59e4-dde5-4eb9-b541-4f79efe3a2d5-image.png

    The context above describes the fundamental data structure that constitutes Vite. The following sections of transfer transactions and snapshot block generation are based on these data structures.

    f3adf2a9-35b7-4c7d-adf7-792e313579c0-image.png

    These two data structures represent the Vite account chain and snapshot chain respectively.

    AccountStateBlockChain is a map data structure, key is the address of each account, value is a linked list of AccountStateBlock, pointing to the previous block via PreHash.

    Transaction Process

    Send Transaction

    5e834145-1032-40b1-aad0-44e36410185f-image.png

    The core code of the send transaction is as shown above:

    1. Construct a transaction and sign the transaction
    2. Pack the account status block and sign the packaged account status block
    3. Insert the account status block into the account chain and broadcast the block results

    Receive Transaction

    e2b265ef-431b-4bfb-afb3-e5b79b29aa7b-image.png
    There will be a coroutine waiting for the broadcast event to be generated and verify the validity of the block. Meanwhile, if it needs to generate the corresponding received transaction, it will forward Tx to the corresponding node.

    a531a9d3-eaa8-4d91-a06b-6de17f33823a-image.png

    After the corresponding node receives the send transaction and passes the verification:

    1. Generate a received transaction and sign it
    2. Pack the account status block and sign the packaged account status block
    3. Insert the account status block into the account chain and broadcast the block structure

    The Snapshotting Process of Snapshot Chain

    63dca424-3401-4c4d-80bc-a2c0295d44ac-image.png

    The mining account will periodically generate the snapshot block and insert it into the snapshot chain.

    Summary

    The key point of this article is to briefly introduce the main process of naive-vite, and at the same time let everyone know the fundamental principles of Vite through this simple demo.

    For more details, visit official Vite website and vite blog, and watch the official implementation of the first version on Github.

    References



  • good output 👍 👍 👍


Log in to reply