database

package
v0.0.0-...-163fc3c Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const AddrIndexKeySize = ripemd160.Size + 1

AddrIndexKeySize is the number of bytes used by keys into the BlockAddrIndex. tianwei.cai: type + addr

Variables

View Source
var (
	ErrAddrIndexDoesNotNeedInit = errors.New("address index has been initialized")
	ErrAddrIndexDoesNotExist    = errors.New("address index hasn't been built or is an older version")
	ErrAddrIndexVersionNotFound = errors.New("address index version not found")
	ErrAddrIndexInvalidVersion  = errors.New("address index version is not allowed")
	ErrUnsupportedAddressType   = errors.New("address type is not supported by the address-index")
	ErrPrevShaMissing           = errors.New("previous sha missing from database")
	ErrTxShaMissing             = errors.New("requested transaction does not exist")
	ErrBlockShaMissing          = errors.New("requested block does not exist")
	ErrInvalidBlockHeight       = errors.New("invalid block height buffer")
	ErrDuplicateSha             = errors.New("duplicate insert attempted")
	ErrDbDoesNotExist           = errors.New("non-existent database")
	ErrDbUnknownType            = errors.New("non-existent database type")
	ErrNotImplemented           = errors.New("method has not yet been implemented")
	ErrInvalidBlockStorageMeta  = errors.New("invalid block storage meta")
	ErrInvalidAddrIndexMeta     = errors.New("invalid addr index meta")
	ErrDeleteNonNewestBlock     = errors.New("delete block that is not newest")
)

Errors that the various database functions may return.

Functions

func RegisterDriver

func RegisterDriver(driver Driver) error

RegisterDriver adds a backend database driver to available interfaces. ErrDbTypeRegistered will be returned if the database type for the driver has already been registered.

func SupportedDrivers

func SupportedDrivers() []string

SupportedDrivers returns a slice of strings that represent the database drivers that have been registered and are therefore supported.

Types

type AddrIndexData

type AddrIndexData struct {
	TxIndex             TxAddrIndex
	BindingTxIndex      BindingTxAddrIndex
	BindingTxSpentIndex BindingTxSpentAddrIndex
}

type AddrIndexOutPoint

type AddrIndexOutPoint struct {
	TxLoc *wire.TxLoc
	Index uint32
}

type BLHeight

type BLHeight struct {
	BitLength   int    // Bit Length Of Public Key
	BlockHeight uint64 // Block Height
}

type BindingTxAddrIndex

type BindingTxAddrIndex map[[ripemd160.Size]byte][]*AddrIndexOutPoint

type BindingTxReply

type BindingTxReply struct {
	Height     uint64
	TxSha      *wire.Hash
	IsCoinbase bool
	Value      int64
	Index      uint32
}

type BindingTxSpent

type BindingTxSpent struct {
	SpentTxLoc     *wire.TxLoc
	BTxBlockHeight uint64
	BTxLoc         *wire.TxLoc
	BTxIndex       uint32
}

BindingTxSpent BindingTx --> BTx

type BindingTxSpentAddrIndex

type BindingTxSpentAddrIndex map[[ripemd160.Size]byte][]*BindingTxSpent

type BlockAddrIndex

type BlockAddrIndex map[[AddrIndexKeySize]byte][]*AddrIndexOutPoint

BlockAddrIndex represents the indexing structure for addresses. It maps a hash160 to a list of transaction locations within a block that either pays to or spends from the passed UTXO for the hash160.

type BlockLoc

type BlockLoc struct {
	Height uint64    // Block Height
	Hash   wire.Hash // Block Hash
	File   uint32    // Block File Number
	Offset uint64    // Block File Offset
	Length uint64    // Block Length
}

BlockLoc

type DB

