Skip to content

Commit

Permalink
add megabundle pool
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche committed Sep 21, 2021
1 parent 5091156 commit 373e49f
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 59 deletions.
9 changes: 6 additions & 3 deletions src/Nethermind/Nethermind.Mev.Test/BundlePoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ public TestContext(ITimestamper? timestamper = null, IBundleSimulator? bundleSim
new TxValidator(BlockTree.ChainId),
new TestSpecProvider(London.Instance),
config ?? new MevConfig(),
LimboLogs.Instance);
LimboLogs.Instance,
EthereumEcdsa);

if (!addTestBundles) return;
BundleTransaction tx1 = CreateTransaction(TestItem.PrivateKeyA);
Expand Down Expand Up @@ -372,8 +373,10 @@ private void AddBundle(MevBundle bundle)
public TestBundlePool BundlePool { get; }

public ITimestamper Timestamper { get; private set; } =
new ManualTimestamper(DateTime.UnixEpoch.AddSeconds(DefaultTimestamp));

new ManualTimestamper(DateTime.UnixEpoch.AddSeconds(DefaultTimestamp));

public IEthereumEcdsa EthereumEcdsa { get; } = Substitute.For<IEthereumEcdsa>();

private MevBundle CreateBundle(long blockNumber, params BundleTransaction[] transactions) => new(blockNumber, transactions);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Nethermind/Nethermind.Mev.Test/MetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.Mev.Data;
Expand Down Expand Up @@ -166,7 +167,8 @@ private static TestBundlePool CreateTestBundlePool()
new TxValidator(blockTree.ChainId),
new TestSpecProvider(London.Instance),
new MevConfig(),
LimboLogs.Instance);
LimboLogs.Instance,
Substitute.For<IEthereumEcdsa>());
return bundlePool;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using Nethermind.Core.Test;
using Nethermind.Core.Test.Blockchain;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Int256;
using Nethermind.JsonRpc;
using Nethermind.JsonRpc.Test.Modules;
Expand Down Expand Up @@ -186,7 +187,7 @@ protected override BlockProcessor CreateBlockProcessor()
ProcessingOptions.ProducingBlock);

TxBundleSimulator txBundleSimulator = new(_tracerFactory, GasLimitCalculator, Timestamper, TxPool, SpecProvider, Signer);
BundlePool = new TestBundlePool(BlockTree, txBundleSimulator, Timestamper, new TxValidator(BlockTree.ChainId), SpecProvider, _mevConfig, LogManager);
BundlePool = new TestBundlePool(BlockTree, txBundleSimulator, Timestamper, new TxValidator(BlockTree.ChainId), SpecProvider, _mevConfig, LogManager, EthereumEcdsa);

return blockProcessor;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Nethermind/Nethermind.Mev.Test/TestBundlePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Nethermind.Blockchain;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Crypto;
using Nethermind.Logging;
using Nethermind.Mev.Data;
using Nethermind.Mev.Execution;
Expand All @@ -39,8 +40,9 @@ public TestBundlePool(IBlockTree blockTree,
ITxValidator txValidator,
ISpecProvider specProvider,
IMevConfig mevConfig,
ILogManager logManager)
: base(blockTree, simulator, timestamper, txValidator, specProvider, mevConfig, logManager)
ILogManager logManager,
IEthereumEcdsa ecdsa)
: base(blockTree, simulator, timestamper, txValidator, specProvider, mevConfig, logManager, ecdsa)
{
}

Expand Down
32 changes: 32 additions & 0 deletions src/Nethermind/Nethermind.Mev/Data/MegabundleEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using Nethermind.Core;

namespace Nethermind.Mev.Data
{
public class MegabundleEventArgs : BundleEventArgs
{
public Address RelayAddress { get; }

public MegabundleEventArgs(MevBundle mevBundle, Address relayAddress): base(mevBundle)
{
RelayAddress = relayAddress;
}
}
}
4 changes: 0 additions & 4 deletions src/Nethermind/Nethermind.Mev/Data/MevBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Int256;
using Nethermind.Mev.Source;
using Nethermind.Serialization.Rlp;

