observer

package
v0.0.0-...-290478e Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 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 DeductDepositorFee

func DeductDepositorFee(deposited, depositorFee float64) (float64, error)

DeductDepositorFee returns the inbound amount after deducting the depositor fee. returns an error if the deposited amount is lower than the depositor fee.

func GetSenderAddressByVin

func GetSenderAddressByVin(
	ctx context.Context,
	rpc RPC,
	vin btcjson.Vin,
	net *chaincfg.Params,
) (string, error)

GetSenderAddressByVin get the sender address from the transaction input (vin)

func ParseScriptFromWitness

func ParseScriptFromWitness(witness []string, logger zerolog.Logger) []byte

ParseScriptFromWitness attempts to parse the script from the witness data. Ideally it should be handled by bitcoin library, however, it's not found in existing library version. Replace this with actual library implementation if libraries are updated.

func ValidateStandardMemo

func ValidateStandardMemo(memoStd memo.InboundMemo, chainID int64) error

ValidateStandardMemo validates the standard memo in Bitcoin context

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 ZEVM receiver address
	ToAddress string

	// Value is the amount of BTC
	Value float64

	// DepositorFee is the deposit fee
	DepositorFee float64

	// MemoBytes is the memo of inbound
	MemoBytes []byte

	// MemoStd is the standard inbound memo if it can be decoded
	MemoStd *memo.InboundMemo

	// BlockNumber is the block number of the inbound
	BlockNumber uint64

	// TxHash is the hash of the inbound
	TxHash string

	// Status is the status of the inbound event
	Status crosschaintypes.InboundStatus
}

BTCInboundEvent represents an incoming transaction event

func FilterAndParseIncomingTx

func FilterAndParseIncomingTx(
	ctx context.Context,
	rpc RPC,
	txs []btcjson.TxRawResult,
	blockNumber uint64,
	tssAddress string,
	logger zerolog.Logger,
	netParams *chaincfg.Params,
) ([]*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(
	ctx context.Context,
	rpc RPC,
	tx btcjson.TxRawResult,
	tssAddress string,
	blockNumber uint64,
	logger zerolog.Logger,
	netParams *chaincfg.Params,
	feeCalculator common.DepositorFeeCalculator,
) (*BTCInboundEvent, error)

GetBtcEvent returns a valid BTCInboundEvent or nil it uses witness data to extract the sender address

func GetBtcEventWithWitness

func GetBtcEventWithWitness(
	ctx context.Context,
	rpc RPC,
	tx btcjson.TxRawResult,
	tssAddress string,
	blockNumber uint64,
	logger zerolog.Logger,
	netParams *chaincfg.Params,
	feeCalculator common.DepositorFeeCalculator,
) (*BTCInboundEvent, error)

GetBtcEventWithWitness either returns a valid BTCInboundEvent or nil. This method supports data with more than 80 bytes by scanning the witness for possible presence of a tapscript. It will first prioritize OP_RETURN over tapscript.

func (*BTCInboundEvent) Category

func (event *BTCInboundEvent) Category() clienttypes.InboundCategory

Category returns the category of the inbound event

func (*BTCInboundEvent) DecodeMemoBytes

func (event *BTCInboundEvent) DecodeMemoBytes(chainID int64) error

DecodeMemoBytes decodes the contained memo bytes as either standard or legacy memo It updates the event object with the decoded data

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 New

func New(chain chains.Chain, baseObserver *base.Observer, rpc RPC) (*Observer, error)

New BTC Observer constructor.

func (*Observer) CheckRPCStatus

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

CheckRPCStatus checks the RPC status of the Bitcoin chain

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) 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(ctx context.Context, blockNumber int64) (*BTCBlockNHeader, error)

GetBlockByNumberCached gets cached block (and header) by block number

func (*Observer) GetInboundVoteFromBtcEvent

func (ob *Observer) GetInboundVoteFromBtcEvent(event *BTCInboundEvent) *types.MsgVoteInbound

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

Returns:

  • a valid MsgVoteInbound message, or
  • nil if no valid message can be created for whatever reasons: invalid data, not processable, invalid amount, etc.

func (*Observer) GetPendingNonce

func (ob *Observer) GetPendingNonce() uint64

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

func (*Observer) IsEventProcessable

func (ob *Observer) IsEventProcessable(event BTCInboundEvent) bool

IsEventProcessable checks if the inbound event is processable

func (*Observer) LoadBroadcastedTxMap

func (ob *Observer) LoadBroadcastedTxMap() error

LoadBroadcastedTxMap loads broadcasted transactions from the database

func (*Observer) LoadLastBlockScanned

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

LoadLastBlockScanned loads the last scanned block from the database

func (*Observer) NewInboundVoteFromLegacyMemo

func (ob *Observer) NewInboundVoteFromLegacyMemo(
	event *BTCInboundEvent,
	amountSats *big.Int,
) *crosschaintypes.MsgVoteInbound

NewInboundVoteFromLegacyMemo creates a MsgVoteInbound message for inbound that uses legacy memo

func (*Observer) NewInboundVoteFromStdMemo

func (ob *Observer) NewInboundVoteFromStdMemo(
	event *BTCInboundEvent,
	amountSats *big.Int,
) *crosschaintypes.MsgVoteInbound

NewInboundVoteFromStdMemo creates a MsgVoteInbound message for inbound that uses standard memo TODO: upgrade to ProtocolContractVersion_V2 and enable more options https://github.com/zeta-chain/node/issues/2711

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) ProcessOutboundTrackers

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

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) VoteOutboundIfConfirmed

func (ob *Observer) VoteOutboundIfConfirmed(
	ctx context.Context,
	cctx *crosschaintypes.CrossChainTx,
) (bool, error)

VoteOutboundIfConfirmed checks outbound status and returns (continueKeysign, error)

type RPC

type RPC interface {
	Healthcheck(ctx context.Context, tssAddress btcutil.Address) (time.Time, error)

	GetBlockCount(ctx context.Context) (int64, error)
	GetBlockHash(ctx context.Context, blockHeight int64) (*hash.Hash, error)
	GetBlockHeader(ctx context.Context, hash *hash.Hash) (*wire.BlockHeader, error)
	GetBlockVerbose(ctx context.Context, hash *hash.Hash) (*btcjson.GetBlockVerboseTxResult, error)

	GetRawTransaction(ctx context.Context, hash *hash.Hash) (*btcutil.Tx, error)
	GetRawTransactionVerbose(ctx context.Context, hash *hash.Hash) (*btcjson.TxRawResult, error)
	GetRawTransactionResult(
		ctx context.Context,
		hash *hash.Hash,
		res *btcjson.GetTransactionResult,
	) (btcjson.TxRawResult, error)

	GetTransactionFeeAndRate(ctx context.Context, tx *btcjson.TxRawResult) (int64, int64, error)

	EstimateSmartFee(
		ctx context.Context,
		confTarget int64,
		mode *btcjson.EstimateSmartFeeMode,
	) (*btcjson.EstimateSmartFeeResult, error)

	ListUnspentMinMaxAddresses(
		ctx context.Context,
		minConf, maxConf int,
		addresses []btcutil.Address,
	) ([]btcjson.ListUnspentResult, error)

	GetBlockHeightByStr(ctx context.Context, blockHash string) (int64, error)
	GetTransactionByStr(ctx context.Context, hash string) (*hash.Hash, *btcjson.GetTransactionResult, error)
}

Jump to

Keyboard shortcuts

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