rpcutils

package
v8.0.0-...-e9c7312 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: ISC Imports: 17 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAncestorAtGenesis      = errors.New("no ancestor: at genesis")
	ErrAncestorMaxChainLength = errors.New("no ancestor: max chain length reached")
)

Functions

func BuildBlockHeaderVerbose

func BuildBlockHeaderVerbose(header *wire.BlockHeader, params *chaincfg.Params,
	currentHeight int64, nextHash ...string) *chainjson.GetBlockHeaderVerboseResult

BuildBlockHeaderVerbose creates a *chainjson.GetBlockHeaderVerboseResult from an input *wire.BlockHeader and current best block height, which is used to compute confirmations. The next block hash may optionally be provided.

func CommonAncestor

func CommonAncestor(client BlockFetcher, hashA, hashB chainhash.Hash) (*chainhash.Hash, []chainhash.Hash, []chainhash.Hash, error)

CommonAncestor attempts to determine the common ancestor block for two chains specified by the hash of the chain tip block. The full chains from the tips back to but not including the common ancestor are also returned. The first element in the chain slices is the lowest block following the common ancestor, while the last element is the chain tip. The common ancestor will never by one of the chain tips. Thus, if one of the chain tips is on the other chain, that block will be shared between the two chains, and the common ancestor will be the previous block. However, the intended use of this function is to find a common ancestor for two chains with no common blocks.

func ConnectNodeRPC

func ConnectNodeRPC(host, user, pass, cert string, disableTLS, disableReconnect bool,
	ntfnHandlers ...*rpcclient.NotificationHandlers) (*rpcclient.Client, semver.Semver, error)

ConnectNodeRPC attempts to create a new websocket connection to a dcrd node, with the given credentials and optional notification handlers.

func DisableLog

func DisableLog()

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

func GetBlock

func GetBlock(ind int64, client BlockFetcher) (*dcrutil.Block, *chainhash.Hash, error)

GetBlock gets a block at the given height from a chain server.

func GetBlockByHash

func GetBlockByHash(blockhash *chainhash.Hash, client BlockFetcher) (*dcrutil.Block, error)

GetBlockByHash gets the block with the given hash from a chain server.

func GetBlockHeaderVerbose

func GetBlockHeaderVerbose(client BlockFetcher, idx int64) *chainjson.GetBlockHeaderVerboseResult

GetBlockHeaderVerbose creates a *chainjson.GetBlockHeaderVerboseResult for the block at height idx via an RPC connection to a chain server.

func GetBlockHeaderVerboseByString

func GetBlockHeaderVerboseByString(client BlockFetcher, hash string) *chainjson.GetBlockHeaderVerboseResult

GetBlockHeaderVerboseByString creates a *chainjson.GetBlockHeaderVerboseResult for the block specified by hash via an RPC connection to a chain server.

func GetBlockVerbose

func GetBlockVerbose(client VerboseBlockGetter, idx int64, verboseTx bool) *chainjson.GetBlockVerboseResult

GetBlockVerbose creates a *chainjson.GetBlockVerboseResult for the block index specified by idx via an RPC connection to a chain server.

func GetBlockVerboseByHash

func GetBlockVerboseByHash(client VerboseBlockGetter, hash string, verboseTx bool) *chainjson.GetBlockVerboseResult

GetBlockVerboseByHash creates a *chainjson.GetBlockVerboseResult for the specified block hash via an RPC connection to a chain server.

func GetChainWork

func GetChainWork(client BlockFetcher, hash *chainhash.Hash) (string, error)

GetChainWork fetches the chainjson.BlockHeaderVerbose and returns only the ChainWork field as a string.

func OrphanedTipLength

func OrphanedTipLength(ctx context.Context, client BlockHashGetter,
	tipHeight int64, hashFunc func(int64) (string, error)) (int64, error)

OrphanedTipLength finds a common ancestor by iterating block heights backwards until a common block hash is found. Unlike CommonAncestor, an orphaned DB tip whose corresponding block is not known to dcrd will not cause an error. The number of blocks that have been orphaned is returned. Realistically, this should rarely be anything but 0 or 1, but no limits are placed here on the number of blocks checked.

