From caeb8959c45816c432ca3c36fbab02a3d6477fe9 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Wed, 27 Mar 2024 11:19:23 +0100 Subject: [PATCH] node: add TestUnconfirmedParents --- internal/node/transactionpool.go | 23 ++++++++------- internal/node/transactionpool_test.go | 40 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 internal/node/transactionpool_test.go diff --git a/internal/node/transactionpool.go b/internal/node/transactionpool.go index c5582a757..51b5f4141 100644 --- a/internal/node/transactionpool.go +++ b/internal/node/transactionpool.go @@ -41,7 +41,18 @@ func (tp txpool) AcceptTransactionSet(txns []types.Transaction) error { } func (tp txpool) UnconfirmedParents(txn types.Transaction) ([]types.Transaction, error) { - pool := tp.Transactions() + return unconfirmedParents(txn, tp.Transactions()), nil +} + +func (tp txpool) Subscribe(subscriber modules.TransactionPoolSubscriber) { + tp.tp.TransactionPoolSubscribe(subscriber) +} + +func (tp txpool) Close() error { + return tp.tp.Close() +} + +func unconfirmedParents(txn types.Transaction, pool []types.Transaction) []types.Transaction { outputToParent := make(map[types.SiacoinOutputID]*types.Transaction) for i, txn := range pool { for j := range txn.SiacoinOutputs { @@ -58,15 +69,7 @@ func (tp txpool) UnconfirmedParents(txn types.Transaction) ([]types.Transaction, } } } - return parents, nil -} - -func (tp txpool) Subscribe(subscriber modules.TransactionPoolSubscriber) { - tp.tp.TransactionPoolSubscribe(subscriber) -} - -func (tp txpool) Close() error { - return tp.tp.Close() + return parents } func NewTransactionPool(tp modules.TransactionPool) bus.TransactionPool { diff --git a/internal/node/transactionpool_test.go b/internal/node/transactionpool_test.go new file mode 100644 index 000000000..c24e2c190 --- /dev/null +++ b/internal/node/transactionpool_test.go @@ -0,0 +1,40 @@ +package node + +import ( + "reflect" + "testing" + + "go.sia.tech/core/types" +) + +func TestUnconfirmedParents(t *testing.T) { + grandparent := types.Transaction{ + SiacoinOutputs: []types.SiacoinOutput{{}}, + } + parent := types.Transaction{ + SiacoinInputs: []types.SiacoinInput{ + { + ParentID: grandparent.SiacoinOutputID(0), + }, + }, + SiacoinOutputs: []types.SiacoinOutput{{}}, + } + txn := types.Transaction{ + SiacoinInputs: []types.SiacoinInput{ + { + ParentID: parent.SiacoinOutputID(0), + }, + }, + SiacoinOutputs: []types.SiacoinOutput{{}}, + } + pool := []types.Transaction{grandparent, parent} + + parents := unconfirmedParents(txn, pool) + if len(parents) != 2 { + t.Fatalf("expected 2 parents, got %v", len(parents)) + } else if !reflect.DeepEqual(parents[0], grandparent) { + t.Fatalf("expected grandparent") + } else if !reflect.DeepEqual(parents[1], parent) { + t.Fatalf("expected parent") + } +}