stakedb

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2019 License: ISC Imports: 22 Imported by: 122

Documentation

Index

Constants

View Source
const (

	// DefaultStakeDbName is the default name of the stakedb database folder
	DefaultStakeDbName = "stakenodes"
	// DefaultTicketPoolDbFolder is the default name of the ticket pool database
	DefaultTicketPoolDbFolder = "ticket_pool.bdgr"
	// DefaultTicketPoolDbName is the default name of the old storm database
	DefaultTicketPoolDbName = "ticket_pool.db"
)

Variables

This section is empty.

Functions

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until UseLogger is called.

func MigrateFromStorm

func MigrateFromStorm(stormDBFile string, db *badger.DB) (bool, error)

MigrateFromStorm attempts to load the storm DB specified by the given file name, and migrate all ticket pool diffs to the badger db.

func UseLogger

func UseLogger(logger slog.Logger)

UseLogger uses a specified Logger to output package logging info.

Types

type ChainMonitor

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

ChainMonitor connects blocks to the stake DB as they come in.

func (*ChainMonitor) BlockConnectedHandler

func (p *ChainMonitor) BlockConnectedHandler()

BlockConnectedHandler handles block connected notifications, which trigger data collection and storage.

func (*ChainMonitor) ConnectBlock

func (p *ChainMonitor) ConnectBlock(hash *chainhash.Hash) (err error)

ConnectBlock is a sychronous version of BlockConnectedHandler that collects and stores data for a block specified by the given hash.

func (*ChainMonitor) ReorgHandler

func (p *ChainMonitor) ReorgHandler()

ReorgHandler receives notification of a chain reorganization and initiates a corresponding reorganization of the stakedb.StakeDatabase.

func (*ChainMonitor) SetNewBlockChan

func (p *ChainMonitor) SetNewBlockChan(blockChan chan *chainhash.Hash)

SetNewBlockChan specifies the new-block channel to be used by BlockConnectedHandler. Note that BlockConnectedHandler is not required if using a direct call to ConnectBlock.

type PoolDiff

type PoolDiff struct {
	In  []chainhash.Hash
	Out []chainhash.Hash
}

PoolDiff represents the tickets going in and out of the live ticket pool from one height to the next.

func LoadAllPoolDiffs

func LoadAllPoolDiffs(db *badger.DB) ([]PoolDiff, error)

LoadAllPoolDiffs loads all found ticket pool diffs from badger DB.

type PoolDiffDBItem

type PoolDiffDBItem struct {
	Height   int64 `storm:"id"`
	PoolDiff `storm:"inline"`
}

PoolDiffDBItem is the type in the live ticket DB. The primary key (id) is Height.

type PoolInfoCache

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

PoolInfoCache contains a map of block hashes to ticket pool info data at that block height.

func NewPoolInfoCache

func NewPoolInfoCache(size int) (*PoolInfoCache, error)

NewPoolInfoCache constructs a new PoolInfoCache, and is needed to initialize the internal map.

func (*PoolInfoCache) Get

Get attempts to fetch the ticket pool info for a given block hash, returning a *apitypes.TicketPoolInfo, and a bool indicating if the hash was found in the map.

func (*PoolInfoCache) Set

Set stores the ticket pool info for the given hash in the pool info cache.

func (*PoolInfoCache) SetCapacity

func (c *PoolInfoCache) SetCapacity(size int) error

SetCapacity sets the cache capacity to the specified number of elements. If the new capacity is smaller than the current cache size, elements are automatically evicted until the desired size is reached.

type StakeDatabase

type StakeDatabase struct {
	NodeClient *rpcclient.Client

	StakeDB  database.DB
	BestNode *stake.Node

	PoolDB *TicketPool
	// contains filtered or unexported fields
}

StakeDatabase models data for the stake database

func LoadAndRecover

func LoadAndRecover(client *rpcclient.Client, params *chaincfg.Params,
	dataDir string, toHeight int64) (*StakeDatabase, error)

LoadAndRecover attempts to load the StakeDatabase and it's TicketPool, rewinding either TicketPool or StakeDatabase so that they are at the same height, and then further rewinding both to the specified height. Finally, it advances the TicketPool to tip, and if there is an error it rewinds both back to that height - 1. Normally use NewStakeDatabase.

func NewStakeDatabase

func NewStakeDatabase(client *rpcclient.Client, params *chaincfg.Params,
	dataDir string) (*StakeDatabase, int64, error)

NewStakeDatabase creates a StakeDatabase instance, opening or creating a new ffldb-backed stake database, and loads all live tickets into a cache. The smaller height of the StakeDatabase and TicketPool is also returned to aid in recovery (they should be the same height). The live ticket cache is only populated if there are no errors.

