Skip to content

Commit 9e58b75

Browse files
authored
CMD genesis generator and update circleci config (#865)
* cmd genesis generator and update circleci config * fix golangci warning * remove unused constant * build job only for staging and develop * account balance 0 as default * wrong path given when get root via wd
1 parent fe8efa5 commit 9e58b75

11 files changed

+136
-30
lines changed

.circleci/config.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ jobs:
8585
if [ ! -d rsync ]; then
8686
sudo apt-get update && sudo apt-get install rsync
8787
fi
88+
- run:
89+
name: GENERATING GENESIS
90+
command: |
91+
if [[ ${CIRCLE_BRANCH} == "develop" || ${CIRCLE_BRANCH} == "staging" ]]; then
92+
go run cmd/main.go genesis generate -e ${CIRLE_BRANCH} -o dist \
93+
&& cp ./dist/generated/genesis/genesis.go ./common/constant/genesis.go
94+
fi
8895
- run:
8996
name: BUILD
9097
command: |
@@ -120,7 +127,7 @@ jobs:
120127
- run:
121128
name: DEPLOY
122129
command: |
123-
for host in $ln1 $ln2 $ln3; do rsync -vae ssh ./dist/ root@$host:/root/zoobc --exclude='*.db'; done
130+
for host in $ln1 $ln2 $ln3; do rsync -vae ssh ./dist/ root@$host:/root/zoobc --exclude='*.db' --exclude='generated'; done
124131
- run:
125132
name: UP SERVICES
126133
command: |
@@ -152,7 +159,7 @@ jobs:
152159
- run:
153160
name: DEPLOY APP
154161
command: |
155-
rsync -vae ssh ./dist/ root@$ln4:/root/zoobc --exclude='*.db'
162+
rsync -vae ssh ./dist/ root@$ln4:/root/zoobc --exclude='*.db' --exclude='generated'
156163
- run:
157164
name: UP SERVICE
158165
command: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ accounts.txt
2525
.editorconfig
2626
.manual
2727
release/
28+
dist/
2829
github.token
2930
*.back
3031
*.bak

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ VERSION ?= latest
66
BINARY_CORE := zoobc
77
BINARY_CLI := zoomd
88
GITHUB_TOKEN ?= $(shell cat github.token)
9+
genesis := false
10+
gen-target:= alpha
11+
gen-output := resource
912

1013
.PHONY: test
1114
test: go-fmt golangci-lint
@@ -30,8 +33,17 @@ go-fmt:
3033
$(info running go-fmt...)
3134
go fmt `go list ./... | egrep -v 'common/model|common/service|vendor'`
3235

36+
.PHONY: generate-gen
37+
generate-gen:
38+
$(info generating new genesis file and replace old genesis file ...)
39+
go run cmd/main.go genesis generate -e ${gen-target} -o ${gen-output}
40+
mv ./${gen-output}/generated/genesis/genesis.go ./common/constant/genesis.go
41+
3342
.PHONY: build
3443
build:
44+
ifdef genesis
45+
$(MAKE) generate-gen
46+
endif
3547
$(info build core with host os as target...)
3648
mkdir -p release
3749
go build -o release/$(BINARY_CORE)-$(VERSION)

cmd/genesisblock/cmd.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"io/ioutil"
99
"log"
1010
"os"
11+
"path"
12+
"strings"
1113
"text/template"
1214
"time"
1315

@@ -56,6 +58,9 @@ var (
5658
Short: "genesis generate command used to generate a new genesis.go and cluster_config.json file",
5759
Long: `genesis generate command generate a genesis.go file from a list of accounts and/or from current database.`,
5860
Run: func(cmd *cobra.Command, args []string) {
61+
if _, ok := envTargetValue[envTarget]; !ok {
62+
log.Fatal("Invalid env-target flag given, only: develop,staging,alpha")
63+
}
5964
generateGenesisFiles(withDbLastState, dbPath, extraNodesCount)
6065
},
6166
}
@@ -82,9 +87,11 @@ func init() {
8287
genesisGeneratorCmd.Flags().StringVar(&deploymentName, "deploymentName", "zoobc-alpha",
8388
"nomad task name associated to this deployment")
8489
genesisGeneratorCmd.Flags().StringVar(&kvFileCustomConfigFile, "kvFileCustomConfigFile", "",
85-
"(optional) full path (path + fileNane) of a custom cluster_config.json file to use to generate consulKvInitScript."+
90+
"(optional) full path (path + fileName) of a custom cluster_config.json file to use to generate consulKvInitScript."+
8691
"sh instead of the automatically generated in resource/generated/genesis directory")
8792

93+
genesisGeneratorCmd.Flags().StringVarP(&envTarget, "env-target", "e", "alpha", "env mode indeed a.k.a develop,staging,alpha")
94+
genesisGeneratorCmd.Flags().StringVarP(&output, "output", "o", "resource", "output generated files target")
8895
genesisCmd.AddCommand(genesisGeneratorCmd)
8996
}
9097

@@ -116,8 +123,7 @@ func generateGenesisFiles(withDbLastState bool, dbPath string, extraNodesCount i
116123
log.Fatal(err)
117124
}
118125
}
119-
120-
file, err := ioutil.ReadFile("./genesisblock/templates/preRegisteredNodes.json")
126+
file, err := ioutil.ReadFile(path.Join(getRootPath(), fmt.Sprintf("./cmd/genesisblock/templates/%s.preRegisteredNodes.json", envTarget)))
121127
if err == nil {
122128
err = json.Unmarshal(file, &preRegisteredNodes)
123129
if err != nil {
@@ -158,7 +164,7 @@ func generateGenesisFiles(withDbLastState bool, dbPath string, extraNodesCount i
158164
}
159165

160166
// generate extra nodes from a json file containing only account addresses
161-
file, err = ioutil.ReadFile("./genesisblock/templates/genesisAccountAddresses.json")
167+
file, err = ioutil.ReadFile(path.Join(getRootPath(), fmt.Sprintf("./cmd/genesisblock/templates/%s.genesisAccountAddresses.json", envTarget)))
162168
if err == nil {
163169
// read custom addresses from file
164170
err = json.Unmarshal(file, &preRegisteredAccountAddresses)
@@ -175,7 +181,8 @@ func generateGenesisFiles(withDbLastState bool, dbPath string, extraNodesCount i
175181
}
176182

177183
// append to preRegistered nodes/accounts previous entries from a blockchain db file
178-
outPath := "../resource/generated/genesis"
184+
var outPath = path.Join(getRootPath(), fmt.Sprintf("%s/generated/genesis", output))
185+
179186
if err := os.MkdirAll(outPath, os.ModePerm); err != nil {
180187
log.Fatalf("can't create folder %s. error: %s", outPath, err)
181188
}
@@ -194,14 +201,14 @@ func generateGenesisFiles(withDbLastState bool, dbPath string, extraNodesCount i
194201
accountNodes = append(accountNodes, newEntry)
195202
}
196203
generateAccountNodesFile(accountNodes, fmt.Sprintf("%s/accountNodes.json", outPath))
197-
fmt.Println("Command executed successfully\ngenesis.go.new has been generated in resource/generated/genesis")
198-
fmt.Println("to apply new genesis to the core-node, please overwrite common/constant/genesis." +
199-
"go with the new one: resource/generated/genesis/genesis.go")
204+
fmt.Printf("Command executed successfully\ngenesis.go.new has been generated in %s\n", outPath)
205+
fmt.Printf("to apply new genesis to the core-node, please overwrite common/constant/genesis."+
206+
"go with the new one: %s/genesis.go", outPath)
200207
}
201208

202209
// generateRandomGenesisEntry randomly generates a genesis node entry
203210
// note: the account address is mandatory for the node registration, but as there is no wallet connected to it
204-
// and we are not storing the relaitve seed, needed to sign transactions, these nodes can smith but their owners
211+
// and we are not storing the relative seed, needed to sign transactions, these nodes can smith but their owners
205212
// can't perform any transaction.
206213
// This is only useful to test multiple smithing-nodes, for instence in a network stress test of tens of nodes connected together
207214
func generateRandomGenesisEntry(nodeIdx int, accountAddress string) genesisEntry {
@@ -333,8 +340,7 @@ func getDbLastState(dbPath string) (bcEntries []genesisEntry, err error) {
333340
// generateGenesisFile generates a genesis file with given entries, starting from a template
334341
func generateGenesisFile(genesisEntries []genesisEntry, newGenesisFilePath string) {
335342
// read and execute genesis template, outputting the genesis.go to stdout
336-
// genesisTmpl, err := helpers.ReadTemplateFile("./genesis.tmpl")
337-
tmpl, err := template.ParseFiles("./genesisblock/templates/genesis.tmpl")
343+
tmpl, err := template.ParseFiles(path.Join(getRootPath(), "./cmd/genesisblock/templates/genesis.tmpl"))
338344
if err != nil {
339345
log.Fatalf("Error while reading genesis.tmpl file: %s", err)
340346
}
@@ -464,7 +470,7 @@ func generateAccountNodesFile(accountNodeEntris []accountNodeEntry, configFilePa
464470
func generateConsulKvInitScript(clusterConfigEntries []clusterConfigEntry, consulKvInitScriptPath string) {
465471
// read and execute genesis template, outputting the genesis.go to stdout
466472
// genesisTmpl, err := helpers.ReadTemplateFile("./genesis.tmpl")
467-
tmpl, err := template.ParseFiles("./genesisblock/templates/consulKvInit.tmpl")
473+
tmpl, err := template.ParseFiles(path.Join(getRootPath(), "./cmd/genesisblock/templates/consulKvInit.tmpl"))
468474
if err != nil {
469475
log.Fatalf("Error while reading consulKvInit.tmpl file: %s", err)
470476
}
@@ -506,6 +512,14 @@ func generateConsulKvInitScript(clusterConfigEntries []clusterConfigEntry, consu
506512
}
507513
}
508514

515+
func getRootPath() string {
516+
wd, _ := os.Getwd()
517+
if strings.Contains(wd, "zoobc-core/") {
518+
return path.Join(wd, "../")
519+
}
520+
return wd
521+
}
522+
509523
func (ge *genesisEntry) FormatPubKeyByteString() string {
510524
if ge.NodePublicKeyB64 == "" {
511525
return ""

cmd/genesisblock/const.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ var (
2222
wellKnownPeers string
2323
deploymentName string
2424
kvFileCustomConfigFile string
25+
26+
/*
27+
ENV Target
28+
*/
29+
envTarget string
30+
output string
31+
envTargetValue = map[string]uint32{
32+
"develop": 0,
33+
"staging": 1,
34+
"alpha": 2,
35+
"local": 3,
36+
}
2537
)

cmd/genesisblock/templates/preRegisteredNodes.json.example renamed to cmd/genesisblock/templates/alpha.preRegisteredNodes.json.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
"Smithing": true
2525
},
2626
{
27-
"AccountBalance": 100000000000,
27+
"AccountBalance": 0,
2828
"AccountAddress": "nK_ouxdDDwuJiogiDAi_zs1LqeN7f5ZsXbFtXGqGc0Pd",
2929
"NodeSeed": "spree uplifted stapling quotable disfigure lair deduct untying timothy maggot cryptic unrigged",
3030
"NodePublicKeyB64": "Keu41kYXmVloKfr4MwdFWeq1ZKMtRZhGNMmTRgbyNNw=",
3131
"NodeAddress": "li1627-168.members.linode.com:7071",
3232
"Smithing": true
3333
},
3434
{
35-
"AccountBalance": 100000000000,
35+
"AccountBalance": 0,
3636
"AccountAddress": "iSJt3H8wFOzlWKsy_UoEWF_OjF6oymHMqthyUMDKSyxb",
3737
"NodeSeed": "merge plug donor drop cancel inherit extra toss cabbage awkward destroy rail",
3838
"NodePublicKeyB64": "WyTkRmVeuva6BE6OraK7rcpR81yNeJTcKaDQXq6mPs8=",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"AccountAddress": "BCZEGOb3WNx3fDOVf9ZS4EjvOIv_UeW4TVBQJ_6tHKlE",
4+
"AccountBalance": 0,
5+
"NodeSeed": "sprinkled sneak species pork outpost thrift unwind cheesy vexingly dizzy neurology neatness",
6+
"NodePublicKeyB64": "mToyyAc9bOXMMMeRFWN9SzEtdmHbUPL0ZIaQ9iWQ1Yc=",
7+
"LockedBalance": 0,
8+
"NodeAddress": "172.104.34.10:8080",
9+
"ParticipationScore": 0,
10+
"Smithing": true
11+
},
12+
{
13+
"AccountAddress": "OnEYzI-EMV6UTfoUEzpQUjkSlnqB82-SyRN7469lJTWH",
14+
"AccountBalance": 0,
15+
"NodeSeed": "demanding unlined hazard neuter condone anime asleep ascent capitol sitter marathon armband",
16+
"NodePublicKeyB64": "AA4G2qo2PDICQoJ34ut+ywUMmMKqkis/4GV/8T6Yu/8=",
17+
"LockedBalance": 0,
18+
"NodeAddress": "45.79.39.58:8080",
19+
"ParticipationScore": 0,
20+
"Smithing": true
21+
},
22+
{
23+
"AccountAddress": "iSJt3H8wFOzlWKsy_UoEWF_OjF6oymHMqthyUMDKSyxb",
24+
"AccountBalance": 0,
25+
"NodeSeed": "merge plug donor drop cancel inherit extra toss cabbage awkward destroy rail",
26+
"NodePublicKeyB64": "WyTkRmVeuva6BE6OraK7rcpR81yNeJTcKaDQXq6mPs8=",
27+
"LockedBalance": 0,
28+
"NodeAddress": "85.90.246.90:8080",
29+
"ParticipationScore": 0,
30+
"Smithing": true
31+
}
32+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"AccountAddress": "iSJt3H8wFOzlWKsy_UoEWF_OjF6oymHMqthyUMDKSyxb",
4+
"AccountBalance": 0,
5+
"NodeSeed": "merge plug donor drop cancel inherit extra toss cabbage awkward destroy rail",
6+
"NodePublicKeyB64": "WyTkRmVeuva6BE6OraK7rcpR81yNeJTcKaDQXq6mPs8=",
7+
"LockedBalance": 0,
8+
"NodeAddress": "172.104.47.168:8080",
9+
"ParticipationScore": 0,
10+
"Smithing": true
11+
}
12+
]

cmd/readme.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,27 @@ go run main.go generate account multisig --addresses "BCZnSfqpP5tqFQlMTYkDeBVFWn
139139
```
140140

141141
## Other Commands
142+
### Genesis
142143
```bash
143-
go run main.go genesis generate
144+
Usage:
145+
zoobc genesis generate [flags]
146+
147+
Flags:
148+
-f, --dbPath string path of blockchain's database to be used as data source in case the -w flag is used. If not set, the default resource folder is used (default "../resource/")
149+
--deploymentName string nomad task name associated to this deployment (default "zoobc-alpha")
150+
-e, --env-target string env mode indeed a.k.a develop,staging,alpha (default "alpha")
151+
-n, --extraNodes int number of 'extra' autogenerated nodes to be deployed using cluster_config.json
152+
-h, --help help for generate
153+
--kvFileCustomConfigFile string (optional) full path (path + fileName) of a custom cluster_config.json file to use to generate consulKvInitScript.sh instead of the automatically generated in resource/generated/genesis directory
154+
--logLevels string default log levels for all nodes (for kvConsulScript.sh). example: 'warn info fatal error panic' (default "fatal error panic")
155+
-o, --output string output generated files target (default "resource")
156+
--wellKnownPeers string default wellKnownPeers for all nodes (for kvConsulScript.sh). example: 'n0.alpha.proofofparticipation.network n1.alpha.proofofparticipation.network n2.alpha.proofofparticipation.network' (default "127.0.0.1:8001")
144157
```
145-
outputs cmd/genesis.go.new and cmd/cluster_config.json
158+
159+
```bash
160+
go run main.go genesis generate -e {local,staging,develop,alpa} -o dist
161+
```
162+
It will generate files such as genesis.go consul script and more, you can check these inside `${-o}/generated/genesis` directory.
146163
147164
```bash
148165
### Genesis Generate From cmd/genesisblock/preRegisteredNodes.json and resource/zoobc.db

readme.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
1717
Table of Contents:
1818

19-
- [Environments](#environments)
20-
- [Install](#install)
21-
- [Build](#build)
22-
- [Run](#run)
23-
- [Tests](#tests)
24-
- [Swagger](#swagger)
25-
- [Contributing](#contributing)
26-
- [GRPC web proxy for browser](#grpc-web-proxy-for-browser)
19+
- [Environments](#environments)
20+
- [Install](#install)
21+
- [Build](#build)
22+
- [Run](#run)
23+
- [Tests](#tests)
24+
- [Swagger](#swagger)
25+
- [Contributing](#contributing)
26+
- [GRPC web proxy for browser](#grpc-web-proxy-for-browser)
2727

2828
### Environments
2929

@@ -52,16 +52,15 @@ Table of Contents:
5252
To make use of the `Makefile` please rename `github.token.example` to `github.token` and place your github token there. This is required since we are accessing private repository for one of our dependencies.
5353

5454
- Core
55-
56-
note: For cross compilation please install and activate docker.
57-
55+
> note: For cross compilation please install and activate docker.
5856
For:
5957

6058
- host: `go build -o zoobc`
6159
- darwin: `make VERSION=v1.10.1 core-darwin`
6260
- linux (386 & amd64): `make VERSION=v1.10.1 core-linux`
6361
- windows (32 & 64bit): `make VERSION=v1.10.1 core-windows`
64-
62+
> With genesis replacement, you can add argument `genesis=true` and what your target is {develop,staging,alhpa(default),local}, like:
63+
`make build genesis=true gen-target=develop gen-output=dist` for the local target you need create `local.preRegisteredNodes.json`.
6564
- CMD
6665

6766
For:

0 commit comments

Comments
 (0)