-
Notifications
You must be signed in to change notification settings - Fork 464
/
SyncProgressResolverTests.cs
193 lines (170 loc) · 8.64 KB
/
SyncProgressResolverTests.cs
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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using Nethermind.Blockchain;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Test.Builders;
using Nethermind.Db;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.Synchronization.FastBlocks;
using Nethermind.Synchronization.ParallelSync;
using Nethermind.Synchronization.SnapSync;
using Nethermind.Trie.Pruning;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
using NUnit.Framework;
namespace Nethermind.Synchronization.Test
{
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class SyncProgressResolverTests
{
[Test]
public void Header_block_is_0_when_no_header_was_suggested()
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
IStateReader stateReader = Substitute.For<IStateReader>();
SyncConfig syncConfig = new()
{
PivotNumber = "1",
};
SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance);
blockTree.BestSuggestedHeader.ReturnsNull();
Assert.That(syncProgressResolver.FindBestHeader(), Is.EqualTo(0));
}
[Test]
public void Best_block_is_0_when_no_block_was_suggested()
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
IStateReader stateReader = Substitute.For<IStateReader>();
SyncConfig syncConfig = new()
{
PivotNumber = "1",
};
SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance);
blockTree.BestSuggestedBody.ReturnsNull();
Assert.That(syncProgressResolver.FindBestFullBlock(), Is.EqualTo(0));
}
[Test]
public void Best_state_is_head_when_there_are_no_suggested_blocks()
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
IStateReader stateReader = Substitute.For<IStateReader>();
SyncConfig syncConfig = new()
{
PivotNumber = "1",
};
SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance);
Block head = Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(5).WithStateRoot(TestItem.KeccakA).TestObject).TestObject;
blockTree.Head.Returns(head);
blockTree.BestSuggestedHeader.Returns(head.Header);
stateReader.HasStateForRoot(head.StateRoot!).Returns(true);
Assert.That(syncProgressResolver.FindBestFullState(), Is.EqualTo(head.Number));
}
[Test]
public void Best_state_is_suggested_if_there_is_suggested_block_with_state()
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
IStateReader stateReader = Substitute.For<IStateReader>();
SyncConfig syncConfig = new()
{
PivotNumber = "1",
};
SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance);
Block head = Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(5).WithStateRoot(TestItem.KeccakA).TestObject).TestObject;
BlockHeader suggested = Build.A.BlockHeader.WithNumber(6).WithStateRoot(TestItem.KeccakB).TestObject;
blockTree.Head.Returns(head);
blockTree.BestSuggestedHeader.Returns(suggested);
blockTree.FindHeader(Arg.Any<Hash256>(), BlockTreeLookupOptions.TotalDifficultyNotNeeded).Returns(head.Header);
stateReader.HasStateForRoot(head.StateRoot!).Returns(true);
stateReader.HasStateForRoot(suggested.StateRoot!).Returns(true);
Assert.That(syncProgressResolver.FindBestFullState(), Is.EqualTo(suggested.Number));
}
[Test]
public void Best_state_is_head_if_there_is_suggested_block_without_state()
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
IStateReader stateReader = Substitute.For<IStateReader>();
SyncConfig syncConfig = new()
{
PivotNumber = "1",
};
SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance);
Block head = Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(5).WithStateRoot(TestItem.KeccakA).TestObject).TestObject;
BlockHeader suggested = Build.A.BlockHeader.WithNumber(6).WithStateRoot(TestItem.KeccakB).TestObject;
blockTree.Head.Returns(head);
blockTree.BestSuggestedHeader.Returns(suggested);
blockTree.FindHeader(Arg.Any<Hash256>(), BlockTreeLookupOptions.TotalDifficultyNotNeeded).Returns(head.Header);
stateReader.HasStateForRoot(head.StateRoot!).Returns(true);
stateReader.HasStateForRoot(suggested.StateRoot!).Returns(false);
Assert.That(syncProgressResolver.FindBestFullState(), Is.EqualTo(head.Number));
}
[Test]
public void Is_fast_block_finished_returns_true_when_no_fast_block_sync_is_used()
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
IStateReader stateReader = Substitute.For<IStateReader>();
SyncConfig syncConfig = new()
{
FastBlocks = false,
PivotNumber = "1",
};
SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance);
Assert.True(syncProgressResolver.IsFastBlocksHeadersFinished());
Assert.True(syncProgressResolver.IsFastBlocksBodiesFinished());
Assert.True(syncProgressResolver.IsFastBlocksReceiptsFinished());
}
[Test]
public void Is_fast_block_bodies_finished_returns_false_when_blocks_not_downloaded()
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
IStateReader stateReader = Substitute.For<IStateReader>();
SyncConfig syncConfig = new()
{
FastBlocks = true,
FastSync = true,
DownloadBodiesInFastSync = true,
DownloadReceiptsInFastSync = true,
PivotNumber = "1",
};
blockTree.LowestInsertedHeader.Returns(Build.A.BlockHeader.WithNumber(1).WithStateRoot(TestItem.KeccakA).TestObject);
blockTree.LowestInsertedBodyNumber.Returns(2);
SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance);
Assert.False(syncProgressResolver.IsFastBlocksBodiesFinished());
}
[Test]
public void Is_fast_block_receipts_finished_returns_true_when_receipts_not_downloaded_and_we_do_not_want_to_download_receipts()
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
IStateReader stateReader = Substitute.For<IStateReader>();
SyncConfig syncConfig = new()
{
FastBlocks = true,
DownloadBodiesInFastSync = true,
DownloadReceiptsInFastSync = false,
PivotNumber = "1",
};
blockTree.LowestInsertedHeader.Returns(Build.A.BlockHeader.WithNumber(1).WithStateRoot(TestItem.KeccakA).TestObject);
blockTree.LowestInsertedBodyNumber.Returns(1);
SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, true, syncConfig, LimboLogs.Instance);
Assert.True(syncProgressResolver.IsFastBlocksReceiptsFinished());
}
private SyncProgressResolver CreateProgressResolver(IBlockTree blockTree, IStateReader stateReader, bool isReceiptFinished, SyncConfig syncConfig, LimboLogs limboLogs)
{
ISyncFeed<ReceiptsSyncBatch?> receiptFeed = Substitute.For<ISyncFeed<ReceiptsSyncBatch?>>();
receiptFeed.IsFinished.Returns(isReceiptFinished);
return new SyncProgressResolver(
blockTree,
new FullStateFinder(blockTree, stateReader),
syncConfig,
Substitute.For<ISyncFeed<HeadersSyncBatch?>>(),
Substitute.For<ISyncFeed<BodiesSyncBatch?>>(),
receiptFeed,
Substitute.For<ISyncFeed<SnapSyncBatch?>>(),
limboLogs
);
}
}
}