stakedb

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2018 License: ISC Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// DefaultStakeDbName is the default database name
	DefaultStakeDbName = "ffldb_stake"
)

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 UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info.

Types

type ChainMonitor

type ChainMonitor struct {
	ConnectingLock chan struct{}
	DoneConnecting chan struct{}

	// reorg handling
	sync.Mutex
	// 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) BlockConnectedSync added in v0.7.0

func (p *ChainMonitor) BlockConnectedSync(hash *chainhash.Hash)

BlockConnectedSync is the synchronous (blocking call) handler for the newly connected block given by the hash.

func (*ChainMonitor) ReorgHandler

func (p *ChainMonitor) ReorgHandler()

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

type PoolDiff added in v1.3.0

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.

type PoolDiffDBItem added in v1.3.0

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

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

type PoolInfoCache added in v0.7.0

type PoolInfoCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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

func NewPoolInfoCache added in v0.7.0

func NewPoolInfoCache() *PoolInfoCache

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

func (*PoolInfoCache) Get added in v0.7.0

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

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

type ReorgData

type ReorgData struct {
	OldChainHead   chainhash.Hash
	OldChainHeight int32
	NewChainHead   chainhash.Hash
	NewChainHeight int32
}

ReorgData contains the information from a reoranization notification

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 NewStakeDatabase

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

NewStakeDatabase creates a StakeDatabase instance, opening or creating a new ffldb-backed stake database, and loads all live tickets into a cache.

func (*StakeDatabase) Close added in v1.3.0

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() error

DisconnectBlock attempts to disconnect the current best block from the stake DB and updates the best stake node.

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) NewChainMonitor

func (db *StakeDatabase) NewChainMonitor(quit chan struct{}, wg *sync.WaitGroup,
	blockChan chan *chainhash.Hash, reorgChan chan *ReorgData) *ChainMonitor

NewChainMonitor creates a new ChainMonitor

func (*StakeDatabase) Open

func (db *StakeDatabase) Open() error

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

func (*StakeDatabase) PoolAtHash added in v1.3.0

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

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

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

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

type TicketPool added in v1.3.0

type TicketPool struct {
	*sync.RWMutex
	// 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 storm wrapper for boltdb.

func NewTicketPool added in v1.3.0

func NewTicketPool(dbFile string) (*TicketPool, error)

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

func (*TicketPool) AdvanceToTip added in v1.3.0

func (tp *TicketPool) AdvanceToTip() 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 added in v1.3.0

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

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

func (tp *TicketPool) Close() error

Close closes the persistent diff DB.

func (*TicketPool) CurrentPool added in v1.3.0

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

func (tp *TicketPool) CurrentPoolSize() int

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

func (*TicketPool) Cursor added in v1.3.0

func (tp *TicketPool) Cursor() int64

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

func (*TicketPool) Pool added in v1.3.0

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

func (tp *TicketPool) Tip() int64

Tip returns the current length of the diffs slice.

func (*TicketPool) Trim added in v1.3.0

func (tp *TicketPool) Trim() int64

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