bitcoin

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 45 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BridgeDepositServiceName = "BitcoinBridgeDepositService"
	BatchDepositWaitTimeout  = 10 * time.Second
	DepositErrTimeout        = 20 * time.Second
	BatchDepositLimit        = 100
	WaitMinedTimeout         = 20 * time.Minute
	HandleDepositTimeout     = 1 * time.Second
	DepositRetry             = 10 // temp fix, Increase retry times
)
View Source
const (
	BridgeWithdrawServiceName = "BitcoinBridgeWithdrawService"
	WithdrawHandleTime        = 10
	WithdrawTXConfirmTime     = 60 * 5

	// P2SHSize 23 bytes.
	P2SHSize = 23
	// P2SHOutputSize 32 bytes
	//      - value: 8 bytes
	//      - var_int: 1 byte (pkscript_length)
	//      - pkscript (p2sh): 23 bytes
	P2SHOutputSize = 8 + 1 + P2SHSize
	// InputSize 41 bytes
	//	- PreviousOutPoint:
	//		- Hash: 32 bytes
	//		- Index: 4 bytes
	//	- OP_DATA: 1 byte (ScriptSigLength)
	//	- ScriptSig: 0 bytes
	//	- Witness <----	we use "Witness" instead of "ScriptSig" for
	// 			transaction validation, but "Witness" is stored
	// 			separately and weight for it size is smaller. So
	// 			we separate the calculation of ordinary data
	// 			from witness data.
	//	- Sequence: 4 bytes
	InputSize = 32 + 4 + 1 + 4
	// MultiSigSize 71 bytes
	//	- OP_2: 1 byte
	//	- OP_DATA: 1 byte (pubKeyAlice length)
	//	- pubKeyAlice: 33 bytes
	//	- OP_DATA: 1 byte (pubKeyBob length)
	//	- pubKeyBob: 33 bytes
	//	- OP_2: 1 byte
	//	- OP_CHECKMULTISIG: 1 byte
	MultiSigSize = 1 + 1 + 33 + 1 + 33 + 1 + 1
)
View Source
const (
	// tx type
	TxTypeTransfer = "transfer" // btc transfer
	TxTypeWithdraw = "withdraw" // btc withdraw
)
View Source
const (
	ServiceName = "BitcoinIndexerService"

	NewBlockWaitTimeout = 60 * time.Second

	IndexTxTimeout    = 100 * time.Millisecond
	IndexBlockTimeout = 2 * time.Second
)

Variables

View Source
var (
	ErrBridgeDepositTxHashExist                 = errors.New("non-repeatable processing")
	ErrBridgeDepositContractInsufficientBalance = errors.New("insufficient balance")
	ErrBridgeWaitMinedStatus                    = errors.New("tx wait mined status failed")
	ErrBridgeFromGasInsufficient                = errors.New("gas required exceeds allowanc")
	ErrAAAddressNotFound                        = errors.New("address not found")
	ErrOldNonceToHeight                         = errors.New("old nonce params to height")
)
View Source
var (
	ErrParsePkScript         = errors.New("parse pkscript err")
	ErrDecodeListenAddress   = errors.New("decode listen address err")
	ErrTargetConfirmations   = errors.New("target confirmation number was not reached")
	ErrParsePubKey           = errors.New("parse pubkey failed, not found pubkey or nonsupport ")
	ErrParsePkScriptNullData = errors.New("parse pkscript null data err")
)
View Source
var ErrServerStop = errors.New("server stop")

Functions

This section is empty.

Types

type B2ExplorerStatus

type B2ExplorerStatus struct {
	GasPrices struct {
		Fast    float64 `json:"fast"`
		Slow    float64 `json:"slow"`
		Average float64 `json:"average"`
	} `json:"gas_prices"`
}

type Bridge

type Bridge struct {
	EthRPCURL            string
	EthPrivKey           *ecdsa.PrivateKey
	ContractAddress      common.Address
	ABI                  string
	BaseGasPriceMultiple int64
	B2ExplorerURL        string

	// aa server
	AAPubKeyAPI string
	// contains filtered or unexported fields
}

Bridge bridge TODO: only L1 -> L2, More calls may be supported later

func NewBridge

func NewBridge(bridgeCfg config.BridgeConfig, abiFileDir string, log log.Logger, bitcoinParam *chaincfg.Params) (*Bridge, error)

NewBridge new bridge

func (*Bridge) ABIPack

func (b *Bridge) ABIPack(abiData string, method string, args ...interface{}) ([]byte, error)

ABIPack the given method name to conform the ABI. Method call's data

func (*Bridge) BitcoinAddressToEthAddress

func (b *Bridge) BitcoinAddressToEthAddress(bitcoinAddress b2types.BitcoinFrom) (string, error)

BitcoinAddressToEthAddress bitcoin address to eth address

func (*Bridge) Deposit

