-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathAuraBlockProcessorTests.cs
More file actions
232 lines (211 loc) · 10.7 KB
/
AuraBlockProcessorTests.cs
File metadata and controls
232 lines (211 loc) · 10.7 KB
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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Nethermind.Blockchain;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Receipts;
using Nethermind.Blockchain.Test.Validators;
using Nethermind.Blockchain.Tracing;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Consensus.ExecutionRequests;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Transactions;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Core.Test;
using Nethermind.Core.Test.Builders;
using Nethermind.Evm;
using Nethermind.Evm.State;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.Specs;
using Nethermind.Specs.Forks;
using Nethermind.TxPool;
using NSubstitute;
using NUnit.Framework;
namespace Nethermind.AuRa.Test
{
public class AuraBlockProcessorTests
{
[Test]
public async Task Prepared_block_contains_author_field()
{
BranchProcessor processor = CreateProcessor().Processor;
BlockHeader header = Build.A.BlockHeader.WithAuthor(TestItem.AddressD).TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;
Block[] processedBlocks = await processor.Process(
null,
new List<Block> { block },
ProcessingOptions.None,
NullBlockTracer.Instance);
Assert.That(processedBlocks.Length, Is.EqualTo(1), "length");
Assert.That(processedBlocks[0].Author, Is.EqualTo(block.Author), "author");
}
[Test]
public void For_not_empty_block_tx_filter_should_be_called()
{
ITxFilter txFilter = Substitute.For<ITxFilter>();
txFilter
.IsAllowed(Arg.Any<Transaction>(), Arg.Any<BlockHeader>(), Arg.Any<IReleaseSpec>())
.Returns(AcceptTxResult.Accepted);
BranchProcessor processor = CreateProcessor(txFilter).Processor;
BlockHeader header = Build.A.BlockHeader.WithAuthor(TestItem.AddressD).WithNumber(3).TestObject;
Transaction tx = Nethermind.Core.Test.Builders.Build.A.Transaction.WithData(new byte[] { 0, 1 })
.SignedAndResolved().WithChainId(105).WithGasPrice(0).WithValue(0).TestObject;
Block block = Build.A.Block.WithHeader(header).WithTransactions(new Transaction[] { tx }).TestObject;
_ = processor.Process(
null,
new List<Block> { block },
ProcessingOptions.None,
NullBlockTracer.Instance);
txFilter.Received().IsAllowed(Arg.Any<Transaction>(), Arg.Any<BlockHeader>(), Arg.Any<IReleaseSpec>());
}
[Test]
public void For_normal_processing_it_should_not_fail_with_gas_remaining_rules()
{
BranchProcessor processor = CreateProcessor().Processor;
int gasLimit = 10000000;
BlockHeader header = Build.A.BlockHeader.WithAuthor(TestItem.AddressD).WithNumber(3).TestObject;
Transaction tx = Nethermind.Core.Test.Builders.Build.A.Transaction.WithData(new byte[] { 0, 1 })
.SignedAndResolved().WithChainId(105).WithGasPrice(0).WithValue(0).WithGasLimit(gasLimit + 1).TestObject;
Block block = Build.A.Block.WithHeader(header).WithTransactions(new Transaction[] { tx })
.WithGasLimit(gasLimit).TestObject;
Assert.DoesNotThrowAsync(() => processor.Process(
null,
new List<Block> { block },
ProcessingOptions.None,
NullBlockTracer.Instance));
}
[Test]
public async Task Should_rewrite_contracts([Values] bool isPostMerge)
{
static async Task<BlockHeader> Process(BranchProcessor auRaBlockProcessor, BlockHeader parent, IBlockTree blockTree, bool isPostMerge)
{
BlockHeader header = Build.A.BlockHeader
.WithAuthor(TestItem.AddressD)
.WithParent(parent)
.WithTimestamp(parent.Timestamp + 12)
.WithTotalDifficulty(0).TestObject;
header.IsPostMerge = isPostMerge;
Block block = Build.A.Block.WithHeader(header).TestObject;
BlockHeader res = (await auRaBlockProcessor.Process(
parent,
new List<Block> { block },
ProcessingOptions.None,
NullBlockTracer.Instance))[0].Header;
blockTree.Insert(res);
return res;
}
Dictionary<long, IDictionary<Address, byte[]>> contractOverrides = new()
{
{
2,
new Dictionary<Address, byte[]>()
{
{TestItem.AddressA, Bytes.FromHexString("0x123")},
{TestItem.AddressB, Bytes.FromHexString("0x321")},
}
},
{
3,
new Dictionary<Address, byte[]>()
{
{TestItem.AddressA, Bytes.FromHexString("0x456")},
{TestItem.AddressB, Bytes.FromHexString("0x654")},
}
},
};
(ulong, Address, byte[])[] contractOverridesTimestamp = [
(1000024, TestItem.AddressC, Bytes.FromHexString("0x123")),
(1000024, TestItem.AddressD, Bytes.FromHexString("0x321")),
(1000036, TestItem.AddressC, Bytes.FromHexString("0x456")),
(1000036, TestItem.AddressD, Bytes.FromHexString("0x654"))
];
(BranchProcessor processor, IWorldState stateProvider, IBlockTree blockTree) =
CreateProcessor(contractRewriter: new ContractRewriter(contractOverrides, contractOverridesTimestamp));
Hash256 stateRoot;
using (stateProvider.BeginScope(IWorldState.PreGenesis))
{
stateProvider.CreateAccount(TestItem.AddressA, UInt256.One);
stateProvider.CreateAccount(TestItem.AddressB, UInt256.One);
stateProvider.CreateAccount(TestItem.AddressC, UInt256.One);
stateProvider.CreateAccount(TestItem.AddressD, UInt256.One);
stateProvider.Commit(London.Instance);
stateProvider.CommitTree(0);
stateProvider.RecalculateStateRoot();
stateRoot = stateProvider.StateRoot;
}
BlockHeader currentBlock = Build.A.BlockHeader.WithNumber(0).WithStateRoot(stateRoot).TestObject;
currentBlock = await Process(processor, currentBlock, blockTree, isPostMerge);
using (stateProvider.BeginScope(currentBlock))
{
stateProvider.GetCode(TestItem.AddressA).Should().BeEquivalentTo(Array.Empty<byte>());
stateProvider.GetCode(TestItem.AddressB).Should().BeEquivalentTo(Array.Empty<byte>());
stateProvider.GetCode(TestItem.AddressC).Should().BeEquivalentTo(Array.Empty<byte>());
stateProvider.GetCode(TestItem.AddressD).Should().BeEquivalentTo(Array.Empty<byte>());
}
currentBlock = await Process(processor, currentBlock, blockTree, isPostMerge);
using (stateProvider.BeginScope(currentBlock))
{
stateProvider.GetCode(TestItem.AddressA).Should().BeEquivalentTo(Bytes.FromHexString("0x123"));
stateProvider.GetCode(TestItem.AddressB).Should().BeEquivalentTo(Bytes.FromHexString("0x321"));
stateProvider.GetCode(TestItem.AddressC).Should().BeEquivalentTo(Bytes.FromHexString("0x123"));
stateProvider.GetCode(TestItem.AddressD).Should().BeEquivalentTo(Bytes.FromHexString("0x321"));
}
currentBlock = await Process(processor, currentBlock, blockTree, isPostMerge);
using (stateProvider.BeginScope(currentBlock))
{
stateProvider.GetCode(TestItem.AddressA).Should().BeEquivalentTo(Bytes.FromHexString("0x456"));
stateProvider.GetCode(TestItem.AddressB).Should().BeEquivalentTo(Bytes.FromHexString("0x654"));
stateProvider.GetCode(TestItem.AddressC).Should().BeEquivalentTo(Bytes.FromHexString("0x456"));
stateProvider.GetCode(TestItem.AddressD).Should().BeEquivalentTo(Bytes.FromHexString("0x654"));
}
}
private (BranchProcessor Processor, IWorldState StateProvider, IBlockTree blockTree) CreateProcessor(ITxFilter? txFilter = null, ContractRewriter? contractRewriter = null)
{
IWorldState stateProvider = TestWorldStateFactory.CreateForTest(parallel: true);
IBlockTree blockTree = Build.A.BlockTree(GnosisSpecProvider.Instance).TestObject;
ITransactionProcessor transactionProcessor = Substitute.For<ITransactionProcessor>();
AuRaBlockProcessor processor = new(
GnosisSpecProvider.Instance,
new AuRaChainSpecEngineParameters(),
TestBlockValidator.AlwaysValid,
NoBlockRewards.Instance,
new BlockProcessor.BlockValidationTransactionsExecutor(
stateProvider,
new ExecuteTransactionProcessorAdapter(transactionProcessor),
new BlobBaseFeeCalculator(),
HoodiSpecProvider.Instance,
Substitute.For<IBlockhashProvider>(),
Substitute.For<ICodeInfoRepository>(),
LimboLogs.Instance),
stateProvider,
NullReceiptStorage.Instance,
new BeaconBlockRootHandler(transactionProcessor, stateProvider),
LimboLogs.Instance,
blockTree,
new WithdrawalProcessor(stateProvider, LimboLogs.Instance),
new ExecutionRequestsProcessor(transactionProcessor),
auRaValidator: null,
txFilter,
contractRewriter: contractRewriter);
BranchProcessor branchProcessor = new(
processor,
GnosisSpecProvider.Instance,
stateProvider,
new BeaconBlockRootHandler(transactionProcessor, stateProvider),
Substitute.For<IBlockhashProvider>(),
LimboLogs.Instance);
return (branchProcessor, stateProvider, blockTree);
}
}
}