本文整理汇总了Golang中github.com/decred/dcrwallet/walletdb.View函数的典型用法代码示例。如果您正苦于以下问题:Golang View函数的具体用法?Golang View怎么用?Golang View使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了View函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: accountIsUsed
// accountIsUsed checks if an account has ever been used by scanning the
// first acctSeekWidth many addresses for usage.
func (w *Wallet) accountIsUsed(ctx *discoveryContext, account uint32) (bool, error) {
for branch := uint32(0); branch < 2; branch++ {
for i := uint32(0); i < acctSeekWidth; i++ {
var addr dcrutil.Address
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
var err error
addr, err = ctx.deriveAddr(addrmgrNs, i, account, branch)
return err
})
// Skip erroneous keys, which happen rarely.
if e, ok := err.(waddrmgr.ManagerError); ok && e.Err == hdkeychain.ErrInvalidChild {
continue
}
if err != nil {
return false, err
}
exists, err := ctx.chainClient.ExistsAddress(addr)
if err != nil {
return false, err
}
if exists {
return true, nil
}
}
}
return false, nil
}
示例2: RescanFromHeight
// RescanFromHeight is an alternative to Rescan that takes a block height
// instead of a hash. See Rescan for more details.
func (w *Wallet) RescanFromHeight(chainClient *chain.RPCClient, startHeight int32) <-chan error {
errc := make(chan error)
go func() (err error) {
defer func() {
select {
case errc <- err:
default:
if err != nil {
log.Errorf("Rescan failed: %v", err)
}
close(errc)
}
}()
var startHash chainhash.Hash
err = walletdb.View(w.db, func(tx walletdb.ReadTx) error {
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
var err error
startHash, err = w.TxStore.GetMainChainBlockHashForHeight(
txmgrNs, startHeight)
return err
})
if err != nil {
return err
}
return w.rescan(chainClient, &startHash, startHeight, nil, nil)
}()
return errc
}
示例3: Rescan
// Rescan starts a rescan of the wallet for all blocks on the main chain
// beginning at startHash.
//
// An error channel is returned for consumers of this API, but it is not
// required to be read. If the error can not be immediately written to the
// returned channel, the error will be logged and the channel will be closed.
func (w *Wallet) Rescan(chainClient *chain.RPCClient, startHash *chainhash.Hash) <-chan error {
errc := make(chan error)
go func() (err error) {
defer func() {
select {
case errc <- err:
default:
if err != nil {
log.Errorf("Rescan failed: %v", err)
}
close(errc)
}
}()
var startHeight int32
err = walletdb.View(w.db, func(tx walletdb.ReadTx) error {
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
header, err := w.TxStore.GetSerializedBlockHeader(txmgrNs, startHash)
if err != nil {
return err
}
startHeight = wtxmgr.ExtractBlockHeaderHeight(header)
return nil
})
if err != nil {
return err
}
return w.rescan(chainClient, startHash, startHeight, nil, nil)
}()
return errc
}
示例4: TicketHashesForVotingAddress
// TicketHashesForVotingAddress returns the hashes of all tickets with voting
// rights delegated to votingAddr. This function does not return the hashes of
// pruned tickets.
func (w *Wallet) TicketHashesForVotingAddress(votingAddr dcrutil.Address) ([]chainhash.Hash, error) {
var ticketHashes []chainhash.Hash
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
stakemgrNs := tx.ReadBucket(wstakemgrNamespaceKey)
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
var err error
ticketHashes, err = w.StakeMgr.DumpSStxHashesForAddress(
stakemgrNs, votingAddr)
if err != nil {
return err
}
// Exclude the hash if the transaction is not saved too. No
// promises of hash order are given (and at time of writing,
// they are copies of iterators of a Go map in wstakemgr) so
// when one must be removed, replace it with the last and
// decrease the len.
for i := 0; i < len(ticketHashes); {
if w.TxStore.ExistsTx(txmgrNs, &ticketHashes[i]) {
i++
continue
}
ticketHashes[i] = ticketHashes[len(ticketHashes)-1]
ticketHashes = ticketHashes[:len(ticketHashes)-1]
}
return nil
})
return ticketHashes, err
}
示例5: FetchP2SHMultiSigOutput
// FetchP2SHMultiSigOutput fetches information regarding a wallet's P2SH
// multi-signature output.
func (w *Wallet) FetchP2SHMultiSigOutput(outPoint *wire.OutPoint) (*P2SHMultiSigOutput, error) {
var (
mso *wtxmgr.MultisigOut
redeemScript []byte
)
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
var err error
mso, err = w.TxStore.GetMultisigOutput(txmgrNs, outPoint)
if err != nil {
return err
}
redeemScript, err = w.TxStore.GetTxScript(txmgrNs, mso.ScriptHash[:])
if err != nil {
return err
}
// returns nil, nil when it successfully found no script. That error is
// only used to return early when the database is closed.
if redeemScript == nil {
return errors.New("script not found")
}
return nil
})
if err != nil {
return nil, err
}
p2shAddr, err := dcrutil.NewAddressScriptHashFromHash(
mso.ScriptHash[:], w.chainParams)
if err != nil {
return nil, err
}
multiSigOutput := P2SHMultiSigOutput{
OutPoint: *mso.OutPoint,
OutputAmount: mso.Amount,
ContainingBlock: BlockIdentity{
Hash: mso.BlockHash,
Height: int32(mso.BlockHeight),
},
P2SHAddress: p2shAddr,
RedeemScript: redeemScript,
M: mso.M,
N: mso.N,
Redeemer: nil,
}
if mso.Spent {
multiSigOutput.Redeemer = &OutputRedeemer{
TxHash: mso.SpentBy,
InputIndex: mso.SpentByIndex,
}
}
return &multiSigOutput, nil
}
示例6: bisectLastAddrIndex
// bisectLastAddrIndex is a helper function for search through addresses.
func (w *Wallet) bisectLastAddrIndex(hi, low int, account uint32, branch uint32) int {
chainClient, err := w.requireChainClient()
if err != nil {
return 0
}
// Logarithmically scan address indexes to find the last used
// address index. Each time the algorithm receives an end point,
// scans a chunk of addresses at the end point, and if no
// addresses are found, divides the address index by two and
// repeats until it finds the last used index.
offset := low
for i := hi - low - 1; i > 0; i /= 2 {
if i+offset+int(addrSeekWidth) < waddrmgr.MaxAddressesPerAccount {
start := i + offset
end := i + offset + int(addrSeekWidth)
exists, idx, err := w.scanAddressRange(account, branch, start, end,
chainClient)
// Skip erroneous keys, which happen rarely. Don't skip
// other errors.
if err == errDerivation {
continue
}
if err != nil {
log.Warnf("unexpected error encountered during bisection "+
"scan of account %v, branch %v: %s", account, branch,
err.Error())
return 0
}
if exists {
return idx
}
} else {
var addr dcrutil.Address
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
var err error
addr, err = w.Manager.AddressDerivedFromDbAcct(addrmgrNs,
uint32(i+offset), account, branch)
return err
})
// Skip erroneous keys, which happen rarely.
if err != nil {
continue
}
exists, err := chainClient.ExistsAddress(addr)
if err != nil {
return 0
}
if exists {
return i + offset
}
}
}
return 0
}
示例7: AddressPoolIndex
// AddressPoolIndex returns the next to use address index for the passed
// branch of the passed account.
func (w *Wallet) AddressPoolIndex(account uint32, branch uint32) (uint32, error) {
var index uint32
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
waddrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
var err error
index, err = w.addressPoolIndex(waddrmgrNs, account, branch)
return err
})
return index, err
}
示例8: TxDetails
// TxDetails calls wtxmgr.Store.TxDetails under a single database view transaction.
func (u unstableAPI) TxDetails(txHash *chainhash.Hash) (*wtxmgr.TxDetails, error) {
var details *wtxmgr.TxDetails
err := walletdb.View(u.w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
var err error
details, err = u.w.TxStore.TxDetails(txmgrNs, txHash)
return err
})
return details, err
}
示例9: FetchAllRedeemScripts
// FetchAllRedeemScripts returns all P2SH redeem scripts saved by the wallet.
func (w *Wallet) FetchAllRedeemScripts() ([][]byte, error) {
var redeemScripts [][]byte
err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
var err error
redeemScripts, err = w.TxStore.StoredTxScripts(txmgrNs)
return err
})
return redeemScripts, err
}
示例10: UnspentMultisigCreditsForAddress
// UnspentMultisigCreditsForAddress calls
// wtxmgr.Store.UnspentMultisigCreditsForAddress under a single database view
// transaction.
func (u unstableAPI) UnspentMultisigCreditsForAddress(p2shAddr *dcrutil.AddressScriptHash) ([]*wtxmgr.MultisigCredit, error) {
var multisigCredits []*wtxmgr.MultisigCredit
err := walletdb.View(u.w.db, func(tx walletdb.ReadTx) error {
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
var err error
multisigCredits, err = u.w.TxStore.UnspentMultisigCreditsForAddress(
txmgrNs, p2shAddr)
return err
})
return multisigCredits, err
}
示例11: scanAddressRange
// scanAddressRange scans backwards from end to start many addresses in the
// account branch, and return the first index that is found on the blockchain.
// If the address doesn't exist, false is returned as the first argument.
func (w *Wallet) scanAddressRange(account uint32, branch uint32, start int,
end int, chainClient *chain.RPCClient) (bool, int, error) {
var addresses []dcrutil.Address
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
var err error
addresses, err = w.Manager.AddressesDerivedFromDbAcct(addrmgrNs,
uint32(start), uint32(end+1), account, branch)
if err != nil {
return errDerivation
}
return nil
})
if err != nil {
return false, 0, err
}
// Whether or not the addresses exist is encoded as a binary
// bitset.
exists, err := chainClient.ExistsAddresses(addresses)
if err != nil {
return false, 0, err
}
existsB, err := hex.DecodeString(exists)
if err != nil {
return false, 0, err
}
set := bitset.Bytes(existsB)
// Prevent a panic when an empty message is passed as a response.
if len(set) == 0 {
return false, 0, nil
}
// Scan backwards and return if we find an address exists.
idx := end
itr := len(addresses) - 1
for idx >= start {
// If the address exists in the mempool or blockchain according
// to the bit set returned, return this index.
if set.Get(itr) {
return true, idx, nil
}
itr--
idx--
}
return false, 0, nil
}
示例12: bisectLastAddrIndex
// bisectLastAddrIndex is a helper function for search through addresses.
func (w *Wallet) bisectLastAddrIndex(ctx *discoveryContext, hi, low uint32,
account uint32, branch uint32) (uint32, error) {
// Logarithmically scan address indexes to find the last used
// address index. Each time the algorithm receives an end point,
// scans a chunk of addresses at the end point, and if no
// addresses are found, divides the address index by two and
// repeats until it finds the last used index.
offset := low
for i := hi - low - 1; i > 0; i /= 2 {
if i+offset+addrSeekWidth < waddrmgr.MaxAddressesPerAccount {
start := i + offset
end := i + offset + addrSeekWidth
exists, idx, err := w.scanAddressRange(ctx, account, branch, start, end)
// Skip erroneous keys, which happen rarely. Don't skip
// other errors.
if e, ok := err.(waddrmgr.ManagerError); ok && e.Err == hdkeychain.ErrInvalidChild {
continue
}
if err != nil {
return 0, err
}
if exists {
return idx, nil
}
} else {
var addr dcrutil.Address
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
var err error
addr, err = w.Manager.AddressDerivedFromDbAcct(addrmgrNs,
i+offset, account, branch)
return err
})
// Skip erroneous keys, which happen rarely.
if err != nil {
continue
}
exists, err := ctx.chainClient.ExistsAddress(addr)
if err != nil {
return 0, err
}
if exists {
return i + offset, nil
}
}
}
return 0, nil
}
示例13: VoteBitsForTicket
// VoteBitsForTicket returns the per-ticket vote bits, if any are saved, falling
// back to the wallet's default vote bits when missing.
func (w *Wallet) VoteBitsForTicket(ticketHash *chainhash.Hash) (stake.VoteBits, error) {
var voteBits stake.VoteBits
var ok bool
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
stakemgrNs := tx.ReadBucket(wstakemgrNamespaceKey)
var err error
ok, voteBits, err = w.StakeMgr.SStxVoteBits(stakemgrNs, ticketHash)
return err
})
if !ok {
voteBits = w.VoteBits
}
return voteBits, err
}
示例14: StakePoolUserInfo
// StakePoolUserInfo returns the stake pool user information for a user
// identified by their P2SH voting address.
func (w *Wallet) StakePoolUserInfo(userAddress dcrutil.Address) (*wstakemgr.StakePoolUser, error) {
switch userAddress.(type) {
case *dcrutil.AddressPubKeyHash: // ok
case *dcrutil.AddressScriptHash: // ok
default:
return nil, errors.New("stake pool user address must be P2PKH or P2SH")
}
var user *wstakemgr.StakePoolUser
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
stakemgrNs := tx.ReadBucket(wstakemgrNamespaceKey)
var err error
user, err = w.StakeMgr.StakePoolUserInfo(stakemgrNs, userAddress)
return err
})
return user, err
}
示例15: RescanProgressFromHeight
// RescanProgressFromHeight rescans for relevant transactions in all blocks in
// the main chain starting at startHeight. Progress notifications and any
// errors are sent to the channel p. This function blocks until the rescan
// completes or ends in an error. p is closed before returning.
func (w *Wallet) RescanProgressFromHeight(chainClient *chain.RPCClient, startHeight int32, p chan<- RescanProgress, cancel <-chan struct{}) {
defer close(p)
var startHash chainhash.Hash
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
var err error
startHash, err = w.TxStore.GetMainChainBlockHashForHeight(
txmgrNs, startHeight)
return err
})
if err != nil {
p <- RescanProgress{Err: err}
return
}
err = w.rescan(chainClient, &startHash, startHeight, p, cancel)
if err != nil {
p <- RescanProgress{Err: err}
}
}