From 01d6b8f227e9285aea4bb78abbbca650f4bcbab1 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 27 Mar 2024 15:39:42 +0100 Subject: [PATCH] Applies blockInterval --- .../NethereumWorkflow/NethereumInteraction.cs | 40 ++++++++++--------- .../Utils/{BlockRange.cs => BlockInterval.cs} | 4 +- .../CodexContractsAccess.cs | 20 +++++----- ProjectPlugins/GethPlugin/GethNode.cs | 11 +++-- Tests/CodexTests/BasicTests/ExampleTests.cs | 17 ++++---- Tools/TestNetRewarder/ChainState.cs | 4 +- Tools/TestNetRewarder/Processor.cs | 4 +- 7 files changed, 51 insertions(+), 49 deletions(-) rename Framework/Utils/{BlockRange.cs => BlockInterval.cs} (84%) diff --git a/Framework/NethereumWorkflow/NethereumInteraction.cs b/Framework/NethereumWorkflow/NethereumInteraction.cs index 3240d9b..197cf2d 100644 --- a/Framework/NethereumWorkflow/NethereumInteraction.cs +++ b/Framework/NethereumWorkflow/NethereumInteraction.cs @@ -89,26 +89,9 @@ namespace NethereumWorkflow } } - public List> GetEvents(string address, TimeRange timeRange) where TEvent : IEventDTO, new() + public List> GetEvents(string address, BlockInterval blockRange) where TEvent : IEventDTO, new() { - var wrapper = new Web3Wrapper(web3, log); - var blockTimeFinder = new BlockTimeFinder(blockCache, wrapper, log); - - var fromBlock = blockTimeFinder.GetLowestBlockNumberAfter(timeRange.From); - var toBlock = blockTimeFinder.GetHighestBlockNumberBefore(timeRange.To); - - if (!fromBlock.HasValue) - { - log.Error("Failed to find lowest block for time range: " + timeRange); - throw new Exception("Failed"); - } - if (!toBlock.HasValue) - { - log.Error("Failed to find highest block for time range: " + timeRange); - throw new Exception("Failed"); - } - - return GetEvents(address, fromBlock.Value, toBlock.Value); + return GetEvents(address, blockRange.From, blockRange.To); } public List> GetEvents(string address, ulong fromBlockNumber, ulong toBlockNumber) where TEvent : IEventDTO, new() @@ -119,5 +102,24 @@ namespace NethereumWorkflow var blockFilter = Time.Wait(eventHandler.CreateFilterBlockRangeAsync(from, to)); return Time.Wait(eventHandler.GetAllChangesAsync(blockFilter)); } + + public BlockInterval ConvertTimeRangeToBlockRange(TimeRange timeRange) + { + var wrapper = new Web3Wrapper(web3, log); + var blockTimeFinder = new BlockTimeFinder(blockCache, wrapper, log); + + var fromBlock = blockTimeFinder.GetLowestBlockNumberAfter(timeRange.From); + var toBlock = blockTimeFinder.GetHighestBlockNumberBefore(timeRange.To); + + if (fromBlock == null || toBlock == null) + { + throw new Exception("Failed to convert time range to block range."); + } + + return new BlockInterval( + from: fromBlock.Value, + to: toBlock.Value + ); + } } } diff --git a/Framework/Utils/BlockRange.cs b/Framework/Utils/BlockInterval.cs similarity index 84% rename from Framework/Utils/BlockRange.cs rename to Framework/Utils/BlockInterval.cs index fa0574e..79229bf 100644 --- a/Framework/Utils/BlockRange.cs +++ b/Framework/Utils/BlockInterval.cs @@ -1,8 +1,8 @@ namespace Utils { - public class BlockRange + public class BlockInterval { - public BlockRange(ulong from, ulong to) + public BlockInterval(ulong from, ulong to) { if (from < to) { diff --git a/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs b/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs index 0113020..4279347 100644 --- a/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs +++ b/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs @@ -19,13 +19,13 @@ namespace CodexContractsPlugin TestToken GetTestTokenBalance(IHasEthAddress owner); TestToken GetTestTokenBalance(EthAddress ethAddress); - Request[] GetStorageRequests(BlockRange blockRange); + Request[] GetStorageRequests(BlockInterval blockRange); EthAddress? GetSlotHost(Request storageRequest, decimal slotIndex); RequestState GetRequestState(Request request); - RequestFulfilledEventDTO[] GetRequestFulfilledEvents(BlockRange blockRange); - RequestCancelledEventDTO[] GetRequestCancelledEvents(BlockRange blockRange); - SlotFilledEventDTO[] GetSlotFilledEvents(BlockRange blockRange); - SlotFreedEventDTO[] GetSlotFreedEvents(BlockRange blockRange); + RequestFulfilledEventDTO[] GetRequestFulfilledEvents(BlockInterval blockRange); + RequestCancelledEventDTO[] GetRequestCancelledEvents(BlockInterval blockRange); + SlotFilledEventDTO[] GetSlotFilledEvents(BlockInterval blockRange); + SlotFreedEventDTO[] GetSlotFreedEvents(BlockInterval blockRange); } public enum RequestState @@ -77,7 +77,7 @@ namespace CodexContractsPlugin return balance.TestTokens(); } - public Request[] GetStorageRequests(BlockRange blockRange) + public Request[] GetStorageRequests(BlockInterval blockRange) { var events = gethNode.GetEvents(Deployment.MarketplaceAddress, blockRange); var i = StartInteraction(); @@ -93,7 +93,7 @@ namespace CodexContractsPlugin .ToArray(); } - public RequestFulfilledEventDTO[] GetRequestFulfilledEvents(BlockRange blockRange) + public RequestFulfilledEventDTO[] GetRequestFulfilledEvents(BlockInterval blockRange) { var events = gethNode.GetEvents(Deployment.MarketplaceAddress, blockRange); return events.Select(e => @@ -104,7 +104,7 @@ namespace CodexContractsPlugin }).ToArray(); } - public RequestCancelledEventDTO[] GetRequestCancelledEvents(BlockRange blockRange) + public RequestCancelledEventDTO[] GetRequestCancelledEvents(BlockInterval blockRange) { var events = gethNode.GetEvents(Deployment.MarketplaceAddress, blockRange); return events.Select(e => @@ -115,7 +115,7 @@ namespace CodexContractsPlugin }).ToArray(); } - public SlotFilledEventDTO[] GetSlotFilledEvents(BlockRange blockRange) + public SlotFilledEventDTO[] GetSlotFilledEvents(BlockInterval blockRange) { var events = gethNode.GetEvents(Deployment.MarketplaceAddress, blockRange); return events.Select(e => @@ -127,7 +127,7 @@ namespace CodexContractsPlugin }).ToArray(); } - public SlotFreedEventDTO[] GetSlotFreedEvents(BlockRange blockRange) + public SlotFreedEventDTO[] GetSlotFreedEvents(BlockInterval blockRange) { var events = gethNode.GetEvents(Deployment.MarketplaceAddress, blockRange); return events.Select(e => diff --git a/ProjectPlugins/GethPlugin/GethNode.cs b/ProjectPlugins/GethPlugin/GethNode.cs index 8783665..bfa3d0f 100644 --- a/ProjectPlugins/GethPlugin/GethNode.cs +++ b/ProjectPlugins/GethPlugin/GethNode.cs @@ -6,7 +6,6 @@ using Nethereum.Contracts; using Nethereum.RPC.Eth.DTOs; using NethereumWorkflow; using Utils; -using BlockRange = Utils.BlockRange; namespace GethPlugin { @@ -25,9 +24,9 @@ namespace GethPlugin decimal? GetSyncedBlockNumber(); bool IsContractAvailable(string abi, string contractAddress); GethBootstrapNode GetBootstrapRecord(); - List> GetEvents(string address, BlockRange blockRange) where TEvent : IEventDTO, new(); + List> GetEvents(string address, BlockInterval blockRange) where TEvent : IEventDTO, new(); List> GetEvents(string address, TimeRange timeRange) where TEvent : IEventDTO, new(); - BlockRange ConvertTimeRangeToBlockRange(TimeRange timeRange); + BlockInterval ConvertTimeRangeToBlockRange(TimeRange timeRange); } public class DeploymentGethNode : BaseGethNode, IGethNode @@ -146,17 +145,17 @@ namespace GethPlugin return StartInteraction().IsContractAvailable(abi, contractAddress); } - public List> GetEvents(string address, BlockRange blockRange) where TEvent : IEventDTO, new() + public List> GetEvents(string address, BlockInterval blockRange) where TEvent : IEventDTO, new() { return StartInteraction().GetEvents(address, blockRange); } public List> GetEvents(string address, TimeRange timeRange) where TEvent : IEventDTO, new() { - return StartInteraction().GetEvents(address, timeRange); + return StartInteraction().GetEvents(address, ConvertTimeRangeToBlockRange(timeRange)); } - public BlockRange ConvertTimeRangeToBlockRange(TimeRange timeRange) + public BlockInterval ConvertTimeRangeToBlockRange(TimeRange timeRange) { return StartInteraction().ConvertTimeRangeToBlockRange(timeRange); } diff --git a/Tests/CodexTests/BasicTests/ExampleTests.cs b/Tests/CodexTests/BasicTests/ExampleTests.cs index 0470643..6fdb974 100644 --- a/Tests/CodexTests/BasicTests/ExampleTests.cs +++ b/Tests/CodexTests/BasicTests/ExampleTests.cs @@ -1,7 +1,6 @@ using CodexContractsPlugin; using CodexDiscordBotPlugin; using CodexPlugin; -using DistTestCore; using GethPlugin; using Nethereum.Hex.HexConvertors.Extensions; using NUnit.Framework; @@ -110,9 +109,11 @@ namespace CodexTests.BasicTests AssertBalance(contracts, seller, Is.LessThan(sellerInitialBalance), "Collateral was not placed."); - var request = GetOnChainStorageRequest(contracts); + var blockRange = geth.ConvertTimeRangeToBlockRange(GetTestRunTimeRange()); + + var request = GetOnChainStorageRequest(contracts, blockRange); AssertStorageRequest(request, purchase, contracts, buyer); - AssertSlotFilledEvents(contracts, purchase, request, seller); + AssertSlotFilledEvents(contracts, purchase, request, seller, blockRange); AssertContractSlot(contracts, request, 0, seller); purchaseContract.WaitForStorageContractFinished(); @@ -139,15 +140,15 @@ namespace CodexTests.BasicTests Assert.That(discN, Is.LessThan(bootN)); } - private void AssertSlotFilledEvents(ICodexContracts contracts, StoragePurchaseRequest purchase, Request request, ICodexNode seller) + private void AssertSlotFilledEvents(ICodexContracts contracts, StoragePurchaseRequest purchase, Request request, ICodexNode seller, BlockInterval blockRange) { // Expect 1 fulfilled event for the purchase. - var requestFulfilledEvents = contracts.GetRequestFulfilledEvents(GetTestRunTimeRange()); + var requestFulfilledEvents = contracts.GetRequestFulfilledEvents(blockRange); Assert.That(requestFulfilledEvents.Length, Is.EqualTo(1)); CollectionAssert.AreEqual(request.RequestId, requestFulfilledEvents[0].RequestId); // Expect 1 filled-slot event for each slot in the purchase. - var filledSlotEvents = contracts.GetSlotFilledEvents(GetTestRunTimeRange()); + var filledSlotEvents = contracts.GetSlotFilledEvents(blockRange); Assert.That(filledSlotEvents.Length, Is.EqualTo(purchase.MinRequiredNumberOfNodes)); for (var i = 0; i < purchase.MinRequiredNumberOfNodes; i++) { @@ -164,9 +165,9 @@ namespace CodexTests.BasicTests Assert.That(request.Ask.Slots, Is.EqualTo(purchase.MinRequiredNumberOfNodes)); } - private Request GetOnChainStorageRequest(ICodexContracts contracts) + private Request GetOnChainStorageRequest(ICodexContracts contracts, BlockInterval blockRange) { - var requests = contracts.GetStorageRequests(GetTestRunTimeRange()); + var requests = contracts.GetStorageRequests(blockRange); Assert.That(requests.Length, Is.EqualTo(1)); return requests.Single(); } diff --git a/Tools/TestNetRewarder/ChainState.cs b/Tools/TestNetRewarder/ChainState.cs index 0bb1fdd..40e4bf6 100644 --- a/Tools/TestNetRewarder/ChainState.cs +++ b/Tools/TestNetRewarder/ChainState.cs @@ -1,6 +1,6 @@ using CodexContractsPlugin; using CodexContractsPlugin.Marketplace; -using BlockRange = Utils.BlockRange; +using Utils; namespace TestNetRewarder { @@ -8,7 +8,7 @@ namespace TestNetRewarder { private readonly HistoricState historicState; - public ChainState(HistoricState historicState, ICodexContracts contracts, BlockRange blockRange) + public ChainState(HistoricState historicState, ICodexContracts contracts, BlockInterval blockRange) { NewRequests = contracts.GetStorageRequests(blockRange); historicState.ProcessNewRequests(NewRequests); diff --git a/Tools/TestNetRewarder/Processor.cs b/Tools/TestNetRewarder/Processor.cs index 9c0ddc2..425c0e3 100644 --- a/Tools/TestNetRewarder/Processor.cs +++ b/Tools/TestNetRewarder/Processor.cs @@ -11,7 +11,7 @@ namespace TestNetRewarder private static readonly HistoricState historicState = new HistoricState(); private static readonly RewardRepo rewardRepo = new RewardRepo(); private readonly ILog log; - private BlockRange? lastBlockRange; + private BlockInterval? lastBlockRange; public Processor(ILog log) { @@ -42,7 +42,7 @@ namespace TestNetRewarder } } - private bool IsNewBlockRange(BlockRange blockRange) + private bool IsNewBlockRange(BlockInterval blockRange) { if (lastBlockRange == null || lastBlockRange.From != blockRange.From ||