Skip to content

Commit

Permalink
Merge branch 'master' into pectra_devnet_4
Browse files Browse the repository at this point in the history
  • Loading branch information
rjnrohit authored Oct 28, 2024
2 parents 0f7468b + bc95db2 commit c0bb82c
Show file tree
Hide file tree
Showing 77 changed files with 2,241 additions and 400 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/sync-supported-chains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ jobs:
config: ${{fromJson(needs.setup-matrix.outputs.matrix)}}
runs-on: ubuntu-latest
steps:
- name: Destroy VM (if initialization failed)
uses: kamilchodola/linode-github-runner/.github/actions/linode-machine-manager@main
if: ${{ failure() && needs.create_a_runner.result == 'failure' }}
with:
linode_token: ${{ secrets.LINODE_TOKEN }}
github_token: "${{ secrets.REPOSITORY_DISPATCH_TOKEN }}"
action: "destroy-machine"
runner_label: t-${{ github.run_id }}-${{ matrix.config.network }}
search_phrase: t-${{ github.run_id }}-${{ matrix.config.network }}
root_password: ${{ secrets.LINODE_ROOT_PASSWORD }}
organization: "NethermindEth"
repo_name: "nethermind"

- name: Destroy Runner
uses: kamilchodola/linode-github-runner/.github/actions/linode-machine-manager@main
with:
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
<PackageVersion Include="Nethermind.DotNetty.Handlers" Version="1.0.1" />
<PackageVersion Include="Nethermind.DotNetty.Transport" Version="1.0.1" />
<PackageVersion Include="Nethermind.Gmp" Version="1.0.1" />
<PackageVersion Include="Nethermind.Libp2p" Version="1.0.0-preview.33" />
<PackageVersion Include="Nethermind.Libp2p" Version="1.0.0-preview.34" />
<PackageVersion Include="Nethermind.Libp2p.Protocols.PubsubPeerDiscovery" Version="1.0.0-preview.34" />
<PackageVersion Include="Nethermind.Numerics.Int256" Version="1.2.0" />
<PackageVersion Include="Nito.Collections.Deque" Version="1.2.1" />
<PackageVersion Include="NLog" Version="5.3.2" />
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Api/IApiWithStores.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface IApiWithStores : IBasicApi
IReceiptFinder? ReceiptFinder { get; set; }
IReceiptMonitor? ReceiptMonitor { get; set; }
IWallet? Wallet { get; set; }
IBlockStore? BadBlocksStore { get; set; }
IBadBlockStore? BadBlocksStore { get; set; }

public ContainerBuilder ConfigureContainerBuilderFromApiWithStores(ContainerBuilder builder)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Api/NethermindApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public ISealEngine SealEngine
public BackgroundTaskScheduler BackgroundTaskScheduler { get; set; } = null!;
public CensorshipDetector CensorshipDetector { get; set; } = null!;
public IWallet? Wallet { get; set; }
public IBlockStore? BadBlocksStore { get; set; }
public IBadBlockStore? BadBlocksStore { get; set; }
public ITransactionComparerProvider? TransactionComparerProvider { get; set; }
public IWebSocketsManager WebSocketsManager { get; set; } = new WebSocketsManager();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Nethermind.Blockchain.Blocks;
using Nethermind.Core;
using Nethermind.Core.Test;
using Nethermind.Core.Test.Builders;
using NUnit.Framework;

namespace Nethermind.Blockchain.Test.Blocks;

