Skip to content

Commit

Permalink
Merge branch 'master' into fix/recoversignatures
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 authored Oct 10, 2024
2 parents e9827e4 + 8296752 commit 2cb7f81
Show file tree
Hide file tree
Showing 70 changed files with 1,098 additions and 879 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/build-tools.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build tools

on:
merge_group:
types: [checks_requested]
pull_request:
branches: [master]
push:
branches: [master]

jobs:
build:
name: Build
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
config: [release]
project:
- docgen/DocGen.sln
- HiveCompare/HiveCompare.sln
- HiveConsensusWorkflowGenerator/HiveConsensusWorkflowGenerator.csproj
- Nethermind.Tools.Kute/Nethermind.Tools.Kute.csproj
- SendBlobs/SendBlobs.sln
- TxParser/TxParser.csproj
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up .NET
uses: actions/setup-dotnet@v4
- name: Build ${{ matrix.project }}
working-directory: tools
run: dotnet build ./${{ matrix.project }} -c ${{ matrix.config }}
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Config/IBlocksConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public interface IBlocksConfig : IConfig
[ConfigItem(Description = "Whether to pre-warm the state when processing blocks. This can lead to an up to 2x speed-up in the main loop block processing.", DefaultValue = "True")]
bool PreWarmStateOnBlockProcessing { get; set; }

[ConfigItem(Description = "Block Production timeout, in milliseconds.", DefaultValue = "4000")]
[ConfigItem(Description = "The block production timeout, in milliseconds.", DefaultValue = "4000")]
int BlockProductionTimeoutMs { get; set; }

[ConfigItem(Description = "Genesis block load timeout, in milliseconds.", DefaultValue = "40000")]
[ConfigItem(Description = "The genesis block load timeout, in milliseconds.", DefaultValue = "40000")]
int GenesisTimeoutMs { get; set; }

byte[] GetExtraDataBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only


using FluentAssertions;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Test.Builders;
Expand Down Expand Up @@ -29,5 +30,21 @@ public void Is_bump_on_1559_eip_block()
long actualValue = targetedAdjustedGasLimitCalculator.GetGasLimit(header);
Assert.That(actualValue, Is.EqualTo(gasLimit * Eip1559Constants.DefaultElasticityMultiplier));
}

[TestCase(30_000_000, 100_000_000, 30029295)]
public void Is_calculating_correct_gasLimit(long currentGasLimit, long targetGasLimit, long expectedGasLimit)
{
int blockNumber = 20_000_000;
long gasLimit = currentGasLimit;
TestSpecProvider specProvider = new(Prague.Instance);
TargetAdjustedGasLimitCalculator targetedAdjustedGasLimitCalculator = new(specProvider,
new BlocksConfig()
{
TargetBlockGasLimit = targetGasLimit
});
BlockHeader header = Build.A.BlockHeader.WithNumber(blockNumber - 1).WithGasLimit(gasLimit).TestObject;
long actualValue = targetedAdjustedGasLimitCalculator.GetGasLimit(header);
actualValue.Should().Be(expectedGasLimit);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Nethermind.Consensus.Processing.CensorshipDetector;

public class CensorshipDetectorConfig : ICensorshipDetectorConfig
{
public bool Enabled { get; set; } = true;
public bool Enabled { get; set; } = false;
public uint BlockCensorshipThreshold { get; set; } = 2;
public string[]? AddressesForCensorshipDetection { get; set; } = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ namespace Nethermind.Consensus.Processing.CensorshipDetector;

public interface ICensorshipDetectorConfig : IConfig
{
[ConfigItem(DefaultValue = "true",
Description = "Enabling censorship detection feature")]
[ConfigItem(DefaultValue = "false", Description = "Whether to enable censorship detection.")]
bool Enabled { get; set; }

[ConfigItem(DefaultValue = "2",
Description = "Number of consecutive blocks with detected potential censorship to report censorship attempt")]
Description = "The number of the consecutive blocks with detected potential censorship to report.")]
uint BlockCensorshipThreshold { get; set; }

[ConfigItem(DefaultValue = "null",
Description = "The addresses for which censorship is being detected.")]
[ConfigItem(DefaultValue = "null", Description = "The addresses to detect censorship for.")]
string[]? AddressesForCensorshipDetection { get; set; }
}
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Core/Collections/ArrayPoolList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,5 +363,6 @@ public void Dispose()