func (b *Bridge) Deposit(
	hash string,
	bitcoinAddress b2types.BitcoinFrom,
	amount int64,
	oldTx *types.Transaction,
	nonce uint64,
	resetNonce bool,
) (*types.Transaction, []byte, string, string, error)

Deposit to ethereum

func (*Bridge) EnableEoaTransfer

func (b *Bridge) EnableEoaTransfer() bool

func (*Bridge) FromAddress

func (b *Bridge) FromAddress() string

func (*Bridge) TransactionByHash

func (b *Bridge) TransactionByHash(hash string) (*types.Transaction, bool, error)

func (*Bridge) TransactionReceipt

func (b *Bridge) TransactionReceipt(hash string) (*types.Receipt, error)

func (*Bridge) Transfer

func (b *Bridge) Transfer(bitcoinAddress b2types.BitcoinFrom,
	amount int64,
	oldTx *types.Transaction,
	nonce uint64,
	resetNonce bool,
) (*types.Transaction, string, error)

Transfer to ethereum TODO: temp handle, future remove

func (*Bridge) WaitMined

func (b *Bridge) WaitMined(ctx context.Context, tx *types.Transaction, _ []byte) (*types.Receipt, error)

WaitMined wait tx mined

type BridgeDepositService

type BridgeDepositService struct {
	service.BaseService
	// contains filtered or unexported fields
}

BridgeDepositService l1->l2

func NewBridgeDepositService

func NewBridgeDepositService(
	bridge types.BITCOINBridge,
	btcIndexer types.BITCOINTxIndexer,
	db *gorm.DB,
	logger log.Logger,
) *BridgeDepositService

NewBridgeDepositService returns a new service instance.

func (*BridgeDepositService) CheckDeposit

func (bis *BridgeDepositService) CheckDeposit()

func (*BridgeDepositService) Deposit

func (bis *BridgeDepositService) Deposit()

func (*BridgeDepositService) EoaTransfer

func (bis *BridgeDepositService) EoaTransfer(deposit model.Deposit, oldTx *ethTypes.Transaction, nonce uint64, resetNonce bool) error

func (*BridgeDepositService) HandleDeposit

func (bis *BridgeDepositService) HandleDeposit(deposit model.Deposit, oldTx *ethTypes.Transaction, nonce uint64, resetNonce bool) error

func (*BridgeDepositService) HandleEoaTransfer

func (bis *BridgeDepositService) HandleEoaTransfer() error

func (*BridgeDepositService) HandleUnconfirmedDeposit

func (bis *BridgeDepositService) HandleUnconfirmedDeposit(deposit model.Deposit) error

HandleUnconfirmedDeposit 1. tx mined, update status 2. tx not mined, isPending, need reset gasprice 3. tx not mined, tx not mempool, need retry send tx

func (*BridgeDepositService) HandleUnconfirmedEoa

func (bis *BridgeDepositService) HandleUnconfirmedEoa(deposit model.Deposit) error

HandleUnconfirmedEoa

func (*BridgeDepositService) OnStart

func (bis *BridgeDepositService) OnStart() error

OnStart

func (*BridgeDepositService) OnStop

func (bis *BridgeDepositService) OnStop()

func (*BridgeDepositService) UnconfirmedDeposit

func (bis *BridgeDepositService) UnconfirmedDeposit() error

func (*BridgeDepositService) UnconfirmedEoa

func (bis *BridgeDepositService) UnconfirmedEoa() error

func (*BridgeDepositService) WaitMined

func (bis *BridgeDepositService) WaitMined(ctx1 context.Context, b2Tx *ethTypes.Transaction, deposit model.Deposit) error

type BridgeWithdrawService

type BridgeWithdrawService struct {
	service.BaseService
	// contains filtered or unexported fields
}

BridgeWithdrawService indexes transactions for json-rpc service.

func NewBridgeWithdrawService

func NewBridgeWithdrawService(
	btcCli *rpcclient.Client,
	ethCli *ethclient.Client,
	config *config.BitcoinConfig,
	db *gorm.DB,
	log log.Logger,
) *BridgeWithdrawService

NewBridgeWithdrawService returns a new service instance.

func (*BridgeWithdrawService) BroadcastTx

func (bis *BridgeWithdrawService) BroadcastTx(tx *wire.MsgTx) (*chainhash.Hash, error)

func (*BridgeWithdrawService) ConstructTx

func (bis *BridgeWithdrawService) ConstructTx(destAddressList []string, amounts []int64, b2TxHashes []byte) (string, string, error)

func (*BridgeWithdrawService) GetFeeRate

func (bis *BridgeWithdrawService) GetFeeRate() (*model.FeeRates, error)

func (*BridgeWithdrawService) GetMempoolURL

func (bis *BridgeWithdrawService) GetMempoolURL() string

func (*BridgeWithdrawService) GetMultiSigScript

func (bis *BridgeWithdrawService) GetMultiSigScript(pubs []string, minSignNum int) ([]byte, error)