type DB interface {
	// Close cleanly shuts down the database and syncs all data.
	Close() (err error)

	// RollbackClose discards the recent database changes to the previously
	// saved data at last Sync and closes the database.
	RollbackClose() (err error)
	Rollback()

	// Sync verifies that the database is coherent on disk and no
	// outstanding transactions are in flight.
	Sync() (err error)

	// Commit commits batches in a single transaction
	Commit(blockSha wire.Hash) error

	// InitByGenesisBlock init database by setting genesis block
	InitByGenesisBlock(block *chainutil.Block) (err error)

	// SubmitBlock inserts raw block and transaction data from a block
	// into the database.  The first block inserted into the database
	// will be treated as the genesis block.  Every subsequent block insert
	// requires the referenced parent block to already exist.
	SubmitBlock(block *chainutil.Block) (err error)

	// DeleteBlock will remove any blocks from the database after
	// the given block.  It terminates any existing transaction and performs
	// its operations in an atomic transaction which is committed before
	// the function returns.
	DeleteBlock(blockSha *wire.Hash) (err error)

	// ExistsBlockSha  returns whether or not the given block hash is present in
	// the database.
	ExistsBlockSha(blockSha *wire.Hash) (exists bool, err error)

	// FetchBlockBySha returns a Block.  The implementation may
	// cache the underlying data if desired.
	FetchBlockBySha(blockSha *wire.Hash) (block *chainutil.Block, err error)

	// FetchBlockHeightBySha returns the block height for the given hash.
	FetchBlockHeightBySha(blockSha *wire.Hash) (height uint64, err error)
	// FetchBlockHeaderBySha returns a wire.BlockHeader for the given
	// sha.  The implementation may cache the underlying data if desired.
	FetchBlockHeaderBySha(blockSha *wire.Hash) (blockHeader *wire.BlockHeader, err error)
	// FetchBlockShaByHeight returns a block hash based on its height in the
	// block chain.
	FetchBlockShaByHeight(height uint64) (blockSha *wire.Hash, err error)
	// FetchBlockLocByHeight fetch block location info by block height
	FetchBlockLocByHeight(height uint64) (*BlockLoc, error)
	// ExistsTxSha returns whether or not the given tx hash is present in
	// the database
	ExistsTxSha(txSha *wire.Hash) (exists bool, err error)
	// FetchTxByLoc fetch tx location info by block height and tx offset
	FetchTxByLoc(blockHeight uint64, txOffset int, txLen int) (*wire.MsgTx, error)
	// FetchTxByFileLoc returns transactions saved in file, including
	// those revoked with chain reorganization, for file is in APPEND mode.
	FetchTxByFileLoc(blockLoc *BlockLoc, txLoc *wire.TxLoc) (*wire.MsgTx, error)
	// FetchTxBySha returns some data for the given transaction hash. The
	// implementation may cache the underlying data if desired.
	FetchTxBySha(txSha *wire.Hash) ([]*TxReply, error)
	// GetUnspentTxData GetTxData returns the block height, txOffset, txLen for the given transaction hash,
	// including unspent and fully spent transaction
	GetUnspentTxData(txSha *wire.Hash) (uint64, int, int, error)
	// IsTxOutSpent is tx out spent
	IsTxOutSpent(txSha *wire.Hash, index int) (bool, error)
	// FetchTxByShaList fetch unspent staking pool tx
	//FetchUnspentStakingPoolTx()([]*TxReply, error)
	// FetchTxByShaList returns a TxReply given an array of transaction
	// hashes.  The implementation may cache the underlying data if desired.
	// This differs from FetchUnSpentTxByShaList in that it will return
	// the most recent known Tx, if it is fully spent or not.
	//
	// NOTE: This function does not return an error directly since it MUST
	// return at least one TxReply instance for each requested
	// transaction.  Each TxReply instance then contains an Err field
	// which can be used to detect errors.
	FetchTxByShaList(txShaList []*wire.Hash) []*TxReply

	// FetchLastFullySpentTxBeforeHeight Returns database.ErrTxShaMissing if no transaction found
	FetchLastFullySpentTxBeforeHeight(txSha *wire.Hash, height uint64) (tx *wire.MsgTx, blockHeight uint64, blockSha *wire.Hash, err error)

	// FetchUnSpentTxByShaList returns a TxReply given an array of
	// transaction hashes.  The implementation may cache the underlying
	// data if desired. Fully spent transactions will not normally not
	// be returned in this operation.
	//
	// NOTE: This function does not return an error directly since it MUST
	// return at least one TxReply instance for each requested
	// transaction.  Each TxReply instance then contains an Err field
	// which can be used to detect errors.
	FetchUnSpentTxByShaList(txShaList []*wire.Hash) []*TxReply

	// FetchUnSpentStakingPoolTxOutByHeight fetch unspent staking pool tx out by block height
	FetchUnSpentStakingPoolTxOutByHeight(startHeight uint64, endHeight uint64) ([]*TxOutReply, error)

	// FetchUnexpiredStakingRank returns only currently unexpired staking rank at
	// target height. This function is for mining and validating block.
	FetchUnexpiredStakingRank(height uint64, onlyOnList bool) ([]Rank, error)
	FetchStakingStakingRewardInfo(height uint64) (*StakingRewardInfo, error)

	// FetchStakingAwardedRecordByTime fetch staking award record by time
	FetchStakingAwardedRecordByTime(queryTime uint64) ([]StakingAwardedRecord, error)

	// InsertGovernConfig insert govern config
	InsertGovernConfig(id uint16, height, activeHeight uint64, shadow bool, txSha *wire.Hash, data []byte) error

	// FetchStakingRank returns staking rank at any height. This
	// function may be slow.
	FetchStakingRank(height uint64, onlyOnList bool) ([]Rank, error)

	// FetchStakingTxMap fetch a map of all staking transactions in database
	FetchStakingTxMap() (StakingNodes, error)

	// FetchExpiredStakingTxListByHeight fetch expired staking tx by block height as start height
	FetchExpiredStakingTxListByHeight(height uint64) (StakingNodes, error)

	FetchHeightRange(startHeight, endHeight uint64) ([]wire.Hash, error)

	// NewestSha returns the hash and block height of the most recent (end)
	// block of the block chain.  It will return the zero hash, -1 for
	// the block height, and no error (nil) if there are not any blocks in
	// the database yet.
	NewestSha() (blockSha *wire.Hash, height uint64, err error)

	// FetchFaultPubKeyBySha returns the FaultPubKey by Hash of that PubKey.
	// Hash = DoubleHashB(PubKey.SerializeUnCompressed()).
	// It will return ErrNotFound if this PubKey is not banned.
	FetchFaultPubKeyBySha(sha *wire.Hash) (faultPubKey *wire.FaultPubKey, height uint64, err error)

	FetchAllFaultPubKeys() ([]*wire.FaultPubKey, []uint64, error)

	// FetchFaultPubKeyListByHeight returns the newly added FaultPubKey List
	// on given height. It will return ErrNotFound if this height has not
	// mined. And will return empty slice if there aren't any new banned Pks.
	FetchFaultPubKeyListByHeight(blockHeight uint64) ([]*wire.FaultPubKey, error)

	// ExistsFaultPubKey returns whether or not the given FaultPubKey hash is present in
	// the database.
	ExistsFaultPubKey(sha *wire.Hash) (bool, error)

	// FetchAllPunishment returns all faultPubKey stored in db, with random order.
	FetchAllPunishment() ([]*wire.FaultPubKey, error)

	// ExistsPunishment returns whether or not the given PoC PublicKey is present in
	// the database.
	ExistsPunishment(pubKey *pocec.PublicKey) (bool, error)

	// InsertPunishment insert a fpk into punishment storage instantly.
	InsertPunishment(faultPubKey *wire.FaultPubKey) error

	FetchMinedBlocks(pubKey *pocec.PublicKey) ([]uint64, error)

	// FetchAddrIndexTip returns the hash and block height of the most recent
	// block which has had its address index populated. It will return
	// ErrAddrIndexDoesNotExist along with a zero hash, and math.MaxUint64 if
	// it hasn't yet been built up.
	FetchAddrIndexTip() (sha *wire.Hash, height uint64, err error)

	SubmitAddrIndex(hash *wire.Hash, height uint64, addrIndexData *AddrIndexData) (err error)

	DeleteAddrIndex(hash *wire.Hash, height uint64) (err error)

	// FetchScriptHashRelatedTx  returns all relevant txHash mapped by block height
	FetchScriptHashRelatedTx(scriptHashes [][]byte, startBlock, stopBlock uint64) (map[uint64][]*wire.TxLoc, error)

	CheckScriptHashUsed(scriptHash []byte) (bool, error)

	FetchScriptHashRelatedBindingTx(scriptHash []byte, chainParams *config.Params) ([]*BindingTxReply, error)
	// ExportDbEntries For testing purpose
	ExportDbEntries() map[string][]byte

	IndexPubKeyBLHeight(rebuild bool) error

	// FetchGovernConfigData fetch govern config data in database format, height start block height
	FetchGovernConfigData(class uint16, height uint64, includeShadow bool) ([]*GovernConfigData, error)

	GetPubkeyBLHeightRecord(*pocec.PublicKey) ([]*BLHeight, error)
}

