Documentation
¶
Overview ¶
Package observer implements the Bitcoin chain observer
Index ¶
- Constants
- func DeductDepositorFee(deposited, depositorFee float64) (float64, error)
- func GetSenderAddressByVin(ctx context.Context, rpc RPC, vin btcjson.Vin, net *chaincfg.Params) (string, error)
- func ParseScriptFromWitness(witness []string, logger zerolog.Logger) []byte
- func ValidateStandardMemo(memoStd memo.InboundMemo, chainID int64) error
- type BTCBlockNHeader
- type BTCInboundEvent
- func FilterAndParseIncomingTx(ctx context.Context, rpc RPC, txs []btcjson.TxRawResult, blockNumber uint64, ...) ([]*BTCInboundEvent, error)
- func GetBtcEvent(ctx context.Context, rpc RPC, tx btcjson.TxRawResult, tssAddress string, ...) (*BTCInboundEvent, error)
- func GetBtcEventWithWitness(ctx context.Context, rpc RPC, tx btcjson.TxRawResult, tssAddress string, ...) (*BTCInboundEvent, error)
- type Logger
- type Observer
- func (ob *Observer) CheckRPCStatus(ctx context.Context) error
- func (ob *Observer) CheckReceiptForBtcTxHash(ctx context.Context, txHash string, vote bool) (string, error)
- func (ob *Observer) ConfirmationsThreshold(amount *big.Int) int64
- func (ob *Observer) FetchUTXOs(ctx context.Context) error
- func (ob *Observer) GetBlockByNumberCached(ctx context.Context, blockNumber int64) (*BTCBlockNHeader, error)
- func (ob *Observer) GetInboundVoteFromBtcEvent(event *BTCInboundEvent) *types.MsgVoteInbound
- func (ob *Observer) GetPendingNonce() uint64
- func (ob *Observer) IsEventProcessable(event BTCInboundEvent) bool
- func (ob *Observer) LoadBroadcastedTxMap() error
- func (ob *Observer) LoadLastBlockScanned(ctx context.Context) error
- func (ob *Observer) NewInboundVoteFromLegacyMemo(event *BTCInboundEvent, amountSats *big.Int) *crosschaintypes.MsgVoteInbound
- func (ob *Observer) NewInboundVoteFromStdMemo(event *BTCInboundEvent, amountSats *big.Int) *crosschaintypes.MsgVoteInbound
- func (ob *Observer) ObserveInbound(ctx context.Context) error
- func (ob *Observer) PostGasPrice(ctx context.Context) error
- func (ob *Observer) ProcessInboundTrackers(ctx context.Context) error
- func (ob *Observer) ProcessOutboundTrackers(ctx context.Context) error
- func (ob *Observer) SaveBroadcastedTx(txHash string, nonce uint64)
- func (ob *Observer) SelectUTXOs(ctx context.Context, amount float64, utxosToSpend uint16, nonce uint64, ...) ([]btcjson.ListUnspentResult, float64, uint16, float64, error)
- func (ob *Observer) VoteOutboundIfConfirmed(ctx context.Context, cctx *crosschaintypes.CrossChainTx) (bool, error)
- type RPC
Constants ¶
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 ¶
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 ¶
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 (*Observer) CheckRPCStatus ¶
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 ¶
ConfirmationsThreshold returns number of required Bitcoin confirmations depending on sent BTC amount.
func (*Observer) FetchUTXOs ¶
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 ¶
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 ¶
LoadBroadcastedTxMap loads broadcasted transactions from the database
func (*Observer) LoadLastBlockScanned ¶
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 ¶
ObserveInbound observes the Bitcoin chain for inbounds and post votes to zetacore TODO(revamp): simplify this function into smaller functions
func (*Observer) PostGasPrice ¶
PostGasPrice posts gas price to zetacore TODO(revamp): move to gas price file
func (*Observer) ProcessInboundTrackers ¶
ProcessInboundTrackers processes inbound trackers TODO(revamp): move inbound tracker logic in a specific file
func (*Observer) ProcessOutboundTrackers ¶
func (*Observer) SaveBroadcastedTx ¶
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) }