Blockchain tutorial fabric1.0 source code analysis blockfile block file storage 1

Source: Internet
Author: User
Fabric 1.0 source code note blockfile (Block file storage) 1. blockfile Overview

Blockfile: Fabric block file storage. The default directory is/var/hyperledger/production/ledgersdata/chains, which contains two subdirectories: Index and chains.
Here, index is the index Directory, which is implemented by leveldb. Chains are the blockchain files of various ledger, and the subdirectories are implemented by the file system in the name of ledgerid.
Block Files are prefixed with blockfile _. the maximum size is 64 MB by default.

Blockfile, which is in the common/ledger/blkstorage/fsblkstorage directory. The directory structure is as follows:

  • Blockfile_mgr.go, blockfilemgr, and checkpointinfo struct and method.
  • Block_stream.go, blockfilestream, blockstream, blockplacementinfo struct and method.
  • Blockfile_r1_go, blockfilewriter, and blockfilereader struct and method (blockfilereader is not used ).
  • Blockindex. Go, index interface definition, index interface implementation is blockindex structure and method definition, and blockidxinfo, locpointer, filelocpointer struct and method.
  • Blockfile_helper.go defines four tool functions: constructcheckpointinfofromblockfiles, retrievelastfilesuffix, isblockfilename, and getfileinfoorpanic.
    Scan the latest blockfile, reconstruct the checkpoint information, obtain the latest file suffix, determine whether the block file is a block File Based on the file prefix, and obtain the File status information.
  • Block_serialization.go: block serialization tool functions.
  • Blocks_itr.go: the structure and method of blocksitr.
2. Block struct determination, block serialization 2.1, and block-related struct

Block struct:

