-
Notifications
You must be signed in to change notification settings - Fork 641
Fix Taiko Engine Api #8974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix Taiko Engine Api #8974
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/Nethermind/Nethermind.Core.Test/Container/OrderedComponentsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Autofac; | ||
| using FluentAssertions; | ||
| using Nethermind.Core.Container; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Nethermind.Core.Test.Container; | ||
|
|
||
| public class OrderedComponentsTests | ||
| { | ||
| [Test] | ||
| public void TestNestedModuleConsistency() | ||
| { | ||
| using IContainer ctx = new ContainerBuilder() | ||
| .AddModule(new ModuleA()) | ||
| .AddLast(_ => new Item("4")) | ||
| .Build(); | ||
|
|
||
| ctx.Resolve<Item[]>().Select(item => item.Name).Should().BeEquivalentTo(["1", "2", "3", "4"]); | ||
| ctx.Resolve<IEnumerable<Item>>().Select(item => item.Name).Should().BeEquivalentTo(["1", "2", "3", "4"]); | ||
| ctx.Resolve<IReadOnlyList<Item>>().Select(item => item.Name).Should().BeEquivalentTo(["1", "2", "3", "4"]); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestAddFirst() | ||
| { | ||
| using IContainer ctx = new ContainerBuilder() | ||
| .AddLast(_ => new Item("2")) | ||
| .AddLast(_ => new Item("3")) | ||
| .AddFirst(_ => new Item("1")) | ||
| .Build(); | ||
|
|
||
| ctx.Resolve<Item[]>().Select(item => item.Name).Should().BeEquivalentTo(["1", "2", "3"]); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestDisallowIndividualRegistration() | ||
| { | ||
| Action act = () => new ContainerBuilder() | ||
| .AddLast(_ => new Item("1")) | ||
| .AddSingleton<Item>(_ => new Item("2")) | ||
| .Build(); | ||
|
|
||
| act.Should().Throw<InvalidOperationException>(); | ||
| } | ||
|
|
||
| private class ModuleA : Module | ||
| { | ||
| protected override void Load(ContainerBuilder builder) => | ||
| builder | ||
| .AddModule(new ModuleB()) | ||
| .AddLast(_ => new Item("3")); | ||
| } | ||
|
|
||
| private class ModuleB : Module | ||
| { | ||
| protected override void Load(ContainerBuilder builder) => | ||
| builder | ||
| .AddModule(new ModuleC()) | ||
| .AddLast(_ => new Item("2")); | ||
| } | ||
|
|
||
|
|
||
| private class ModuleC : Module | ||
| { | ||
| protected override void Load(ContainerBuilder builder) => | ||
| builder | ||
| .AddLast(_ => new Item("1")); | ||
| } | ||
| private record Item(string Name); | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/Nethermind/Nethermind.Core/Container/OrderedComponents.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| namespace Nethermind.Core.Container; | ||
|
|
||
| public class OrderedComponents<T> | ||
| { | ||
| private IList<T> _components = new List<T>(); | ||
| public IEnumerable<T> Components => _components; | ||
|
|
||
| public void AddLast(T item) | ||
| { | ||
| _components.Add(item); | ||
| } | ||
|
|
||
| public void AddFirst(T item) | ||
| { | ||
| _components.Insert(0, item); | ||
| } | ||
| } | ||
74 changes: 74 additions & 0 deletions
74
src/Nethermind/Nethermind.Core/Container/OrderedComponentsContainerBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Autofac; | ||
| using Autofac.Core; | ||
|
|
||
| namespace Nethermind.Core.Container; | ||
|
|
||
| /// <summary> | ||
| /// A set of dsl to register components where the order matter. Internally it has an <see cref="OrderedComponents{T}"/> | ||
| /// and uses decorators to add the items. The order of invocation matter, but there is explicit method like <see cref="AddFirst{T}"/> | ||
| /// allowing component that should appear first to appear first. | ||
| /// </summary> | ||
| public static class OrderedComponentsContainerBuilderExtensions | ||
| { | ||
| public static ContainerBuilder AddLast<T>(this ContainerBuilder builder, Func<IComponentContext, T> factory) => | ||
| builder | ||
| .EnsureOrderedComponents<T>() | ||
| .AddDecorator<OrderedComponents<T>>((ctx, orderedComponents) => | ||
| { | ||
| orderedComponents.AddLast(factory(ctx)); | ||
| return orderedComponents; | ||
| }); | ||
|
|
||
| public static ContainerBuilder AddFirst<T>(this ContainerBuilder builder, Func<IComponentContext, T> factory) => | ||
| builder | ||
| .EnsureOrderedComponents<T>() | ||
| .AddDecorator<OrderedComponents<T>>((ctx, orderedComponents) => | ||
| { | ||
| orderedComponents.AddFirst(factory(ctx)); | ||
| return orderedComponents; | ||
| }); | ||
|
|
||
| private static ContainerBuilder EnsureOrderedComponents<T>(this ContainerBuilder builder) | ||
| { | ||
| string registeredMarker = $"Registerd OrderedComponents For {typeof(T).Name}"; | ||
| if (!builder.Properties.TryAdd(registeredMarker, null)) | ||
| { | ||
| return builder; | ||
| } | ||
|
|
||
| // Prevent registering separately which has no explicit ordering | ||
| builder.RegisterBuildCallback(scope => | ||
| { | ||
| if (scope.ComponentRegistry.ServiceRegistrationsFor(new TypedService(typeof(T))).Any()) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Service of type {typeof(T).Name} must only be registered with one of DSL in {nameof(OrderedComponentsContainerBuilderExtensions)}"); | ||
| } | ||
| }); | ||
|
|
||
| // Not a singleton which allow it to work seamlessly with scoped lifetime with additional component | ||
| builder.Add<OrderedComponents<T>>(); | ||
|
|
||
| builder | ||
| .Register((ctx) => ctx.Resolve<OrderedComponents<T>>().Components) | ||
| .As<IEnumerable<T>>() | ||
| .Fixed(); | ||
|
|
||
| builder.Register((ctx) => ctx.Resolve<OrderedComponents<T>>().Components.ToArray()) | ||
| .As<IReadOnlyList<T>>() | ||
| .As<T[]>() | ||
| .Fixed(); | ||
|
|
||
| builder.Register((ctx) => ctx.Resolve<OrderedComponents<T>>().Components.ToList()) | ||
| .As<IList<T>>() | ||
| .Fixed(); | ||
|
|
||
| return builder; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering, this API isn't enforcing which will be the last element? In case of two consecutive
AddLastcalls, the first call becomes effectively invalid and it may get overridden. Would it make sense to somehow enforce calling these functions only once?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ow, I did not expect that use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that can make sense for other use case, but in this case, there are other rpc module also. We just need taiko to run after merge.