func SideChainFull

func SideChainFull(client BlockFetcher, tipHash string) ([]string, error)

SideChainFull gets all of the blocks in the side chain with the specified tip block hash. The first block in the slice is the lowest height block in the side chain, and its previous block is the main/side common ancestor, which is not included in the slice since it is main chain. The last block in the slice is thus the side chain tip.

func SideChains

func SideChains(client ChainTipsGetter) ([]chainjson.GetChainTipsResult, error)

SideChains gets a slice of known side chain tips. This corresponds to the results of the getchaintips node RPC where the block tip "status" is either "valid-headers" or "valid-fork".

func UnconfirmedTxnsForAddress

func UnconfirmedTxnsForAddress(client MempoolTxGetter, address string,
	params *chaincfg.Params) (*txhelpers.AddressOutpoints, int64, error)

UnconfirmedTxnsForAddress returns the chainhash.Hash of all transactions in mempool that (1) pay to the given address, or (2) spend a previous outpoint that paid to the address.

func UseLogger

func UseLogger(logger slog.Logger)

UseLogger uses a specified Logger to output package logging info.

Types

type AsyncTxClient

type AsyncTxClient struct {
	*rpcclient.Client
}

AsyncTxClient is a blueprint for creating a type that satisfies both txhelpers.VerboseTransactionPromiseGetter and txhelpers.TransactionPromiseGetter from an rpcclient.Client.

func NewAsyncTxClient

func NewAsyncTxClient(c *rpcclient.Client) *AsyncTxClient

NewAsyncTxClient creates an AsyncTxClient from a rpcclient.Client.

func (*AsyncTxClient) GetRawTransactionPromise

func (cl *AsyncTxClient) GetRawTransactionPromise(ctx context.Context, txHash *chainhash.Hash) txhelpers.TxReceiver

GetRawTransactionPromise gives txhelpers.TransactionPromiseGetter.

func (*AsyncTxClient) GetRawTransactionVerbosePromise

func (cl *AsyncTxClient) GetRawTransactionVerbosePromise(ctx context.Context, txHash *chainhash.Hash) txhelpers.VerboseTxReceiver

GetRawTransactionVerbosePromise gives txhelpers.VerboseTransactionPromiseGetter.

type BlockFetcher

type BlockFetcher interface {
	GetBestBlock(ctx context.Context) (*chainhash.Hash, int64, error)
	GetBlock(ctx context.Context, blockHash *chainhash.Hash) (*wire.MsgBlock, error)
	GetBlockHash(ctx context.Context, blockHeight int64) (*chainhash.Hash, error)
	GetBlockHeaderVerbose(ctx context.Context, hash *chainhash.Hash) (*chainjson.GetBlockHeaderVerboseResult, error)
}

BlockFetcher implements a few basic block data retrieval functions.

type BlockGetter

type BlockGetter interface {
	GetBestBlock(ctx context.Context) (*chainhash.Hash, int64, error)
	GetBlockHash(ctx context.Context, blockHeight int64) (*chainhash.Hash, error)
	GetBlock(ctx context.Context, blockHash *chainhash.Hash) (*wire.MsgBlock, error)
}

type BlockHashGetter

type BlockHashGetter interface {
	GetBlockHash(context.Context, int64) (*chainhash.Hash, error)
}

BlockHashGetter is an interface implementing GetBlockHash to retrieve a block hash from a height.

type BlockPrefetchClient

type BlockPrefetchClient struct {

	// The next block data is protected by a Mutex so the fetcher goroutine and
	// the exported methods may safely access the data concurrently.
	sync.Mutex
	// contains filtered or unexported fields
}

BlockPrefetchClient uses a BlockFetcher to prefetch the next block after a block request. It implements the BlockFetcher for retrieving block data.

func NewBlockPrefetchClient

func NewBlockPrefetchClient(f BlockFetcher) *BlockPrefetchClient

NewBlockPrefetchClient constructs a new BlockPrefetchClient configured to prefetch the next block after a request using the given BlockFetcher (e.g. an *rpcclient.Client).

func (*BlockPrefetchClient) GetBestBlock

func (p *BlockPrefetchClient) GetBestBlock(ctx context.Context) (*chainhash.Hash, int64, error)