public class BadBlockStoreTests
{
[Test]
public void Test_CanInsert()
{
BadBlockStore badBlockStore = new BadBlockStore(new TestMemDb(), 10);

List<Block> toAdd = new()
{
Build.A.Block.WithNumber(1).TestObject,
Build.A.Block.WithNumber(2).TestObject,
Build.A.Block.WithNumber(3).TestObject,
};

foreach (Block block in toAdd)
{
badBlockStore.Insert(block);
}

badBlockStore.GetAll().Should().BeEquivalentTo(toAdd);
}

[Test]
public void Test_LimitStoredBlock()
{
BadBlockStore badBlockStore = new BadBlockStore(new TestMemDb(), 2);

List<Block> toAdd = new()
{
Build.A.Block.WithNumber(1).TestObject,
Build.A.Block.WithNumber(2).TestObject,
Build.A.Block.WithNumber(3).TestObject,
};

foreach (Block block in toAdd)
{
badBlockStore.Insert(block);
}

badBlockStore.GetAll().Count().Should().Be(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Numerics;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Routing;
using Nethermind.Consensus.Messages;
using Nethermind.Consensus.Validators;
using Nethermind.Core;
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Blockchain/BlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public partial class BlockTree : IBlockTree
private readonly IHeaderStore _headerStore;
private readonly IDb _blockInfoDb;
private readonly IDb _metadataDb;
private readonly IBlockStore _badBlockStore;
private readonly IBadBlockStore _badBlockStore;

private readonly LruCache<ValueHash256, Block> _invalidBlocks =
new(128, 128, "invalid blocks");
Expand Down Expand Up @@ -113,7 +113,7 @@ public BlockTree(
IHeaderStore? headerDb,
IDb? blockInfoDb,
IDb? metadataDb,
IBlockStore? badBlockStore,
IBadBlockStore? badBlockStore,
IChainLevelInfoRepository? chainLevelInfoRepository,
ISpecProvider? specProvider,
IBloomStorage? bloomStorage,
Expand Down
52 changes: 52 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/Blocks/BadBlockStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.Linq;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Db;
using Nethermind.Serialization.Rlp;

namespace Nethermind.Blockchain.Blocks;

public class BadBlockStore(IDb blockDb, long maxSize) : IBadBlockStore
{
private readonly BlockDecoder _blockDecoder = new();

public void Insert(Block block, WriteFlags writeFlags = WriteFlags.None)
{
if (block.Hash is null)
{
throw new InvalidOperationException("An attempt to store a block with a null hash.");
}

using NettyRlpStream newRlp = _blockDecoder.EncodeToNewNettyStream(block);
blockDb.Set(block.Number, block.Hash, newRlp.AsSpan(), writeFlags);

TruncateToMaxSize();
}

public IEnumerable<Block> GetAll()
{
return blockDb.GetAllValues(true).Select(bytes => _blockDecoder.Decode(ByteArrayExtensions.AsRlpStream((byte[]?)bytes)));
}

private void TruncateToMaxSize()
{
int toDelete = (int)(blockDb.GatherMetric().Size - maxSize!);
if (toDelete > 0)
{
foreach (var blockToDelete in GetAll().Take(toDelete))
{
Delete(blockToDelete.Number, blockToDelete.Hash);
}
}
}

private void Delete(long blockNumber, Hash256 blockHash)
{
blockDb.Delete(blockNumber, blockHash);
}
}
59 changes: 13 additions & 46 deletions src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using Nethermind.Core;
using Nethermind.Core.Caching;
using Nethermind.Core.Crypto;
Expand All @@ -14,42 +12,22 @@

namespace Nethermind.Blockchain.Blocks;

public class BlockStore : IBlockStore
public class BlockStore(IDb blockDb) : IBlockStore
{
private readonly IDb _blockDb;
private readonly BlockDecoder _blockDecoder = new();
public const int CacheSize = 128 + 32;

private readonly ClockCache<ValueHash256, Block>
_blockCache = new(CacheSize);
private readonly long? _maxSize;

public BlockStore(IDb blockDb, long? maxSize = null)
{
_blockDb = blockDb;
_maxSize = maxSize;
}

public void SetMetadata(byte[] key, byte[] value)
{
_blockDb.Set(key, value);
blockDb.Set(key, value);
}

public byte[]? GetMetadata(byte[] key)
{
return _blockDb.Get(key);
}

private void TruncateToMaxSize()
{
int toDelete = (int)(_blockDb.GatherMetric().Size - _maxSize!);
if (toDelete > 0)
{
foreach (var blockToDelete in GetAll().Take(toDelete))
{
Delete(blockToDelete.Number, blockToDelete.Hash);
}
}
return blockDb.Get(key);
}

public void Insert(Block block, WriteFlags writeFlags = WriteFlags.None)
Expand All @@ -63,12 +41,7 @@ public void Insert(Block block, WriteFlags writeFlags = WriteFlags.None)
// Although cpu is the main bottleneck since NettyRlpStream uses pooled memory which avoid unnecessary allocations..
using NettyRlpStream newRlp = _blockDecoder.EncodeToNewNettyStream(block);

_blockDb.Set(block.Number, block.Hash, newRlp.AsSpan(), writeFlags);

if (_maxSize is not null)
{
TruncateToMaxSize();
}
blockDb.Set(block.Number, block.Hash, newRlp.AsSpan(), writeFlags);
}

private static void GetBlockNumPrefixedKey(long blockNumber, Hash256 blockHash, Span<byte> output)
Expand All @@ -80,33 +53,33 @@ private static void GetBlockNumPrefixedKey(long blockNumber, Hash256 blockHash,
public void Delete(long blockNumber, Hash256 blockHash)
{
_blockCache.Delete(blockHash);
_blockDb.Delete(blockNumber, blockHash);
_blockDb.Remove(blockHash.Bytes);
blockDb.Delete(blockNumber, blockHash);
blockDb.Remove(blockHash.Bytes);
}

public Block? Get(long blockNumber, Hash256 blockHash, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = false)
{
Block? b = _blockDb.Get(blockNumber, blockHash, _blockDecoder, _blockCache, rlpBehaviors, shouldCache);
Block? b = blockDb.Get(blockNumber, blockHash, _blockDecoder, _blockCache, rlpBehaviors, shouldCache);
if (b is not null) return b;
return _blockDb.Get(blockHash, _blockDecoder, _blockCache, rlpBehaviors, shouldCache);
return blockDb.Get(blockHash, _blockDecoder, _blockCache, rlpBehaviors, shouldCache);
}

public byte[]? GetRaw(long blockNumber, Hash256 blockHash)
public byte[]? GetRlp(long blockNumber, Hash256 blockHash)
{
Span<byte> dbKey = stackalloc byte[40];
KeyValueStoreExtensions.GetBlockNumPrefixedKey(blockNumber, blockHash, dbKey);
var b = _blockDb.Get(dbKey);
var b = blockDb.Get(dbKey);
if (b is not null) return b;
return _blockDb.Get(blockHash);
return blockDb.Get(blockHash);
}

public ReceiptRecoveryBlock? GetReceiptRecoveryBlock(long blockNumber, Hash256 blockHash)
{
Span<byte> keyWithBlockNumber = stackalloc byte[40];
GetBlockNumPrefixedKey(blockNumber, blockHash, keyWithBlockNumber);

MemoryManager<byte>? memoryOwner = _blockDb.GetOwnedMemory(keyWithBlockNumber);
memoryOwner ??= _blockDb.GetOwnedMemory(blockHash.Bytes);
MemoryManager<byte>? memoryOwner = blockDb.GetOwnedMemory(keyWithBlockNumber);
memoryOwner ??= blockDb.GetOwnedMemory(blockHash.Bytes);

return BlockDecoder.DecodeToReceiptRecoveryBlock(memoryOwner, memoryOwner?.Memory ?? Memory<byte>.Empty, RlpBehaviors.None);
}
Expand All @@ -115,10 +88,4 @@ public void Cache(Block block)
{
_blockCache.Set(block.Hash, block);
}

public IEnumerable<Block> GetAll()
{
return _blockDb.GetAllValues(true).Select(bytes => _blockDecoder.Decode(bytes.AsRlpStream()));
}

}
13 changes: 13 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/Blocks/IBadBlockStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using Nethermind.Core;

namespace Nethermind.Blockchain.Blocks;

public interface IBadBlockStore
{
void Insert(Block block, WriteFlags writeFlags = WriteFlags.None);
IEnumerable<Block> GetAll();
}
4 changes: 1 addition & 3 deletions src/Nethermind/Nethermind.Blockchain/Blocks/IBlockStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ public interface IBlockStore
void Insert(Block block, WriteFlags writeFlags = WriteFlags.None);
void Delete(long blockNumber, Hash256 blockHash);
Block? Get(long blockNumber, Hash256 blockHash, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true);
byte[]? GetRaw(long blockNumber, Hash256 blockHash);
IEnumerable<Block> GetAll();
byte[]? GetRlp(long blockNumber, Hash256 blockHash);
ReceiptRecoveryBlock? GetReceiptRecoveryBlock(long blockNumber, Hash256 blockHash);
void Cache(Block block);


// These two are used by blocktree. Try not to use them...
void SetMetadata(byte[] key, byte[] value);
byte[]? GetMetadata(byte[] key);
Expand Down
4 changes: 4 additions & 0 deletions src/Nethermind/Nethermind.Config/BlocksConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ public byte[] GetExtraDataBytes()
{
return _extraDataBytes;
}

public string GasToken { get => GasTokenTicker; set => GasTokenTicker = value; }

public static string GasTokenTicker { get; set; } = "ETH";
}
}
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Config/IBlocksConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ public interface IBlocksConfig : IConfig
[ConfigItem(Description = "The genesis block load timeout, in milliseconds.", DefaultValue = "40000")]
int GenesisTimeoutMs { get; set; }

[ConfigItem(Description = "The ticker that gas rewards are denominated in for processing logs", DefaultValue = "ETH", HiddenFromDocs = true)]
string GasToken { get; set; }

byte[] GetExtraDataBytes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,20 @@ private void WarmupTransactions(ParallelOptions parallelOptions, IReleaseSpec sp
scope.WorldState.CreateAccountIfNotExists(senderAddress, UInt256.Zero);
}
UInt256 nonceDelta = UInt256.Zero;
for (int prev = 0; prev < i; prev++)
{
if (senderAddress == block.Transactions[prev].SenderAddress)
{
scope.WorldState.IncrementNonce(senderAddress);
nonceDelta++;
}
}
if (!nonceDelta.IsZero)
{
scope.WorldState.IncrementNonce(senderAddress, nonceDelta);
}
if (spec.UseTxAccessLists)
{
scope.WorldState.WarmUp(tx.AccessList); // eip-2930
Expand Down
Loading

0 comments on commit c0bb82c

Please sign in to comment.