diff --git a/pool/pool.go b/pool/pool.go index ae3f0dd..a020bad 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -106,9 +106,9 @@ func (p *Pool) Update(b *blockchain.Block, bc *blockchain.BlockChain) bool { return true } -// GetTxns returns the the largest of l or size of pool transactions. -// It selects the highest priority transactions. -func (p *Pool) GetTxns(l int) []*blockchain.Transaction { +// PopTxns returns the the largest of l or size of pool transactions. +// It selects the highest priority transactions, and removes them from the pool. +func (p *Pool) PopTxns(l int) []*blockchain.Transaction { if p.Len() == 0 { return make([]*blockchain.Transaction, 0) } @@ -117,7 +117,9 @@ func (p *Pool) GetTxns(l int) []*blockchain.Transaction { } txns := make([]*blockchain.Transaction, l) for i := 0; i < l; i++ { - txns[i] = p.GetN(i) + t := p.GetN(i) + txns[i] = t + p.Delete(t) } return txns } diff --git a/pool/pool_test.go b/pool/pool_test.go index 834a3f9..df09bf8 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -75,17 +75,20 @@ func TestGetTxns(t *testing.T) { } } nTxns := len(b.Transactions) + 12 // arbitrary. - txns := p.GetTxns(nTxns) + txns := p.PopTxns(nTxns) for _, tr := range txns { if ok, _ := b.ContainsTransaction(tr); !ok { t.FailNow() } } + if p.Len() != 0 { + t.FailNow() + } } func TestGetNewBlockEmpty(t *testing.T) { p := New() - txns := p.GetTxns(305) + txns := p.PopTxns(305) if len(txns) != 0 { t.FailNow() }