/ ProjectPlugins / GethPlugin / GethContainerRecipe.cs
GethContainerRecipe.cs
  1  using KubernetesWorkflow;
  2  using KubernetesWorkflow.Recipe;
  3  
  4  namespace GethPlugin
  5  {
  6      public class GethContainerRecipe : ContainerRecipeFactory
  7      {
  8          public static string DockerImage { get; } = "codexstorage/dist-tests-geth:latest";
  9          public static TimeSpan BlockInterval { get; } = TimeSpan.FromSeconds(1.0);
 10          private const string defaultArgs = "--ipcdisable --syncmode full";
 11  
 12          public const string HttpPortTag = "http_port";
 13          public const string DiscoveryPortTag = "disc_port";
 14          public const string ListenPortTag = "listen_port";
 15          public const string WsPortTag = "ws_port";
 16          public const string AuthRpcPortTag = "auth_rpc_port";
 17          public const string AccountsFilename = "accounts.csv";
 18  
 19          public override string AppName => "geth";
 20          public override string Image => DockerImage;
 21  
 22          protected override void Initialize(StartupConfig startupConfig)
 23          {
 24              var config = startupConfig.Get<GethStartupConfig>();
 25  
 26              var args = CreateArgs(config);
 27  
 28              SetSchedulingAffinity(notIn: "false");
 29              SetSystemCriticalPriority();
 30  
 31              AddEnvVar("GETH_ARGS", args);
 32          }
 33  
 34          private string CreateArgs(GethStartupConfig config)
 35          {
 36              if (config.IsMiner)
 37              {
 38                  AddEnvVar("ENABLE_MINER", "1");
 39                  UnlockAccounts(0, 1);
 40              }
 41  
 42              var httpPort = CreateApiPort(config, tag: HttpPortTag);
 43              var discovery = CreateDiscoveryPort(config);
 44              var listen = CreateListenPort(config);
 45              var authRpc = CreateP2pPort(config, tag: AuthRpcPortTag);
 46              var wsPort = CreateP2pPort(config, tag: WsPortTag);
 47  
 48              var args = $"--http.addr 0.0.0.0 --http.port {httpPort.Number} --port {listen.Number} --discovery.port {discovery.Number} {defaultArgs}";
 49  
 50              if (config.BootstrapNode != null)
 51              {
 52                  var bootPubKey = config.BootstrapNode.PublicKey;
 53                  var bootIp = config.BootstrapNode.IpAddress;
 54                  var bootPort = config.BootstrapNode.Port;
 55                  var bootstrapArg = $" --bootnodes enode://{bootPubKey}@{bootIp}:{bootPort}";
 56                  args += bootstrapArg;
 57              }
 58              if (config.IsPublicTestNet != null)
 59              {
 60                  AddEnvVar("NAT_PUBLIC_IP_AUTO", PublicIpService.Address);
 61              }
 62  
 63              return args + $" --authrpc.port {authRpc.Number} --ws --ws.addr 0.0.0.0 --ws.port {wsPort.Number}";
 64          }
 65  
 66          private void UnlockAccounts(int startIndex, int numberOfAccounts)
 67          {
 68              if (startIndex < 0) throw new ArgumentException();
 69              if (numberOfAccounts < 0) throw new ArgumentException();
 70              if (startIndex + numberOfAccounts > 1000) throw new ArgumentException("Out of accounts!");
 71  
 72              AddEnvVar("UNLOCK_START_INDEX", startIndex.ToString());
 73              AddEnvVar("UNLOCK_NUMBER", numberOfAccounts.ToString());
 74          }
 75  
 76          private Port CreateDiscoveryPort(GethStartupConfig config)
 77          {
 78              if (config.IsPublicTestNet == null) return AddInternalPort(DiscoveryPortTag);
 79  
 80              return AddExposedPort(config.IsPublicTestNet.DiscoveryPort, DiscoveryPortTag, PortProtocol.UDP);
 81          }
 82  
 83          private Port CreateListenPort(GethStartupConfig config)
 84          {
 85              if (config.IsPublicTestNet == null) return AddInternalPort(ListenPortTag);
 86  
 87              return AddExposedPort(config.IsPublicTestNet.ListenPort, ListenPortTag);
 88          }
 89  
 90          private Port CreateP2pPort(GethStartupConfig config, string tag)
 91          {
 92              if (config.IsPublicTestNet != null)
 93              {
 94                  return AddExposedPort(tag);
 95              }
 96              return AddInternalPort(tag);
 97          }
 98  
 99          private Port CreateApiPort(GethStartupConfig config, string tag)
100          {
101              if (config.IsPublicTestNet != null)
102              {
103                  return AddInternalPort(tag);
104              }
105              return AddExposedPort(tag);
106          }
107      }
108  }