public Span<T> AsSpan() => _array.AsSpan(0, Count);

public ReadOnlyMemory<T> AsMemory() => new(_array, 0, Count);
public Memory<T> AsMemory() => new(_array, 0, Count);
public ReadOnlyMemory<T> AsReadOnlyMemory() => new(_array, 0, Count);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public CompositePublisher(params IPublisher[] publishers)

public async Task PublishAsync<T>(T data) where T : class
{
// TODO: .Net 9 stackalloc
Task[] tasks = new Task[_publishers.Length];
for (int i = 0; i < _publishers.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

namespace Nethermind.Evm.Test;

public class BlsMultiMulG1PrecompileTests
public class BlsG1MSMPrecompileTests
{
[Test]
public void Test()
{
foreach ((byte[] input, ReadOnlyMemory<byte> expectedResult) in Inputs)
{
IPrecompile precompile = G1MultiMulPrecompile.Instance;
(ReadOnlyMemory<byte> output, bool success) = precompile.Run(input, MuirGlacier.Instance);
IPrecompile precompile = G1MSMPrecompile.Instance;
(ReadOnlyMemory<byte> output, bool success) = precompile.Run(input, Prague.Instance);
output.ToArray().Should().BeEquivalentTo(expectedResult.ToArray());
success.Should().BeTrue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Nethermind.Evm.Test;

public class BlsAddG2PrecompileTests
public class BlsG2AddPrecompileTests
{
[Test]
public void Test()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
using Nethermind.Evm.Precompiles;
using Nethermind.Evm.Precompiles.Bls;
using NUnit.Framework;
using static Nethermind.Specs.Forks.MuirGlacier;
using Nethermind.Specs.Forks;

namespace Nethermind.Evm.Test;

public class BlsMultiMulG2PrecompileTests
public class BlsG2MSMPrecompileTests
{
[Test]
public void Test()
{
foreach ((byte[] input, ReadOnlyMemory<byte> expectedResult) in Inputs)
{
IPrecompile precompile = G2MultiMulPrecompile.Instance;
(ReadOnlyMemory<byte> output, bool success) = precompile.Run(input, Instance);
IPrecompile precompile = G2MSMPrecompile.Instance;
(ReadOnlyMemory<byte> output, bool success) = precompile.Run(input, Prague.Instance);
output.ToArray().Should().BeEquivalentTo(expectedResult.ToArray());
success.Should().BeTrue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

namespace Nethermind.Evm.Test;

public class BlsMapToG2Tests
public class BlsMapFp2ToG2Tests
{
[Test]
public void Test()
{
foreach ((byte[] input, ReadOnlyMemory<byte> expectedResult) in Inputs)
{
IPrecompile precompile = MapToG2Precompile.Instance;
IPrecompile precompile = MapFp2ToG2Precompile.Instance;
(ReadOnlyMemory<byte> output, bool success) = precompile.Run(input, MuirGlacier.Instance);

output.ToArray().Should().BeEquivalentTo(expectedResult.ToArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

namespace Nethermind.Evm.Test;

public class BlsMapToG1Tests
public class BlsMapFpToG1Tests
{
[Test]
public void Test()
{
foreach ((byte[] input, ReadOnlyMemory<byte> expectedResult) in Inputs)
{
IPrecompile precompile = MapToG1Precompile.Instance;
IPrecompile precompile = MapFpToG1Precompile.Instance;
(ReadOnlyMemory<byte> output, bool success) = precompile.Run(input, MuirGlacier.Instance);

output.ToArray().Should().BeEquivalentTo(expectedResult.ToArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

namespace Nethermind.Evm.Test;

public class BlsPairingPrecompileTests
public class BlsPairingCheckPrecompileTests
{
[Test]
public void Test()
{
foreach ((byte[] input, ReadOnlyMemory<byte> expectedResult) in Inputs)
{
IPrecompile precompile = PairingPrecompile.Instance;
IPrecompile precompile = PairingCheckPrecompile.Instance;
(ReadOnlyMemory<byte> output, bool success) = precompile.Run(input, MuirGlacier.Instance);

output.ToArray().Should().BeEquivalentTo(expectedResult.ToArray());
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ private static FrozenDictionary<AddressAsKey, CodeInfo> InitializePrecompiledCon

[G1AddPrecompile.Address] = new(G1AddPrecompile.Instance),
[G1MulPrecompile.Address] = new(G1MulPrecompile.Instance),
[G1MultiMulPrecompile.Address] = new(G1MultiMulPrecompile.Instance),
[G1MSMPrecompile.Address] = new(G1MSMPrecompile.Instance),
[G2AddPrecompile.Address] = new(G2AddPrecompile.Instance),
[G2MulPrecompile.Address] = new(G2MulPrecompile.Instance),
[G2MultiMulPrecompile.Address] = new(G2MultiMulPrecompile.Instance),
[PairingPrecompile.Address] = new(PairingPrecompile.Instance),
[MapToG1Precompile.Address] = new(MapToG1Precompile.Instance),
[MapToG2Precompile.Address] = new(MapToG2Precompile.Instance),
[G2MSMPrecompile.Address] = new(G2MSMPrecompile.Instance),
[PairingCheckPrecompile.Address] = new(PairingCheckPrecompile.Instance),
[MapFpToG1Precompile.Address] = new(MapFpToG1Precompile.Instance),
[MapFp2ToG2Precompile.Address] = new(MapFp2ToG2Precompile.Instance),

[PointEvaluationPrecompile.Address] = new(PointEvaluationPrecompile.Instance),

Expand Down
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Evm/Precompiles/Bls/BlsConst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Nethermind.Evm.Precompiles.Bls;

public static class BlsConst
{
public const bool DisableConcurrency = false;
public const bool DisableSubgroupChecks = false;
public const int LenFr = 32;
public const int LenFp = 64;
public const int LenFpTrimmed = 48;
Expand All @@ -16,6 +18,7 @@ public static class BlsConst
public const int LenG1Trimmed = 2 * LenFpTrimmed;
public const int LenG2 = 4 * LenFp;
public const int LenG2Trimmed = 4 * LenFpTrimmed;

public static readonly byte[] BaseFieldOrder = [0x1a, 0x01, 0x11, 0xea, 0x39, 0x7f, 0xe6, 0x9a, 0x4b, 0x1b, 0xa7, 0xb6, 0x43, 0x4b, 0xac, 0xd7, 0x64, 0x77, 0x4b, 0x84, 0xf3, 0x85, 0x12, 0xbf, 0x67, 0x30, 0xd2, 0xa0, 0xf6, 0xb0, 0xf6, 0x24, 0x1e, 0xab, 0xff, 0xfe, 0xb1, 0x53, 0xff, 0xff, 0xb9, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xaa, 0xab];
public static readonly ReadOnlyMemory<byte> G1Inf = Enumerable.Repeat<byte>(0, 128).ToArray();
public static readonly ReadOnlyMemory<byte> G2Inf = Enumerable.Repeat<byte>(0, 256).ToArray();
Expand Down
76 changes: 62 additions & 14 deletions src/Nethermind/Nethermind.Evm/Precompiles/Bls/BlsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,6 @@ public static bool TryDecodeRaw(this G1 p, ReadOnlySpan<byte> raw)
return true;
}

public static ReadOnlyMemory<byte> EncodeRaw(this G1 p)
{
if (p.IsInf())
{
return BlsConst.G1Inf;
}

byte[] raw = new byte[BlsConst.LenG1];
ReadOnlySpan<byte> trimmed = p.Serialize();
trimmed[..BlsConst.LenFpTrimmed].CopyTo(raw.AsSpan()[BlsConst.LenFpPad..BlsConst.LenFp]);
trimmed[BlsConst.LenFpTrimmed..].CopyTo(raw.AsSpan()[(BlsConst.LenFp + BlsConst.LenFpPad)..]);
return raw;
}

public static bool TryDecodeRaw(this G2 p, ReadOnlySpan<byte> raw)
{
if (raw.Length != BlsConst.LenG2)
Expand Down Expand Up @@ -96,6 +82,20 @@ public static bool TryDecodeRaw(this G2 p, ReadOnlySpan<byte> raw)
return true;
}

public static ReadOnlyMemory<byte> EncodeRaw(this G1 p)
{
if (p.IsInf())
{
return BlsConst.G1Inf;
}

byte[] raw = new byte[BlsConst.LenG1];
ReadOnlySpan<byte> trimmed = p.Serialize();
trimmed[..BlsConst.LenFpTrimmed].CopyTo(raw.AsSpan()[BlsConst.LenFpPad..BlsConst.LenFp]);
trimmed[BlsConst.LenFpTrimmed..].CopyTo(raw.AsSpan()[(BlsConst.LenFp + BlsConst.LenFpPad)..]);
return raw;
}

public static ReadOnlyMemory<byte> EncodeRaw(this G2 p)
{
if (p.IsInf())
Expand Down Expand Up @@ -128,4 +128,52 @@ public static bool ValidRawFp(ReadOnlySpan<byte> fp)
// check that fp < base field order
return fp[BlsConst.LenFpPad..].SequenceCompareTo(BlsConst.BaseFieldOrder.AsSpan()) < 0;
}

public static bool TryDecodeG1ToBuffer(ReadOnlyMemory<byte> inputData, Memory<long> pointBuffer, Memory<byte> scalarBuffer, int dest, int index)
=> TryDecodePointToBuffer(inputData, pointBuffer, scalarBuffer, dest, index, BlsConst.LenG1, G1MSMPrecompile.ItemSize, DecodeAndCheckG1);

public static bool TryDecodeG2ToBuffer(ReadOnlyMemory<byte> inputData, Memory<long> pointBuffer, Memory<byte> scalarBuffer, int dest, int index)
=> TryDecodePointToBuffer(inputData, pointBuffer, scalarBuffer, dest, index, BlsConst.LenG2, G2MSMPrecompile.ItemSize, DecodeAndCheckG2);

private static bool DecodeAndCheckG1(ReadOnlyMemory<byte> rawPoint, Memory<long> pointBuffer, int dest)
{
G1 p = new(pointBuffer.Span[(dest * G1.Sz)..]);
return p.TryDecodeRaw(rawPoint.Span) && (BlsConst.DisableSubgroupChecks || p.InGroup());
}

private static bool DecodeAndCheckG2(ReadOnlyMemory<byte> rawPoint, Memory<long> pointBuffer, int dest)
{
G2 p = new(pointBuffer.Span[(dest * G2.Sz)..]);
return p.TryDecodeRaw(rawPoint.Span) && (BlsConst.DisableSubgroupChecks || p.InGroup());
}

private static bool TryDecodePointToBuffer(
ReadOnlyMemory<byte> inputData,
Memory<long> pointBuffer,
Memory<byte> scalarBuffer,
int dest,
int index,
int pointLen,
int itemSize,
Func<ReadOnlyMemory<byte>, Memory<long>, int, bool> decodeAndCheckPoint)
{
if (dest == -1)
{
return true;
}

int offset = index * itemSize;
ReadOnlyMemory<byte> rawPoint = inputData[offset..(offset + pointLen)];
ReadOnlyMemory<byte> reversedScalar = inputData[(offset + pointLen)..(offset + itemSize)];

if (!decodeAndCheckPoint(rawPoint, pointBuffer, dest))
{
return false;
}

int destOffset = dest * 32;
reversedScalar.CopyTo(scalarBuffer[destOffset..]);
scalarBuffer[destOffset..(destOffset + 32)].Span.Reverse();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;
using Nethermind.Core;
using Nethermind.Core.Specs;

Expand All @@ -26,6 +27,7 @@ private G1AddPrecompile()

public long DataGasCost(ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec) => 0L;

[SkipLocalsInit]
public (ReadOnlyMemory<byte>, bool) Run(ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
{
Metrics.BlsG1AddPrecompile++;
Expand Down
Loading

0 comments on commit 2cb7f81

Please sign in to comment.