observer

package
v0.0.0-...-2da98db Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: MIT Imports: 33 Imported by: 0

Documentation

Overview

Package observer implements the Bitcoin chain observer

Index

Constants

View Source
const (

	// RegnetStartBlock is the hardcoded start block for regnet
	RegnetStartBlock = 100

	// BigValueSats contains the threshold to determine a big value in Bitcoin represents 2 BTC
	BigValueSats = 200000000

	// BigValueConfirmationCount represents the number of confirmation necessary for bigger values: 6 confirmations
	BigValueConfirmationCount = 6
)

Variables

This section is empty.

Functions

func GetSenderAddressByVin

func GetSenderAddressByVin(rpcClient interfaces.BTCRPCClient, vin btcjson.Vin, net *chaincfg.Params) (string, error)

GetSenderAddressByVin get the sender address from the previous transaction TODO(revamp): move in upper package to separate file (e.g., rpc.go)

Types

type BTCBlockNHeader

type BTCBlockNHeader struct {
	Header *wire.BlockHeader
	Block  *btcjson.GetBlockVerboseTxResult
}

BTCBlockNHeader contains bitcoin block and the header

type BTCInboundEvent

type BTCInboundEvent struct {
	// FromAddress is the first input address
	FromAddress string

	// ToAddress is the TSS address
	ToAddress string

	// Value is the amount of BTC
	Value float64

	MemoBytes   []byte
	BlockNumber uint64
	TxHash      string
}

BTCInboundEvent represents an incoming transaction event TODO(revamp): Move to inbound

func FilterAndParseIncomingTx

func FilterAndParseIncomingTx(
	rpcClient interfaces.BTCRPCClient,
	txs []btcjson.TxRawResult,
	blockNumber uint64,
	tssAddress string,
	logger zerolog.Logger,
	netParams *chaincfg.Params,
	depositorFee float64,
) ([]*BTCInboundEvent, error)

FilterAndParseIncomingTx given txs list returned by the "getblock 2" RPC command, return the txs that are relevant to us relevant tx must have the following vouts as the first two vouts: vout0: p2wpkh to the TSS address (targetAddress) vout1: OP_RETURN memo, base64 encoded

func GetBtcEvent

func GetBtcEvent(
	rpcClient interfaces.BTCRPCClient,
	tx btcjson.TxRawResult,
	tssAddress string,
	blockNumber uint64,
	logger zerolog.Logger,
	netParams *chaincfg.Params,
	depositorFee float64,
) (*BTCInboundEvent, error)

GetBtcEvent either returns a valid BTCInboundEvent or nil Note: the caller should retry the tx on error (e.g., GetSenderAddressByVin failed) TODO(revamp): simplify this function

type Logger

type Logger struct {
	// base.Logger contains a list of base observer loggers
	base.ObserverLogger

	// UTXOs is the logger for UTXOs management
	UTXOs zerolog.Logger
}

Logger contains list of loggers used by Bitcoin chain observer

type Observer

type Observer struct {
	// base.Observer implements the base chain observer
	base.Observer
	// contains filtered or unexported fields
}

Observer is the Bitcoin chain observer

func NewObserver

func NewObserver(
	chain chains.Chain,
	btcClient interfaces.BTCRPCClient,
	chainParams observertypes.ChainParams,
	zetacoreClient interfaces.ZetacoreClient,
	tss interfaces.TSSSigner,
	dbpath string,
	logger base.Logger,
	ts *metrics.TelemetryServer,
) (*Observer, error)

NewObserver returns a new Bitcoin chain observer

func (*Observer) BtcClient

func (ob *Observer) BtcClient() interfaces.BTCRPCClient

BtcClient returns the btc client

func (*Observer) CheckReceiptForBtcTxHash

func (ob *Observer) CheckReceiptForBtcTxHash(ctx context.Context, txHash string, vote bool) (string, error)

CheckReceiptForBtcTxHash checks the receipt for a btc tx hash

func (*Observer) ConfirmationsThreshold

func (ob *Observer) ConfirmationsThreshold(amount *big.Int) int64

ConfirmationsThreshold returns number of required Bitcoin confirmations depending on sent BTC amount.

func (*Observer) DoesInboundContainsRestrictedAddress

func (ob *Observer) DoesInboundContainsRestrictedAddress(inTx *BTCInboundEvent) bool

DoesInboundContainsRestrictedAddress returns true if the inbound contains restricted addresses TODO(revamp): move all compliance related functions in a specific file

func (*Observer) FetchUTXOs

func (ob *Observer) FetchUTXOs(ctx context.Context) error

FetchUTXOs fetches TSS-owned UTXOs from the Bitcoin node TODO(revamp): move to UTXO file

func (*Observer) GetBlockByNumberCached

func (ob *Observer) GetBlockByNumberCached(blockNumber int64) (*BTCBlockNHeader, error)

GetBlockByNumberCached gets cached block (and header) by block number

func (*Observer) GetChainParams

func (ob *Observer) GetChainParams() observertypes.ChainParams

GetChainParams returns the chain params for the observer Note: chain params is accessed concurrently

func (*Observer) GetInboundVoteMessageFromBtcEvent

func (ob *Observer) GetInboundVoteMessageFromBtcEvent(inbound *BTCInboundEvent) *crosschaintypes.MsgVoteInbound