namespace Nethermind.Mev.Data
{
Expand Down
42 changes: 42 additions & 0 deletions src/Nethermind/Nethermind.Mev/Data/MevMegabundle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using Nethermind.Core.Crypto;

namespace Nethermind.Mev.Data
{
public class MevMegabundle: IEquatable<MevMegabundle>
{
public MevMegabundle(MevBundle bundle, Signature relaySignature)
{
Bundle = bundle;
RelaySignature = relaySignature;
}

public MevBundle Bundle { get; }
public Signature RelaySignature { get; }

public bool Equals(MevMegabundle? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(Bundle, other.Bundle)
&& Equals(RelaySignature, other.RelaySignature);
}
}
}
6 changes: 5 additions & 1 deletion src/Nethermind/Nethermind.Mev/IMevConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Linq;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Int256;
using Nethermind.Mev.Data;

namespace Nethermind.Mev
{
Expand All @@ -44,5 +45,8 @@ public interface IMevConfig : IConfig
[ConfigItem(Description = "Defines the trusted relay addresses to receive megabundles from",
DefaultValue = "")]
string TrustedRelays { get; set; }

public IEnumerable<Address> GetTrustedRelayAddresses() =>
TrustedRelays.Split(",").Select(address => new Address(address));
}
}
8 changes: 7 additions & 1 deletion src/Nethermind/Nethermind.Mev/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ public static class Metrics
{
[Description("Total number of bundles received for inclusion")]
public static int BundlesReceived { get; set; } = 0;

[Description("Total number of valid bundles received for inclusion")]
public static int ValidBundlesReceived { get; set; } = 0;

[Description("Total number of megabundles received for inclusion")]
public static int MegabundlesReceived { get; set; } = 0;

[Description("Total number of valid megabundles received for inclusion")]
public static int ValidMegabundlesReceived { get; set; } = 0;

[Description("Total number of bundles simulated")]
public static int BundlesSimulated { get; set; } = 0;

Expand Down
3 changes: 0 additions & 3 deletions src/Nethermind/Nethermind.Mev/MevConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Int256;
using Nethermind.Mev.Data;

namespace Nethermind.Mev
{
Expand Down
43 changes: 35 additions & 8 deletions src/Nethermind/Nethermind.Mev/MevPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
//

using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Threading.Tasks;
using Nethermind.Api;
using Nethermind.Api.Extensions;
using Nethermind.Blockchain;
Expand All @@ -29,15 +27,12 @@
using Nethermind.Consensus;
using Nethermind.Consensus.Transactions;
using Nethermind.Core;
using Nethermind.Db;
using Nethermind.Facade;
using Nethermind.JsonRpc;
using Nethermind.JsonRpc.Modules;
using Nethermind.Logging;
using Nethermind.Mev.Data;
using Nethermind.Mev.Execution;
using Nethermind.Mev.Source;
using Nethermind.TxPool;

namespace Nethermind.Mev
{
Expand Down Expand Up @@ -91,7 +86,8 @@ private BundlePool BundlePool
getFromApi.TxValidator!,
getFromApi.SpecProvider!,
_mevConfig,
getFromApi.LogManager);
getFromApi.LogManager,
getFromApi.EthereumEcdsa!);
}

return _bundlePool;
Expand Down Expand Up @@ -162,7 +158,7 @@ public async Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPl
_nethermindApi.BlockProducerEnvFactory.TransactionsExecutorFactory = new MevBlockProducerTransactionsExecutorFactory(_nethermindApi.SpecProvider!, _nethermindApi.LogManager);

List<MevBlockProducer.MevBlockProducerInfo> blockProducers =
new(_mevConfig.MaxMergedBundles + 1);
new(_mevConfig.MaxMergedBundles + _mevConfig.GetTrustedRelayAddresses().Count() + 1);