DB defines a generic interface that is used to request and insert data into the block chain. This interface is intended to be agnostic to actual mechanism used for backend data storage. The AddDBDriver function can be used to add a new backend data storage method.

func Create

func Create(dbType string, args ...interface{}) (DB, error)

Create initializes and opens a database for the specified type. The arguments are specific to the database type driver. See the documentation for the database driver for further details.

ErrDbUnknownType will be returned if the the database type is not registered.

func Open

func Open(dbType string, args ...interface{}) (DB, error)

Open opens an existing database for the specified type. The arguments are specific to the database type driver. See the documentation for the database driver for further details.

ErrDbUnknownType will be returned if the the database type is not registered.

type Driver

type Driver struct {
	// DbType is the identifier used to uniquely identify a specific
	// database driver.  There can be only one driver with the same name.
	DbType string

	// Create is the function that will be invoked with all user-specified
	// arguments to create the database.  This function must return
	// ErrDbExists if the database already exists.
	Create func(args ...interface{}) (DB, error)

	// Open is the function that will be invoked with all user-specified
	// arguments to open the database.  This function must return
	// ErrDbDoesNotExist if the database has not already been created.
	Open func(args ...interface{}) (DB, error)

	// UseLogger uses a specified Logger to output package logging info.
	UseLogger func(logger logging.Logger)
}

