state

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2021 License: LGPL-3.0 Imports: 26 Imported by: 0

Documentation

Overview

Copyright 2020 ChainSafe Systems (ON) Corp. This file is part of gossamer.

The gossamer library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The gossamer library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.

Index

Constants

This section is empty.

Variables

View Source
var ErrTrieDoesNotExist = errors.New("trie with given root does not exist")

ErrTrieDoesNotExist is returned when attempting to interact with a trie that is not stored in the StorageState

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, r byte)

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 NewInMemoryDB added in v0.3.0

func NewInMemoryDB(t *testing.T) chaindb.Database

NewInMemoryDB creates a new in-memory database

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, root common.Hash) 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 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 chaindb.Database, bt *blocktree.BlockTree) (*BlockState, error)

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

func NewBlockStateFromGenesis

func NewBlockStateFromGenesis(db chaindb.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) AddBlockToBlockTree added in v0.3.2

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

AddBlockToBlockTree adds the given block to the blocktree. It does not write it to the database.

func (*BlockState) AddBlockWithArrivalTime

func (bs *BlockState) AddBlockWithArrivalTime(block *types.Block, arrivalTime time.Time) 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) BestBlockStateRoot added in v0.2.0

func (bs *BlockState) BestBlockStateRoot() (common.Hash, error)

BestBlockStateRoot returns the state root 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) DeleteBlock added in v0.2.0

func (bs *BlockState) DeleteBlock(hash common.Hash) error

DeleteBlock deletes all instances of the block and its related data in the database

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) (time.Time, error)

GetArrivalTime returns the arrival time in nanoseconds since the Unix epoch of a block given its hash

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(num *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) GetFinalizedHash added in v0.2.0

func (bs *BlockState) GetFinalizedHash(round, setID uint64) (common.Hash, error)

GetFinalizedHash gets the latest finalized block header

func (*BlockState) GetFinalizedHeader added in v0.2.0

func (bs *BlockState) GetFinalizedHeader(round, setID uint64) (*types.Header, error)

GetFinalizedHeader returns the latest finalized block header

func (*BlockState) GetHeader

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

GetHeader returns a BlockHeader for a given hash

func (*BlockState) GetHeaderByNumber added in v0.2.0

func (bs *BlockState) GetHeaderByNumber(num *big.Int) (*types.Header, error)

GetHeaderByNumber returns a block header given a number

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) GetRound added in v0.2.0

func (bs *BlockState) GetRound() (uint64, error)

GetRound gets the latest finalized GRANDPA round from the db

func (*BlockState) GetSlotForBlock

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

GetSlotForBlock returns the slot for a block

func (*BlockState) HasArrivalTime added in v0.2.0

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

HasArrivalTime returns true if the db contains the block's arrival time

func (*BlockState) HasBlockBody added in v0.2.0

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

HasBlockBody returns true if the db contains the block body

func (*BlockState) HasFinalizedBlock added in v0.2.0

func (bs *BlockState) HasFinalizedBlock(round, setID uint64) (bool, error)

HasFinalizedBlock returns true if there is a finalized block for a given round and setID, false otherwise

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) HasJustification added in v0.2.0

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

HasJustification returns if the db contains a Justification at the given hash

func (*BlockState) HasMessageQueue added in v0.2.0

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

HasMessageQueue returns if the db contains a MessageQueue at the given hash

func (*BlockState) HasReceipt added in v0.2.0

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

HasReceipt returns if the db contains a receipt at the given hash

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) RegisterFinalizedChannel added in v0.2.0

func (bs *BlockState) RegisterFinalizedChannel(ch chan<- *types.Header) (byte, error)

RegisterFinalizedChannel registers a channel for block notification upon block finalization. It returns the channel ID (used for unregistering the channel)

func (*BlockState) RegisterImportedChannel added in v0.2.0

func (bs *BlockState) RegisterImportedChannel(ch chan<- *types.Block) (byte, error)

RegisterImportedChannel registers a channel for block notification upon block import. It returns the channel ID (used for unregistering the channel)

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) SetFinalizedHash added in v0.2.0

func (bs *BlockState) SetFinalizedHash(hash common.Hash, round, setID uint64) error

SetFinalizedHash sets the latest finalized block header

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) SetRound added in v0.2.0

func (bs *BlockState) SetRound(round uint64) error

SetRound sets the latest finalized GRANDPA round in the db TODO: this needs to use both setID and round

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

func (*BlockState) UnregisterFinalizedChannel added in v0.2.0

func (bs *BlockState) UnregisterFinalizedChannel(id byte)

UnregisterFinalizedChannel removes the block finalization notification channel with the given ID. A channel must be unregistered before closing it.

func (*BlockState) UnregisterImportedChannel added in v0.2.0

func (bs *BlockState) UnregisterImportedChannel(id byte)

