-
Notifications
You must be signed in to change notification settings - Fork 479
/
node_test.go
1029 lines (865 loc) · 33.2 KB
/
node_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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2019-2024 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 node
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/algorand/go-algorand/agreement"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/account"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/network"
"github.com/algorand/go-algorand/network/p2p"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/stateproof"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/algorand/go-algorand/util"
"github.com/algorand/go-algorand/util/db"
)
var expectedAgreementTime = 2*config.Protocol.BigLambda + config.Protocol.SmallLambda + config.Consensus[protocol.ConsensusCurrentVersion].AgreementFilterTimeout + 2*time.Second
var sinkAddr = basics.Address{0x7, 0xda, 0xcb, 0x4b, 0x6d, 0x9e, 0xd1, 0x41, 0xb1, 0x75, 0x76, 0xbd, 0x45, 0x9a, 0xe6, 0x42, 0x1d, 0x48, 0x6d, 0xa3, 0xd4, 0xef, 0x22, 0x47, 0xc4, 0x9, 0xa3, 0x96, 0xb8, 0x2e, 0xa2, 0x21}
var poolAddr = basics.Address{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
var defaultConfig = config.Local{
Archival: false,
GossipFanout: 4,
NetAddress: "",
BaseLoggerDebugLevel: 1,
IncomingConnectionsLimit: -1,
}
type nodeInfo struct {
idx int
host string
wsPort int
p2pPort int
p2pID p2p.PeerID
rootDir string
genesis bookkeeping.Genesis
}
func (ni nodeInfo) wsNetAddr() string {
return fmt.Sprintf("%s:%d", ni.host, ni.wsPort)
}
func (ni nodeInfo) p2pNetAddr() string {
return fmt.Sprintf("%s:%d", ni.host, ni.p2pPort)
}
func (ni nodeInfo) p2pMultiAddr() string {
return fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", ni.host, ni.p2pPort, ni.p2pID.String())
}
type configHook func(ni nodeInfo, cfg config.Local) (nodeInfo, config.Local)
type phonebookHook func([]nodeInfo, int) []string
func setupFullNodes(t *testing.T, proto protocol.ConsensusVersion, customConsensus config.ConsensusProtocols) ([]*AlgorandFullNode, []string) {
minMoneyAtStart := 10000
maxMoneyAtStart := 100000
gen := rand.New(rand.NewSource(2))
const numAccounts = 10
acctStake := make([]basics.MicroAlgos, numAccounts)
for i := range acctStake {
acctStake[i] = basics.MicroAlgos{Raw: uint64(minMoneyAtStart + (gen.Int() % (maxMoneyAtStart - minMoneyAtStart)))}
}
configHook := func(ni nodeInfo, cfg config.Local) (nodeInfo, config.Local) {
cfg.NetAddress = ni.wsNetAddr()
return ni, cfg
}
phonebookHook := func(nodes []nodeInfo, nodeIdx int) []string {
phonebook := make([]string, 0, len(nodes)-1)
for i := range nodes {
if i != nodeIdx {
phonebook = append(phonebook, nodes[i].wsNetAddr())
}
}
return phonebook
}
nodes, wallets := setupFullNodesEx(t, proto, customConsensus, acctStake, configHook, phonebookHook)
require.Len(t, nodes, numAccounts)
require.Len(t, wallets, numAccounts)
return nodes, wallets
}
func setupFullNodesEx(
t *testing.T, proto protocol.ConsensusVersion, customConsensus config.ConsensusProtocols,
acctStake []basics.MicroAlgos, configHook configHook, phonebookHook phonebookHook,
) ([]*AlgorandFullNode, []string) {
util.SetFdSoftLimit(1000)
f, _ := os.Create(t.Name() + ".log")
logging.Base().SetJSONFormatter()
logging.Base().SetOutput(f)
logging.Base().SetLevel(logging.Debug)
t.Logf("Logging to %s\n", t.Name()+".log")
firstRound := basics.Round(0)
lastRound := basics.Round(200)
// The genesis configuration is missing allocations, but that's OK
// because we explicitly generated the sqlite database above (in
// installFullNode).
g := bookkeeping.Genesis{
SchemaID: "go-test-node-genesis",
Proto: proto,
Network: config.Devtestnet,
FeeSink: sinkAddr.String(),
RewardsPool: poolAddr.String(),
}
genesis := make(map[basics.Address]basics.AccountData)
numAccounts := len(acctStake)
wallets := make([]string, numAccounts)
nodeInfos := make([]nodeInfo, numAccounts)
for i := range wallets {
rootDirectory := t.TempDir()
nodeInfos[i] = nodeInfo{
idx: i,
host: "127.0.0.1",
wsPort: 10000 + 100*i,
p2pPort: 10000 + 100*i + 1,
rootDir: rootDirectory,
genesis: g,
}
ni, cfg := configHook(nodeInfos[i], defaultConfig)
nodeInfos[i] = ni
cfg.SaveToDisk(rootDirectory)
t.Logf("Root directory of node %d (%s): %s\n", i, ni.wsNetAddr(), rootDirectory)
genesisDir := filepath.Join(rootDirectory, g.ID())
os.Mkdir(genesisDir, 0700)
wname := config.RootKeyFilename(t.Name() + "wallet" + strconv.Itoa(i))
pname := config.PartKeyFilename(t.Name()+"wallet"+strconv.Itoa(i), uint64(firstRound), uint64(lastRound))
wallets[i] = wname
filename := filepath.Join(genesisDir, wname)
access, err := db.MakeAccessor(filename, false, false)
if err != nil {
panic(err)
}
root, err := account.GenerateRoot(access)
access.Close()
if err != nil {
panic(err)
}
filename = filepath.Join(genesisDir, pname)
access, err = db.MakeAccessor(filename, false, false)
if err != nil {
panic(err)
}
part, err := account.FillDBWithParticipationKeys(access, root.Address(), firstRound, lastRound, config.Consensus[protocol.ConsensusCurrentVersion].DefaultKeyDilution)
if err != nil {
panic(err)
}
access.Close()
data := basics.AccountData{
Status: basics.Online,
MicroAlgos: acctStake[i],
SelectionID: part.VRFSecrets().PK,
VoteID: part.VotingSecrets().OneTimeSignatureVerifier,
}
short := root.Address()
genesis[short] = data
}
genesis[poolAddr] = basics.AccountData{
Status: basics.Online,
MicroAlgos: basics.MicroAlgos{Raw: uint64(100000)},
}
for addr, data := range genesis {
g.Allocation = append(g.Allocation, bookkeeping.GenesisAllocation{
Address: addr.String(),
State: bookkeeping.GenesisAccountData{
Status: data.Status,
MicroAlgos: data.MicroAlgos,
VoteID: data.VoteID,
StateProofID: data.StateProofID,
SelectionID: data.SelectionID,
VoteFirstValid: data.VoteFirstValid,
VoteLastValid: data.VoteLastValid,
VoteKeyDilution: data.VoteKeyDilution,
},
})
}
nodes := make([]*AlgorandFullNode, numAccounts)
for i := range nodes {
rootDirectory := nodeInfos[i].rootDir
genesisDir := filepath.Join(rootDirectory, g.ID())
if customConsensus != nil {
err0 := config.SaveConfigurableConsensus(genesisDir, customConsensus)
require.Nil(t, err0)
err0 = config.LoadConfigurableConsensusProtocols(genesisDir)
require.Nil(t, err0)
}
cfg, err := config.LoadConfigFromDisk(rootDirectory)
phonebook := phonebookHook(nodeInfos, i)
require.NoError(t, err)
node, err := MakeFull(logging.Base(), rootDirectory, cfg, phonebook, g)
nodes[i] = node
require.NoError(t, err)
}
return nodes, wallets
}
func TestSyncingFullNode(t *testing.T) {
partitiontest.PartitionTest(t)
if testing.Short() {
t.Skip("Test takes ~50 seconds.")
}
if (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" && runtime.GOOS != "darwin") &&
strings.ToUpper(os.Getenv("CIRCLECI")) == "TRUE" {
t.Skip("Test is too heavy for amd64 builder running in parallel with other packages")
}
nodes, wallets := setupFullNodes(t, protocol.ConsensusCurrentVersion, nil)
for i := 0; i < len(nodes); i++ {
defer os.Remove(wallets[i])
defer nodes[i].Stop()
}
initialRound := nodes[0].ledger.NextRound()
startAndConnectNodes(nodes, defaultFirstNodeStartDelay)
counter := 0
for tests := uint64(0); tests < 16; tests++ {
timer := time.NewTimer(30*time.Second + 2*expectedAgreementTime)
for i := range wallets {
select {
case <-nodes[i].ledger.Wait(initialRound + basics.Round(tests)):
if i == 0 {
counter++
if counter == 5 {
go func() {
// after 5 blocks, have this node partitioned
nodes[0].net.DisconnectPeers()
time.Sleep(20 * time.Second)
nodes[0].net.RequestConnectOutgoing(false, nil)
}()
}
}
case <-timer.C:
require.Fail(t, fmt.Sprintf("no block notification for account: %d - %v. Iteration: %v", i, wallets[i], tests))
return
}
}
}
roundsCompleted := nodes[0].ledger.LastRound()
for i := basics.Round(0); i < roundsCompleted; i++ {
for wallet := range wallets {
e0, err := nodes[0].ledger.Block(i)
if err != nil {
panic(err)
}
ei, err := nodes[wallet].ledger.Block(i)
if err != nil {
panic(err)
}
require.Equal(t, e0.Hash(), ei.Hash())
}
}
}
func TestInitialSync(t *testing.T) {
partitiontest.PartitionTest(t)
if testing.Short() {
t.Skip("Test takes ~25 seconds.")
}
if (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" && runtime.GOOS != "darwin") &&
strings.ToUpper(os.Getenv("CIRCLECI")) == "TRUE" {
t.Skip("Test is too heavy for amd64 builder running in parallel with other packages")
}
nodes, wallets := setupFullNodes(t, protocol.ConsensusCurrentVersion, nil)
for i := 0; i < len(nodes); i++ {
defer os.Remove(wallets[i])
defer nodes[i].Stop()
}
initialRound := nodes[0].ledger.NextRound()
startAndConnectNodes(nodes, defaultFirstNodeStartDelay)
select {
case <-nodes[0].ledger.Wait(initialRound):
e0, err := nodes[0].ledger.Block(initialRound)
if err != nil {
panic(err)
}
e1, err := nodes[1].ledger.Block(initialRound)
if err != nil {
panic(err)
}
require.Equal(t, e1.Hash(), e0.Hash())
case <-time.After(60 * time.Second):
require.Fail(t, fmt.Sprintf("no block notification for wallet: %v.", wallets[0]))
return
}
}
func TestSimpleUpgrade(t *testing.T) {
partitiontest.PartitionTest(t)
if testing.Short() {
t.Skip("Test takes ~50 seconds.")
}
if (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" && runtime.GOOS != "darwin") &&
strings.ToUpper(os.Getenv("CIRCLECI")) == "TRUE" {
t.Skip("Test is too heavy for amd64 builder running in parallel with other packages")
}
// ConsensusTest0 is a version of ConsensusV0 used for testing
// (it has different approved upgrade paths).
const consensusTest0 = protocol.ConsensusVersion("test0")
// ConsensusTest1 is an extension of ConsensusTest0 that
// supports a sorted-list balance commitment.
const consensusTest1 = protocol.ConsensusVersion("test1")
configurableConsensus := make(config.ConsensusProtocols)
testParams0 := config.Consensus[protocol.ConsensusCurrentVersion]
testParams0.MinUpgradeWaitRounds = 0
testParams0.SupportGenesisHash = false
testParams0.UpgradeVoteRounds = 2
testParams0.UpgradeThreshold = 1
testParams0.DefaultUpgradeWaitRounds = 2
testParams0.MaxVersionStringLen = 64
testParams0.MaxTxnBytesPerBlock = 1000000
testParams0.DefaultKeyDilution = 10000
testParams0.ApprovedUpgrades = map[protocol.ConsensusVersion]uint64{
consensusTest1: 0,
}
configurableConsensus[consensusTest0] = testParams0
testParams1 := config.Consensus[protocol.ConsensusCurrentVersion]
testParams1.MinUpgradeWaitRounds = 0
testParams1.SupportGenesisHash = false
testParams1.UpgradeVoteRounds = 10
testParams1.UpgradeThreshold = 8
testParams1.DefaultUpgradeWaitRounds = 10
testParams1.MaxVersionStringLen = 64
testParams1.MaxTxnBytesPerBlock = 1000000
testParams1.DefaultKeyDilution = 10000
testParams1.ApprovedUpgrades = map[protocol.ConsensusVersion]uint64{}
configurableConsensus[consensusTest1] = testParams1
nodes, wallets := setupFullNodes(t, consensusTest0, configurableConsensus)
for i := 0; i < len(nodes); i++ {
defer os.Remove(wallets[i])
defer nodes[i].Stop()
}
initialRound := nodes[0].ledger.NextRound()
startAndConnectNodes(nodes, nodelayFirstNodeStartDelay)
maxRounds := basics.Round(16)
roundsCheckedForUpgrade := 0
for tests := basics.Round(0); tests < maxRounds; tests++ {
blocks := make([]bookkeeping.Block, len(wallets))
for i := range wallets {
select {
case <-nodes[i].ledger.Wait(initialRound + tests):
blk, err := nodes[i].ledger.Block(initialRound + tests)
if err != nil {
panic(err)
}
blocks[i] = blk
case <-time.After(60 * time.Second):
require.Fail(t, fmt.Sprintf("no block notification for account: %v. Iteration: %v", wallets[i], tests))
return
}
}
blockDigest := blocks[0].Hash()
for i := range wallets {
require.Equal(t, blockDigest, blocks[i].Hash())
}
// On the first round, check that we did not upgrade
if tests == 0 {
roundsCheckedForUpgrade++
for i := range wallets {
require.Equal(t, consensusTest0, blocks[i].CurrentProtocol)
}
}
// On the last round, check that we upgraded
if tests == maxRounds-1 {
roundsCheckedForUpgrade++
for i := range wallets {
require.Equal(t, consensusTest1, blocks[i].CurrentProtocol)
}
}
}
require.Equal(t, 2, roundsCheckedForUpgrade)
}
const defaultFirstNodeStartDelay = 20 * time.Second
const nodelayFirstNodeStartDelay = 0
func startAndConnectNodes(nodes []*AlgorandFullNode, delayStartFirstNode time.Duration) {
var wg sync.WaitGroup
for i := range nodes {
if delayStartFirstNode != nodelayFirstNodeStartDelay && i == 0 {
continue
}
wg.Add(1)
go func(i int) {
defer wg.Done()
nodes[i].Start()
}(i)
}
wg.Wait()
if delayStartFirstNode != nodelayFirstNodeStartDelay {
connectPeers(nodes[1:])
delayStartNode(nodes[0], nodes[1:], delayStartFirstNode)
} else {
connectPeers(nodes)
}
}
func connectPeers(nodes []*AlgorandFullNode) {
for _, node := range nodes {
node.net.RequestConnectOutgoing(false, nil)
}
}
func delayStartNode(node *AlgorandFullNode, peers []*AlgorandFullNode, delay time.Duration) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(delay)
node.Start()
}()
wg.Wait()
for _, peer := range peers {
peer.net.RequestConnectOutgoing(false, nil)
}
node.net.RequestConnectOutgoing(false, nil)
}
func TestStatusReport_TimeSinceLastRound(t *testing.T) {
partitiontest.PartitionTest(t)
type fields struct {
LastRoundTimestamp time.Time
}
tests := []struct {
name string
fields fields
want time.Duration
wantError bool
}{
// test cases
{
name: "test1",
fields: fields{
LastRoundTimestamp: time.Time{},
},
want: time.Duration(0),
wantError: false,
},
{
name: "test2",
fields: fields{
LastRoundTimestamp: time.Now(),
},
want: time.Duration(0),
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
status := StatusReport{
LastRoundTimestamp: tt.fields.LastRoundTimestamp,
}
if got := status.TimeSinceLastRound(); got != tt.want {
if !tt.wantError {
t.Errorf("StatusReport.TimeSinceLastRound() = %v, want = %v", got, tt.want)
}
} else if tt.wantError {
t.Errorf("StatusReport.TimeSinceLastRound() = %v, want != %v", got, tt.want)
}
})
}
}
type mismatchingDirectroyPermissionsLog struct {
logging.Logger
t *testing.T
}
func (m mismatchingDirectroyPermissionsLog) Errorf(fmts string, args ...interface{}) {
fmtStr := fmt.Sprintf(fmts, args...)
require.Contains(m.t, fmtStr, "Unable to create genesis directory")
}
// TestMismatchingGenesisDirectoryPermissions tests to see that the os.MkDir check we have in MakeFull works as expected. It tests both the return error as well as the logged error.
func TestMismatchingGenesisDirectoryPermissions(t *testing.T) {
partitiontest.PartitionTest(t)
testDirectroy := t.TempDir()
genesis := bookkeeping.Genesis{
SchemaID: "go-test-node-genesis",
Proto: protocol.ConsensusCurrentVersion,
Network: config.Devtestnet,
FeeSink: sinkAddr.String(),
RewardsPool: poolAddr.String(),
}
log := mismatchingDirectroyPermissionsLog{logging.TestingLog(t), t}
require.NoError(t, os.Chmod(testDirectroy, 0200))
node, err := MakeFull(log, testDirectroy, config.GetDefaultLocal(), []string{}, genesis)
require.Nil(t, node)
require.Error(t, err)
require.Contains(t, err.Error(), "permission denied")
require.NoError(t, os.Chmod(testDirectroy, 1700))
require.NoError(t, os.RemoveAll(testDirectroy))
}
// TestDefaultResourcePaths confirms that when no extra configuration is provided, all resources are created in the dataDir
func TestDefaultResourcePaths(t *testing.T) {
partitiontest.PartitionTest(t)
testDirectory := t.TempDir()
genesis := bookkeeping.Genesis{
SchemaID: "gen",
Proto: protocol.ConsensusCurrentVersion,
Network: config.Devtestnet,
FeeSink: sinkAddr.String(),
RewardsPool: poolAddr.String(),
}
cfg := config.GetDefaultLocal()
// the logger is set up by the server, so we don't test this here
log := logging.Base()
n, err := MakeFull(log, testDirectory, cfg, []string{}, genesis)
n.Start()
defer n.Stop()
require.NoError(t, err)
// confirm genesis dir exists in the data dir, and that resources exist in the expected locations
require.DirExists(t, filepath.Join(testDirectory, genesis.ID()))
_, err = os.Stat(filepath.Join(testDirectory, genesis.ID(), "ledger.tracker.sqlite"))
require.NoError(t, err)
_, err = os.Stat(filepath.Join(testDirectory, genesis.ID(), "stateproof.sqlite"))
require.NoError(t, err)
_, err = os.Stat(filepath.Join(testDirectory, genesis.ID(), "ledger.block.sqlite"))
require.NoError(t, err)
_, err = os.Stat(filepath.Join(testDirectory, genesis.ID(), "partregistry.sqlite"))
require.NoError(t, err)
_, err = os.Stat(filepath.Join(testDirectory, genesis.ID(), "crash.sqlite"))
require.NoError(t, err)
}
// TestConfiguredDataDirs tests to see that when HotDataDir and ColdDataDir are set, underlying resources are created in the correct locations
// Not all resources are tested here, because not all resources use the paths provided to them immediately. For example, catchpoint only creates
// a directory when writing a catchpoint file, which is not being done here with this simple node
func TestConfiguredDataDirs(t *testing.T) {
partitiontest.PartitionTest(t)
testDirectory := t.TempDir()
testDirHot := t.TempDir()
testDirCold := t.TempDir()
genesis := bookkeeping.Genesis{
SchemaID: "go-test-node-genesis",
Proto: protocol.ConsensusCurrentVersion,
Network: config.Devtestnet,
FeeSink: sinkAddr.String(),
RewardsPool: poolAddr.String(),
}
cfg := config.GetDefaultLocal()
cfg.HotDataDir = testDirHot
cfg.ColdDataDir = testDirCold
cfg.CatchpointTracking = 2
cfg.CatchpointInterval = 1
// the logger is set up by the server, so we don't test this here
log := logging.Base()
n, err := MakeFull(log, testDirectory, cfg, []string{}, genesis)
require.NoError(t, err)
n.Start()
defer n.Stop()
// confirm hot data dir exists and contains a genesis dir
require.DirExists(t, filepath.Join(testDirHot, genesis.ID()))
// confirm the tracker is in the genesis dir of hot data dir
require.FileExists(t, filepath.Join(testDirHot, genesis.ID(), "ledger.tracker.sqlite"))
// confirm the stateproof db in the genesis dir of hot data dir
require.FileExists(t, filepath.Join(testDirHot, genesis.ID(), "stateproof.sqlite"))
// confirm cold data dir exists and contains a genesis dir
require.DirExists(t, filepath.Join(testDirCold, genesis.ID()))
// confirm the blockdb is in the genesis dir of cold data dir
require.FileExists(t, filepath.Join(testDirCold, genesis.ID(), "ledger.block.sqlite"))
// confirm the partregistry is in the genesis dir of cold data dir
require.FileExists(t, filepath.Join(testDirCold, genesis.ID(), "partregistry.sqlite"))
// confirm the agreement crash DB is in the genesis dir of hot data dir
require.FileExists(t, filepath.Join(testDirHot, genesis.ID(), "crash.sqlite"))
}
// TestConfiguredResourcePaths tests to see that when TrackerDbFilePath, BlockDbFilePath, StateproofDir, and CrashFilePath are set, underlying resources are created in the correct locations
func TestConfiguredResourcePaths(t *testing.T) {
partitiontest.PartitionTest(t)
testDirectory := t.TempDir()
testDirHot := t.TempDir()
testDirCold := t.TempDir()
// add a path for each resource now
trackerPath := filepath.Join(testDirectory, "custom_tracker")
blockPath := filepath.Join(testDirectory, "custom_block")
stateproofDir := filepath.Join(testDirectory, "custom_stateproof")
crashPath := filepath.Join(testDirectory, "custom_crash")
genesis := bookkeeping.Genesis{
SchemaID: "go-test-node-genesis",
Proto: protocol.ConsensusCurrentVersion,
Network: config.Devtestnet,
FeeSink: sinkAddr.String(),
RewardsPool: poolAddr.String(),
}
cfg := config.GetDefaultLocal()
cfg.HotDataDir = testDirHot
cfg.ColdDataDir = testDirCold
cfg.TrackerDBDir = trackerPath
cfg.BlockDBDir = blockPath
cfg.StateproofDir = stateproofDir
cfg.CrashDBDir = crashPath
// the logger is set up by the server, so we don't test this here
log := logging.Base()
n, err := MakeFull(log, testDirectory, cfg, []string{}, genesis)
require.NoError(t, err)
n.Start()
defer n.Stop()
// confirm hot data dir exists and contains a genesis dir
require.DirExists(t, filepath.Join(testDirHot, genesis.ID()))
// the tracker shouldn't be in the hot data dir, but rather the custom path's genesis dir
require.NoFileExists(t, filepath.Join(testDirHot, genesis.ID(), "ledger.tracker.sqlite"))
require.FileExists(t, filepath.Join(cfg.TrackerDBDir, genesis.ID(), "ledger.tracker.sqlite"))
// same with stateproofs
require.NoFileExists(t, filepath.Join(testDirHot, genesis.ID(), "stateproof.sqlite"))
require.FileExists(t, filepath.Join(cfg.StateproofDir, genesis.ID(), "stateproof.sqlite"))
// confirm cold data dir exists and contains a genesis dir
require.DirExists(t, filepath.Join(testDirCold, genesis.ID()))
// block db shouldn't be in the cold data dir, but rather the custom path's genesis dir
require.NoFileExists(t, filepath.Join(testDirCold, genesis.ID(), "ledger.block.sqlite"))
require.FileExists(t, filepath.Join(cfg.BlockDBDir, genesis.ID(), "ledger.block.sqlite"))
require.NoFileExists(t, filepath.Join(testDirCold, genesis.ID(), "crash.sqlite"))
require.FileExists(t, filepath.Join(cfg.CrashDBDir, genesis.ID(), "crash.sqlite"))
}
// TestOfflineOnlineClosedBitStatus a test that validates that the correct bits are being set
func TestOfflineOnlineClosedBitStatus(t *testing.T) {
partitiontest.PartitionTest(t)
tests := []struct {
name string
acctData basics.OnlineAccountData
expectedInt int
}{
{"online 1", basics.OnlineAccountData{
VotingData: basics.VotingData{VoteFirstValid: 1, VoteLastValid: 100},
MicroAlgosWithRewards: basics.MicroAlgos{Raw: 0}}, 0},
{"online 2", basics.OnlineAccountData{
VotingData: basics.VotingData{VoteFirstValid: 1, VoteLastValid: 100},
MicroAlgosWithRewards: basics.MicroAlgos{Raw: 1}}, 0},
{"offline & not closed", basics.OnlineAccountData{
VotingData: basics.VotingData{VoteFirstValid: 0, VoteLastValid: 0},
MicroAlgosWithRewards: basics.MicroAlgos{Raw: 1}}, 0 | bitAccountOffline},
{"offline & closed", basics.OnlineAccountData{
VotingData: basics.VotingData{VoteFirstValid: 0, VoteLastValid: 0},
MicroAlgosWithRewards: basics.MicroAlgos{Raw: 0}}, 0 | bitAccountOffline | bitAccountIsClosed},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.expectedInt, getOfflineClosedStatus(test.acctData))
})
}
}
// TestMaxSizesCorrect tests that constants defined in the protocol package are correct
// and match the MaxSize() values of associated msgp encodable structs.
// the test is located here since it needs to import various other packages.
//
// If this test fails, DO NOT JUST UPDATE THE CONSTANTS OR MODIFY THE TEST!
// Instead you need to introduce a new version of the protocol and mechanisms
// to ensure that nodes on different proto versions don't reject each others messages due to exceeding
// max size network protocol version
func TestMaxSizesCorrect(t *testing.T) {
partitiontest.PartitionTest(t)
/************************************************
* ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! *
* Read the comment before touching this test! *
* ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! *
*************************************************
*/ ////////////////////////////////////////////////
avSize := uint64(agreement.UnauthenticatedVoteMaxSize())
require.Equal(t, avSize, protocol.AgreementVoteTag.MaxMessageSize())
miSize := uint64(network.MessageOfInterestMaxSize())
require.Equal(t, miSize, protocol.MsgOfInterestTag.MaxMessageSize())
npSize := uint64(NetPrioResponseSignedMaxSize())
require.Equal(t, npSize, protocol.NetPrioResponseTag.MaxMessageSize())
nsSize := uint64(network.IdentityVerificationMessageSignedMaxSize())
require.Equal(t, nsSize, protocol.NetIDVerificationTag.MaxMessageSize())
piSize := uint64(network.PingLength)
require.Equal(t, piSize, protocol.PingTag.MaxMessageSize())
pjSize := uint64(network.PingLength)
require.Equal(t, pjSize, protocol.PingReplyTag.MaxMessageSize())
ppSize := uint64(agreement.TransmittedPayloadMaxSize())
require.Equal(t, ppSize, protocol.ProposalPayloadTag.MaxMessageSize())
spSize := uint64(stateproof.SigFromAddrMaxSize())
require.Equal(t, spSize, protocol.StateProofSigTag.MaxMessageSize())
txSize := uint64(transactions.SignedTxnMaxSize())
require.Equal(t, txSize, protocol.TxnTag.MaxMessageSize())
msSize := uint64(crypto.DigestMaxSize())
require.Equal(t, msSize, protocol.MsgDigestSkipTag.MaxMessageSize())
// UE is a handrolled message not using msgp
// including here for completeness ensured by protocol.TestMaxSizesTested
ueSize := uint64(67)
require.Equal(t, ueSize, protocol.UniEnsBlockReqTag.MaxMessageSize())
// VB and TS are the largest messages and are using the default network max size
// including here for completeness ensured by protocol.TestMaxSizesTested
vbSize := uint64(network.MaxMessageLength)
require.Equal(t, vbSize, protocol.VoteBundleTag.MaxMessageSize())
tsSize := uint64(network.MaxMessageLength)
require.Equal(t, tsSize, protocol.TopicMsgRespTag.MaxMessageSize())
}
// TestNodeHybridTopology set ups 3 nodes network with the following topology:
// N -- R -- A and ensures N can discover A and download blocks from it.
//
// N is a non-part node that joins the network later
// R is a non-archival relay node with block service disabled. It MUST NOT service blocks to force N to discover A.
// A is a archival node that can only provide blocks.
// Nodes N and A have only R in their initial phonebook, and all nodes are in hybrid mode.
func TestNodeHybridTopology(t *testing.T) {
partitiontest.PartitionTest(t)
const consensusTest0 = protocol.ConsensusVersion("test0")
configurableConsensus := make(config.ConsensusProtocols)
testParams0 := config.Consensus[protocol.ConsensusCurrentVersion]
testParams0.AgreementFilterTimeoutPeriod0 = 500 * time.Millisecond
configurableConsensus[consensusTest0] = testParams0
minMoneyAtStart := 1_000_000
maxMoneyAtStart := 100_000_000_000
gen := rand.New(rand.NewSource(2))
const numAccounts = 3
acctStake := make([]basics.MicroAlgos, numAccounts)
for i := range acctStake {
acctStake[i] = basics.MicroAlgos{Raw: uint64(minMoneyAtStart + (gen.Int() % (maxMoneyAtStart - minMoneyAtStart)))}
}
acctStake[0] = basics.MicroAlgos{} // no stake at node 0
configHook := func(ni nodeInfo, cfg config.Local) (nodeInfo, config.Local) {
cfg = config.GetDefaultLocal()
if ni.idx != 2 {
cfg.EnableBlockService = false
cfg.EnableGossipBlockService = false
cfg.EnableLedgerService = false
cfg.CatchpointInterval = 0
cfg.Archival = false
} else {
// node 2 is archival
cfg.EnableBlockService = true
cfg.EnableGossipBlockService = true
cfg.EnableLedgerService = true
cfg.CatchpointInterval = 200
cfg.Archival = true
}
if ni.idx == 0 {
// do not allow node 0 (N) to make any outgoing connections
cfg.GossipFanout = 0
}
cfg.NetAddress = ni.wsNetAddr()
cfg.EnableP2PHybridMode = true
cfg.EnableDHTProviders = true
cfg.P2PPersistPeerID = true
privKey, err := p2p.GetPrivKey(cfg, ni.rootDir)
require.NoError(t, err)
ni.p2pID, err = p2p.PeerIDFromPublicKey(privKey.GetPublic())
require.NoError(t, err)
cfg.P2PNetAddress = ni.p2pNetAddr()
return ni, cfg
}
phonebookHook := func(ni []nodeInfo, i int) []string {
switch i {
case 0:
// node 0 (N) only accept connections at the beginning to learn about archival node from DHT
t.Logf("Node%d phonebook: empty", i)
return []string{}
case 1:
// node 1 (R) connects to all
t.Logf("Node%d phonebook: %s, %s, %s, %s", i, ni[0].wsNetAddr(), ni[2].wsNetAddr(), ni[0].p2pMultiAddr(), ni[2].p2pMultiAddr())
return []string{ni[0].wsNetAddr(), ni[2].wsNetAddr(), ni[0].p2pMultiAddr(), ni[2].p2pMultiAddr()}
case 2:
// node 2 (A) connects to R
t.Logf("Node%d phonebook: %s, %s", i, ni[1].wsNetAddr(), ni[1].p2pMultiAddr())
return []string{ni[1].wsNetAddr(), ni[1].p2pMultiAddr()}
default:
t.Errorf("not expected number of nodes: %d", i)
t.FailNow()
}
return nil
}
nodes, wallets := setupFullNodesEx(t, consensusTest0, configurableConsensus, acctStake, configHook, phonebookHook)
require.Len(t, nodes, 3)
require.Len(t, wallets, 3)
for i := 0; i < len(nodes); i++ {
defer os.Remove(wallets[i])
defer nodes[i].Stop()
}
startAndConnectNodes(nodes, 10*time.Second)
initialRound := nodes[0].ledger.NextRound()
targetRound := initialRound + 10
select {
case <-nodes[0].ledger.Wait(targetRound):
e0, err := nodes[0].ledger.Block(targetRound)
require.NoError(t, err)
e1, err := nodes[1].ledger.Block(targetRound)
require.NoError(t, err)
require.Equal(t, e1.Hash(), e0.Hash())
case <-time.After(120 * time.Second):
require.Fail(t, fmt.Sprintf("no block notification for wallet: %v.", wallets[0]))
}
}
// TestNodeP2PRelays creates a network of 3 nodes with the following topology:
// R1 (relay, DHT) -> R2 (relay, phonebook) <- N (part node)
// Expect N to discover R1 via DHT and connect to it.
func TestNodeP2PRelays(t *testing.T) {
partitiontest.PartitionTest(t)
const consensusTest0 = protocol.ConsensusVersion("test0")
configurableConsensus := make(config.ConsensusProtocols)
testParams0 := config.Consensus[protocol.ConsensusCurrentVersion]
testParams0.AgreementFilterTimeoutPeriod0 = 500 * time.Millisecond
configurableConsensus[consensusTest0] = testParams0
minMoneyAtStart := 1_000_000
maxMoneyAtStart := 100_000_000_000
gen := rand.New(rand.NewSource(2))
const numAccounts = 3
acctStake := make([]basics.MicroAlgos, numAccounts)
// only node N has stake
acctStake[2] = basics.MicroAlgos{Raw: uint64(minMoneyAtStart + (gen.Int() % (maxMoneyAtStart - minMoneyAtStart)))}
configHook := func(ni nodeInfo, cfg config.Local) (nodeInfo, config.Local) {
cfg = config.GetDefaultLocal()
cfg.BaseLoggerDebugLevel = uint32(logging.Debug)
cfg.EnableP2P = true
cfg.NetAddress = ""
cfg.EnableDHTProviders = true
cfg.P2PPersistPeerID = true
privKey, err := p2p.GetPrivKey(cfg, ni.rootDir)
require.NoError(t, err)
ni.p2pID, err = p2p.PeerIDFromPublicKey(privKey.GetPublic())
require.NoError(t, err)
switch ni.idx {
case 2:
// N is not a relay
default:
cfg.NetAddress = ni.p2pNetAddr()
}
return ni, cfg
}
phonebookHook := func(ni []nodeInfo, i int) []string {
switch i {
case 0:
// node R1 connects to R2
t.Logf("Node%d phonebook: %s", i, ni[1].p2pMultiAddr())
return []string{ni[1].p2pMultiAddr()}
case 1:
// node R2 connects to none one
t.Logf("Node%d phonebook: empty", i)
return []string{}
case 2:
// node N only connects to R1
t.Logf("Node%d phonebook: %s", i, ni[1].p2pMultiAddr())
return []string{ni[1].p2pMultiAddr()}
default:
t.Errorf("not expected number of nodes: %d", i)
t.FailNow()
}
return nil
}