func (*StakeDatabase) BlockCached

func (db *StakeDatabase) BlockCached(ind int64) (*dcrutil.Block, bool)

BlockCached attempts to find the block at the specified height in the block cache. The returned boolean indicates if it was found.

func (*StakeDatabase) Close

func (db *StakeDatabase) Close() error

Close will close the ticket pool and stake databases.

func (*StakeDatabase) ConnectBlock

func (db *StakeDatabase) ConnectBlock(block *dcrutil.Block) error

ConnectBlock connects the input block to the tip of the stake DB and updates the best stake node. This exported function gets any revoked and spend tickets from the input block, and any maturing tickets from the past block in which those tickets would be found, and passes them to connectBlock.

func (*StakeDatabase) ConnectBlockHash

func (db *StakeDatabase) ConnectBlockHash(hash *chainhash.Hash) (*dcrutil.Block, error)

ConnectBlockHash is a wrapper for ConnectBlock. For the input block hash, it gets the block from the node RPC client and calls ConnectBlock.

func (*StakeDatabase) DBPrevBlock

func (db *StakeDatabase) DBPrevBlock() (*dcrutil.Block, error)

DBPrevBlock gets the dcrutil.Block for the previous best block in the stake database. It used DBState to get the best block hash, and the node RPC client to get the block itself.

func (*StakeDatabase) DBPrevBlockHeader

func (db *StakeDatabase) DBPrevBlockHeader() (*wire.BlockHeader, error)

DBPrevBlockHeader gets the block header for the previous best block in the stake database. It used DBState to get the best block hash, and the node RPC client to get the header.

func (*StakeDatabase) DBState

func (db *StakeDatabase) DBState() (uint32, *chainhash.Hash, error)

DBState queries the stake database for the best block height and hash.

func (*StakeDatabase) DBTipBlock

func (db *StakeDatabase) DBTipBlock() (*dcrutil.Block, error)

DBTipBlock gets the dcrutil.Block for the current best block in the stake database. It used DBState to get the best block hash, and the node RPC client to get the block itself.

func (*StakeDatabase) DBTipBlockHeader

func (db *StakeDatabase) DBTipBlockHeader() (*wire.BlockHeader, error)

DBTipBlockHeader gets the block header for the current best block in the stake database. It used DBState to get the best block hash, and the node RPC client to get the header.

func (*StakeDatabase) DisconnectBlock

func (db *StakeDatabase) DisconnectBlock(neglectCache bool) error

DisconnectBlock attempts to disconnect the current best block from the stake DB and updates the best stake node. If the ticket pool db is advanced to the tip, it is trimmed, and the cache and pool value are updated. If neglectCache is true, the trim is performed, but cache and pool value are not updated. Only use neglectCache=true if you plan to

func (*StakeDatabase) DisconnectBlocks

func (db *StakeDatabase) DisconnectBlocks(count int64) error

DisconnectBlocks disconnects N blocks from the head of the chain.

func (*StakeDatabase) ForgetBlock

func (db *StakeDatabase) ForgetBlock(ind int64)

ForgetBlock deletes the block with the input height from the block cache.

func (*StakeDatabase) Height

func (db *StakeDatabase) Height() uint32

Height gets the block height of the best stake node. It is thread-safe, unlike using db.BestNode.Height(), and checks that the stake database is opened first.

func (*StakeDatabase) LockStakeNode

func (db *StakeDatabase) LockStakeNode()

LockStakeNode locks the StakeNode from functions that respect the mutex.

func (*StakeDatabase) NewChainMonitor

func (db *StakeDatabase) NewChainMonitor(ctx context.Context, wg *sync.WaitGroup,
	reorgChan chan *txhelpers.ReorgData) *ChainMonitor

NewChainMonitor creates a new ChainMonitor

func (*StakeDatabase) Open

func (db *StakeDatabase) Open(dbName string) error

Open attempts to open an existing stake database, and will create a new one if one does not exist.

func (*StakeDatabase) PoolAtHash

func (db *StakeDatabase) PoolAtHash(hash chainhash.Hash) ([]chainhash.Hash, error)

PoolAtHash gets the entire list of live tickets at the given block hash.

func (*StakeDatabase) PoolAtHeight

func (db *StakeDatabase) PoolAtHeight(height int64) ([]chainhash.Hash, error)

PoolAtHeight gets the entire list of live tickets at the given chain height.

func (*StakeDatabase) PoolInfo

func (db *StakeDatabase) PoolInfo(hash chainhash.Hash) (*apitypes.TicketPoolInfo, bool)

PoolInfo attempts to fetch the ticket pool info for the specified block hash from an internal pool info cache. If it is not found, you should attempt to use PoolInfoBest if the target block is at the tip of the chain.

