diff --git a/Framework/GethConnector/GethConnector.cs b/Framework/GethConnector/GethConnector.cs index 6b713fa..afb2e57 100644 --- a/Framework/GethConnector/GethConnector.cs +++ b/Framework/GethConnector/GethConnector.cs @@ -1,4 +1,5 @@ -using CodexContractsPlugin; +using BlockchainUtils; +using CodexContractsPlugin; using CodexContractsPlugin.Marketplace; using GethPlugin; using Logging; @@ -19,7 +20,7 @@ namespace GethConnector return null; } - var gethNode = new CustomGethNode(log, GethInput.GethHost, GethInput.GethPort, GethInput.PrivateKey); + var gethNode = new CustomGethNode(log, new BlockCache(), GethInput.GethHost, GethInput.GethPort, GethInput.PrivateKey); var config = GetCodexMarketplaceConfig(gethNode, GethInput.MarketplaceAddress); diff --git a/Framework/NethereumWorkflow/NethereumInteraction.cs b/Framework/NethereumWorkflow/NethereumInteraction.cs index 4b543e9..efb2fd5 100644 --- a/Framework/NethereumWorkflow/NethereumInteraction.cs +++ b/Framework/NethereumWorkflow/NethereumInteraction.cs @@ -10,15 +10,16 @@ namespace NethereumWorkflow { public class NethereumInteraction { - private readonly static BlockCache blockCache = new BlockCache(); // WRONG: parallel environments! + private readonly BlockCache blockCache; private readonly ILog log; private readonly Web3 web3; - internal NethereumInteraction(ILog log, Web3 web3) + internal NethereumInteraction(ILog log, Web3 web3, BlockCache blockCache) { this.log = log; this.web3 = web3; + this.blockCache = blockCache; } public string SendEth(string toAddress, decimal ethAmount) diff --git a/Framework/NethereumWorkflow/NethereumInteractionCreator.cs b/Framework/NethereumWorkflow/NethereumInteractionCreator.cs index 8e83cc1..bd7d2d9 100644 --- a/Framework/NethereumWorkflow/NethereumInteractionCreator.cs +++ b/Framework/NethereumWorkflow/NethereumInteractionCreator.cs @@ -1,4 +1,5 @@ -using Logging; +using BlockchainUtils; +using Logging; using Nethereum.Web3; namespace NethereumWorkflow @@ -6,13 +7,15 @@ namespace NethereumWorkflow public class NethereumInteractionCreator { private readonly ILog log; + private readonly BlockCache blockCache; private readonly string ip; private readonly int port; private readonly string privateKey; - public NethereumInteractionCreator(ILog log, string ip, int port, string privateKey) + public NethereumInteractionCreator(ILog log, BlockCache blockCache, string ip, int port, string privateKey) { this.log = log; + this.blockCache = blockCache; this.ip = ip; this.port = port; this.privateKey = privateKey; @@ -21,7 +24,7 @@ namespace NethereumWorkflow public NethereumInteraction CreateWorkflow() { log.Debug("Starting interaction to " + ip + ":" + port); - return new NethereumInteraction(log, CreateWeb3()); + return new NethereumInteraction(log, CreateWeb3(), blockCache); } private Web3 CreateWeb3() diff --git a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainState.cs b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainState.cs index a5f73c8..0a04cac 100644 --- a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainState.cs +++ b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainState.cs @@ -1,7 +1,7 @@ -using CodexContractsPlugin.Marketplace; +using BlockchainUtils; +using CodexContractsPlugin.Marketplace; using GethPlugin; using Logging; -using NethereumWorkflow.BlockUtils; using System.Numerics; using Utils; diff --git a/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs b/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs index 2537d33..d7191b7 100644 --- a/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs +++ b/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs @@ -1,4 +1,5 @@ -using CodexContractsPlugin.Marketplace; +using BlockchainUtils; +using CodexContractsPlugin.Marketplace; using GethPlugin; using Logging; using Nethereum.ABI; diff --git a/ProjectPlugins/CodexContractsPlugin/CodexContractsEvents.cs b/ProjectPlugins/CodexContractsPlugin/CodexContractsEvents.cs index ef30026..7667994 100644 --- a/ProjectPlugins/CodexContractsPlugin/CodexContractsEvents.cs +++ b/ProjectPlugins/CodexContractsPlugin/CodexContractsEvents.cs @@ -1,9 +1,9 @@ -using CodexContractsPlugin.Marketplace; +using BlockchainUtils; +using CodexContractsPlugin.Marketplace; using GethPlugin; using Logging; using Nethereum.Contracts; using Nethereum.Hex.HexTypes; -using NethereumWorkflow.BlockUtils; using Utils; namespace CodexContractsPlugin diff --git a/ProjectPlugins/CodexContractsPlugin/ContractInteractions.cs b/ProjectPlugins/CodexContractsPlugin/ContractInteractions.cs index 85d12cf..feecd05 100644 --- a/ProjectPlugins/CodexContractsPlugin/ContractInteractions.cs +++ b/ProjectPlugins/CodexContractsPlugin/ContractInteractions.cs @@ -1,4 +1,5 @@ -using CodexContractsPlugin.Marketplace; +using BlockchainUtils; +using CodexContractsPlugin.Marketplace; using GethPlugin; using Logging; using Nethereum.ABI.FunctionEncoding.Attributes; diff --git a/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs b/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs index 413d340..b04bf59 100644 --- a/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs +++ b/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs @@ -1,6 +1,6 @@ #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. +using BlockchainUtils; using GethPlugin; -using NethereumWorkflow.BlockUtils; using Newtonsoft.Json; namespace CodexContractsPlugin.Marketplace diff --git a/ProjectPlugins/GethPlugin/CoreInterfaceExtensions.cs b/ProjectPlugins/GethPlugin/CoreInterfaceExtensions.cs index a1d0ecd..764fa21 100644 --- a/ProjectPlugins/GethPlugin/CoreInterfaceExtensions.cs +++ b/ProjectPlugins/GethPlugin/CoreInterfaceExtensions.cs @@ -1,4 +1,5 @@ -using Core; +using BlockchainUtils; +using Core; namespace GethPlugin { @@ -9,15 +10,15 @@ namespace GethPlugin return Plugin(ci).DeployGeth(setup); } - public static IGethNode WrapGethDeployment(this CoreInterface ci, GethDeployment deployment) + public static IGethNode WrapGethDeployment(this CoreInterface ci, GethDeployment deployment, BlockCache blockCache) { - return Plugin(ci).WrapGethDeployment(deployment); + return Plugin(ci).WrapGethDeployment(deployment, blockCache); } - public static IGethNode StartGethNode(this CoreInterface ci, Action setup) + public static IGethNode StartGethNode(this CoreInterface ci, BlockCache blockCache, Action setup) { var deploy = DeployGeth(ci, setup); - return WrapGethDeployment(ci, deploy); + return WrapGethDeployment(ci, deploy, blockCache); } private static GethPlugin Plugin(CoreInterface ci) diff --git a/ProjectPlugins/GethPlugin/GethNode.cs b/ProjectPlugins/GethPlugin/GethNode.cs index 641aea4..2866f28 100644 --- a/ProjectPlugins/GethPlugin/GethNode.cs +++ b/ProjectPlugins/GethPlugin/GethNode.cs @@ -1,11 +1,11 @@ -using Core; +using BlockchainUtils; +using Core; using KubernetesWorkflow.Types; using Logging; using Nethereum.ABI.FunctionEncoding.Attributes; using Nethereum.Contracts; using Nethereum.RPC.Eth.DTOs; using NethereumWorkflow; -using NethereumWorkflow.BlockUtils; using Utils; namespace GethPlugin @@ -34,10 +34,12 @@ namespace GethPlugin public class DeploymentGethNode : BaseGethNode, IGethNode { private readonly ILog log; + private readonly BlockCache blockCache; - public DeploymentGethNode(ILog log, GethDeployment startResult) + public DeploymentGethNode(ILog log, BlockCache blockCache, GethDeployment startResult) { this.log = log; + this.blockCache = blockCache; StartResult = startResult; } @@ -60,7 +62,7 @@ namespace GethPlugin var address = StartResult.Container.GetAddress(GethContainerRecipe.HttpPortTag); var account = StartResult.Account; - var creator = new NethereumInteractionCreator(log, address.Host, address.Port, account.PrivateKey); + var creator = new NethereumInteractionCreator(log, blockCache, address.Host, address.Port, account.PrivateKey); return creator.CreateWorkflow(); } } @@ -68,6 +70,7 @@ namespace GethPlugin public class CustomGethNode : BaseGethNode, IGethNode { private readonly ILog log; + private readonly BlockCache blockCache; private readonly string gethHost; private readonly int gethPort; private readonly string privateKey; @@ -75,9 +78,10 @@ namespace GethPlugin public GethDeployment StartResult => throw new NotImplementedException(); public RunningContainer Container => throw new NotImplementedException(); - public CustomGethNode(ILog log, string gethHost, int gethPort, string privateKey) + public CustomGethNode(ILog log, BlockCache blockCache, string gethHost, int gethPort, string privateKey) { this.log = log; + this.blockCache = blockCache; this.gethHost = gethHost; this.gethPort = gethPort; this.privateKey = privateKey; @@ -90,7 +94,7 @@ namespace GethPlugin protected override NethereumInteraction StartInteraction() { - var creator = new NethereumInteractionCreator(log, gethHost, gethPort, privateKey); + var creator = new NethereumInteractionCreator(log, blockCache, gethHost, gethPort, privateKey); return creator.CreateWorkflow(); } } diff --git a/ProjectPlugins/GethPlugin/GethPlugin.cs b/ProjectPlugins/GethPlugin/GethPlugin.cs index bda6845..07249ec 100644 --- a/ProjectPlugins/GethPlugin/GethPlugin.cs +++ b/ProjectPlugins/GethPlugin/GethPlugin.cs @@ -1,4 +1,5 @@ -using Core; +using BlockchainUtils; +using Core; namespace GethPlugin { @@ -36,10 +37,10 @@ namespace GethPlugin return starter.StartGeth(startupConfig); } - public IGethNode WrapGethDeployment(GethDeployment startResult) + public IGethNode WrapGethDeployment(GethDeployment startResult, BlockCache blockCache) { startResult = SerializeGate.Gate(startResult); - return starter.WrapGethContainer(startResult); + return starter.WrapGethContainer(startResult, blockCache); } } } diff --git a/ProjectPlugins/GethPlugin/GethStarter.cs b/ProjectPlugins/GethPlugin/GethStarter.cs index edfba4e..0083c01 100644 --- a/ProjectPlugins/GethPlugin/GethStarter.cs +++ b/ProjectPlugins/GethPlugin/GethStarter.cs @@ -1,4 +1,5 @@ -using Core; +using BlockchainUtils; +using Core; using KubernetesWorkflow; namespace GethPlugin @@ -41,10 +42,10 @@ namespace GethPlugin return new GethDeployment(containers, discoveryPort, httpPort, wsPort, account, pubKey); } - public IGethNode WrapGethContainer(GethDeployment startResult) + public IGethNode WrapGethContainer(GethDeployment startResult, BlockCache blockCache) { startResult = SerializeGate.Gate(startResult); - return new DeploymentGethNode(tools.GetLog(), startResult); + return new DeploymentGethNode(tools.GetLog(), blockCache, startResult); } private void Log(string msg) diff --git a/Tests/CodexReleaseTests/MarketTests/ContractFailedTest.cs b/Tests/CodexReleaseTests/MarketTests/ContractFailedTest.cs index 3e025d4..fee3056 100644 --- a/Tests/CodexReleaseTests/MarketTests/ContractFailedTest.cs +++ b/Tests/CodexReleaseTests/MarketTests/ContractFailedTest.cs @@ -21,8 +21,11 @@ namespace CodexReleaseTests.MarketTests private readonly TestToken pricePerSlotPerSecond = 10.TstWei(); [Test] - [Ignore("TODO - Test in which hosts are punished for failing a contract")] - public void ContractFailed() + [Combinatorial] + public void ContractFailed( + [Values(0, 1, 2, 3)] int a, + [Values(0, 1, 2, 3)] int b + ) { var hosts = StartHosts(); var client = StartClients().Single(); diff --git a/Tests/CodexReleaseTests/MarketTests/ContractSuccessfulTest.cs b/Tests/CodexReleaseTests/MarketTests/ContractSuccessfulTest.cs index b007945..34433bd 100644 --- a/Tests/CodexReleaseTests/MarketTests/ContractSuccessfulTest.cs +++ b/Tests/CodexReleaseTests/MarketTests/ContractSuccessfulTest.cs @@ -18,7 +18,11 @@ namespace CodexReleaseTests.MarketTests private readonly TestToken pricePerSlotPerSecond = 10.TstWei(); [Test] - public void ContractSuccessful() + [Combinatorial] + public void ContractSuccessful( + [Values(0, 1, 2, 3)] int a, + [Values(0, 1, 2, 3)] int b + ) { var hosts = StartHosts(); var client = StartClients().Single(); diff --git a/Tests/CodexReleaseTests/MarketTests/MarketplaceAutoBootstrapDistTest.cs b/Tests/CodexReleaseTests/MarketTests/MarketplaceAutoBootstrapDistTest.cs index 79e1037..ef181c3 100644 --- a/Tests/CodexReleaseTests/MarketTests/MarketplaceAutoBootstrapDistTest.cs +++ b/Tests/CodexReleaseTests/MarketTests/MarketplaceAutoBootstrapDistTest.cs @@ -19,7 +19,7 @@ namespace CodexReleaseTests.MarketTests protected override void LifecycleStart(TestLifecycle lifecycle) { base.LifecycleStart(lifecycle); - var geth = Ci.StartGethNode(s => s.IsMiner()); + var geth = StartGethNode(s => s.IsMiner()); var contracts = Ci.StartCodexContracts(geth); handles.Add(lifecycle, new MarketplaceHandle(geth, contracts)); } diff --git a/Tests/ExperimentalTests/BasicTests/ExampleTests.cs b/Tests/ExperimentalTests/BasicTests/ExampleTests.cs index f555100..b2cb76f 100644 --- a/Tests/ExperimentalTests/BasicTests/ExampleTests.cs +++ b/Tests/ExperimentalTests/BasicTests/ExampleTests.cs @@ -53,9 +53,9 @@ namespace CodexTests.BasicTests [Test] public void GethBootstrapTest() { - var boot = Ci.StartGethNode(s => s.WithName("boot").IsMiner()); - var disconnected = Ci.StartGethNode(s => s.WithName("disconnected")); - var follow = Ci.StartGethNode(s => s.WithBootstrapNode(boot).WithName("follow")); + var boot = StartGethNode(s => s.WithName("boot").IsMiner()); + var disconnected = StartGethNode(s => s.WithName("disconnected")); + var follow = StartGethNode(s => s.WithBootstrapNode(boot).WithName("follow")); Thread.Sleep(12000); diff --git a/Tests/ExperimentalTests/BasicTests/MarketplaceTests.cs b/Tests/ExperimentalTests/BasicTests/MarketplaceTests.cs index d8e178f..16f6dd6 100644 --- a/Tests/ExperimentalTests/BasicTests/MarketplaceTests.cs +++ b/Tests/ExperimentalTests/BasicTests/MarketplaceTests.cs @@ -28,7 +28,7 @@ namespace CodexTests.BasicTests plusSizeBytes ); - var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth")); + var geth = StartGethNode(s => s.IsMiner().WithName("disttest-geth")); var contracts = Ci.StartCodexContracts(geth); var numberOfHosts = 5; diff --git a/Tests/ExperimentalTests/CodexDistTest.cs b/Tests/ExperimentalTests/CodexDistTest.cs index 13365bf..3f0c0b4 100644 --- a/Tests/ExperimentalTests/CodexDistTest.cs +++ b/Tests/ExperimentalTests/CodexDistTest.cs @@ -1,4 +1,5 @@ -using CodexContractsPlugin; +using BlockchainUtils; +using CodexContractsPlugin; using CodexNetDeployer; using CodexPlugin; using CodexPlugin.OverwatchSupport; @@ -7,6 +8,7 @@ using Core; using DistTestCore; using DistTestCore.Helpers; using DistTestCore.Logs; +using GethPlugin; using Logging; using MetricsPlugin; using Newtonsoft.Json; @@ -19,6 +21,7 @@ namespace CodexTests public class CodexDistTest : DistTest { private static readonly Dictionary writers = new Dictionary(); + private static readonly Dictionary blockCaches = new Dictionary(); public CodexDistTest() { @@ -73,6 +76,11 @@ namespace CodexTests return group; } + public IGethNode StartGethNode(Action setup) + { + return Ci.StartGethNode(GetBlockCache(), setup); + } + public PeerConnectionTestHelpers CreatePeerConnectionTestHelpers() { return new PeerConnectionTestHelpers(GetTestLog()); @@ -223,6 +231,16 @@ namespace CodexTests if (!outputFile.EndsWith(".owts")) outputFile += ".owts"; return outputFile; } + + private BlockCache GetBlockCache() + { + var lifecycle = Get(); + if (!blockCaches.ContainsKey(lifecycle)) + { + blockCaches[lifecycle] = new BlockCache(); + } + return blockCaches[lifecycle]; + } } [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] diff --git a/Tests/ExperimentalTests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs b/Tests/ExperimentalTests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs index e6a42c5..00dacd2 100644 --- a/Tests/ExperimentalTests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs +++ b/Tests/ExperimentalTests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs @@ -20,7 +20,7 @@ namespace CodexTests.DownloadConnectivityTests [Test] public void MarketplaceDoesNotInterfereWithPeerDownload() { - var geth = Ci.StartGethNode(s => s.IsMiner()); + var geth = StartGethNode(s => s.IsMiner()); var contracts = Ci.StartCodexContracts(geth); var nodes = StartCodex(2, s => s.EnableMarketplace(geth, contracts, m => m .WithInitial(10.Eth(), 1000.TstWei()))); diff --git a/Tests/ExperimentalTests/PeerDiscoveryTests/PeerDiscoveryTests.cs b/Tests/ExperimentalTests/PeerDiscoveryTests/PeerDiscoveryTests.cs index 938e873..6ec3516 100644 --- a/Tests/ExperimentalTests/PeerDiscoveryTests/PeerDiscoveryTests.cs +++ b/Tests/ExperimentalTests/PeerDiscoveryTests/PeerDiscoveryTests.cs @@ -29,7 +29,7 @@ namespace CodexTests.PeerDiscoveryTests [Test] public void MarketplaceDoesNotInterfereWithPeerDiscovery() { - var geth = Ci.StartGethNode(s => s.IsMiner()); + var geth = StartGethNode(s => s.IsMiner()); var contracts = Ci.StartCodexContracts(geth); var nodes = StartCodex(2, s => s.EnableMarketplace(geth, contracts, m => m .WithInitial(10.Eth(), 1000.TstWei()))); diff --git a/Tests/ExperimentalTests/UtilityTests/DiscordBotTests.cs b/Tests/ExperimentalTests/UtilityTests/DiscordBotTests.cs index b22fcba..d157f66 100644 --- a/Tests/ExperimentalTests/UtilityTests/DiscordBotTests.cs +++ b/Tests/ExperimentalTests/UtilityTests/DiscordBotTests.cs @@ -30,7 +30,7 @@ namespace CodexTests.UtilityTests [Ignore("Used to debug testnet bots.")] public void BotRewardTest() { - var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth")); + var geth = StartGethNode(s => s.IsMiner().WithName("disttest-geth")); var contracts = Ci.StartCodexContracts(geth); var gethInfo = CreateGethInfo(geth, contracts); diff --git a/Tests/FrameworkTests/NethereumWorkflow/BlockTimeFinderTests.cs b/Tests/FrameworkTests/NethereumWorkflow/BlockTimeFinderTests.cs index 59539a8..a085dfe 100644 --- a/Tests/FrameworkTests/NethereumWorkflow/BlockTimeFinderTests.cs +++ b/Tests/FrameworkTests/NethereumWorkflow/BlockTimeFinderTests.cs @@ -2,7 +2,6 @@ using Logging; using Moq; using NethereumWorkflow; -using NethereumWorkflow.BlockUtils; using NUnit.Framework; namespace FrameworkTests.NethereumWorkflow diff --git a/Tools/CodexNetDeployer/Deployer.cs b/Tools/CodexNetDeployer/Deployer.cs index 7915c37..1b8a4fe 100644 --- a/Tools/CodexNetDeployer/Deployer.cs +++ b/Tools/CodexNetDeployer/Deployer.cs @@ -1,4 +1,5 @@ -using CodexContractsPlugin; +using BlockchainUtils; +using CodexContractsPlugin; using CodexDiscordBotPlugin; using CodexPlugin; using Core; @@ -59,7 +60,7 @@ namespace CodexNetDeployer Log("Deploying Geth instance..."); var gethDeployment = DeployGeth(ci); - var gethNode = ci.WrapGethDeployment(gethDeployment); + var gethNode = ci.WrapGethDeployment(gethDeployment, new BlockCache()); Log("Geth started. Deploying Codex contracts..."); var contractsDeployment = ci.DeployCodexContracts(gethNode);