state

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2020 License: LGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddBlocksToState

func AddBlocksToState(t *testing.T, blockState *BlockState, depth int) ([]*types.Header, []*types.Header)

AddBlocksToState adds blocks to a BlockState up to depth, with random branches

func AddBlocksToStateWithFixedBranches

func AddBlocksToStateWithFixedBranches(t *testing.T, blockState *BlockState, depth int, branches map[int]int)

AddBlocksToStateWithFixedBranches adds blocks to a BlockState up to depth, with fixed branches branches are provided with a map of depth -> # of branches

func LoadBestBlockHash

func LoadBestBlockHash(db database.Database) (common.Hash, error)

LoadBestBlockHash loads the hash stored at BestBlockHashKey

func LoadGenesisData

func LoadGenesisData(db database.Database) (*genesis.Data, error)

LoadGenesisData retrieves the genesis data stored at the known GenesisDataKey.

func LoadLatestStorageHash

func LoadLatestStorageHash(db database.Database) (common.Hash, error)

LoadLatestStorageHash retrieves the hash stored at LatestStorageHashKey from the DB

func LoadTrie

func LoadTrie(db database.Database, t *trie.Trie, root common.Hash) error

LoadTrie loads an encoded trie from the DB where the key is `root`

func StoreBestBlockHash

func StoreBestBlockHash(db database.Database, hash common.Hash) error

StoreBestBlockHash stores the hash at the BestBlockHashKey

func StoreGenesisData

func StoreGenesisData(db database.Database, gen *genesis.Data) error

StoreGenesisData stores the given genesis data at the known GenesisDataKey.

func StoreLatestStorageHash

func StoreLatestStorageHash(db database.Database, t *trie.Trie) error

StoreLatestStorageHash stores the current root hash in the database at LatestStorageHashKey

func StoreTrie

func StoreTrie(db database.Database, t *trie.Trie) error

StoreTrie encodes the entire trie and writes it to the DB The key to the DB entry is the root hash of the trie

Types

type BlockDB

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

BlockDB stores block's in an underlying Database

func NewBlockDB

func NewBlockDB(db database.Database) *BlockDB

NewBlockDB instantiates a badgerDB instance for storing relevant BlockData

func (*BlockDB) Get

func (blockDB *BlockDB) Get(key []byte) ([]byte, error)

Get appends `block` to the key and retrieves the value from the db

func (*BlockDB) Has

func (blockDB *BlockDB) Has(key []byte) (bool, error)

Has appends `block` to the key and checks for existence in the db

func (*BlockDB) Put

func (blockDB *BlockDB) Put(key, value []byte) error

Put appends `block` to the key and sets the key-value pair in the db

type BlockState

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

BlockState defines fields for manipulating the state of blocks, such as BlockTree, BlockDB and Header

func NewBlockState

func NewBlockState(db database.Database, bt *blocktree.BlockTree) (*BlockState, error)

NewBlockState will create a new BlockState backed by the database located at basePath

func NewBlockStateFromGenesis

func NewBlockStateFromGenesis(db database.Database, header *types.Header) (*BlockState, error)

NewBlockStateFromGenesis initializes a BlockState from a genesis header, saving it to the database located at basePath

func (*BlockState) AddBlock

func (bs *BlockState) AddBlock(block *types.Block) error

AddBlock adds a block to the blocktree and the DB with arrival time as current unix time

func (*BlockState) AddBlockWithArrivalTime

func (bs *BlockState) AddBlockWithArrivalTime(block *types.Block, arrivalTime uint64) error

AddBlockWithArrivalTime adds a block to the blocktree and the DB with the given arrival time

func (*BlockState) BestBlock

func (bs *BlockState) BestBlock() (*types.Block, error)

BestBlock returns the current head of the chain

func (*BlockState) BestBlockHash

func (bs *BlockState) BestBlockHash() common.Hash

BestBlockHash returns the hash of the head of the current chain

func (*BlockState) BestBlockHeader

func (bs *BlockState) BestBlockHeader() (*types.Header, error)

BestBlockHeader returns the block header of the current head of the chain

func (*BlockState) BestBlockNumber

func (bs *BlockState) BestBlockNumber() (*big.Int, error)