func (*StakeDatabase) PoolInfoBest

func (db *StakeDatabase) PoolInfoBest() *apitypes.TicketPoolInfo

PoolInfoBest computes ticket pool value using the database and, if needed, the node RPC client to fetch ticket values that are not cached. Returned are a structure including ticket pool value, size, and average value.

func (*StakeDatabase) PoolSize

func (db *StakeDatabase) PoolSize() int

PoolSize returns the ticket pool size in the best node of the stake database

func (*StakeDatabase) PopulateLiveTicketCache

func (db *StakeDatabase) PopulateLiveTicketCache() error

PopulateLiveTicketCache loads the hashes of all tickets in BestNode into the cache and computes the internally-stored pool value.

func (*StakeDatabase) Rewind

func (db *StakeDatabase) Rewind(to int64, neglectCache bool) error

Rewind disconnects blocks until the new height is the specified height. During disconnect, the ticket pool cache and value are kept accurate, unless neglectCache is true.

func (*StakeDatabase) SetPoolCacheCapacity

func (db *StakeDatabase) SetPoolCacheCapacity(cap int) error

SetPoolCacheCapacity sets the pool info cache capacity to the specified number of elements.

func (*StakeDatabase) SetPoolInfo

func (db *StakeDatabase) SetPoolInfo(blockHash chainhash.Hash, tpi *apitypes.TicketPoolInfo)

SetPoolInfo stores the ticket pool info for the given hash in the pool info cache.

func (*StakeDatabase) UnlockStakeNode

func (db *StakeDatabase) UnlockStakeNode()

UnlockStakeNode unlocks the StakeNode for functions that respect the mutex.

func (*StakeDatabase) WaitForHeight

func (db *StakeDatabase) WaitForHeight(height int64) chan *chainhash.Hash

WaitForHeight provides a notification channel to which the hash of the block at the requested height will be sent when it becomes available.

type TicketPool

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

TicketPool contains the live ticket pool diffs (tickets in/out) between adjacent block heights in a chain. Diffs are applied in sequence by inserting and removing ticket hashes from a pool, represented as a map. A []PoolDiff stores these diffs, with a cursor pointing to the next unapplied diff. An on-disk database of diffs is maintained using the badger database.

func NewTicketPool

func NewTicketPool(dataDir, dbSubDir string) (tp *TicketPool, err error)

NewTicketPool constructs a TicketPool by opening the persistent diff db, loading all known diffs, initializing the TicketPool values.

func (*TicketPool) AdvanceToTip

func (tp *TicketPool) AdvanceToTip() (int64, error)

AdvanceToTip advances the pool map by applying all stored diffs. Note that the cursor will stop just beyond the last element of the diffs slice. It will not be possible to advance further, only retreat.

func (*TicketPool) Append

func (tp *TicketPool) Append(diff *PoolDiff, height int64) error

Append grows the diffs slice with the specified diff, and stores it in the on-disk DB. The height of the diff is used to check that it builds on the chain tip, and as a primary key in the DB.

func (*TicketPool) AppendAndAdvancePool

func (tp *TicketPool) AppendAndAdvancePool(diff *PoolDiff, height int64) error

AppendAndAdvancePool functions like Append, except that after growing the diffs slice and storing the diff in DB, the ticket pool is advanced.

func (*TicketPool) Close

func (tp *TicketPool) Close() error

Close closes the persistent diff DB.

func (*TicketPool) CurrentPool

func (tp *TicketPool) CurrentPool() ([]chainhash.Hash, int64)

CurrentPool gets the ticket hashes from the live ticket pool, and the current cursor (the height corresponding to the current pool). NOTE that the order of the ticket hashes is random as they are extracted from a the pool map with a range statement.

func (*TicketPool) CurrentPoolSize

func (tp *TicketPool) CurrentPoolSize() int

CurrentPoolSize returns the number of tickets stored in the current pool map.

func (*TicketPool) Cursor

func (tp *TicketPool) Cursor() int64

Cursor returns the current cursor, the location of the next unapplied diff.

func (*TicketPool) Pool

func (tp *TicketPool) Pool(height int64) ([]chainhash.Hash, error)

Pool attempts to get the tickets in the live pool at the specified height. It will advance/retreat the cursor as needed to reach the desired height, and then extract the tickets from the resulting pool map.

func (*TicketPool) Tip

func (tp *TicketPool) Tip() int64

Tip returns the current length of the diffs slice.

func (*TicketPool) Trim

func (tp *TicketPool) Trim() (int64, PoolDiff)

Trim removes the end diff and decrements the tip height. If the cursor would fall beyond the end of the diffs, the removed diffs are applied in reverse.

Jump to

Keyboard shortcuts

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