chainsdb

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2021 License: GPL-3.0, GPL-3.0 Imports: 22 Imported by: 0

README

Atlas multilChain introduce

Multichain databases are used to store data from different chains,Distinguishing different chains with ChainType uint64。The data structure is:

type HeaderChainStore struct {
	chainDb          ethdb.Database
	currentChainType rawdb.ChainType
	Mu               sync.RWMutex // blockchaindb insertion lock
	rand             *mrand.Rand
}

Call through the following function when you want to use multilChainDb. ps:The only identification of the chain is chainType

func GetStoreMgr(chainType rawdb.ChainType) (*HeaderChainStore, error) {
	if storeMgr == nil {
		return nil, error01
	}
	storeMgr.currentChainType = chainType
	return storeMgr, nil
}
for Example:
    hc, error := GetStoreMgr(chainType)
    if error !=nil {
        ....
    }

Building the multilChain db source

When the project starts, the multilChain db is initialized within the makeFullNode() function. ps:【multiChain.NewStoreDb(ctx, &cfg.Eth) 】

Interface function

Write block header information:

name params introduce
WriteHeader header headerInfo (ethereum.Header)

Get block header information with hash and number .

name params introduce return type
ReadHeader Hash hashValue *ethereum.Header
number blockNumber
Insert what you want to deposit
name params introduce
InsertHeaderChain chains Incoming []*ethereum.Header
start time.Time for log

for Example:
   	status, error := hc.InsertHeaderChain(chain, time.Now())
    if error !=nil {
        ....
    }
   	if status != wantStatus {
   		t.Errorf("wrong write status from InsertHeaderChain: got %v, want %v", status, wantStatus)
   	}
ps:
status value
NonStatTyState   WriteStatus = iota // not the Canonical will be ignore
CanonStatTyState                    // the Canonical
SideStatTyState                     // the branch

Get block number with hash .

name params return type
GetBlockNumber hash *uint64

Gets the block height of the current type chain:

name return type
CurrentHeaderNumber uint64

Gets the block lastHash of the current type chain:

name return type
CurrentHeaderHash common.Hash

Get difficulty with hash:

name return type
GetTdByHash *big.Int
Get header information by hash:
name return type
GetHeaderByHash *Header
Get header information for a type of chain through number:
name return type
GetHeaderByNumber *Header

Get the headhash of a type of specification chain through number:

name return type
ReadCanonicalHash common.Hash

Documentation

Index

Constants

View Source
const (
	DefaultChainType = rawdb.ChainType(0)
)

Variables

This section is empty.

Functions

func CopyHeader

func CopyHeader(h *ethereum.Header) *ethereum.Header

CopyHeader creates a deep copy of a block header to prevent side effects from modifying a header variable.

func GenerateChain

func GenerateChain(config *params.ChainConfig, parent *types.Block, engine consensus.Engine, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts)

GenerateChain creates a chain of n blocks. The first block's parent will be the provided parent. db is used to store intermediate states and should contain the parent's state trie.

The generator function is called with a new block generator for every block. Any transactions and uncles added to the generator become part of the block. If gen is nil, the blocks will be empty and their coinbase will be the zero address.

Blocks created by GenerateChain do not contain valid proof of work values. Inserting them into BlockChain requires use of FakePow or a similar non-validating proof of work implementation.

func Genesis

func Genesis()

func OpenDatabase

func OpenDatabase(file string, cache, handles int) (ethdb.Database, error)

Types

type BlockGen

type BlockGen struct {
	// contains filtered or unexported fields
}

BlockGen creates blocks for testing. See GenerateChain for a detailed explanation.

func (*BlockGen) SetCoinbase

func (b *BlockGen) SetCoinbase(addr common.Address)

SetCoinbase sets the coinbase of the generated block. It can be called at most once.

type HeaderChainStore

type HeaderChainStore struct {
	Mu sync.RWMutex // blockchaindb insertion lock
	// contains filtered or unexported fields
}

func GetStoreMgr

func GetStoreMgr(chainType rawdb.ChainType) (*HeaderChainStore, error)

func NewStoreDb

func NewStoreDb(ctx *cli.Context, DatabaseCache int, DatabaseHandles int) *HeaderChainStore

func (*HeaderChainStore) CurrentHeaderHash

func (hc *HeaderChainStore) CurrentHeaderHash() common.Hash

func (*HeaderChainStore) CurrentHeaderNumber

func (hc *HeaderChainStore) CurrentHeaderNumber() uint64

func (*HeaderChainStore) DeleteHeader

func (hc *HeaderChainStore) DeleteHeader(hash common.Hash, number uint64)

func (*HeaderChainStore) GetBlockHashesFromHash

func (hc *HeaderChainStore) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash

GetBlockHashesFromHash retrieves a number of block hashes starting at a given hash, fetching towards the genesis block.

func (*HeaderChainStore) GetBlockNumber

func (hc *HeaderChainStore) GetBlockNumber(hash common.Hash) *uint64

GetBlockNumber retrieves the block number belonging to the given hash from the cache or database

func (*HeaderChainStore) GetHeader

func (hc *HeaderChainStore) GetHeader(hash common.Hash, number uint64) *ethereum.Header

func (*HeaderChainStore) GetHeaderByHash

func (hc *HeaderChainStore) GetHeaderByHash(hash common.Hash) *ethereum.Header

GetHeaderByHash retrieves a block header from the database by hash, caching it if found.

func (*HeaderChainStore) GetHeaderByNumber

func (hc *HeaderChainStore) GetHeaderByNumber(number uint64) *ethereum.Header

GetHeaderByNumber retrieves a block header from the database by number, caching it (associated with its hash) if found.

func (*HeaderChainStore) GetStoreMgr

func (hc *HeaderChainStore) GetStoreMgr(chainType rawdb.ChainType) (*HeaderChainStore, error)

func (*HeaderChainStore) GetTd

func (hc *HeaderChainStore) GetTd(hash common.Hash, number uint64) *big.Int

func (*HeaderChainStore) GetTdByHash

func (hc *HeaderChainStore) GetTdByHash(hash common.Hash) *big.Int

GetTdByHash retrieves a block's total difficulty in the canonical chain from the database by hash, caching it if found.

func (*HeaderChainStore) HasHeader

func (hc *HeaderChainStore) HasHeader(hash common.Hash, number uint64) bool

func (*HeaderChainStore) InsertHeaderChain

func (hc *HeaderChainStore) InsertHeaderChain(chains []*ethereum.Header, start time.Time) (WriteStatus, error)

func (*HeaderChainStore) ReadCanonicalHash

func (hc *HeaderChainStore) ReadCanonicalHash(number uint64) common.Hash

func (*HeaderChainStore) ReadFistBlock

func (hc *HeaderChainStore) ReadFistBlock(number uint64) common.Hash

func (*HeaderChainStore) ReadHeader

func (hc *HeaderChainStore) ReadHeader(Hash common.Hash, number uint64) *ethereum.Header

func (*HeaderChainStore) SetChainType

func (hc *HeaderChainStore) SetChainType(m rawdb.ChainType)

func (*HeaderChainStore) ValidateHeaderChain

func (hc *HeaderChainStore) ValidateHeaderChain(chain []*ethereum.Header, checkFreq int) (int, error)

func (*HeaderChainStore) WriteHeader

func (hc *HeaderChainStore) WriteHeader(header *ethereum.Header)

type WriteStatus

type WriteStatus byte

WriteStatus status of write

const (
	NonStatTyState   WriteStatus = iota // the no
	CanonStatTyState                    // the Canonical
	SideStatTyState                     // the branch
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL