Program.cs
1 using ArgsUniform; 2 using AutoClient; 3 using AutoClient.Modes; 4 using CodexClient; 5 using GethPlugin; 6 using Utils; 7 using WebUtils; 8 using Logging; 9 10 public class Program 11 { 12 private readonly App app; 13 14 public Program(Configuration config) 15 { 16 app = new App(config); 17 } 18 19 public static void Main(string[] args) 20 { 21 var cts = new CancellationTokenSource(); 22 Console.CancelKeyPress += (sender, args) => cts.Cancel(); 23 24 var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args); 25 var config = uniformArgs.Parse(true); 26 27 if (config.NumConcurrentPurchases < 1) 28 { 29 throw new Exception("Number of concurrent purchases must be > 0"); 30 } 31 32 var p = new Program(config); 33 p.Run(); 34 } 35 36 public void Run() 37 { 38 if (app.Config.ContractDurationMinutes - 1 < 5) throw new Exception("Contract duration config option not long enough!"); 39 var codexNodes = CreateCodexWrappers(); 40 var loadBalancer = new LoadBalancer(app, codexNodes); 41 loadBalancer.Start(); 42 43 var folderStore = new FolderStoreMode(app, loadBalancer); 44 folderStore.Start(); 45 46 app.Cts.Token.WaitHandle.WaitOne(); 47 48 folderStore.Stop(); 49 loadBalancer.Stop(); 50 51 app.Log.Log("Done"); 52 } 53 54 private CodexWrapper[] CreateCodexWrappers() 55 { 56 var endpointStrs = app.Config.CodexEndpoints.Split(";", StringSplitOptions.RemoveEmptyEntries); 57 var result = new List<CodexWrapper>(); 58 59 var i = 1; 60 foreach (var e in endpointStrs) 61 { 62 result.Add(CreateCodexWrapper(e, i)); 63 i++; 64 } 65 66 return result.ToArray(); 67 } 68 69 private readonly string LogLevel = "TRACE;info:discv5,providers,routingtable,manager,cache;warn:libp2p,multistream,switch,transport,tcptransport,semaphore,asyncstreamwrapper,lpstream,mplex,mplexchannel,noise,bufferstream,mplexcoder,secure,chronosstream,connection,websock,ws-session,muxedupgrade,upgrade,identify,contracts,clock,serde,json,serialization,JSONRPC-WS-CLIENT,JSONRPC-HTTP-CLIENT,repostore"; 70 71 private CodexWrapper CreateCodexWrapper(string endpoint, int number) 72 { 73 var splitIndex = endpoint.LastIndexOf(':'); 74 var host = endpoint.Substring(0, splitIndex); 75 var port = Convert.ToInt32(endpoint.Substring(splitIndex + 1)); 76 77 var address = new Address( 78 logName: $"cdx@{host}:{port}", 79 host: host, 80 port: port 81 ); 82 83 var numberStr = number.ToString().PadLeft(3, '0'); 84 var log = new LogPrefixer(app.Log, $"[{numberStr}] "); 85 var httpFactory = new HttpFactory(log, new AutoClientWebTimeSet()); 86 var codexNodeFactory = new CodexNodeFactory(log: log, httpFactory: httpFactory, dataDir: app.Config.DataPath); 87 var instance = CodexInstance.CreateFromApiEndpoint($"[AC-{numberStr}]", address, EthAccountGenerator.GenerateNew()); 88 var node = codexNodeFactory.CreateCodexNode(instance); 89 90 node.SetLogLevel(LogLevel); 91 return new CodexWrapper(app, node); 92 } 93 94 private static void PrintHelp() 95 { 96 Console.WriteLine("Generates fake data and creates Codex storage contracts for it."); 97 } 98 }