func (*BridgeWithdrawService) GetMultiSigWitnessSize

func (bis *BridgeWithdrawService) GetMultiSigWitnessSize() int

func (*BridgeWithdrawService) GetUisatURL

func (bis *BridgeWithdrawService) GetUisatURL() string

func (*BridgeWithdrawService) GetUnspentList

func (bis *BridgeWithdrawService) GetUnspentList(address string, cursor int64) (int64, int64, []*model.UnspentOutput, error)

func (*BridgeWithdrawService) OnStart

func (bis *BridgeWithdrawService) OnStart() error

OnStart implements service.Service by subscribing for new blocks and indexing them by events.

type DepositData

type DepositData struct {
	Caller      string `json:"caller"`
	ToAddress   string `json:"to_address"`
	Amount      string `json:"amount"`
	Timestamp   int64  `json:"timestamp"`
	BlockNumber int64  `json:"block_number"`
	LogIndex    int    `json:"log_index"`
	TxHash      string `json:"tx_hash"`
}

type EpsResponse

type EpsResponse struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type EpsService

type EpsService struct {
	EthRPCURL string
	// contains filtered or unexported fields
}

EpsService eps service

func NewEpsService

func NewEpsService(
	bridgeCfg config.BridgeConfig,
	config config.EpsConfig,
	log log.Logger,
	db *gorm.DB,
) (*EpsService, error)

NewEpsService new eps

func (*EpsService) Deposit

func (e *EpsService) Deposit(data DepositData) error

func (*EpsService) GetTransactionByHash

func (e *EpsService) GetTransactionByHash(txHash string) (*EthTransactionResponse, error)

func (*EpsService) OnStart

func (e *EpsService) OnStart() error

type EthRequest

type EthRequest struct {
	Jsonrpc string   `json:"jsonrpc"`
	Method  string   `json:"method"`
	Params  []string `json:"params"`
	ID      int      `json:"id"`
}

type EthResponse

type EthResponse struct {
	Jsonrpc string                 `json:"jsonrpc"`
	ID      int                    `json:"id"`
	Result  EthTransactionResponse `json:"result"`
}

type EthTransactionResponse

type EthTransactionResponse struct {
	BlockNumber      string `json:"blockNumber"`
	From             string `json:"from"`
	To               string `json:"to"`
	TransactionIndex string `json:"transactionIndex"`
	Value            string `json:"value"`
}

type Indexer

type Indexer struct {
	// contains filtered or unexported fields
}

Indexer bitcoin indexer, parse and forward data

func NewBitcoinIndexer

func NewBitcoinIndexer(
	log log.Logger,
	client *rpcclient.Client,
	chainParams *chaincfg.Params,
	listenAddress string,
	targetConfirmations uint64,
) (*Indexer, error)

NewBitcoinIndexer new bitcoin indexer

func (*Indexer) BlockChainInfo

func (b *Indexer) BlockChainInfo() (*btcjson.GetBlockChainInfoResult, error)

BlockChainInfo get block chain info

func (*Indexer) CheckConfirmations

func (b *Indexer) CheckConfirmations(hash string) error

func (*Indexer) LatestBlock

func (b *Indexer) LatestBlock() (int64, error)

LatestBlock get latest block height in the longest block chain.

func (*Indexer) ParseAddress

func (b *Indexer) ParseAddress(pkScript []byte) (string, error)

parseAddress from pkscript parse address

func (*Indexer) ParseBlock

func (b *Indexer) ParseBlock(height int64, txIndex int64) ([]*types.BitcoinTxParseResult, *wire.BlockHeader, error)

ParseBlock parse block data by block height NOTE: Currently, only transfer transactions are supported.

type IndexerService

type IndexerService struct {
	service.BaseService
	// contains filtered or unexported fields
}

IndexerService indexes transactions for json-rpc service.

func NewIndexerService

func NewIndexerService(
	txIdxr types.BITCOINTxIndexer,

	db *gorm.DB,
	logger log.Logger,
) *IndexerService

NewIndexerService returns a new service instance.

func (*IndexerService) HandleResults

func (bis *IndexerService) HandleResults(
	txResults []*types.BitcoinTxParseResult,
	btcIndex model.BtcIndex,
	btcBlockTime time.Time,
	currentBlock int64,
) (int64, int64, error)

func (*IndexerService) OnStart

func (bis *IndexerService) OnStart() error

OnStart

func (*IndexerService) SaveParsedResult

func (bis *IndexerService) SaveParsedResult(
	parseResult *types.BitcoinTxParseResult,
	btcBlockNumber int64,
	b2TxStatus int,
	btcBlockTime time.Time,
	btcIndex model.BtcIndex,
) error

save index tx to db

func (*IndexerService) ToInFroms

func (bis *IndexerService) ToInFroms(a []types.BitcoinFrom, s string) bool

Jump to

Keyboard shortcuts

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