Driver defines a structure for backend drivers to use when they registered themselves as a backend which implements the DB interface.

type GovernConfigData

type GovernConfigData struct {
	Id           uint16     // 2 bytes
	BlockHeight  uint64     // 8 bytes
	TxSha        *wire.Hash // 32 bytes
	Shadow       bool       // 1 byte  0 enable | 1 shadow
	ActiveHeight uint64     // 8 bytes
	Data         []byte     // var
}

type Pair

type Pair struct {
	Key    [sha256.Size]byte
	Value  int64
	Weight *safetype.Uint128
}

type PairList

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

func (PairList) Len

func (pl PairList) Len() int

func (PairList) Less

func (pl PairList) Less(i, j int) bool

func (PairList) Swap

func (pl PairList) Swap(i, j int)

type Pairs

type Pairs []Pair

func SortMap

func SortMap(m map[[sha256.Size]byte][]StakingTxInfo, newestHeight uint64, isOnlyReward bool) (Pairs, error)

func (Pairs) Len

func (p Pairs) Len() int

func (Pairs) Less

func (p Pairs) Less(i, j int) bool

func (Pairs) Swap

func (p Pairs) Swap(i, j int)

type Rank

type Rank struct {
	Rank       int32
	Value      int64
	ScriptHash [sha256.Size]byte
	Weight     *safetype.Uint128
	StakingTx  []StakingTxInfo
}

type SenateWeight

type SenateWeight struct {
	Weight     uint64            // weight
	ScriptHash [sha256.Size]byte // The income address script of the equity
}

SenateWeight senate node info

type StakingAwardRecordAtTime

type StakingAwardRecordAtTime map[uint64]map[wire.Hash]StakingAwardedRecord

func (StakingAwardRecordAtTime) Put

func (sar StakingAwardRecordAtTime) Put(record StakingAwardedRecord) (success bool)

type StakingAwardedRecord

type StakingAwardedRecord struct {
	AwardedTime uint64    // award timestamp
	TxId        wire.Hash // award tx sha
}

StakingAwardedRecord Staking Awarded Record

type StakingNodes

type StakingNodes map[[sha256.Size]byte]StakingTxOutAtHeight

func (StakingNodes) Delete

func (nodes StakingNodes) Delete(key [sha256.Size]byte, blockHeight uint64, op wire.OutPoint) bool

func (StakingNodes) Get

func (StakingNodes) IsEmpty

func (nodes StakingNodes) IsEmpty(key [sha256.Size]byte) bool

type StakingRewardInfo

type StakingRewardInfo struct {
	CurrentTime     uint64
	LastRecord      StakingAwardedRecord
	RewardAddresses []Rank
}

StakingRewardInfo staking reward info

type StakingTxInfo

type StakingTxInfo struct {
	Value        uint64
	FrozenPeriod uint64
	BlockHeight  uint64
}

type StakingTxOutAtHeight

type StakingTxOutAtHeight map[uint64]map[wire.OutPoint]StakingTxInfo

func (StakingTxOutAtHeight) Put

func (sh StakingTxOutAtHeight) Put(op wire.OutPoint, stk StakingTxInfo) (success bool)

Put StakingTxOutAtHeight Returns false if already exists

type TxAddrIndex

type TxAddrIndex map[[sha256.Size]byte][]*wire.TxLoc

TxAddrIndex represents the indexing structure for txs

type TxOutReply

type TxOutReply struct {
	TxSha    *wire.Hash // Tx id
	BlockSha *wire.Hash // block sha
	Spent    bool       // spent
	Height   uint64     // block height
	Coinbase bool       // is coinbase
	Index    uint32     // tx out index
	Value    int64      // tx out value
	PkScript []byte     // pubKey script
}

TxOutReply only Tx Out info

type TxReply

type TxReply struct {
	TxSha    *wire.Hash
	Tx       *wire.MsgTx
	BlockSha *wire.Hash
	Height   uint64
	TxSpent  []bool
	Err      error
}

TxReply is used to return individual transaction information when data about multiple transactions is requested in a single call. see also TxData

type TxReplyStore

type TxReplyStore map[wire.Hash]*TxReply

type UtxoReply

type UtxoReply struct {
	TxSha    *wire.Hash
	Height   uint64
	Coinbase bool
	Index    uint32
	Value    chainutil.Amount
}

Directories

Path Synopsis
ldb
LSN (log sequence number) This file is only used to check data correctness for 1.1.0
LSN (log sequence number) This file is only used to check data correctness for 1.1.0
Package memdb implements an instance of the database package that uses memory for the block storage.
Package memdb implements an instance of the database package that uses memory for the block storage.

Jump to

Keyboard shortcuts

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