// Add non-mev block
MevBlockProducer.MevBlockProducerInfo standardProducer = await CreateProducer(consensusPlugin);
Expand All @@ -176,6 +172,13 @@ public async Task<IBlockProducer> InitBlockProducer(IConsensusPlugin consensusPl
blockProducers.Add(bundleProducer);
}

foreach (Address address in _mevConfig.GetTrustedRelayAddresses())
{
MegabundleSelector megabundleSelector = new(BundlePool, address);
MevBlockProducer.MevBlockProducerInfo bundleProducer = await CreateProducer(consensusPlugin, address, new BundleTxSource(megabundleSelector, _nethermindApi.Timestamper));
blockProducers.Add(bundleProducer);
}

return new MevBlockProducer(consensusPlugin.DefaultBlockProductionTrigger, _nethermindApi.LogManager, blockProducers.ToArray());
}

Expand Down Expand Up @@ -207,6 +210,30 @@ bool BundleLimitTriggerCondition(BlockProductionEventArgs e)
return new MevBlockProducer.MevBlockProducerInfo(producer, manualTrigger, new BeneficiaryTracer());
}

private async Task<MevBlockProducer.MevBlockProducerInfo> CreateProducer(
IConsensusPlugin consensusPlugin,
Address relayAddress,
ITxSource? additionalTxSource = null)
{
bool MegabundleTriggerCondition(BlockProductionEventArgs e)
{
BlockHeader? parent = _nethermindApi.BlockTree!.GetProducedBlockParent(e.ParentHeader);
if (parent is not null)
{
MevBundle? bundle = BundlePool.GetMegabundle(parent, _nethermindApi.Timestamper, relayAddress);
return bundle is not null;
}

return false;
}

IManualBlockProductionTrigger manualTrigger = new BuildBlocksWhenRequested();
IBlockProductionTrigger trigger = new TriggerWithCondition(manualTrigger, MegabundleTriggerCondition);

IBlockProducer producer = await consensusPlugin.InitBlockProducer(trigger, additionalTxSource);
return new MevBlockProducer.MevBlockProducerInfo(producer, manualTrigger, new BeneficiaryTracer());
}

public bool Enabled => _mevConfig.Enabled;

public ValueTask DisposeAsync() => ValueTask.CompletedTask;
Expand Down
17 changes: 5 additions & 12 deletions src/Nethermind/Nethermind.Mev/MevRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,22 @@

using System;
using System.Collections.Generic;
using System.Numerics;
using System.Linq;
using System.Text;
using System.Threading;
using Nethermind.Blockchain.Find;
using Nethermind.JsonRpc;
using Nethermind.JsonRpc.Data;
using Nethermind.Int256;
using Nethermind.Core;
using Nethermind.Facade;
using Nethermind.Logging;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Tracing;
using Nethermind.Consensus;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Int256;
using Nethermind.JsonRpc;
using Nethermind.JsonRpc.Modules;
using Nethermind.Mev.Data;
using Nethermind.Mev.Execution;
using Nethermind.Mev.Source;
using Nethermind.Serialization.Rlp;
using Nethermind.State;
using Nethermind.Trie;
using Newtonsoft.Json;

namespace Nethermind.Mev
{
Expand Down Expand Up @@ -88,7 +80,8 @@ public ResultWrapper<bool> eth_sendMegabundle(MevMegabundleRpc mevMegabundleRpc)
{
BundleTransaction[] txs = Decode(mevMegabundleRpc.Txs, mevMegabundleRpc.RevertingTxHashes?.ToHashSet());
MevBundle bundle = new(mevMegabundleRpc.BlockNumber, txs, mevMegabundleRpc.MinTimestamp, mevMegabundleRpc.MaxTimestamp);
bool result = _bundlePool.AddBundle(bundle);
MevMegabundle megabundle = new(bundle, mevMegabundleRpc.RelaySignature!);
bool result = _bundlePool.AddMegabundle(megabundle);
return ResultWrapper<bool>.Success(result);
}

Expand Down
Loading

0 comments on commit 373e49f

Please sign in to comment.