github.com/klaytn/klaytn@v1.12.1/cmd/homi/docker/service/validator.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // Copyright 2017 AMIS Technologies
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package service
    19  
    20  import (
    21  	"bytes"
    22  	"fmt"
    23  	"text/template"
    24  )
    25  
    26  type Validator struct {
    27  	Identity       int
    28  	Genesis        string
    29  	SCGenesis      string
    30  	Address        string
    31  	NodeKey        string
    32  	StaticNodes    string
    33  	BridgeNodes    string
    34  	Port           int
    35  	RPCPort        int
    36  	PrometheusPort int
    37  	IP             string
    38  	EthStats       string
    39  	Name           string
    40  	DockerImageId  string
    41  	UseFastHttp    bool
    42  	NetworkId      int
    43  	ParentChainId  int
    44  	NodeType       string
    45  	AddPrivKey     bool
    46  }
    47  
    48  func NewValidator(identity int, genesis, scGenesis string, nodeAddress string, nodeKey string, staticNodes, bridgeNodes string, port int, rpcPort int,
    49  	prometheusPort int, ethStats string, ip string, dockerImageId string, useFastHttp bool, networkId, parentChainId int,
    50  	namePrefix string, nodeType string, addPrivKey bool,
    51  ) *Validator {
    52  	return &Validator{
    53  		Identity:       identity,
    54  		Genesis:        genesis,
    55  		SCGenesis:      scGenesis,
    56  		Address:        nodeAddress,
    57  		NodeKey:        nodeKey,
    58  		Port:           port,
    59  		RPCPort:        rpcPort,
    60  		PrometheusPort: prometheusPort,
    61  		EthStats:       ethStats,
    62  		IP:             ip,
    63  		Name:           fmt.Sprintf("%s-%v", namePrefix, identity),
    64  		DockerImageId:  dockerImageId,
    65  		UseFastHttp:    useFastHttp,
    66  		NetworkId:      networkId,
    67  		ParentChainId:  parentChainId,
    68  		NodeType:       nodeType,
    69  		AddPrivKey:     addPrivKey,
    70  		StaticNodes:    staticNodes,
    71  		BridgeNodes:    bridgeNodes,
    72  	}
    73  }
    74  
    75  func (v Validator) String() string {
    76  	tmpl, err := template.New("validator").Parse(validatorTemplate)
    77  	if err != nil {
    78  		fmt.Printf("Failed to parse template, %v", err)
    79  		return ""
    80  	}
    81  
    82  	result := new(bytes.Buffer)
    83  	err = tmpl.Execute(result, v)
    84  	if err != nil {
    85  		fmt.Printf("Failed to render template, %v", err)
    86  		return ""
    87  	}
    88  	return result.String()
    89  }
    90  
    91  var validatorTemplate = `{{ .Name }}:
    92      hostname: {{ .Name }}
    93      image: {{ .DockerImageId }}
    94      ports:
    95        - '{{ .Port }}:32323'
    96        - '{{ .RPCPort }}:8551'
    97        - '{{ .PrometheusPort }}:61001'
    98  {{- if eq .Name "EN-0" }}
    99        - '50505:50505'
   100  {{- else if eq .Name "SCN-0" }}
   101        - '50506:50506'
   102  {{- end }}
   103      entrypoint:
   104        - /bin/sh
   105        - -c
   106        - |
   107          mkdir -p /klaytn
   108  {{- if eq .NodeType "scn" }}
   109          echo '{{ .SCGenesis }}' > /klaytn/genesis.json
   110  {{- else if eq .NodeType "spn" }}
   111          echo '{{ .SCGenesis }}' > /klaytn/genesis.json
   112  {{- else if eq .NodeType "sen" }}
   113          echo '{{ .SCGenesis }}' > /klaytn/genesis.json
   114  {{- else }}
   115          echo '{{ .Genesis }}' > /klaytn/genesis.json
   116  {{- end }}
   117          echo '{{ .StaticNodes }}' > /klaytn/static-nodes.json
   118  {{- if .BridgeNodes }}
   119          echo '{{ .BridgeNodes }}' > /klaytn/main-bridges.json
   120  {{- end }}
   121          k{{ .NodeType }} --datadir "/klaytn" init "/klaytn/genesis.json"
   122  
   123  {{- if .AddPrivKey}}
   124          echo '{"address":"75a59b94889a05c03c66c3c84e9d2f8308ca4abd","crypto":{"cipher":"aes-128-ctr","ciphertext":"347fef8ab9aaf9d41b6114dfc0d9fd6ecab9d660fa86f687dc7aa1e094b76184","cipherparams":{"iv":"5070268dfc64ced716cf407bee943def"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"2cf44cb912515c5de2aacf4d133fd97a56efece5a8fc691381296300b42fb6c8"},"mac":"ea20c0019321f2a91f1ca51bc99d58c8f7cf1f37cff5cc47ae17ad747c060046"},"id":"a122a8da-a787-4d3d-a627-02034553e674","version":1}' > /klaytn/keystore/mykey
   125          echo "SuperSecret1231" > /klaytn/password.txt
   126  {{- end}}
   127          echo "# docker-compose" >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   128          echo 'NETWORK=""' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   129          echo 'DATA_DIR="/klaytn"' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   130          echo 'LOG_DIR="$$DATA_DIR/log"' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   131          echo 'RPC_ENABLE=1' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   132          echo 'RPC_API="db,eth,klay,net,governance,web3,miner,personal,txpool,debug,admin,istanbul,mainbridge,subbridge"' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   133          echo 'NETWORK_ID="{{ .NetworkId }}"' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   134          echo 'NO_DISCOVER=1' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   135          echo 'ADDITIONAL="$$ADDITIONAL --identity \"{{ .Name }}\""' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   136          echo 'ADDITIONAL="$$ADDITIONAL --nodekeyhex {{ .NodeKey }}"' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   137  {{- if .AddPrivKey}}
   138          echo 'ADDITIONAL="$$ADDITIONAL --unlock \"75a59b94889a05c03c66c3c84e9d2f8308ca4abd\""' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   139          echo 'ADDITIONAL="$$ADDITIONAL --password "/klaytn/password.txt"' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   140  {{- end}}
   141  {{- if eq .NodeType "scn" }}
   142          echo 'PORT=32323' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   143  {{- end}}
   144  {{- if .ParentChainId }}
   145          echo 'ADDITIONAL="$$ADDITIONAL --parentchainid {{ .ParentChainId }}"' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   146          echo 'SC_SUB_BRIDGE=1' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   147          echo 'SC_SUB_BRIDGE_PORT=50506' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   148  {{- end}}
   149  {{- if .UseFastHttp}}
   150          echo 'SERVER_TYPE=fasthttp' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   151  {{- end}}
   152  {{- if eq .NodeType "cn" }}
   153          echo 'REWARDBASE={{ .Address }}' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   154  {{- else if eq .NodeType "pn" }}
   155          echo 'ADDITIONAL="$$ADDITIONAL --txpool.nolocals"' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   156  {{- else if eq .Name "EN-0" }}
   157          echo 'SC_MAIN_BRIDGE=1' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   158          echo 'SC_MAIN_BRIDGE_PORT=50505' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   159          echo 'SC_MAIN_BRIDGE_INDEXING=1' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   160  {{- end }}
   161          echo 'ADDITIONAL="$$ADDITIONAL --debug --metrics --prometheus"' >> /klaytn-docker-pkg/conf/k{{ .NodeType }}d.conf
   162          /klaytn-docker-pkg/bin/k{{ .NodeType }}d start
   163  {{- if eq .NodeType "cn"}}
   164          sleep 1
   165          ken attach --exec "personal.importRawKey('{{ .NodeKey }}', '')" http://localhost:{{ .RPCPort }}
   166          ken attach --exec "personal.unlockAccount('{{ .Address }}', '', 999999999)" http://localhost:{{ .RPCPort }}
   167  {{- end }}
   168          tail -F klaytn/log/k{{ .NodeType }}d.out
   169      networks:
   170        app_net:
   171          ipv4_address: {{ .IP }}
   172      restart: "no"`