Skip to content

Commit 06ca96e

Browse files
committed
Fix
1 parent 70455a4 commit 06ca96e

2 files changed

Lines changed: 41 additions & 9 deletions

File tree

src/Nethermind/Nethermind.Blockchain.Test/BlockProcessorTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,32 @@ public void ParallelWorldState_bal_validation_rejects_surplus_pre_execution_syst
347347
BlockAccessList suggestedBlockAccessList = new();
348348
suggestedBlockAccessList.AddAccountRead(Address.SystemUser);
349349
stateProvider.LoadSuggestedBlockAccessList(suggestedBlockAccessList, 10_000);
350+
Block block = Build.A.Block.WithNumber(1).TestObject;
350351

351-
TestDelegate act = () => stateProvider.ValidateBlockAccessList(Build.A.BlockHeader.TestObject, 0, 10_000);
352+
Assert.That(() => stateProvider.ValidateBlockAccessList(block.Header, 0, 10_000), Throws.Nothing);
352353

354+
TestDelegate act = () => stateProvider.SetBlockAccessList(block, Amsterdam.Instance);
353355
Assert.Throws<ParallelWorldState.InvalidBlockLevelAccessListException>(act);
354356
}
355357

358+
[Test, MaxTime(Timeout.MaxTestTime)]
359+
public void ParallelWorldState_bal_validation_allows_system_account_changed_after_pre_execution()
360+
{
361+
ParallelWorldState stateProvider = new(TestWorldStateFactory.CreateForTest());
362+
BlockAccessList suggestedBlockAccessList = new();
363+
suggestedBlockAccessList.IncrementBlockAccessIndex();
364+
suggestedBlockAccessList.AddBalanceChange(Address.SystemUser, 0, 1);
365+
stateProvider.LoadSuggestedBlockAccessList(suggestedBlockAccessList, 10_000);
366+
BlockHeader header = Build.A.BlockHeader.WithNumber(1).TestObject;
367+
368+
Assert.That(() => stateProvider.ValidateBlockAccessList(header, 0, 10_000), Throws.Nothing);
369+
370+
stateProvider.GeneratedBlockAccessList.IncrementBlockAccessIndex();
371+
stateProvider.GeneratedBlockAccessList.AddBalanceChange(Address.SystemUser, 0, 1);
372+
373+
Assert.That(() => stateProvider.ValidateBlockAccessList(header, 1, 10_000), Throws.Nothing);
374+
}
375+
356376
[Test, MaxTime(Timeout.MaxTestTime)]
357377
public void ParallelWorldState_bal_validation_rejects_missing_explicit_system_account_read()
358378
{

src/Nethermind/Nethermind.State/ParallelWorldState.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,6 @@ void AdvanceSuggested()
320320
}
321321
else if (generatedHead is null)
322322
{
323-
if (IsSystemAccountRead(suggestedHead.Value, index))
324-
{
325-
throw new InvalidBlockLevelAccessListException(block, $"Suggested block-level access list contained surplus changes for {suggestedHead.Value.Address} at index {index}.");
326-
}
327323
if (HasNoChanges(suggestedHead.Value))
328324
{
329325
AdvanceSuggested();
@@ -346,10 +342,6 @@ void AdvanceSuggested()
346342
}
347343
else if (cmp > 0)
348344
{
349-
if (IsSystemAccountRead(suggestedHead.Value, index))
350-
{
351-
throw new InvalidBlockLevelAccessListException(block, $"Suggested block-level access list contained surplus changes for {suggestedHead.Value.Address} at index {index}.");
352-
}
353345
if (HasNoChanges(suggestedHead.Value))
354346
{
355347
AdvanceSuggested();
@@ -396,6 +388,7 @@ public void SetBlockAccessList(Block block, IReleaseSpec spec)
396388
}
397389
else
398390
{
391+
ValidateSuggestedSystemAccountRead(block.Header);
399392
block.GeneratedBlockAccessList = GeneratedBlockAccessList;
400393
block.EncodedBlockAccessList = Rlp.Encode(GeneratedBlockAccessList).Bytes;
401394
block.Header.BlockAccessListHash = new(ValueKeccak.Compute(block.EncodedBlockAccessList).Bytes);
@@ -417,4 +410,23 @@ private static bool HasOptionalStorageReads(in ChangeAtIndex c)
417410

418411
private static bool IsSystemAccountRead(in ChangeAtIndex c, ushort index)
419412
=> index == 0 && c.Address == Address.SystemUser && HasNoChanges(c) && c.Reads == 0;
413+
414+
private void ValidateSuggestedSystemAccountRead(BlockHeader block)
415+
{
416+
if (_suggestedBlockAccessList?.GetAccountChanges(Address.SystemUser) is not { } suggestedSystemAccount ||
417+
GeneratedBlockAccessList.GetAccountChanges(Address.SystemUser) is not null ||
418+
!HasOnlyAccountRead(suggestedSystemAccount))
419+
{
420+
return;
421+
}
422+
423+
throw new InvalidBlockLevelAccessListException(block, $"Suggested block-level access list contained surplus changes for {Address.SystemUser} at index 0.");
424+
}
425+
426+
private static bool HasOnlyAccountRead(AccountChanges accountChanges)
427+
=> accountChanges.StorageChanges.Count == 0 &&
428+
accountChanges.StorageReads.Count == 0 &&
429+
accountChanges.BalanceChanges.Count == 0 &&
430+
accountChanges.NonceChanges.Count == 0 &&
431+
accountChanges.CodeChanges.Count == 0;
420432
}

0 commit comments

Comments
 (0)