UnregisterImportedChannel removes the block import notification channel with the given ID. A channel must be unregistered before closing it.

type EpochState added in v0.2.0

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

EpochState tracks information related to each epoch

func NewEpochState added in v0.2.0

func NewEpochState(db chaindb.Database) (*EpochState, error)

NewEpochState returns a new EpochState

func NewEpochStateFromGenesis added in v0.2.0

func NewEpochStateFromGenesis(db chaindb.Database, genesisConfig *types.BabeConfiguration) (*EpochState, error)

NewEpochStateFromGenesis returns a new EpochState given information for the first epoch, fetched from the runtime

func (*EpochState) GetConfigData added in v0.3.0

func (s *EpochState) GetConfigData(epoch uint64) (*types.ConfigData, error)

GetConfigData returns the BABE config data for a given epoch

func (*EpochState) GetCurrentEpoch added in v0.2.0

func (s *EpochState) GetCurrentEpoch() (uint64, error)

GetCurrentEpoch returns the current epoch

func (*EpochState) GetEpochData added in v0.3.0

func (s *EpochState) GetEpochData(epoch uint64) (*types.EpochData, error)

GetEpochData returns the epoch data for a given epoch

func (*EpochState) GetEpochForBlock added in v0.3.0

func (s *EpochState) GetEpochForBlock(header *types.Header) (uint64, error)

GetEpochForBlock checks the pre-runtime digest to determine what epoch the block was formed in.

func (*EpochState) GetLatestEpochData added in v0.3.0

func (s *EpochState) GetLatestEpochData() (*types.EpochData, error)

GetLatestEpochData returns the EpochData for the current epoch

func (*EpochState) GetStartSlotForEpoch added in v0.2.0

func (s *EpochState) GetStartSlotForEpoch(epoch uint64) (uint64, error)

GetStartSlotForEpoch returns the first slot in the given epoch. If 0 is passed as the epoch, it returns the start slot for the current epoch.

func (*EpochState) HasConfigData added in v0.3.0

func (s *EpochState) HasConfigData(epoch uint64) (bool, error)

HasConfigData returns whether config data exists for a given epoch

func (*EpochState) HasEpochData added in v0.3.0

func (s *EpochState) HasEpochData(epoch uint64) (bool, error)

HasEpochData returns whether epoch data exists for a given epoch

func (*EpochState) SetConfigData added in v0.3.0

func (s *EpochState) SetConfigData(epoch uint64, info *types.ConfigData) error

SetConfigData sets the BABE config data for a given epoch

func (*EpochState) SetCurrentEpoch added in v0.2.0

func (s *EpochState) SetCurrentEpoch(epoch uint64) error

SetCurrentEpoch sets the current epoch

func (*EpochState) SetEpochData added in v0.3.0

func (s *EpochState) SetEpochData(epoch uint64, info *types.EpochData) error

SetEpochData sets the epoch data for a given epoch

func (*EpochState) SetFirstSlot added in v0.3.0

func (s *EpochState) SetFirstSlot(slot uint64) error

SetFirstSlot sets the first slot number of the network

func (*EpochState) SkipVerify added in v0.3.2

func (s *EpochState) SkipVerify(header *types.Header) (bool, error)

SkipVerify returns whether verification for the given header should be skipped or not. Only used in the case of imported state.

type KeyValue added in v0.2.0

type KeyValue struct {
	Key   []byte
	Value []byte
}

KeyValue struct to hold key value pairs

type Service

type Service struct {
	Storage     *StorageState
	Block       *BlockState
	Transaction *TransactionState
	Epoch       *EpochState

	// Below are for testing only.
	BabeThresholdNumerator   uint64
	BabeThresholdDenominator uint64
	// contains filtered or unexported fields
}

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

func NewService

func NewService(path string, lvl log.Lvl) *Service

NewService create a new instance of Service

func (*Service) DB

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

DB returns the Service's database

func (*Service) Import added in v0.3.2

func (s *Service) Import(header *types.Header, t *trie.Trie, firstSlot uint64) error

Import imports the given state corresponding to the given header and sets the head of the chain to it. Additionally, it uses the first slot to correctly set the epoch number of the block.

func (*Service) Initialize

func (s *Service) Initialize(gen *genesis.Genesis, 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) Rewind added in v0.3.1

func (s *Service) Rewind(toBlock int64) error

Rewind rewinds the chain to the given block number. If the given number of blocks is greater than the chain height, it will rewind to genesis.

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 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 chaindb.Database, blockState *BlockState, t *trie.Trie) (*StorageState, error)

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

func (*StorageState) Entries

func (s *StorageState) Entries(root *common.Hash) (map[string][]byte, error)

Entries returns Entries from the trie with the given state root

func (*StorageState) EnumeratedTrieRoot

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

EnumeratedTrieRoot not implemented

func (*StorageState) ExistsStorage