BestBlockNumber returns the block number of the current head of the chain

func (*BlockState) BlocktreeAsString

func (bs *BlockState) BlocktreeAsString() string

BlocktreeAsString returns the blocktree as a string

func (*BlockState) CompareAndSetBlockData

func (bs *BlockState) CompareAndSetBlockData(bd *types.BlockData) error

CompareAndSetBlockData will compare empty fields and set all elements in a block data to db

func (*BlockState) GenesisHash

func (bs *BlockState) GenesisHash() common.Hash

GenesisHash returns the hash of the genesis block

func (*BlockState) GetAllBlocksAtDepth

func (bs *BlockState) GetAllBlocksAtDepth(hash common.Hash) []common.Hash

GetAllBlocksAtDepth returns all hashes with the depth of the given hash plus one

func (*BlockState) GetArrivalTime

func (bs *BlockState) GetArrivalTime(hash common.Hash) (uint64, error)

GetArrivalTime returns the arrival time of a block given its hash

func (*BlockState) GetBabeHeader

func (bs *BlockState) GetBabeHeader(epoch uint64, slot uint64) (*types.BabeHeader, error)

GetBabeHeader retrieves a BabeHeader from the database

func (*BlockState) GetBlockBody

func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.Body, error)

GetBlockBody will return Body for a given hash

func (*BlockState) GetBlockByHash

func (bs *BlockState) GetBlockByHash(hash common.Hash) (*types.Block, error)

GetBlockByHash returns a block for a given hash

func (*BlockState) GetBlockByNumber

func (bs *BlockState) GetBlockByNumber(blockNumber *big.Int) (*types.Block, error)

GetBlockByNumber returns a block for a given blockNumber

func (*BlockState) GetBlockHash

func (bs *BlockState) GetBlockHash(blockNumber *big.Int) (*common.Hash, error)

GetBlockHash returns block hash for a given blockNumber

func (*BlockState) GetFinalizedHead

func (bs *BlockState) GetFinalizedHead() (*types.Header, error)

GetFinalizedHead returns the latest finalized block header TODO: relies on GRANDPA implementation. currently returns genesis header.

func (*BlockState) GetHeader

func (bs *BlockState) GetHeader(hash common.Hash) (*types.Header, error)

GetHeader returns a BlockHeader for a given hash

func (*BlockState) GetJustification

func (bs *BlockState) GetJustification(hash common.Hash) ([]byte, error)

GetJustification retrieves a Justification from the database

func (*BlockState) GetMessageQueue

func (bs *BlockState) GetMessageQueue(hash common.Hash) ([]byte, error)

GetMessageQueue retrieves a MessageQueue from the database

func (*BlockState) GetReceipt

func (bs *BlockState) GetReceipt(hash common.Hash) ([]byte, error)

GetReceipt retrieves a Receipt from the database

func (*BlockState) GetSlotForBlock

func (bs *BlockState) GetSlotForBlock(hash common.Hash) (uint64, error)

GetSlotForBlock returns the slot for a block

func (*BlockState) HasHeader

func (bs *BlockState) HasHeader(hash common.Hash) (bool, error)

HasHeader returns if the db contains a header with the given hash

func (*BlockState) HighestBlockHash

func (bs *BlockState) HighestBlockHash() common.Hash

HighestBlockHash returns the hash of the block with the highest number we have received This block may not necessarily be in the blocktree. TODO: can probably remove this once BlockResponses are implemented

func (*BlockState) HighestBlockNumber

func (bs *BlockState) HighestBlockNumber() *big.Int

HighestBlockNumber returns the largest block number we have seen This block may not necessarily be in the blocktree. TODO: can probably remove this once BlockResponses are implemented

func (*BlockState) HighestCommonAncestor

func (bs *BlockState) HighestCommonAncestor(a, b common.Hash) (common.Hash, error)

HighestCommonAncestor returns the block with the highest number that is an ancestor of both a and b

func (*BlockState) IsDescendantOf

func (bs *BlockState) IsDescendantOf(parent, child common.Hash) (bool, error)

IsDescendantOf returns true if child is a descendant of parent, false otherwise. it returns an error if parent or child are not in the blocktree.

