public void Test(TestJson testJson) { TestSimulator sim = new(testJson); ITailGasPriceCalculator tailGas = testJson.TailGasType switch { TailGasType.Any => new ConstantTailGasPriceCalculator(0.GWei()), TailGasType.Constant80 => new ConstantTailGasPriceCalculator(80.GWei()), _ => throw new ArgumentOutOfRangeException() }; IBundleSource selector = testJson.SelectorType switch { SelectorType.V1 => new V1Selector(sim, sim), SelectorType.V2 => new V2Selector(sim, sim, tailGas, testJson.MaxGasLimitRatio), _ => throw new ArgumentOutOfRangeException() }; IEnumerable <MevBundle> selected = selector.GetBundles(_blockHeader, testJson.GasLimit !.Value); SimulatedMevBundle[]? simulated = sim.Simulate(_blockHeader, testJson.GasLimit !.Value, selected).ToArray(); long totalGasUsedByBundles = simulated.Sum(s => s.GasUsed); long gasLeftForTransactions = testJson.GasLimit !.Value - totalGasUsedByBundles; IEnumerable <Transaction>?txs = sim.GetTransactions(_blockHeader, gasLeftForTransactions); UInt256 totalProfit = simulated.Aggregate <SimulatedMevBundle, UInt256>(0, (profit, x) => profit + x.Profit); totalProfit += txs.Aggregate <Transaction, UInt256>(0, (profit, x) => profit + (x.GasPrice * (UInt256)x.GasLimit)); totalProfit.Should().Be(testJson.OptimalProfit !.Value, testJson.Description); }
private static IEnumerable <TestJson> AllSelectors(TestJson testJson) { foreach (SelectorType value in Enum.GetValues <SelectorType>()) { TestJson withSelector = (TestJson)testJson.Clone(); withSelector.SelectorType = value; yield return(withSelector); } }
private static IEnumerable <TestJson> AllGasLimits(TestJson testJson) { long[] ratios = { 33, 100 }; foreach (long ratio in ratios) { TestJson withRatio = (TestJson)testJson.Clone(); withRatio.MaxGasLimitRatio = ratio; yield return(withRatio); } }
/// <summary> /// Would be nicer with schema but let us keep it simple here. /// </summary> /// <param name="testJson">Test object to validate</param> /// <returns>(<value>True</value>, <value>null</value>) if valid, /// otherwise (<value>False</value>, <value>"error description"</value>).</returns> private static void ValidateTest(TestJson testJson) { if (testJson.GasLimit is null) { Assert.Fail("Gas limit not specified"); } if (testJson.Name is null) { Assert.Fail("Name missing"); } if (testJson.Description is null) { Assert.Fail("Description missing"); } if (testJson.Bundles is null) { Assert.Fail("Bundles missing"); } if (testJson.Txs is null) { Assert.Fail("Transactions missing"); } if (testJson.OptimalProfit is null) { Assert.Fail("Optimal profit not specified"); } if (testJson.Bundles !.Any(item => item is null)) { Assert.Fail("One of the bundles is null"); } if (testJson.Txs !.Any(item => item is null)) { Assert.Fail("One of the transactions is null"); } }
public TestSimulator(TestJson testJson) { ValidateTest(testJson); _testJson = testJson; }