Skip to content

Commit

Permalink
wallet: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEk committed Oct 18, 2023
1 parent e7f9c97 commit 3da9c8e
Showing 1 changed file with 97 additions and 3 deletions.
100 changes: 97 additions & 3 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (

"github.com/btcsuite/btcwallet/walletdb"
"github.com/btcsuite/btcwallet/wtxmgr"
"github.com/stretchr/testify/require"

"github.com/btcsuite/btcd/btcutil"
)

var (
TstSerializedTx, _ = hex.DecodeString("010000000114d9ff358894c486b4ae11c2a8cf7851b1df64c53d2e511278eff17c22fb7373000000008c493046022100995447baec31ee9f6d4ec0e05cb2a44f6b817a99d5f6de167d1c75354a946410022100c9ffc23b64d770b0e01e7ff4d25fbc2f1ca8091053078a247905c39fce3760b601410458b8e267add3c1e374cf40f1de02b59213a82e1d84c2b94096e22e2f09387009c96debe1d0bcb2356ffdcf65d2a83d4b34e72c62eccd8490dbf2110167783b2bffffffff0280969800000000001976a914479ed307831d0ac19ebc5f63de7d5f1a430ddb9d88ac38bfaa00000000001976a914dadf9e3484f28b385ddeaa6c575c0c0d18e9788a88ac00000000")
TstTx, _ = btcutil.NewTxFromBytes(TstSerializedTx)
TstTxHash = TstTx.Hash()
TstSerializedTx, _ = hex.DecodeString("010000000114d9ff358894c486b4ae11c2a8cf7851b1df64c53d2e511278eff17c22fb7373000000008c493046022100995447baec31ee9f6d4ec0e05cb2a44f6b817a99d5f6de167d1c75354a946410022100c9ffc23b64d770b0e01e7ff4d25fbc2f1ca8091053078a247905c39fce3760b601410458b8e267add3c1e374cf40f1de02b59213a82e1d84c2b94096e22e2f09387009c96debe1d0bcb2356ffdcf65d2a83d4b34e72c62eccd8490dbf2110167783b2bffffffff0280969800000000001976a914479ed307831d0ac19ebc5f63de7d5f1a430ddb9d88ac38bfaa00000000001976a914dadf9e3484f28b385ddeaa6c575c0c0d18e9788a88ac00000000")
TstMinedSerializedTx, _ = hex.DecodeString("010000000114d9ff358894c486b4ae11c2a8cf7851b1df64c53d2e511278eff17c22fb7373000000008c493046022100995447baec31ee9f6d4ec0e05cb2a44f6b817a99d5f6de167d1c75354a946410022100c9ffc23b64d770b0e01e7ff4d25fbc2f1ca8091053078a247905c39fce3760b601410458b8e267add3c1e374cf40f1de02b59213a82e1d84c2b94096e22e2f09387009c96debe1d0bcb2356ffdcf65d2a83d4b34e72c62eccd8490dbf2110167783b2bffffffff0280969800000000001976a914479ed307831d0ac19ebc5f63de7d5f1a430ddb9d88ac38bfaa00000000001976a914dadf9e3484f28b385ddeaa6c575c0c0d18e9788a88ac00000000")
TstTx, _ = btcutil.NewTxFromBytes(TstSerializedTx)
TstTxHash = TstTx.Hash()
)

// TestLocateBirthdayBlock ensures we can properly map a block in the chain to a
Expand Down Expand Up @@ -205,3 +207,95 @@ func TestLabelTransaction(t *testing.T) {
})
}
}

// TestGetTransaction writes a tx to disk and then compares
// the hash to the response from the GetTransaction function.
func TestGetTransaction(t *testing.T) {

tests := []struct {
name string

// Whether the transaction should be known to the wallet.
txKnown bool

// Whether the transaction is mined.
minedTx bool

// Block height.
blockHeight int32

// The error we expect to be returned.
expectedErr error
}{
{
name: "existing transaction, not mined",
txKnown: true,
minedTx: false,
expectedErr: nil,
},
{
name: "non-existing transaction",
txKnown: false,
minedTx: false,
expectedErr: ErrNoTx,
},
{
name: "existing confirmed transaction",
txKnown: true,
minedTx: true,
blockHeight: 100,
expectedErr: nil,
},
}
for _, test := range tests {
test := test

t.Run(test.name, func(t *testing.T) {
w, cleanup := testWallet(t)
defer cleanup()

// If the transaction should be known to the store, we
// write txdetail to disk.
if test.txKnown {
rec, err := wtxmgr.NewTxRecord(
TstSerializedTx, time.Now(),
)
require.NoError(t, err)

// Write the tx record to disk.
err = walletdb.Update(w.db,
func(tx walletdb.ReadWriteTx) error {

ns := tx.ReadWriteBucket(
wtxmgrNamespaceKey,
)
if test.minedTx {
return w.TxStore.InsertTx(
ns,
rec,
&wtxmgr.BlockMeta{
Block: wtxmgr.Block{
Height: test.blockHeight,
},
},
)
}
return w.TxStore.InsertTx(ns, rec, nil)

})
require.NoError(t, err, "could not insert tx")
}

tx, err := w.GetTransaction(*TstTxHash)
if err != test.expectedErr {
t.Fatalf("expected: %v, got: %v", test.expectedErr, err)
}
if test.txKnown {
require.True(t, tx.Summary.Hash.IsEqual(TstTxHash))
}
if test.minedTx {
require.Equal(t, test.blockHeight, tx.Height)
}
})
}
}

0 comments on commit 3da9c8e

Please sign in to comment.