func (s *StorageState) ExistsStorage(hash *common.Hash, key []byte) (bool, error)

ExistsStorage check if the key exists in the storage trie with the given storage hash If no hash is provided, the current chain head is used

func (*StorageState) GetBalance

func (s *StorageState) GetBalance(hash *common.Hash, key [32]byte) (uint64, error)

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

func (*StorageState) GetGenesisData added in v0.3.0

func (s *StorageState) GetGenesisData() (*genesis.Data, error)

GetGenesisData retrieves current genesis data from database

func (*StorageState) GetKeysWithPrefix added in v0.3.0

func (s *StorageState) GetKeysWithPrefix(hash *common.Hash, prefix []byte) ([][]byte, error)

GetKeysWithPrefix returns all that match the given prefix for the given hash (or best block state root if hash is nil) in lexicographic order

func (*StorageState) GetStateRootFromBlock added in v0.3.0

func (s *StorageState) GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error)

GetStateRootFromBlock returns the state root hash of a given block hash

func (*StorageState) GetStorage

func (s *StorageState) GetStorage(root *common.Hash, key []byte) ([]byte, error)

GetStorage gets the object from the trie using the given key and storage hash If no hash is provided, the current chain head is used

func (*StorageState) GetStorageByBlockHash added in v0.2.0

func (s *StorageState) GetStorageByBlockHash(bhash common.Hash, key []byte) ([]byte, error)

GetStorageByBlockHash returns the value at the given key at the given block hash

func (*StorageState) GetStorageChild

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

GetStorageChild return GetChild from the trie

func (*StorageState) GetStorageFromChild

func (s *StorageState) GetStorageFromChild(hash *common.Hash, keyToChild, key []byte) ([]byte, error)

GetStorageFromChild return GetFromChild from the trie

func (*StorageState) LoadCode

func (s *StorageState) LoadCode(hash *common.Hash) ([]byte, error)

LoadCode returns the runtime code (located at :code)

func (*StorageState) LoadCodeHash

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

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

func (*StorageState) LoadFromDB

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

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

func (*StorageState) RegisterStorageChangeChannel added in v0.2.0

func (s *StorageState) RegisterStorageChangeChannel(sub StorageSubscription) (byte, error)

RegisterStorageChangeChannel function to register storage change channels

func (*StorageState) StorageRoot

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

StorageRoot returns the root hash of the current storage trie

func (*StorageState) StoreTrie added in v0.2.0

func (s *StorageState) StoreTrie(ts *rtstorage.TrieState) error

StoreTrie stores the given trie in the StorageState and writes it to the database

func (*StorageState) TrieState added in v0.2.0

func (s *StorageState) TrieState(root *common.Hash) (*rtstorage.TrieState, error)

TrieState returns the TrieState for a given state root. If no state root is provided, it returns the TrieState for the current chain head.

func (*StorageState) UnregisterStorageChangeChannel added in v0.2.0

func (s *StorageState) UnregisterStorageChangeChannel(id byte)

UnregisterStorageChangeChannel removes the storage change notification channel with the given ID. A channel must be unregistered before closing it.

type StorageSubscription added in v0.3.0

type StorageSubscription struct {
	Filter   map[string]bool
	Listener chan<- *SubscriptionResult
}

StorageSubscription holds data for Subscription to Storage

type SubscriptionResult added in v0.3.0

type SubscriptionResult struct {
	Hash    common.Hash
	Changes []KeyValue
}

SubscriptionResult holds results of storage changes

type TransactionState added in v0.2.0

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

TransactionState represents the queue of transactions

func NewTransactionState added in v0.2.0

func NewTransactionState() *TransactionState

NewTransactionState returns a new TransactionState

func (*TransactionState) AddToPool added in v0.2.0

AddToPool adds a transaction to the pool

func (*TransactionState) Peek added in v0.2.0

Peek returns the head of the queue without removing it

func (*TransactionState) Pending added in v0.2.0

Pending returns the current transactions in the queue and pool

func (*TransactionState) PendingInPool added in v0.2.0

func (s *TransactionState) PendingInPool() []*transaction.ValidTransaction

PendingInPool returns the current transactions in the pool

func (*TransactionState) Pop added in v0.2.0

Pop removes and returns the head of the queue

func (*TransactionState) Push added in v0.2.0

Push pushes a transaction to the queue, ordered by priority

func (*TransactionState) RemoveExtrinsic added in v0.2.0

func (s *TransactionState) RemoveExtrinsic(ext types.Extrinsic)

RemoveExtrinsic removes an extrinsic from the queue and pool

func (*TransactionState) RemoveExtrinsicFromPool added in v0.2.0

func (s *TransactionState) RemoveExtrinsicFromPool(ext types.Extrinsic)

RemoveExtrinsicFromPool removes an extrinsic from the pool

Jump to

Keyboard shortcuts

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