summaryrefslogtreecommitdiff
path: root/txnsync/service_test.go
blob: 2b262fc1854275c36b6d7dc4eba1ddffd951022c (plain)
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
// Copyright (C) 2019-2021 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 txnsync

import (
	"encoding/binary"
	"math/rand"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/pooldata"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/algorand/go-algorand/util/execpool"
	"github.com/algorand/go-algorand/util/timers"
)

type mockLogger struct {
	logging.Logger
}

type mockNodeConnector struct {
	NodeConnector
	calledEvents        *bool
	peerInfo            PeerInfo
	updatingPeers       bool
	transactionPoolSize int
	peers               []PeerInfo
}

func makeMockNodeConnector(calledEvents *bool) mockNodeConnector {
	return mockNodeConnector{calledEvents: calledEvents}
}

func (fn *mockNodeConnector) Events() <-chan Event {
	if fn.calledEvents != nil {
		*fn.calledEvents = true
	}
	return nil
}

func (fn *mockNodeConnector) GetCurrentRoundSettings() (out RoundSettings) { return }

func (fn *mockNodeConnector) Clock() (out timers.WallClock) {
	return timers.MakeMonotonicClock(time.Now())
}

func (fn *mockNodeConnector) Random(rng uint64) uint64 {
	var xb [8]byte
	rand.Read(xb[:])
	rv := binary.LittleEndian.Uint64(xb[:])
	return rv % rng
}

func (fn *mockNodeConnector) GetPeers() []PeerInfo { return fn.peers }

func (fn *mockNodeConnector) GetPeer(interface{}) (out PeerInfo) {
	return fn.peerInfo
}

func (fn *mockNodeConnector) UpdatePeers(txsyncPeers []*Peer, netPeers []interface{}, peersAverageDataExchangeRate uint64) {
	fn.updatingPeers = true
}
func (fn *mockNodeConnector) SendPeerMessage(netPeer interface{}, msg []byte, callback SendMessageCallback) {
}

func (fn *mockNodeConnector) GetPeerLatency(netPeer interface{}) time.Duration {
	return 0
}

func (fn *mockNodeConnector) GetPendingTransactionGroups() (txGroups []pooldata.SignedTxGroup, latestLocallyOriginatedGroupCounter uint64) {
	return
}
func (fn *mockNodeConnector) IncomingTransactionGroups(peer *Peer, messageSeq uint64, txGroups []pooldata.SignedTxGroup) (transactionPoolSize int) {
	return fn.transactionPoolSize
}
func (fn *mockNodeConnector) NotifyMonitor() chan struct{} { return nil }

type mockThreadPool struct {
	execpool.BacklogPool
}

// TestStartStopTransactionSyncService test that we can start and stop the transaction sync service
func TestStartStopTransactionSyncService(t *testing.T) {
	partitiontest.PartitionTest(t)

	calledEventsInNodeConnector := false

	a := require.New(t)

	mLogger := mockLogger{}
	mNodeConnector := makeMockNodeConnector(&calledEventsInNodeConnector)
	cfg := config.GetDefaultLocal()
	mThreadPool := mockThreadPool{}

	hashDigest := crypto.Hash([]byte{0x41, 0x6b, 0x69, 0x6b, 0x69})

	service := MakeTransactionSyncService(mLogger, &mNodeConnector, true, "GENID", hashDigest, cfg, mThreadPool)

	a.NotNil(service)

	service.Start()
	service.Stop()

	a.True(calledEventsInNodeConnector)

	a.Nil(service.cancelCtx)
	a.Nil(service.ctx)

}

// TestMakeTransactionSyncService tests that an appropriate transaction sync service was made
func TestMakeTransactionSyncService(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	mLogger := mockLogger{}
	mNodeConnector := &mockNodeConnector{}
	cfg := config.GetDefaultLocal()
	mThreadPool := mockThreadPool{}

	hashDigest := crypto.Hash([]byte{0x41, 0x6b, 0x69, 0x6b, 0x69})

	service1 := MakeTransactionSyncService(mLogger, mNodeConnector, true, "GENID", hashDigest, cfg, mThreadPool)

	a.NotNil(service1)

	a.Equal(service1.state.node, mNodeConnector)
	a.Equal(service1.state.log, wrapLogger(mLogger, &cfg))
	a.Equal(service1.state.isRelay, true)
	a.Equal(service1.state.genesisID, "GENID")
	a.Equal(service1.state.genesisHash, hashDigest)
	a.Equal(service1.state.config, cfg)
	a.Equal(service1.state.threadpool, mThreadPool)
	a.Equal(service1.state.service, service1)
	a.Equal(service1.state.xorBuilder.MaxIterations, 10)

	service2 := MakeTransactionSyncService(mLogger, mNodeConnector, false, "GENID2", hashDigest, cfg, mThreadPool)

	a.NotNil(service1)

	a.Equal(service2.state.node, mNodeConnector)
	a.Equal(service2.state.log, wrapLogger(mLogger, &cfg))
	a.Equal(service2.state.isRelay, false)
	a.Equal(service2.state.genesisID, "GENID2")
	a.Equal(service2.state.genesisHash, hashDigest)
	a.Equal(service2.state.config, cfg)
	a.Equal(service2.state.threadpool, mThreadPool)
	a.Equal(service2.state.service, service2)
	a.Equal(service2.state.xorBuilder.MaxIterations, 10)

}