GetBestBlock is a passthrough to the client. It does not retarget the prefetch range since it does not request the actual block, just the hash and height of the best block.

func (*BlockPrefetchClient) GetBlock

func (p *BlockPrefetchClient) GetBlock(ctx context.Context, hash *chainhash.Hash) (*wire.MsgBlock, error)

GetBlock retrieves the wire.MsgBlock for the block with the specified hash. See GetBlockData for details on how this interacts with the prefetcher.

func (*BlockPrefetchClient) GetBlockData

GetBlockData attempts to get the specified block and retargets the prefetcher with the next block's hash. If the block was not already fetched, it is retrieved immediately and stored following retargeting.

func (*BlockPrefetchClient) GetBlockHash

func (p *BlockPrefetchClient) GetBlockHash(ctx context.Context, blockHeight int64) (*chainhash.Hash, error)

GetBlockHash is a passthrough to the client.

func (*BlockPrefetchClient) GetBlockHeaderVerbose

GetBlockHeaderVerbose retrieves the chainjson.GetBlockHeaderVerboseResult for the block with the specified hash. See GetBlockData for details on how this interacts with the prefetcher

func (*BlockPrefetchClient) HaveBlockHash

func (p *BlockPrefetchClient) HaveBlockHash(hash chainhash.Hash) bool

HaveBlockHash checks if the current or prefetched next block is for a block with the specified hash.

func (*BlockPrefetchClient) HaveBlockHeight

func (p *BlockPrefetchClient) HaveBlockHeight(height uint32) bool

HaveBlockHeight checks if the current or prefetched next block is for a block with the specified height. Use HaveBlockHash to be sure it is the desired block.

func (*BlockPrefetchClient) Hits

func (p *BlockPrefetchClient) Hits() uint64

Hits safely returns the number of prefetch hits.

func (*BlockPrefetchClient) Misses

func (p *BlockPrefetchClient) Misses() uint64

Misses safely returns the number of prefetch misses.

func (*BlockPrefetchClient) RetrieveAndStoreNext

func (p *BlockPrefetchClient) RetrieveAndStoreNext(nextHash *chainhash.Hash)

RetrieveAndStoreNext retrieves the next block specified by the Hash, if it is not already the stored next block, and stores the block data. The existing "next" becomes "current".

func (*BlockPrefetchClient) Stop

func (p *BlockPrefetchClient) Stop()

Stop shuts down the fetcher goroutine. The BlockPrefetchClient may not be used after this.

type ChainTipsGetter

type ChainTipsGetter interface {
	GetChainTips(ctx context.Context) ([]chainjson.GetChainTipsResult, error)
}

type MempoolAddressChecker

type MempoolAddressChecker interface {
	UnconfirmedTxnsForAddress(address string) (*txhelpers.AddressOutpoints, int64, error)
}

MempoolAddressChecker is an interface implementing UnconfirmedTxnsForAddress. NewMempoolAddressChecker may be used to create a MempoolAddressChecker from an rpcclient.Client.

func NewMempoolAddressChecker

func NewMempoolAddressChecker(client *rpcclient.Client, params *chaincfg.Params) MempoolAddressChecker

NewMempoolAddressChecker creates a new MempoolAddressChecker from an RPC client for the given network.

type MempoolGetter

type MempoolGetter interface {
	GetRawMempoolVerbose(ctx context.Context, txType chainjson.GetRawMempoolTxTypeCmd) (map[string]chainjson.GetRawMempoolVerboseResult, error)
}

type MempoolTxGetter

MempoolTxGetter must be satisfied for UnconfirmedTxnsForAddress.

type VerboseBlockGetter

type VerboseBlockGetter interface {
	GetBlockHash(ctx context.Context, blockHeight int64) (*chainhash.Hash, error)
	GetBlockVerbose(ctx context.Context, blockHash *chainhash.Hash, verboseTx bool) (*chainjson.GetBlockVerboseResult, error)
	GetBlockHeaderVerbose(ctx context.Context, hash *chainhash.Hash) (*chainjson.GetBlockHeaderVerboseResult, error)
}

Jump to

Keyboard shortcuts

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