multi: use chain.MapRPCErr instead of rpcclient.MapRPCErr

This commit is contained in:
yyforyongyu 2024-07-03 23:43:11 +08:00 committed by Elle Mouton
parent e3687b6aa0
commit 77a4a3233b
No known key found for this signature in database
GPG key ID: D7D916376026F177
2 changed files with 16 additions and 14 deletions

View file

@ -708,9 +708,9 @@ func (d *DefaultWalletImpl) BuildChainControl(
// The broadcast is already always active for neutrino nodes, so we // The broadcast is already always active for neutrino nodes, so we
// don't want to create a rebroadcast loop. // don't want to create a rebroadcast loop.
if partialChainControl.Cfg.NeutrinoCS == nil { if partialChainControl.Cfg.NeutrinoCS == nil {
cs := partialChainControl.ChainSource
broadcastCfg := pushtx.Config{ broadcastCfg := pushtx.Config{
Broadcast: func(tx *wire.MsgTx) error { Broadcast: func(tx *wire.MsgTx) error {
cs := partialChainControl.ChainSource
_, err := cs.SendRawTransaction( _, err := cs.SendRawTransaction(
tx, true, tx, true,
) )
@ -724,7 +724,10 @@ func (d *DefaultWalletImpl) BuildChainControl(
// In case the backend is different from neutrino we // In case the backend is different from neutrino we
// make sure that broadcast backend errors are mapped // make sure that broadcast backend errors are mapped
// to the neutrino broadcastErr. // to the neutrino broadcastErr.
MapCustomBroadcastError: broadcastErrorMapper, MapCustomBroadcastError: func(err error) error {
rpcErr := cs.MapRPCErr(err)
return broadcastErrorMapper(rpcErr)
},
} }
lnWalletConfig.Rebroadcaster = newWalletReBroadcaster( lnWalletConfig.Rebroadcaster = newWalletReBroadcaster(
@ -1475,27 +1478,27 @@ func parseHeaderStateAssertion(state string) (*headerfs.FilterHeader, error) {
// the neutrino BroadcastError which allows the Rebroadcaster which currently // the neutrino BroadcastError which allows the Rebroadcaster which currently
// resides in the neutrino package to use all of its functionalities. // resides in the neutrino package to use all of its functionalities.
func broadcastErrorMapper(err error) error { func broadcastErrorMapper(err error) error {
returnErr := rpcclient.MapRPCErr(err) var returnErr error
// We only filter for specific backend errors which are relevant for the // We only filter for specific backend errors which are relevant for the
// Rebroadcaster. // Rebroadcaster.
switch { switch {
// This makes sure the tx is removed from the rebroadcaster once it is // This makes sure the tx is removed from the rebroadcaster once it is
// confirmed. // confirmed.
case errors.Is(returnErr, rpcclient.ErrTxAlreadyKnown), case errors.Is(err, rpcclient.ErrTxAlreadyKnown),
errors.Is(err, rpcclient.ErrTxAlreadyConfirmed): errors.Is(err, rpcclient.ErrTxAlreadyConfirmed):
returnErr = &pushtx.BroadcastError{ returnErr = &pushtx.BroadcastError{
Code: pushtx.Confirmed, Code: pushtx.Confirmed,
Reason: returnErr.Error(), Reason: err.Error(),
} }
// Transactions which are still in mempool but might fall out because // Transactions which are still in mempool but might fall out because
// of low fees are rebroadcasted despite of their backend error. // of low fees are rebroadcasted despite of their backend error.
case errors.Is(returnErr, rpcclient.ErrTxAlreadyInMempool): case errors.Is(err, rpcclient.ErrTxAlreadyInMempool):
returnErr = &pushtx.BroadcastError{ returnErr = &pushtx.BroadcastError{
Code: pushtx.Mempool, Code: pushtx.Mempool,
Reason: returnErr.Error(), Reason: err.Error(),
} }
// Transactions which are not accepted into mempool because of low fees // Transactions which are not accepted into mempool because of low fees
@ -1504,12 +1507,11 @@ func broadcastErrorMapper(err error) error {
// publishing the transaction. Moreover we log the detailed error so the // publishing the transaction. Moreover we log the detailed error so the
// user can intervene and increase the size of his mempool. // user can intervene and increase the size of his mempool.
case errors.Is(err, rpcclient.ErrMempoolMinFeeNotMet): case errors.Is(err, rpcclient.ErrMempoolMinFeeNotMet):
ltndLog.Warnf("Error while broadcasting transaction: %v", ltndLog.Warnf("Error while broadcasting transaction: %v", err)
returnErr)
returnErr = &pushtx.BroadcastError{ returnErr = &pushtx.BroadcastError{
Code: pushtx.Mempool, Code: pushtx.Mempool,
Reason: returnErr.Error(), Reason: err.Error(),
} }
} }

View file

@ -1191,8 +1191,8 @@ func (b *BtcWallet) ListUnspentWitness(minConfs, maxConfs int32,
return witnessOutputs, nil return witnessOutputs, nil
} }
// mapRpcclientError maps an error from the rpcclient package to defined error // mapRpcclientError maps an error from the `btcwallet/chain` package to
// in this package. // defined error in this package.
// //
// NOTE: we are mapping the errors returned from `sendrawtransaction` RPC or // NOTE: we are mapping the errors returned from `sendrawtransaction` RPC or
// the reject reason from `testmempoolaccept` RPC. // the reject reason from `testmempoolaccept` RPC.
@ -1277,7 +1277,7 @@ func (b *BtcWallet) PublishTransaction(tx *wire.MsgTx, label string) error {
// We need to use the string to create an error type and map it to a // We need to use the string to create an error type and map it to a
// btcwallet error. // btcwallet error.
err = rpcclient.MapRPCErr(errors.New(result.RejectReason)) err = b.chain.MapRPCErr(errors.New(result.RejectReason))
//nolint:lll //nolint:lll
// These two errors are ignored inside `PublishTransaction`: // These two errors are ignored inside `PublishTransaction`:
@ -1922,7 +1922,7 @@ func (b *BtcWallet) CheckMempoolAcceptance(tx *wire.MsgTx) error {
// Mempool check failed, we now map the reject reason to a proper RPC // Mempool check failed, we now map the reject reason to a proper RPC
// error and return it. // error and return it.
if !result.Allowed { if !result.Allowed {
err := rpcclient.MapRPCErr(errors.New(result.RejectReason)) err := b.chain.MapRPCErr(errors.New(result.RejectReason))
return fmt.Errorf("mempool rejection: %w", err) return fmt.Errorf("mempool rejection: %w", err)
} }