Storage structure analysis of Ethereum blocks and transactions

Source: Internet
Author: User
Tags hash value store

LEVELDB is a key-value database, and all data is stored as key-value pairs. Key is generally related to hash, and value is generally the RLP encoding of the data structure to be stored. Chunk storage is stored separately from chunk size and block body.

the storage format for the chunk header is:

Headerprefix + num (UInt64 big endian) + hash Rlpencode (header)

Where key is composed of district header prefix, chunk number (UInt64 big-endian format), chunk hash, and value is the RLP code of the chunk head.

the storage format for block bodies is:

Bodyprefix + num (UInt64 big endian) + Hash-rlpencode (block body)

The key is composed of block prefix, chunk number (UInt64 big-endian format), chunk hash, and value is the RLP code of block body.

The prefixes in key can be used to differentiate between types of data, and various prefixes are defined in Core/database_util.go:

Headerprefix = []byte ("H")//headerprefix + num (UInt64 big endian) + hash header

Tdsuffix = []byte ("T")//headerprefix + num (UInt64 big endian) + hash + tdsuffix TD

Numsuffix = []byte ("n")//headerprefix + num (UInt64 big endian) + Numsuffix-Hash

Blockhashprefix = []byte ("H")//blockhashprefix + hash num (UInt64 big endian)

Bodyprefix = []byte ("B")//bodyprefix + num (UInt64 big endian) + hash block body

Where Headerprefix defines the prefix of the chunk header key to H,bodyprefix defines the chunk body key prefixed with B.

Here is the function that stores the chunk header:

Writeheader serializes a block header into Thedatabase.

Funcwriteheader (db ethdb. Database, Header*types. Header) Error {

Data,err: = RLP. Encodetobytes (header)

If err! = Nil {

return err

}

Hash: =header. Hash (). Bytes ()

Num: =header. Number.uint64 ()

encnum:= encodeblocknumber (num)

Key: = Append (Blockhashprefix, hash ...)

If err: = db. Put (key, Encnum); Err! = Nil {

Glog. Fatalf ("Failed to store hash to numbermapping into database:%v", err)

}

Key = Append (Append (Headerprefix, encnum ...), hash ...)

If err: = db. Put (key, data); Err! = Nil {

Glog. Fatalf ("Failed to store header intodatabase:%v", err)

}

Glog. V (logger. Debug). Infof ("Stored header#%v [%x ...]", header. Number, Hash[:4])

Returnnil

}

It first RLP the chunk header, encodeblocknumber the chunk number into the big-endian format, and then assembles the key. This first stores the record of a chunk hash-> block number in the database, and finally writes the RLP code of the chunk header to the database.

Here is the function that stores the chunk body:

Writebody serializes the body of a block intothe database.

Funcwritebody (db ethdb. Database, Hash common. Hash, number UInt64, body *types. Body) Error {

Data,err: = RLP. Encodetobytes (body)

If err! = Nil {

return err

}

Return WRITEBODYRLP (db, hash, number, data)

}

WRITEBODYRLP writes a serialized body of Ablock into the database.

FUNCWRITEBODYRLP (db ethdb. Database, Hash common. Hash,number UInt64, RLP RLP. RawValue) Error {

Key: = Append (Append (Bodyprefix, encodeblocknumber (number) ...), hash. Bytes () ...)

If err: = db. Put (key, RLP); Err! = Nil {

Glog. Fatalf ("Failed to store block body intodatabase:%v", err)

}

Glog. V (logger. Debug). Infof ("Stored blockbody [%x ...]", hash. Bytes () [: 4])

Returnnil

}

Writebody RLP code The chunk body first, and then calls WRITEBODYRLP to write the RLP encoding of the chunk body to the database. WRITEBODYRLP assembles the key according to the rules above, and then writes a record to the database.

There is also a writeblock function that calls Writebody and writeheader to write chunks to the database, respectively. The GetHeader getbody getblock function is also used to read chunks from the database.

Transaction storage

In addition to chunks, all transactions are stored in the database, and each transaction is stored in the following format:

Txhash-Rlpencode (TX)

Txhash + Txmetasuffix-Rlpencode (Txmeta)

Each transaction corresponds to the storage of two data, one is the transaction itself, and one is the meta-information (meta) of the transaction. The transaction is the hash of the transaction, the RLP code of the transaction is the value store, and the meta-information RLP encoded as the value store Txhash+txmetasuffix the key and meta information. The meta-information contains the chunk hash, block number, and the index of the trade in chunks of the exchange. Specifically, you can see the Writetransactions function:

Writetransactions stores the transactionsassociated with a specific block

into the given database. Beside writing thetransaction, the function Also

Stores a metadata entry along with Thetransaction, detailing the position

Of this within the blockchain.

Funcwritetransactions (db ethdb. Database,block *types. Block) Error {

batch:= db. Newbatch ()

Iterate over each transaction and encode it metadata

For I, TX: = range block. Transactions () {

Encode and queue up the transaction for storage

Data, err: = RLP. Encodetobytes (TX)

If err! = Nil {

return err

}

If Err: = Batch. Put (TX. Hash (). Bytes (), data); err!= Nil {

return err

}

Encode and queue up the transaction metadata for storage

Meta: = struct {

Blockhash Common. Hash

Blockindex UInt64

Index UInt64

}{

Blockhash:block. Hash (),

Blockindex:block. NumberU64 (),

Index:uint64 (i),

}

Data, err = RLP. Encodetobytes (META)

If err! = Nil {

return err

}

If Err: = Batch. Put (Append (TX. Hash (). Bytes (), Txmetasuffix ...), data); Err! = Nil {

return err

}

}

Write the scheduled data into the database

If Err: = Batch. Write (); Err! = Nil {

Glog. Fatalf ("Failed to store transactions into database:%v", err)

}

Returnnil

}

There is also the Gettransaction function, which reads the transaction from the database according to the hash of the transaction, returns the corresponding transaction, the chunk hash of the exchange in the block, the block number of the exchange in the block, and the index of the trade in the chunk.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.