本文整理汇总了Golang中github.com/decred/dcrd/database.Tx类的典型用法代码示例。如果您正苦于以下问题:Golang Tx类的具体用法?Golang Tx怎么用?Golang Tx使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Tx类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: dbFetchTxIndexEntry
// dbFetchTxIndexEntry uses an existing database transaction to fetch the block
// region for the provided transaction hash from the transaction index. When
// there is no entry for the provided hash, nil will be returned for the both
// the region and the error.
func dbFetchTxIndexEntry(dbTx database.Tx, txHash chainhash.Hash) (*database.BlockRegion, error) {
// Load the record from the database and return now if it doesn't exist.
txIndex := dbTx.Metadata().Bucket(txIndexKey)
serializedData := txIndex.Get(txHash[:])
if len(serializedData) == 0 {
return nil, nil
}
// Ensure the serialized data has enough bytes to properly deserialize.
if len(serializedData) < 12 {
return nil, database.Error{
ErrorCode: database.ErrCorruption,
Description: fmt.Sprintf("corrupt transaction index "+
"entry for %s", txHash),
}
}
// Load the block hash associated with the block ID.
hash, err := dbFetchBlockHashBySerializedID(dbTx, serializedData[0:4])
if err != nil {
return nil, database.Error{
ErrorCode: database.ErrCorruption,
Description: fmt.Sprintf("corrupt transaction index "+
"entry for %s: %v", txHash, err),
}
}
// Deserialize the final entry.
region := database.BlockRegion{Hash: &chainhash.Hash{}}
copy(region.Hash[:], hash[:])
region.Offset = byteOrder.Uint32(serializedData[4:8])
region.Len = byteOrder.Uint32(serializedData[8:12])
return ®ion, nil
}
示例2: maybeCreateIndexes
// maybeCreateIndexes determines if each of the enabled indexes have already
// been created and creates them if not.
func (m *Manager) maybeCreateIndexes(dbTx database.Tx) error {
indexesBucket := dbTx.Metadata().Bucket(indexTipsBucketName)
for _, indexer := range m.enabledIndexes {
// Nothing to do if the index tip already exists.
idxKey := indexer.Key()
if indexesBucket.Get(idxKey) != nil {
continue
}
// The tip for the index does not exist, so create it and
// invoke the create callback for the index so it can perform
// any one-time initialization it requires.
if err := indexer.Create(dbTx); err != nil {
return err
}
// Set the tip for the index to values which represent an
// uninitialized index (the genesis block hack and height).
genesisBlockHash := m.params.GenesisBlock.BlockSha()
err := dbPutIndexerTip(dbTx, idxKey, &genesisBlockHash, 0)
if err != nil {
return err
}
}
return nil
}
示例3: DbPutBestState
// DbPutBestState uses an existing database transaction to update the best chain
// state with the given parameters.
func DbPutBestState(dbTx database.Tx, bcs BestChainState) error {
// Serialize the current best chain state.
serializedData := serializeBestChainState(bcs)
// Store the current best chain state into the database.
return dbTx.Metadata().Put(dbnamespace.StakeChainStateKeyName, serializedData)
}
示例4: dbFetchTx
// dbFetchTx looks up the passed transaction hash in the transaction index and
// loads it from the database.
func dbFetchTx(dbTx database.Tx, hash chainhash.Hash) (*wire.MsgTx, error) {
// Look up the location of the transaction.
blockRegion, err := dbFetchTxIndexEntry(dbTx, hash)
if err != nil {
return nil, err
}
if blockRegion == nil {
return nil, fmt.Errorf("transaction %v not found in the txindex", hash)
}
// Load the raw transaction bytes from the database.
txBytes, err := dbTx.FetchBlockRegion(blockRegion)
if err != nil {
return nil, err
}
// Deserialize the transaction.
var msgTx wire.MsgTx
err = msgTx.Deserialize(bytes.NewReader(txBytes))
if err != nil {
return nil, err
}
return &msgTx, nil
}
示例5: DbLoadAllTickets
// DbLoadAllTickets loads all the live tickets from the database into a treap.
func DbLoadAllTickets(dbTx database.Tx, ticketBucket []byte) (*tickettreap.Immutable, error) {
meta := dbTx.Metadata()
bucket := meta.Bucket(ticketBucket)
treap := tickettreap.NewImmutable()
err := bucket.ForEach(func(k []byte, v []byte) error {
if len(v) < 5 {
return ticketDBError(ErrLoadAllTickets, fmt.Sprintf("short "+
"read for ticket key %x when loading tickets", k))
}
h, err := chainhash.NewHash(k)
if err != nil {
return err
}
treapKey := tickettreap.Key(*h)
missed, revoked, spent, expired := undoBitFlagsFromByte(v[4])
treapValue := &tickettreap.Value{
Height: dbnamespace.ByteOrder.Uint32(v[0:4]),
Missed: missed,
Revoked: revoked,
Spent: spent,
Expired: expired,
}
treap = treap.Put(treapKey, treapValue)
return nil
})
if err != nil {
return nil, ticketDBError(ErrLoadAllTickets, fmt.Sprintf("failed to "+
"load all tickets for the bucket %s", string(ticketBucket)))
}
return treap, nil
}
示例6: TxRegionsForAddress
// TxRegionsForAddress returns a slice of block regions which identify each
// transaction that involves the passed address according to the specified
// number to skip, number requested, and whether or not the results should be
// reversed. It also returns the number actually skipped since it could be less
// in the case where there are not enough entries.
//
// NOTE: These results only include transactions confirmed in blocks. See the
// UnconfirmedTxnsForAddress method for obtaining unconfirmed transactions
// that involve a given address.
//
// This function is safe for concurrent access.
func (idx *AddrIndex) TxRegionsForAddress(dbTx database.Tx, addr dcrutil.Address, numToSkip, numRequested uint32, reverse bool) ([]database.BlockRegion, uint32, error) {
addrKey, err := addrToKey(addr, idx.chainParams)
if err != nil {
return nil, 0, err
}
var regions []database.BlockRegion
var skipped uint32
err = idx.db.View(func(dbTx database.Tx) error {
// Create closure to lookup the block hash given the ID using
// the database transaction.
fetchBlockHash := func(id []byte) (chainhash.Hash, error) {
// Deserialize and populate the result.
return dbFetchBlockHashBySerializedID(dbTx, id)
}
var err error
addrIdxBucket := dbTx.Metadata().Bucket(addrIndexKey)
regions, skipped, err = dbFetchAddrIndexEntries(addrIdxBucket,
addrKey, numToSkip, numRequested, reverse,
fetchBlockHash)
return err
})
return regions, skipped, err
}
示例7: DbPutDatabaseInfo
// DbPutDatabaseInfo uses an existing database transaction to store the database
// information.
func DbPutDatabaseInfo(dbTx database.Tx, dbi *DatabaseInfo) error {
meta := dbTx.Metadata()
subsidyBucket := meta.Bucket(dbnamespace.StakeDbInfoBucketName)
val := serializeDatabaseInfo(dbi)
// Store the current database info into the database.
return subsidyBucket.Put(dbnamespace.StakeDbInfoBucketName, val[:])
}
示例8: dbPutIndexerTip
// dbPutIndexerTip uses an existing database transaction to update or add the
// current tip for the given index to the provided values.
func dbPutIndexerTip(dbTx database.Tx, idxKey []byte, hash *chainhash.Hash, height uint32) error {
serialized := make([]byte, chainhash.HashSize+4)
copy(serialized, hash[:])
byteOrder.PutUint32(serialized[chainhash.HashSize:], height)
indexesBucket := dbTx.Metadata().Bucket(indexTipsBucketName)
return indexesBucket.Put(idxKey, serialized)
}
示例9: DbDropNewTickets
// DbDropNewTickets drops new tickets for a mainchain block data at some height.
func DbDropNewTickets(dbTx database.Tx, height uint32) error {
meta := dbTx.Metadata()
bucket := meta.Bucket(dbnamespace.TicketsInBlockBucketName)
k := make([]byte, 4)
dbnamespace.ByteOrder.PutUint32(k, height)
return bucket.Delete(k)
}
示例10: DbPutBlockUndoData
// DbPutBlockUndoData inserts block undo data into the database for a given height.
func DbPutBlockUndoData(dbTx database.Tx, height uint32, utds []UndoTicketData) error {
meta := dbTx.Metadata()
bucket := meta.Bucket(dbnamespace.StakeBlockUndoDataBucketName)
k := make([]byte, 4)
dbnamespace.ByteOrder.PutUint32(k, height)
v := serializeBlockUndoData(utds)
return bucket.Put(k[:], v[:])
}
示例11: dbFetchBlockIDByHash
// dbFetchBlockIDByHash uses an existing database transaction to retrieve the
// block id for the provided hash from the index.
func dbFetchBlockIDByHash(dbTx database.Tx, hash chainhash.Hash) (uint32, error) {
hashIndex := dbTx.Metadata().Bucket(idByHashIndexBucketName)
serializedID := hashIndex.Get(hash[:])
if serializedID == nil {
return 0, errNoBlockIDEntry
}
return byteOrder.Uint32(serializedID), nil
}
示例12: DbPutNewTickets
// DbPutNewTickets inserts new tickets for a mainchain block data into the
// database.
func DbPutNewTickets(dbTx database.Tx, height uint32, ths TicketHashes) error {
meta := dbTx.Metadata()
bucket := meta.Bucket(dbnamespace.TicketsInBlockBucketName)
k := make([]byte, 4)
dbnamespace.ByteOrder.PutUint32(k, height)
v := serializeTicketHashes(ths)
return bucket.Put(k[:], v[:])
}
示例13: dbRemoveTxIndexEntry
// dbRemoveTxIndexEntry uses an existing database transaction to remove the most
// recent transaction index entry for the given hash.
func dbRemoveTxIndexEntry(dbTx database.Tx, txHash chainhash.Hash) error {
txIndex := dbTx.Metadata().Bucket(txIndexKey)
serializedData := txIndex.Get(txHash[:])
if len(serializedData) == 0 {
return fmt.Errorf("can't remove non-existent transaction %s "+
"from the transaction index", txHash)
}
return txIndex.Delete(txHash[:])
}
示例14: DbFetchBestState
// DbFetchBestState uses an existing database transaction to fetch the best chain
// state.
func DbFetchBestState(dbTx database.Tx) (BestChainState, error) {
meta := dbTx.Metadata()
v := meta.Get(dbnamespace.StakeChainStateKeyName)
if v == nil {
return BestChainState{}, ticketDBError(ErrMissingKey,
"missing key for chain state data")
}
return deserializeBestChainState(v)
}
示例15: DbCreate
// DbCreate initializes all the buckets required for the database and stores
// the current database version information.
func DbCreate(dbTx database.Tx) error {
meta := dbTx.Metadata()
// Create the bucket that houses information about the database's
// creation and version.
_, err := meta.CreateBucket(dbnamespace.StakeDbInfoBucketName)
if err != nil {
return err
}
dbInfo := &DatabaseInfo{
Version: currentDatabaseVersion,
Date: time.Now(),
UpgradeStarted: false,
}
err = DbPutDatabaseInfo(dbTx, dbInfo)
if err != nil {
return err
}
// Create the bucket that houses the live tickets of the best node.
_, err = meta.CreateBucket(dbnamespace.LiveTicketsBucketName)
if err != nil {
return err
}
// Create the bucket that houses the missed tickets of the best node.
_, err = meta.CreateBucket(dbnamespace.MissedTicketsBucketName)
if err != nil {
return err
}
// Create the bucket that houses the revoked tickets of the best node.
_, err = meta.CreateBucket(dbnamespace.RevokedTicketsBucketName)
if err != nil {
return err
}
// Create the bucket that houses block undo data for stake states on
// the main chain.
_, err = meta.CreateBucket(dbnamespace.StakeBlockUndoDataBucketName)
if err != nil {
return err
}
// Create the bucket that houses the tickets that were added with
// this block into the main chain.
_, err = meta.CreateBucket(dbnamespace.TicketsInBlockBucketName)
if err != nil {
return err
}
return nil
}