/
pending_transaction_pool_test.go
226 lines (172 loc) · 8.43 KB
/
pending_transaction_pool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package transactionpool
import (
"context"
"github.com/orbs-network/crypto-lib-go/crypto/digest"
"github.com/orbs-network/orbs-network-go/instrumentation/metric"
"github.com/orbs-network/orbs-network-go/test/builders"
"github.com/orbs-network/orbs-network-go/test/crypto/keys"
"github.com/orbs-network/orbs-network-go/test/with"
"github.com/orbs-network/orbs-spec/types/go/primitives"
"github.com/orbs-network/orbs-spec/types/go/protocol"
"github.com/stretchr/testify/require"
"testing"
"time"
)
var nodeAddress = keys.EcdsaSecp256K1KeyPairForTests(8).NodeAddress()
func TestPendingTransactionPoolTracksSizesOfTransactionsAddedAndRemoved(t *testing.T) {
with.Context(func(ctx context.Context) {
p := makePendingPool()
require.Zero(t, p.currentSizeInBytes, "New pending pool created with non-zero size")
tx1 := builders.TransferTransaction().Build()
k1, _ := p.add(tx1, nodeAddress)
require.Equal(t, uint32(len(tx1.Raw())), p.currentSizeInBytes, "pending pool size did not reflect tx1 size")
tx2 := builders.TransferTransaction().WithContract("a contract with a long name so that tx has a different size").Build()
k2, _ := p.add(tx2, nodeAddress)
require.Equal(t, uint32(len(tx1.Raw())+len(tx2.Raw())), p.currentSizeInBytes, "pending pool size did not reflect combined sizes of tx1 + tx2")
p.remove(ctx, k1, 0)
require.Equal(t, uint32(len(tx2.Raw())), p.currentSizeInBytes, "pending pool size did not reflect removal of tx1")
p.remove(ctx, k2, 0)
require.Zero(t, p.currentSizeInBytes, "pending pool size did not reflect removal of tx2")
})
}
func TestPendingTransactionPoolAddRemoveKeepsBothDataStructuresInSync(t *testing.T) {
with.Context(func(ctx context.Context) {
p := makePendingPool()
tx1 := builders.TransferTransaction().Build()
k, _ := p.add(tx1, nodeAddress)
require.True(t, p.has(tx1), "has() returned false for an added item")
require.Len(t, p.getBatch(1, 0), 1, "getBatch() did not return an added item")
p.remove(ctx, k, 0)
require.False(t, p.has(tx1), "has() returned true for removed item")
require.Empty(t, p.getBatch(1, 0), "getBatch() returned a removed item")
require.NotPanics(t, func() {
p.remove(ctx, k, 0)
}, "removing a key that does not exist resulted in a panic")
})
}
func TestPendingTransactionPoolGetBatchReturnsLessThanMaximumIfPoolHasLessTransaction(t *testing.T) {
p := makePendingPool()
add(p, builders.TransferTransaction().Build(), builders.TransferTransaction().Build())
txSet := p.getBatch(3, 0)
require.Len(t, txSet, 2, "expected 2 transactions but got %v transactions: %s", len(txSet), txSet)
}
func TestPendingTransactionPoolGetBatchDoesNotExceedSizeLimitInBytes(t *testing.T) {
p := makePendingPool()
tx1 := builders.TransferTransaction().Build()
tx2 := builders.TransferTransaction().Build()
add(p, tx1, tx2, builders.TransferTransaction().Build())
slightlyMoreThanTwoTransactionsInBytes := uint32(len(tx1.Raw()) + len(tx2.Raw()) + 1)
txSet := p.getBatch(3, slightlyMoreThanTwoTransactionsInBytes)
require.Len(t, txSet, 2, "expected 2 transactions but got %v transactions: %s", len(txSet), txSet)
}
func TestPendingTransactionPoolGetBatchDoesNotExceedLengthLimit(t *testing.T) {
p := makePendingPool()
tx1 := builders.TransferTransaction().Build()
tx2 := builders.TransferTransaction().Build()
tx3 := builders.TransferTransaction().Build()
add(p, tx1, tx2, tx3)
txSet := p.getBatch(2, 0)
require.Len(t, txSet, 2, "expected 2 transactions but got %v transactions: %s", len(txSet), txSet)
}
func TestPendingTransactionPoolGetBatchRetainsInsertionOrder(t *testing.T) {
p := makePendingPool()
// create 50 transactions so as to minimize the chance of randomly returning transactions in the expected order
transactions := make(Transactions, 50)
for i := 0; i < len(transactions); i++ {
transactions[i] = builders.TransferTransaction().Build()
add(p, transactions[i])
}
txSet := p.getBatch(uint32(len(transactions)), 0)
require.Equal(t, transactions, txSet, "got transactions in wrong order")
}
func TestPendingTransactionPoolClearsExpiredTransactions(t *testing.T) {
with.Context(func(ctx context.Context) {
p := makePendingPool()
tx1 := builders.TransferTransaction().WithTimestamp(time.Now().Add(-5 * time.Minute)).Build()
tx2 := builders.TransferTransaction().WithTimestamp(time.Now().Add(-29 * time.Minute)).Build()
tx3 := builders.TransferTransaction().WithTimestamp(time.Now().Add(-31 * time.Minute)).Build()
add(p, tx1, tx2, tx3)
p.clearTransactionsOlderThan(ctx, primitives.TimestampNano(time.Now().Add(-30*time.Minute).UnixNano()))
require.True(t, p.has(tx1), "cleared non-expired transaction")
require.True(t, p.has(tx2), "cleared non-expired transaction")
require.False(t, p.has(tx3), "did not clear expired transaction")
})
}
func TestPendingTransactionPoolDoesNotAddTheSameTransactionTwiceRegardlessOfPublicKey(t *testing.T) {
p := makePendingPool()
tx := builders.Transaction().Build()
_, err := p.add(tx, nodeAddress)
require.Nil(t, err, "got an unexpected error adding the first transaction")
_, err = p.add(tx, nodeAddress)
require.Equal(t, protocol.TRANSACTION_STATUS_DUPLICATE_TRANSACTION_ALREADY_PENDING, err.TransactionStatus, "did not get expected status code")
someOtherAddress := keys.EcdsaSecp256K1KeyPairForTests(3).NodeAddress()
_, err = p.add(tx, someOtherAddress)
require.Equal(t, protocol.TRANSACTION_STATUS_DUPLICATE_TRANSACTION_ALREADY_PENDING, err.TransactionStatus, "did not get expected status code")
}
func TestPendingTransactionPoolCallsRemovalListenerWhenRemovingTransaction(t *testing.T) {
with.Context(func(ctx context.Context) {
var removedTxHash primitives.Sha256
var removalReason protocol.TransactionStatus
p := makePendingPool()
p.onTransactionRemoved = func(ctx context.Context, txHash primitives.Sha256, reason protocol.TransactionStatus) {
removedTxHash = txHash
removalReason = reason
}
tx := builders.Transaction().Build()
p.add(tx, nodeAddress)
txHash := digest.CalcTxHash(tx.Transaction())
p.remove(ctx, txHash, protocol.TRANSACTION_STATUS_REJECTED_TIMESTAMP_WINDOW_EXCEEDED)
require.Equal(t, txHash, removedTxHash, "removed txHash didn't equal expected txHash")
require.Equal(t, protocol.TRANSACTION_STATUS_REJECTED_TIMESTAMP_WINDOW_EXCEEDED, removalReason, "removal reason didn't equal expected reason")
})
}
func TestPendingPoolNotifiesOnNewTransactions(t *testing.T) {
var called bool
p := NewPendingPool(func() uint32 { return 100000 }, metric.NewRegistry(), func() {
called = true
})
p.add(builders.Transaction().Build(), nodeAddress)
require.True(t, called, "pending transaction pool did not notify onNewTransaction")
}
// this test reproduces an issue when a cleanup goroutine wakes up while a commit is taking place. both will attempt to remove elements from the pool
func TestPendingTransactionPool_CleanupWhileRemove_NoRaces(t *testing.T) {
with.Context(func(ctx context.Context) {
p := makePendingPool()
old := time.Now().Add(-1 * time.Hour)
tx1 := builders.TransferTransaction().WithTimestamp(old).Build()
tx2 := builders.TransferTransaction().WithTimestamp(old).Build()
add(p, tx1, tx2) // will be removed by p.clearTransactionsOlderThan() below
var hashes = []primitives.Sha256{digest.CalcTxHash(tx2.Transaction())} // tx2 will be removed by both
for i := 0; i < 10; i++ {
tx := builders.TransferTransaction().Build()
hashes = append(hashes, digest.CalcTxHash(tx.Transaction()))
add(p, tx) // will be removed by p.remove() below
}
checkpoint := make(chan struct{})
go func() {
checkpoint <- struct{}{}
p.clearTransactionsOlderThan(ctx, primitives.TimestampNano(time.Now().Add(-30*time.Minute).UnixNano()))
checkpoint <- struct{}{}
}()
<-checkpoint
for _, hash := range hashes {
p.remove(ctx, hash, protocol.TRANSACTION_STATUS_COMMITTED)
}
<-checkpoint
require.Empty(t, p.getBatch(3, 10000000), "pool wasn't empty after removing al txs")
})
}
func add(p *pendingTxPool, txs ...*protocol.SignedTransaction) {
for _, tx := range txs {
p.add(tx, nodeAddress)
}
}
func makePendingPool() *pendingTxPool {
metricFactory := metric.NewRegistry()
return NewPendingPool(func() uint32 { return 100000 }, metricFactory, func() {})
}