forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxService_test.go
193 lines (168 loc) · 5.28 KB
/
txService_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
// Copyright (C) 2019-2020 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package rpcs
import (
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
"github.com/algorand/go-algorand/components/mocks"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/network"
"github.com/algorand/go-algorand/util/bloom"
)
func TestMain(m *testing.M) {
logging.Base().SetLevel(logging.Debug)
os.Exit(m.Run())
}
type httpTestPeerSource struct {
peers []network.Peer
mocks.MockNetwork
}
func (s *httpTestPeerSource) GetPeers(options ...network.PeerOption) []network.Peer {
return s.peers
}
// implement network.HTTPPeer
type testHTTPPeer string
func (p testHTTPPeer) GetAddress() string {
return string(p)
}
func (p *testHTTPPeer) PrepareURL(x string) string {
return strings.Replace(x, "{genesisID}", "test genesisID", -1)
}
func (p *testHTTPPeer) GetHTTPClient() *http.Client {
return &http.Client{}
}
func (p *testHTTPPeer) GetHTTPPeer() network.HTTPPeer {
return p
}
type basicRPCNode struct {
listener net.Listener
server http.Server
rmux *mux.Router
peers []network.Peer
mocks.MockNetwork
}
func (b *basicRPCNode) RegisterHTTPHandler(path string, handler http.Handler) {
if b.rmux == nil {
b.rmux = mux.NewRouter()
}
b.rmux.Handle(path, handler)
}
func (b *basicRPCNode) RegisterHandlers(dispatch []network.TaggedMessageHandler) {
}
func (b *basicRPCNode) start() bool {
var err error
b.listener, err = net.Listen("tcp", "")
if err != nil {
logging.Base().Error("tcp listen", err)
return false
}
if b.rmux == nil {
b.rmux = mux.NewRouter()
}
b.server.Handler = b.rmux
go b.server.Serve(b.listener)
return true
}
func (b *basicRPCNode) rootURL() string {
addr := b.listener.Addr().String()
rootURL := url.URL{Scheme: "http", Host: addr, Path: ""}
return rootURL.String()
}
func (b *basicRPCNode) stop() {
b.server.Close()
}
func (b *basicRPCNode) GetPeers(options ...network.PeerOption) []network.Peer {
return b.peers
}
func nodePair() (*basicRPCNode, *basicRPCNode) {
nodeA := &basicRPCNode{}
nodeA.start()
nodeB := &basicRPCNode{}
nodeB.start()
httpPeerA := testHTTPPeer(nodeA.rootURL())
httpPeerB := testHTTPPeer(nodeB.rootURL())
nodeB.peers = []network.Peer{&httpPeerA}
nodeA.peers = []network.Peer{&httpPeerB}
return nodeA, nodeB
}
func TestTxSync(t *testing.T) {
// A network with two nodes, A and B
nodeA, nodeB := nodePair()
defer nodeA.stop()
defer nodeB.stop()
pool := makeMockPendingTxAggregate(3)
RegisterTxService(pool, nodeA, "test genesisID", config.GetDefaultLocal().TxPoolSize, config.GetDefaultLocal().TxSyncServeResponseSize)
// B tries to fetch block
handler := mockHandler{}
syncInterval := time.Second
syncTimeout := time.Second
syncerPool := makeMockPendingTxAggregate(0)
syncer := MakeTxSyncer(syncerPool, nodeB, &handler, syncInterval, syncTimeout, config.GetDefaultLocal().TxSyncServeResponseSize)
require.NoError(t, syncer.sync())
require.Equal(t, int32(3), atomic.LoadInt32(&handler.messageCounter))
}
func BenchmarkTxSync(b *testing.B) {
// A network with two nodes, A and B
nodeA, nodeB := nodePair()
defer nodeA.stop()
defer nodeB.stop()
pool := makeMockPendingTxAggregate(10000)
RegisterTxService(pool, nodeA, "test genesisID", config.GetDefaultLocal().TxPoolSize, config.GetDefaultLocal().TxSyncServeResponseSize)
b.ResetTimer()
wg := sync.WaitGroup{}
wg.Add(30)
for i := 0; i < 30; i++ {
go func() {
defer wg.Done()
for j := 0; j < b.N/30; j++ {
handler := mockHandler{}
syncInterval := time.Second
syncTimeout := time.Second
syncPool := makeMockPendingTxAggregate(config.GetDefaultLocal().TxPoolSize)
syncer := MakeTxSyncer(syncPool, nodeB, &handler, syncInterval, syncTimeout, config.GetDefaultLocal().TxSyncServeResponseSize)
syncer.sync()
}
}()
}
wg.Wait()
}
func BenchmarkTransactionFilteringPerformance(b *testing.B) {
pool := makeMockPendingTxAggregate(config.GetDefaultLocal().TxPoolSize)
txService := makeTxService(pool, "test genesisID", config.GetDefaultLocal().TxPoolSize, config.GetDefaultLocal().TxSyncServeResponseSize)
clientPool := makeMockPendingTxAggregate(config.GetDefaultLocal().TxPoolSize)
pending := clientPool.PendingTxIDs()
sizeBits, numHashes := bloom.Optimal(len(pending), bloomFilterFalsePositiveRate)
filter := bloom.New(sizeBits, numHashes, 0)
for _, txid := range pending {
filter.Set(txid[:])
}
txService.updateTxCache()
b.ResetTimer()
for i := 0; i < b.N; i++ {
txService.getFilteredTxns(filter)
i += config.GetDefaultLocal().TxPoolSize - 1
}
}