func (*BlockState) Leaves

func (bs *BlockState) Leaves() []common.Hash

Leaves returns the leaves of the blocktree as an array

func (*BlockState) SetBabeHeader

func (bs *BlockState) SetBabeHeader(epoch uint64, slot uint64, bh *types.BabeHeader) error

SetBabeHeader sets a BabeHeader in the database

func (*BlockState) SetBlockAddedChannel

func (bs *BlockState) SetBlockAddedChannel(rcvr chan<- *types.Block, done <-chan struct{})

SetBlockAddedChannel to sets channel that blocks will be received on

func (*BlockState) SetBlockBody

func (bs *BlockState) SetBlockBody(hash common.Hash, body *types.Body) error

SetBlockBody will add a block body to the db

func (*BlockState) SetHeader

func (bs *BlockState) SetHeader(header *types.Header) error

SetHeader will set the header into DB

func (*BlockState) SetJustification

func (bs *BlockState) SetJustification(hash common.Hash, data []byte) error

SetJustification sets a Justification in the database

func (*BlockState) SetMessageQueue

func (bs *BlockState) SetMessageQueue(hash common.Hash, data []byte) error

SetMessageQueue sets a MessageQueue in the database

func (*BlockState) SetReceipt

func (bs *BlockState) SetReceipt(hash common.Hash, data []byte) error

SetReceipt sets a Receipt in the database

func (*BlockState) SubChain

func (bs *BlockState) SubChain(start, end common.Hash) ([]common.Hash, error)

SubChain returns the sub-blockchain between the starting hash and the ending hash using the block tree

type NetworkState

type NetworkState struct {
	Health       common.Health
	NetworkState common.NetworkState
	Peers        []common.PeerInfo
}

NetworkState defines fields for manipulating the state of network

func NewNetworkState

func NewNetworkState() *NetworkState

NewNetworkState creates NetworkState

func (*NetworkState) GetHealth

func (ns *NetworkState) GetHealth() common.Health

GetHealth retrieves network health from the database

func (*NetworkState) GetNetworkState

func (ns *NetworkState) GetNetworkState() common.NetworkState

GetNetworkState retrieves network state from the database

func (*NetworkState) GetPeers

func (ns *NetworkState) GetPeers() []common.PeerInfo

GetPeers retrieves network state from the database

func (*NetworkState) SetHealth

func (ns *NetworkState) SetHealth(health common.Health)

SetHealth sets network health in the database

func (*NetworkState) SetNetworkState

func (ns *NetworkState) SetNetworkState(networkState common.NetworkState)

SetNetworkState sets network state in the database

func (*NetworkState) SetPeers

func (ns *NetworkState) SetPeers(peers []common.PeerInfo)

SetPeers sets network state in the database

type Service

type Service struct {
	Storage          *StorageState
	Block            *BlockState
	Network          *NetworkState
	TransactionQueue *TransactionQueue
	// contains filtered or unexported fields
}

Service is the struct that holds storage, block and network states

func NewService

func NewService(path string) *Service

NewService create a new instance of Service

func (*Service) DB

func (s *Service) DB() database.Database

DB returns the Service's database

func (*Service) Initialize

func (s *Service) Initialize(data *genesis.Data, header *types.Header, t *trie.Trie) error

Initialize initializes the genesis state of the DB using the given storage trie. The trie should be loaded with the genesis storage state. This only needs to be called during genesis initialization of the node; it doesn't need to be called during normal startup.

func (*Service) Start

func (s *Service) Start() error

Start initializes the Storage database and the Block database.

func (*Service) Stop

func (s *Service) Stop() error

Stop closes each state database

func (*Service) UseMemDB

func (s *Service) UseMemDB()

UseMemDB tells the service to use an in-memory key-value store instead of a persistent database. This should be called after NewService, and before Initialize. This should only be used for testing.

type StorageDB

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

StorageDB stores trie structure in an underlying database

func NewStorageDB

func NewStorageDB(db database.Database) *StorageDB

NewStorageDB instantiates badgerDB instance for storing trie structure

func (*StorageDB) Get

func (storageDB *StorageDB) Get(key []byte) ([]byte, error)

