github.com/number571/tendermint@v0.34.11-gost/cmd/tendermint/build_testnet.c (about)

     1  #include <stdio.h>
     2  #include <stdlib.h>
     3  #include <string.h>
     4  
     5  #include <sys/stat.h>
     6  
     7  #define LOCBSIZE 256
     8  #define BUFFSIZE 4096
     9  
    10  #define TENDERMINT "go run main.go"
    11  #define LEN(obj) sizeof((obj))/sizeof((obj)[0])
    12  
    13  #define NODEPATH(i) nodepath, (i)
    14  #define CONFIGFILE(i) NODEPATH(i), configfile
    15  
    16  static char buffer[BUFFSIZE];
    17  static char nodesid[4][LOCBSIZE];
    18  
    19  static const char *ports_to_replace[] = {"26658", "26657", "26656"};
    20  static const char *nodepath = "./mytestnet/node";
    21  static const char *configfile = "/config/config.toml";
    22  
    23  static int save_nodes_id(void);
    24  static int replace_ports(void);
    25  static int create_scripts(void);
    26  
    27  int main(void) {
    28      int err;
    29  
    30      err = save_nodes_id();
    31      if (err != 0) {
    32          fprintf(stderr, "error: save_nodes_id\n");
    33          return 1;
    34      }
    35  
    36      err = replace_ports();
    37      if (err != 0) {
    38          fprintf(stderr, "error: replace_ports\n");
    39          return 2;
    40      }
    41  
    42      err = create_scripts();
    43      if (err != 0) {
    44          fprintf(stderr, "error: create_scripts\n");
    45          return 3;
    46      }
    47  
    48      return 0;
    49  }
    50  
    51  static int save_nodes_id(void) {
    52      FILE *ptr;
    53  
    54      for (int i = 0; i < LEN(nodesid); ++i) {
    55          snprintf(buffer, BUFFSIZE, "%s show-node-id --home %s%d", TENDERMINT, NODEPATH(i));
    56  
    57          ptr = popen(buffer, "r");
    58          if (ptr == NULL) {
    59              return 1;
    60          }
    61          
    62          fgets(nodesid[i], LOCBSIZE, ptr);
    63          nodesid[i][strlen(nodesid[i])-1] = '\0';
    64  
    65          pclose(ptr);
    66      }
    67  
    68      return 0;
    69  }
    70  
    71  static int replace_ports(void) {
    72      char locbuff[LOCBSIZE];
    73      FILE *ptr;
    74      long int pos;
    75      char *strptr;
    76  
    77      for (int i = 0; i < LEN(nodesid); ++i) {
    78          snprintf(locbuff, LOCBSIZE, "%s%d%s", CONFIGFILE(i));
    79  
    80          ptr = fopen(locbuff, "r+");
    81          if (ptr == NULL) {
    82              return 1;
    83          }
    84  
    85          pos = 0;
    86          while (fgets(buffer, BUFFSIZE, ptr)) {
    87              for (int j = 0; j < LEN(ports_to_replace); ++j) {
    88                  if (strptr = strstr(buffer, ports_to_replace[j])) {
    89                      fseek(ptr, pos, SEEK_SET);
    90                      strptr[2] = i+'0';
    91                      fputs(buffer, ptr);
    92                      fseek(ptr, pos, SEEK_SET);
    93                  }
    94              }
    95              pos = ftell(ptr);
    96          }
    97  
    98          fclose(ptr);
    99      }
   100  
   101      return 0;
   102  }
   103  
   104  static int create_scripts(void) {
   105      char locbuff[LOCBSIZE*2];
   106      FILE *ptr;
   107  
   108      for (int i = 0; i < LEN(nodesid); ++i) {
   109          int in[LEN(nodesid)-1];
   110          for (int j = 0, k = 0; j < LEN(nodesid); ++j) {
   111              if (i == j) {
   112                  continue;
   113              }
   114              in[k++] = j;
   115          }
   116  
   117          snprintf(buffer, BUFFSIZE, "%s start --home %s%d --proxy-app=kvstore --p2p.persistent-peers=\"",
   118              TENDERMINT, NODEPATH(i));
   119          for (int j = 0; j < LEN(nodesid)-1; ++j) {
   120              snprintf(locbuff, LOCBSIZE*2, "%s@127.0.0.1:26%d56,", nodesid[in[j]], in[j]);
   121              strncat(buffer, locbuff, BUFFSIZE);
   122          }
   123          buffer[strlen(buffer)-1] = '\"';
   124  
   125          printf("%s\n", buffer);
   126  
   127          snprintf(locbuff, LOCBSIZE, "run_node%d.sh", i);
   128  
   129          ptr = fopen(locbuff, "w");
   130          if (ptr == NULL) {
   131              return 1;
   132          }
   133          fprintf(ptr, "%s\n\n%s\n", "#!/bin/bash", buffer);
   134          fclose(ptr);
   135  
   136          chmod(locbuff, 0744);
   137      }
   138  
   139      return 0;
   140  }