Type Block struct {???? Header * blockheader // blockheader ???? Data * blockdata // blockdata ???? Metadata * blockmetadata} func (M * block) getheader () * blockheader // obtain blockheader, that is, M. headerfunc (M * block) getdata () * blockdata // obtain blockdata, that is, M. datafunc (M * block) getmetadata () * blockmetadata // M. metadata // code in PROTOS/common/Common. pb. go

Blockheader struct:

Type blockheader struct {???? Number uint64 // block number ???? Previushash [] Byte // hash of the previous block ???? Datahash [] Byte // current block hash} func (M * blockheader) getnumber () uint64 // obtain the block number, that is, M. numberfunc (M * blockheader) getpreviushash () [] Byte // obtain the hash of the previous block, that is, M. previushashfunc (M * blockheader) getdatahash () [] Byte // obtain the hash of the current block, that is, M. datahash // code in PROTOS/common/Common. pb. go

Blockdata struct:

Type blockdata struct {???? Data [] [] Byte/data, store transaction information} func (M * blockdata) getdata () [] [] Byte // get data, that is, M. data // code in PROTOS/common/Common. pb. go

Blockmetadata struct:

Type blockmetadata struct {???? Metadata [] [] Byte // K/V are in [] byte format} func (M * blockmetadata) getmetadata () [] [] Byte // M. metadata // code in PROTOS/common/Common. pb. go

Supplement blockmetadataindex:

type BlockMetadataIndex int32const (????BlockMetadataIndex_SIGNATURES          BlockMetadataIndex = 0????BlockMetadataIndex_LAST_CONFIG         BlockMetadataIndex = 1????BlockMetadataIndex_TRANSACTIONS_FILTER BlockMetadataIndex = 2????BlockMetadataIndex_ORDERER             BlockMetadataIndex = 3)
2.2 block serialization

Serializedblockinfo struct definition and tool functions:

Type serializedblockinfo struct {???? Blockheader * Common. blockheader // blockheader ???? Txoffsets [] * txindexinfo // transaction index information ???? Metadata * Common. blockmetadata} type txindexinfo struct {???? Txid string // transaction ID ???? Loc * locpointer // file pointer} // serialized block, returns the serialized bytes, and serializedblockinfo (including blockheader and transaction index information) func serializeblock (Block * Common. block) ([] Byte, * serializedblockinfo, error) // deserialize the block to construct the block structure func deserializeblock (serializedblockbytes [] Byte) (* Common. block, error) // deserialize the block and construct the serializedblockinfofunc extractserializedblockinfo (serializedblockbytes [] Byte) (* serializedblockinfo, error) // Add the blockheader to serialization, that is, number, datahash, and previushashfunc addheaderbytes (blockheader * Common. blockheader, Buf * Proto. buffer) Error // Add blockdata to serialization, parse txid from blockdata, and return the array of transaction index information func adddatabytes (blockdata * Common. blockdata, Buf * Proto. buffer) ([] * txindexinfo, error) // Add metadatafunc addmetadatabytes (blockmetadata * Common. blockmetadata, Buf * Proto. buffer) Error // deserialize blockheaderfunc extractheader (BUF * ledgerutil. buffer) (* Common. blockheader, error) // deserializes blockdata and returns the array of transaction index information func extractdata (BUF * ledgerutil. buffer) (* Common. blockdata, [] * txindexinfo, error) // deserializes metadatafunc extractmetadata (BUF * ledgerutil. buffer) (* Common. blockmetadata, error) // parse the transaction idfunc extracttxid (txenvelopbytes [] Byte) (string, error) from blockdata // code in common/ledger/blkstorage/fsblkstorage/block_serialization.go
3. checkpointinfo struct definition and Method

Checkpointinfo is the checkpoint information. The struct is defined as follows:

Type checkpointinfo struct {???? Latestfilechunksuffixnum int // The latest block file suffix, such as blockfile_000000 ???? Latestfilechunksize int // The latest block file size ???? Ischainempty bool // do you want to empty the chain ???? Lastblocknumber uint64 // The latest block number} // code in common/ledger/blkstorage/fsblkstorage/blockfile_mgr.go

The procedure is as follows:

Func (I * checkpointinfo) Marshal () ([] Byte, error) // checkpointinfo serializes func (I * checkpointinfo) unmarshal (B [] Byte) error // checkpointinfo deserialization func (I * checkpointinfo) string () string // convert to string // code in common/ledger/blkstorage/fsblkstorage/blockfile_mgr.go
4. blockfilestream related struct and method 4.1, blockfilestream

Blockfilestream is defined as follows:

Type blockfilestream struct {???? Filenum int // blockfile file suffix ???? File * OS. File // OS. File ???? Reader * bufio. Reader // bufio. Reader ???? Currentoffset int64 // current offset} // code in common/ledger/blkstorage/fsblkstorage/block_stream.go

The procedure is as follows:

// Construct blockfilestreamfunc newblockfilestream (rootdir string, filenum int, startoffset int64) (* blockfilestream, error) func (S * blockfilestream) nextblockbytes () ([] Byte, error) // next block, fetch S. nextblockbytesandplacementinfo () // the next block and location information func (S * blockfilestream) nextblockbytesandplacementinfo () ([] Byte, * blockplacementinfo, error) func (S * blockfilestream) Close () error // disable blockfilestream // code in common/ledger/blkstorage/fsblkstorage/block_stream.go

Func (SBlockfilestream) nextblockbytesandplacementinfo () ([] Byte,Blockplacementinfo, error) the code is as follows:

VaR lenbytes [] bytevar err errorvar fileinfo OS. fileinfomorecontentavailable: = truefileinfo, err = S. file. stat () // get the file status remainingbytes: = fileinfo. size ()-S. currentoffset // The remaining byte peekbytes for file reading: = 8if remainingbytes <int64 (peekbytes) {// The remaining byte is less than 8, according to the actual remaining byte, otherwise press 8 ???? Peekbytes = int (remainingbytes )???? Morecontentavailable = false} // storage format: the length of the first N-bit storage block, and the subsequent length is the actual blocklenbytes, err = S. reader. peek (peekbytes) // peek returns a cached slice that references the Data Length of the first peekbytes bytes in the cache, N: = Proto. decodevarint (lenbytes) // read the varint-encoded integer from the slice. It returns the integer and the number of consumed bytes .???? Err = S. Reader. Discard (n) // discard the first n digits of the length of the storage block ???? Blockbytes: = make ([] Byte, length )???? _, Err = Io. readatleast (S. Reader, blockbytes, INT (length ))???? Blockplacementinfo: = & blockplacementinfo {???????? Filenum: S. filenum ,???????? Blockstartoffset: S. currentoffset ,???????? Blockbytesoffset: S. currentoffset + int64 (n )}???? S. currentoffset + = int64 (n) + int64 (length )???? Return blockbytes, blockplacementinfo, nil // code in common/ledger/blkstorage/fsblkstorage/block_stream.go

Supplement blockplacementinfo: block Location Information

Type blockplacementinfo struct {???? Filenum int // block file suffix ???? Blockstartoffset int64 // n + length, before n ???? Blockbytesoffset int64 // n + length, before length} // code in common/ledger/blkstorage/fsblkstorage/block_stream.go
5. blockfilewriter struct definition and Method
Type blockfilewriter struct {???? Filepath string // path ???? File * OS. file // OS. file} func newblockfilewriter (filepath string) (* blockfilewriter, error) // construct blockfilewriter and call writer. open () func (w * blockfilewriter) truncatefile (targetsize INT) Error // truncatefile func (w * blockfilewriter) append (B [] Byte, sync bool) error // append the file func (w * blockfilewriter) open () Error // open the file func (w * blockfilewriter) Close () error // close the file // code in common/ledger/blkstorage/fsblkstorage/blockfile_r1_go
6. blockindex-related struct and method 6.1. index interface definition
Type index interface {???? Getlastblockindexed () (uint64, error) // obtain the last block index (or number )???? Indexblock (blockidxinfo * blockidxinfo) Error // index block ???? Getblocklocbyhash (blockhash [] Byte) (* filelocpointer, error) // obtain the file block pointer ???? Getblocklocbyblocknum (blocknum uint64) (* filelocpointer, error) // obtain the file block pointer ???? Gettxloc (txid string) (* filelocpointer, error) // obtain the file transaction pointer ???? Gettxlocbyblocknumtrannum (blocknum uint64, trannum uint64) (* filelocpointer, error) // obtain the file transaction pointer ???? Getblocklocbytxid (txid string) (* filelocpointer, error) // obtain the file block pointer ???? Gettxvalidationcodebytxid (txid string) (peer. txvalidationcode, error) // obtain the transaction verification code based on the transaction ID} // code in common/ledger/blkstorage/fsblkstorage/blockindex. Go
6.2. blockindex struct

The blockindex struct is defined as follows:

Type blockindex struct {???? Indexitemsmap map [blkstorage. indexableattr] bool // index property ing ???? DB * leveldbhelper. dbhandle // index leveldb operation} // code in common/ledger/blkstorage/fsblkstorage/blockindex. Go

Supplement indexableattr:

Const (???? Indexableattrblocknum = indexableattr ("blocknum ")???? Indexableattrblockhash = indexableattr ("blockhash ")???? Indexableattrtxid = indexableattr ("txid ")???? Indexableattrblocknumtrannum = indexableattr ("blocknumtrannum ")???? Indexableattrblocktxid = indexableattr ("blocktxid ")???? Indexableattrtxvalidationcode = indexableattr ("txvalidationcode") // code in common/ledger/blkstorage/blockstorage. Go

Blockchain tutorial fabric1.0 source code analysis blockfile block file storage 1

Related Article

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.