Skip to content

Commit

Permalink
Merge pull request btcsuite#876 from yyforyongyu/fix-warning-log
Browse files Browse the repository at this point in the history
chain: remove warning log for mempool polling
  • Loading branch information
Roasbeef authored and buck54321 committed Apr 21, 2024
1 parent 691ac8f commit ee47399
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
48 changes: 39 additions & 9 deletions chain/mempool.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package chain

import (
"strings"
"sync"
"time"

"github.com/ltcsuite/ltcd/blockchain"
"github.com/ltcsuite/ltcd/chaincfg/chainhash"
"github.com/ltcsuite/ltcd/ltcutil"
"github.com/ltcsuite/ltcd/wire"
"golang.org/x/sync/errgroup"
)

// txNotFoundErr is an error returned from bitcoind's `getrawtransaction` RPC
// when the requested txid cannot be found.
const txNotFoundErr = "-5: No such mempool or blockchain Transaction"

// cachedInputs caches the inputs of the transactions in the mempool. This is
// used to provide fast lookup between txids and inputs.
type cachedInputs struct {
Expand Down Expand Up @@ -306,11 +312,8 @@ func (m *mempool) LoadMempool() error {

eg.Go(func() error {
// Grab full mempool transaction from hash.
tx, err := m.client.GetRawTransaction(txHash)
if err != nil {
log.Warnf("unable to fetch "+
"transaction %s for "+
"mempool: %v", txHash, err)
tx := m.getRawTxIgnoreErr(txHash)
if tx == nil {
return nil
}

Expand Down Expand Up @@ -360,10 +363,8 @@ func (m *mempool) UpdateMempoolTxes(txids []*chainhash.Hash) []*wire.MsgTx {

eg.Go(func() error {
// Grab full mempool transaction from hash.
tx, err := m.client.GetRawTransaction(txHash)
if err != nil {
log.Warnf("unable to fetch transaction %s "+
"from mempool: %v", txHash, err)
tx := m.getRawTxIgnoreErr(txHash)
if tx == nil {
return nil
}

Expand Down Expand Up @@ -393,3 +394,32 @@ func (m *mempool) UpdateMempoolTxes(txids []*chainhash.Hash) []*wire.MsgTx {

return txesToNotify
}

// getRawTxIgnoreErr wraps the GetRawTransaction call to bitcoind, and ignores
// the error returned since we can't do anything about it here in the mempool.
//
// NOTE: if `txindex` is not enabled, `GetRawTransaction` will only look for
// the txid in bitocind's mempool. If the tx is replaced, confirmed, or not yet
// included in bitcoind's mempool, the error txNotFoundErr will be returned.
func (m *mempool) getRawTxIgnoreErr(txid *chainhash.Hash) *ltcutil.Tx {
tx, err := m.client.GetRawTransaction(txid)

// Exit early if there's no error.
if err == nil {
return tx
}

// If this is the txNotFoundErr, we'll create a debug log.
if strings.Contains(err.Error(), txNotFoundErr) {
log.Debugf("unable to fetch transaction %s from mempool: %v",
txid, err)

} else {
// Otherwise, unexpected error is found, we'll create an error
// log.
log.Errorf("unable to fetch transaction %s from mempool: %v",
txid, err)
}

return nil
}
38 changes: 38 additions & 0 deletions chain/mempool_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chain

import (
"errors"
"math"
"testing"

Expand Down Expand Up @@ -518,3 +519,40 @@ func TestUpdateMempoolTxes(t *testing.T) {
// We should see tx2 being removed.
require.NotContains(m.inputs.txids, tx2Hash)
}

// TestGetRawTxIgnoreErr tests that the mempool's GetRawTxIgnoreErr method
// works as expected.
func TestGetRawTxIgnoreErr(t *testing.T) {
require := require.New(t)

// Create a mock client and init our mempool.
mockRPC := &mockRPCClient{}
m := newMempool(mockRPC)

// Create a normal transaction that has two inputs.
op := wire.OutPoint{Hash: chainhash.Hash{1}}
tx := &wire.MsgTx{
LockTime: 1,
TxIn: []*wire.TxIn{{PreviousOutPoint: op}},
}
txid := tx.TxHash()
btctx := ltcutil.NewTx(tx)

// Mock the client to return the tx.
mockRPC.On("GetRawTransaction", &txid).Return(btctx, nil).Once()

// Call the method and expect the tx to be returned.
resp := m.getRawTxIgnoreErr(&txid)
require.Equal(btctx, resp)

// Mock the client to return an error.
dummyErr := errors.New("dummy error")
mockRPC.On("GetRawTransaction", &txid).Return(nil, dummyErr).Once()

// Call the method again and expect nil response.
resp = m.getRawTxIgnoreErr(&txid)
require.Nil(resp)

// Assert the mock client was called as expected.
mockRPC.AssertExpectations(t)
}
6 changes: 6 additions & 0 deletions chain/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,11 @@ func (m *mockRPCClient) GetRawTransaction(
txHash *chainhash.Hash) (*ltcutil.Tx, error) {

args := m.Called(txHash)

tx := args.Get(0)
if tx == nil {
return nil, args.Error(1)
}

return args.Get(0).(*ltcutil.Tx), args.Error(1)
}

0 comments on commit ee47399

Please sign in to comment.