Get appends `storage` to the key and retrieves the value from the db

func (*StorageDB) Put

func (storageDB *StorageDB) Put(key, value []byte) error

Put appends `storage` to the key and sets the key-value pair in the db

type StorageState

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

StorageState is the struct that holds the trie, db and lock

func NewStorageState

func NewStorageState(db database.Database, t *trie.Trie) (*StorageState, error)

NewStorageState creates a new StorageState backed by the given trie and database located at basePath.

func (*StorageState) ClearPrefix

func (s *StorageState) ClearPrefix(prefix []byte)

ClearPrefix not implemented

func (*StorageState) ClearStorage

func (s *StorageState) ClearStorage(key []byte) error

ClearStorage will delete a key/value from the trie for a given @key

func (*StorageState) Entries

func (s *StorageState) Entries() map[string][]byte

Entries returns Entries from the trie

func (*StorageState) EnumeratedTrieRoot

func (s *StorageState) EnumeratedTrieRoot(values [][]byte)

EnumeratedTrieRoot not implemented

func (*StorageState) ExistsStorage

func (s *StorageState) ExistsStorage(key []byte) (bool, error)

ExistsStorage check if the key exists in the storage trie

func (*StorageState) GetBalance

func (s *StorageState) GetBalance(key [32]byte) (uint64, error)

GetBalance gets the balance for an account with the given public key

func (*StorageState) GetStorage

func (s *StorageState) GetStorage(key []byte) ([]byte, error)

GetStorage gets the object from the trie using key

func (*StorageState) GetStorageChild

func (s *StorageState) GetStorageChild(keyToChild []byte) (*trie.Trie, error)

GetStorageChild return GetChild from the trie

func (*StorageState) GetStorageFromChild

func (s *StorageState) GetStorageFromChild(keyToChild, key []byte) ([]byte, error)

GetStorageFromChild return GetFromChild from the trie

func (*StorageState) LoadCode

func (s *StorageState) LoadCode() ([]byte, error)

LoadCode returns the runtime code (located at :code)

func (*StorageState) LoadCodeHash

func (s *StorageState) LoadCodeHash() (common.Hash, error)

LoadCodeHash returns the hash of the runtime code (located at :code)

func (*StorageState) LoadFromDB

func (s *StorageState) LoadFromDB(root common.Hash) error

LoadFromDB loads an encoded trie from the DB where the key is `root`

func (*StorageState) SetBalance

func (s *StorageState) SetBalance(key [32]byte, balance uint64) error

SetBalance sets the balance for an account with the given public key

func (*StorageState) SetStorage

func (s *StorageState) SetStorage(key []byte, value []byte) error

SetStorage set the storage value for a given key in the trie

func (*StorageState) SetStorageChild

func (s *StorageState) SetStorageChild(keyToChild []byte, child *trie.Trie) error

SetStorageChild return PutChild from the trie

func (*StorageState) SetStorageIntoChild

func (s *StorageState) SetStorageIntoChild(keyToChild, key, value []byte) error

SetStorageIntoChild return PutIntoChild from the trie

func (*StorageState) StorageRoot

func (s *StorageState) StorageRoot() (common.Hash, error)

StorageRoot returns the trie hash

func (*StorageState) StoreInDB

func (s *StorageState) StoreInDB() error

StoreInDB encodes the entire trie and writes it to the DB The key to the DB entry is the root hash of the trie

type TransactionQueue

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

TransactionQueue represents the queue of transactions

func NewTransactionQueue

func NewTransactionQueue() *TransactionQueue

NewTransactionQueue returns a new TransactionQueue

func (*TransactionQueue) Peek

Peek returns the head of the queue without removing it

func (*TransactionQueue) Pending

Pending returns the current transactions in the queue

func (*TransactionQueue) Pop

Pop removes and returns the head of the queue

func (*TransactionQueue) Push

Push pushes a transaction to the queue, ordered by priority

func (*TransactionQueue) RemoveExtrinsic

func (q *TransactionQueue) RemoveExtrinsic(ext types.Extrinsic)

RemoveExtrinsic removes an extrinsic from the queue

Jump to

Keyboard shortcuts

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