GetInboundVoteMessageFromBtcEvent converts a BTCInboundEvent to a MsgVoteInbound to enable voting on the inbound on zetacore

func (*Observer) GetPendingNonce

func (ob *Observer) GetPendingNonce() uint64

GetPendingNonce returns the artificial pending nonce Note: pending nonce is accessed concurrently

func (*Observer) GetTxID

func (ob *Observer) GetTxID(nonce uint64) string

GetTxID returns a unique id for outbound tx

func (*Observer) IsOutboundProcessed

func (ob *Observer) IsOutboundProcessed(
	ctx context.Context,
	cctx *crosschaintypes.CrossChainTx,
	logger zerolog.Logger,
) (bool, bool, error)

IsOutboundProcessed returns isIncluded(or inMempool), isConfirmed, Error TODO(revamp): rename as it vote the outbound and doesn't only check if outbound is processed

func (*Observer) LoadBroadcastedTxMap

func (ob *Observer) LoadBroadcastedTxMap() error

LoadBroadcastedTxMap loads broadcasted transactions from the database

func (*Observer) LoadDB

func (ob *Observer) LoadDB(dbPath string) error

LoadDB open sql database and load data into Bitcoin observer

func (*Observer) LoadLastBlockScanned

func (ob *Observer) LoadLastBlockScanned() error

LoadLastBlockScanned loads the last scanned block from the database

func (*Observer) ObserveInbound

func (ob *Observer) ObserveInbound(ctx context.Context) error

ObserveInbound observes the Bitcoin chain for inbounds and post votes to zetacore TODO(revamp): simplify this function into smaller functions

func (*Observer) PostGasPrice

func (ob *Observer) PostGasPrice(ctx context.Context) error

PostGasPrice posts gas price to zetacore TODO(revamp): move to gas price file

func (*Observer) ProcessInboundTrackers

func (ob *Observer) ProcessInboundTrackers(ctx context.Context) error

ProcessInboundTrackers processes inbound trackers TODO(revamp): move inbound tracker logic in a specific file

func (*Observer) SaveBroadcastedTx

func (ob *Observer) SaveBroadcastedTx(txHash string, nonce uint64)

SaveBroadcastedTx saves successfully broadcasted transaction TODO(revamp): move to db file

func (*Observer) SelectUTXOs

func (ob *Observer) SelectUTXOs(
	ctx context.Context,
	amount float64,
	utxosToSpend uint16,
	nonce uint64,
	consolidateRank uint16,
	test bool,
) ([]btcjson.ListUnspentResult, float64, uint16, float64, error)

SelectUTXOs selects a sublist of utxos to be used as inputs.

Parameters:

  • amount: The desired minimum total value of the selected UTXOs.
  • utxos2Spend: The maximum number of UTXOs to spend.
  • nonce: The nonce of the outbound transaction.
  • consolidateRank: The rank below which UTXOs will be consolidated.
  • test: true for unit test only.

Returns:

  • a sublist (includes previous nonce-mark) of UTXOs or an error if the qualifying sublist cannot be found.
  • the total value of the selected UTXOs.
  • the number of consolidated UTXOs.
  • the total value of the consolidated UTXOs.

TODO(revamp): move to utxo file

func (*Observer) SetChainParams

func (ob *Observer) SetChainParams(params observertypes.ChainParams)

SetChainParams sets the chain params for the observer Note: chain params is accessed concurrently

func (*Observer) Start

func (ob *Observer) Start(ctx context.Context)

Start starts the Go routine processes to observe the Bitcoin chain

func (*Observer) WatchGasPrice

func (ob *Observer) WatchGasPrice(ctx context.Context) error

WatchGasPrice watches Bitcoin chain for gas rate and post to zetacore TODO(revamp): move ticker related functions to a specific file TODO(revamp): move inner logic in a separate function

func (*Observer) WatchInbound

func (ob *Observer) WatchInbound(ctx context.Context) error

WatchInbound watches Bitcoin chain for inbounds on a ticker It starts a ticker and run ObserveInbound TODO(revamp): move all ticker related methods in the same file

func (*Observer) WatchInboundTracker

func (ob *Observer) WatchInboundTracker(ctx context.Context) error

WatchInboundTracker watches zetacore for bitcoin inbound trackers TODO(revamp): move all ticker related methods in the same file

func (*Observer) WatchOutbound

func (ob *Observer) WatchOutbound(ctx context.Context) error

WatchOutbound watches Bitcoin chain for outgoing txs status TODO(revamp): move ticker functions to a specific file TODO(revamp): move into a separate package

func (*Observer) WatchRPCStatus

func (ob *Observer) WatchRPCStatus(_ context.Context) error

WatchRPCStatus watches the RPC status of the Bitcoin chain TODO(revamp): move ticker related functions to a specific file TODO(revamp): move inner logic in a separate function

func (*Observer) WatchUTXOs

func (ob *Observer) WatchUTXOs(ctx context.Context) error

WatchUTXOs watches bitcoin chain for UTXOs owned by the TSS address TODO(revamp): move ticker related functions to a specific file

func (*Observer) WithBtcClient

func (ob *Observer) WithBtcClient(client interfaces.BTCRPCClient)

WithBtcClient attaches a new btc client to the observer

Jump to

Keyboard shortcuts

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