/ ProjectPlugins / CodexPlugin / CodexSetup.cs
CodexSetup.cs
  1  using CodexClient;
  2  using CodexContractsPlugin;
  3  using GethPlugin;
  4  using KubernetesWorkflow;
  5  using Utils;
  6  
  7  namespace CodexPlugin
  8  {
  9      public interface ICodexSetup
 10      {
 11          ICodexSetup WithName(string name);
 12          ICodexSetup At(ILocation location);
 13          ICodexSetup WithBootstrapNode(ICodexNode node);
 14          ICodexSetup WithLogLevel(CodexLogLevel level);
 15          ICodexSetup WithLogLevel(CodexLogLevel level, CodexLogCustomTopics customTopics);
 16          ICodexSetup WithStorageQuota(ByteSize storageQuota);
 17          ICodexSetup WithBlockTTL(TimeSpan duration);
 18          ICodexSetup WithBlockMaintenanceInterval(TimeSpan duration);
 19          ICodexSetup WithBlockMaintenanceNumber(int numberOfBlocks);
 20          ICodexSetup EnableMetrics();
 21          ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Action<IMarketplaceSetup> marketplaceSetup);
 22          /// <summary>
 23          /// Provides an invalid proof every N proofs
 24          /// </summary>
 25          ICodexSetup WithSimulateProofFailures(uint failEveryNProofs);
 26          ICodexSetup AsPublicTestNet(CodexTestNetConfig testNetConfig);
 27      }
 28  
 29      public interface IMarketplaceSetup
 30      {
 31          IMarketplaceSetup WithInitial(Ether eth, TestToken tokens);
 32          IMarketplaceSetup WithAccount(EthAccount account);
 33          IMarketplaceSetup AsStorageNode();
 34          IMarketplaceSetup AsValidator();
 35      }
 36  
 37      public class CodexLogCustomTopics
 38      {
 39          public CodexLogCustomTopics(CodexLogLevel discV5, CodexLogLevel libp2p, CodexLogLevel blockExchange)
 40          {
 41              DiscV5 = discV5;
 42              Libp2p = libp2p;
 43              BlockExchange = blockExchange;
 44          }
 45  
 46          public CodexLogCustomTopics(CodexLogLevel discV5, CodexLogLevel libp2p)
 47          {
 48              DiscV5 = discV5;
 49              Libp2p = libp2p;
 50          }
 51  
 52          public CodexLogLevel DiscV5 { get; set; }
 53          public CodexLogLevel Libp2p { get; set; }
 54          public CodexLogLevel ContractClock { get; set; } = CodexLogLevel.Warn;
 55          public CodexLogLevel? BlockExchange { get; }
 56          public CodexLogLevel JsonSerialize { get; set; } = CodexLogLevel.Warn;
 57          public CodexLogLevel MarketplaceInfra { get; set; } = CodexLogLevel.Warn;
 58      }
 59  
 60      public class CodexSetup : CodexStartupConfig, ICodexSetup
 61      {
 62          public int NumberOfNodes { get; }
 63  
 64          public CodexSetup(int numberOfNodes)
 65          {
 66              NumberOfNodes = numberOfNodes;
 67          }
 68  
 69          public ICodexSetup WithName(string name)
 70          {
 71              NameOverride = name;
 72              return this;
 73          }
 74  
 75          public ICodexSetup At(ILocation location)
 76          {
 77              Location = location;
 78              return this;
 79          }
 80  
 81          public ICodexSetup WithBootstrapNode(ICodexNode node)
 82          {
 83              BootstrapSpr = node.GetDebugInfo().Spr;
 84              return this;
 85          }
 86  
 87          public ICodexSetup WithLogLevel(CodexLogLevel level)
 88          {
 89              LogLevel = level;
 90              return this;
 91          }
 92  
 93          public ICodexSetup WithLogLevel(CodexLogLevel level, CodexLogCustomTopics customTopics)
 94          {
 95              LogLevel = level;
 96              CustomTopics = customTopics;
 97              return this;
 98          }
 99  
100          public ICodexSetup WithStorageQuota(ByteSize storageQuota)
101          {
102              StorageQuota = storageQuota;
103              return this;
104          }
105  
106          public ICodexSetup WithBlockTTL(TimeSpan duration)
107          {
108              BlockTTL = Convert.ToInt32(duration.TotalSeconds);
109              return this;
110          }
111  
112          public ICodexSetup WithBlockMaintenanceInterval(TimeSpan duration)
113          {
114              BlockMaintenanceInterval = duration;
115              return this;
116          }
117  
118          public ICodexSetup WithBlockMaintenanceNumber(int numberOfBlocks)
119          {
120              BlockMaintenanceNumber = numberOfBlocks;
121              return this;
122          }
123  
124          public ICodexSetup EnableMetrics()
125          {
126              MetricsEnabled = true;
127              return this;
128          }
129  
130          public ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Action<IMarketplaceSetup> marketplaceSetup)
131          {
132              var ms = new MarketplaceSetup();
133              marketplaceSetup(ms);
134  
135              MarketplaceConfig = new MarketplaceInitialConfig(ms, gethNode, codexContracts);
136              return this;
137          }
138  
139          public ICodexSetup WithSimulateProofFailures(uint failEveryNProofs)
140          {
141              SimulateProofFailures = failEveryNProofs;
142              return this;
143          }
144  
145          public ICodexSetup AsPublicTestNet(CodexTestNetConfig testNetConfig)
146          {
147              PublicTestNet = testNetConfig;
148              return this;
149          }
150  
151          public string Describe()
152          {
153              var args = string.Join(',', DescribeArgs());
154              return $"({NumberOfNodes} CodexNodes with args:[{args}])";
155          }
156  
157          private IEnumerable<string> DescribeArgs()
158          {
159              if (PublicTestNet != null) yield return $"<!>Public TestNet with listenPort: {PublicTestNet.PublicListenPort}<!>";
160              yield return $"LogLevel={LogLevelWithTopics()}";
161              if (BootstrapSpr != null) yield return $"BootstrapNode={BootstrapSpr}";
162              if (StorageQuota != null) yield return $"StorageQuota={StorageQuota}";
163              if (SimulateProofFailures != null) yield return $"SimulateProofFailures={SimulateProofFailures}";
164              if (MarketplaceConfig != null) yield return $"MarketplaceSetup={MarketplaceConfig.MarketplaceSetup}";
165          }
166      }
167  
168      public class MarketplaceSetup : IMarketplaceSetup
169      {
170          public bool IsStorageNode { get; private set; }
171          public bool IsValidator { get; private set; }
172          public Ether InitialEth { get; private set; } = 0.Eth();
173          public TestToken InitialTestTokens { get; private set; } = 0.Tst();
174          public EthAccountSetup EthAccountSetup { get; } = new EthAccountSetup();
175  
176          public IMarketplaceSetup AsStorageNode()
177          {
178              IsStorageNode = true;
179              return this;
180          }
181  
182          public IMarketplaceSetup AsValidator()
183          {
184              IsValidator = true;
185              return this;
186          }
187  
188          public IMarketplaceSetup WithAccount(EthAccount account)
189          {
190              EthAccountSetup.Pin(account);
191              return this;
192          }
193  
194          public IMarketplaceSetup WithInitial(Ether eth, TestToken tokens)
195          {
196              InitialEth = eth;
197              InitialTestTokens = tokens;
198              return this;
199          }
200  
201          public override string ToString()
202          {
203              var result = "[(clientNode)"; // When marketplace is enabled, being a clientNode is implicit.
204              result += IsStorageNode ? "(storageNode)" : "()";
205              result += IsValidator ? "(validator)" : "() ";
206              result += $"Pinned address: '{EthAccountSetup}' ";
207              result += $"{InitialEth} / {InitialTestTokens}";
208              result += "] ";
209              return result;
210          }
211      }
212  
213      public class EthAccountSetup
214      {
215          private readonly List<EthAccount> accounts = new List<EthAccount>();
216          private bool pinned = false;
217  
218          public void Pin(EthAccount account)
219          {
220              accounts.Add(account);
221              pinned = true;
222          }
223  
224          public EthAccount GetNew()
225          {
226              if (pinned) return accounts.Last();
227  
228              var a = EthAccountGenerator.GenerateNew();
229              accounts.Add(a);
230              return a;
231          }
232  
233          public EthAccount[] GetAll()
234          {
235              return accounts.ToArray();
236          }
237  
238          public override string ToString()
239          {
240              if (!accounts.Any()) return "NoEthAccounts";
241              return string.Join(",", accounts.Select(a => a.ToString()).ToArray());
242          }
243      }
244  }