Skip to content

CMD genesis generator and update circleci config #865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -85,6 +85,13 @@ jobs:
if [ ! -d rsync ]; then
sudo apt-get update && sudo apt-get install rsync
fi
- run:
name: GENERATING GENESIS
command: |
if [[ ${CIRCLE_BRANCH} == "develop" || ${CIRCLE_BRANCH} == "staging" ]]; then
go run cmd/main.go genesis generate -e ${CIRLE_BRANCH} -o dist \
&& cp ./dist/generated/genesis/genesis.go ./common/constant/genesis.go
fi
- run:
name: BUILD
command: |
@@ -120,7 +127,7 @@ jobs:
- run:
name: DEPLOY
command: |
for host in $ln1 $ln2 $ln3; do rsync -vae ssh ./dist/ root@$host:/root/zoobc --exclude='*.db'; done
for host in $ln1 $ln2 $ln3; do rsync -vae ssh ./dist/ root@$host:/root/zoobc --exclude='*.db' --exclude='generated'; done
- run:
name: UP SERVICES
command: |
@@ -152,7 +159,7 @@ jobs:
- run:
name: DEPLOY APP
command: |
rsync -vae ssh ./dist/ root@$ln4:/root/zoobc --exclude='*.db'
rsync -vae ssh ./dist/ root@$ln4:/root/zoobc --exclude='*.db' --exclude='generated'
- run:
name: UP SERVICE
command: |
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ accounts.txt
.editorconfig
.manual
release/
dist/
github.token
*.back
*.bak
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@ VERSION ?= latest
BINARY_CORE := zoobc
BINARY_CLI := zoomd
GITHUB_TOKEN ?= $(shell cat github.token)
genesis := false
gen-target:= alpha
gen-output := resource

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

.PHONY: generate-gen
generate-gen:
$(info generating new genesis file and replace old genesis file ...)
go run cmd/main.go genesis generate -e ${gen-target} -o ${gen-output}
mv ./${gen-output}/generated/genesis/genesis.go ./common/constant/genesis.go

.PHONY: build
build:
ifdef genesis
$(MAKE) generate-gen
endif
$(info build core with host os as target...)
mkdir -p release
go build -o release/$(BINARY_CORE)-$(VERSION)
38 changes: 26 additions & 12 deletions cmd/genesisblock/cmd.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@ import (
"io/ioutil"
"log"
"os"
"path"
"strings"
"text/template"
"time"

@@ -56,6 +58,9 @@ var (
Short: "genesis generate command used to generate a new genesis.go and cluster_config.json file",
Long: `genesis generate command generate a genesis.go file from a list of accounts and/or from current database.`,
Run: func(cmd *cobra.Command, args []string) {
if _, ok := envTargetValue[envTarget]; !ok {
log.Fatal("Invalid env-target flag given, only: develop,staging,alpha")
}
generateGenesisFiles(withDbLastState, dbPath, extraNodesCount)
},
}
@@ -82,9 +87,11 @@ func init() {
genesisGeneratorCmd.Flags().StringVar(&deploymentName, "deploymentName", "zoobc-alpha",
"nomad task name associated to this deployment")
genesisGeneratorCmd.Flags().StringVar(&kvFileCustomConfigFile, "kvFileCustomConfigFile", "",
"(optional) full path (path + fileNane) of a custom cluster_config.json file to use to generate consulKvInitScript."+
"(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")

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

@@ -116,8 +123,7 @@ func generateGenesisFiles(withDbLastState bool, dbPath string, extraNodesCount i
log.Fatal(err)
}
}

file, err := ioutil.ReadFile("./genesisblock/templates/preRegisteredNodes.json")
file, err := ioutil.ReadFile(path.Join(getRootPath(), fmt.Sprintf("./cmd/genesisblock/templates/%s.preRegisteredNodes.json", envTarget)))
if err == nil {
err = json.Unmarshal(file, &preRegisteredNodes)
if err != nil {
@@ -158,7 +164,7 @@ func generateGenesisFiles(withDbLastState bool, dbPath string, extraNodesCount i
}

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

// append to preRegistered nodes/accounts previous entries from a blockchain db file
outPath := "../resource/generated/genesis"
var outPath = path.Join(getRootPath(), fmt.Sprintf("%s/generated/genesis", output))

if err := os.MkdirAll(outPath, os.ModePerm); err != nil {
log.Fatalf("can't create folder %s. error: %s", outPath, err)
}
@@ -194,14 +201,14 @@ func generateGenesisFiles(withDbLastState bool, dbPath string, extraNodesCount i
accountNodes = append(accountNodes, newEntry)
}
generateAccountNodesFile(accountNodes, fmt.Sprintf("%s/accountNodes.json", outPath))
fmt.Println("Command executed successfully\ngenesis.go.new has been generated in resource/generated/genesis")
fmt.Println("to apply new genesis to the core-node, please overwrite common/constant/genesis." +
"go with the new one: resource/generated/genesis/genesis.go")
fmt.Printf("Command executed successfully\ngenesis.go.new has been generated in %s\n", outPath)
fmt.Printf("to apply new genesis to the core-node, please overwrite common/constant/genesis."+
"go with the new one: %s/genesis.go", outPath)
}

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

func getRootPath() string {
wd, _ := os.Getwd()
if strings.Contains(wd, "zoobc-core/") {
return path.Join(wd, "../")
}
return wd
}

func (ge *genesisEntry) FormatPubKeyByteString() string {
if ge.NodePublicKeyB64 == "" {
return ""
12 changes: 12 additions & 0 deletions cmd/genesisblock/const.go
Original file line number Diff line number Diff line change
@@ -22,4 +22,16 @@ var (
wellKnownPeers string
deploymentName string
kvFileCustomConfigFile string

/*
ENV Target
*/
envTarget string
output string
envTargetValue = map[string]uint32{
"develop": 0,
"staging": 1,
"alpha": 2,
"local": 3,
}
)
Original file line number Diff line number Diff line change
@@ -24,15 +24,15 @@
"Smithing": true
},
{
"AccountBalance": 100000000000,
"AccountBalance": 0,
"AccountAddress": "nK_ouxdDDwuJiogiDAi_zs1LqeN7f5ZsXbFtXGqGc0Pd",
"NodeSeed": "spree uplifted stapling quotable disfigure lair deduct untying timothy maggot cryptic unrigged",
"NodePublicKeyB64": "Keu41kYXmVloKfr4MwdFWeq1ZKMtRZhGNMmTRgbyNNw=",
"NodeAddress": "li1627-168.members.linode.com:7071",
"Smithing": true
},
{
"AccountBalance": 100000000000,
"AccountBalance": 0,
"AccountAddress": "iSJt3H8wFOzlWKsy_UoEWF_OjF6oymHMqthyUMDKSyxb",
"NodeSeed": "merge plug donor drop cancel inherit extra toss cabbage awkward destroy rail",
"NodePublicKeyB64": "WyTkRmVeuva6BE6OraK7rcpR81yNeJTcKaDQXq6mPs8=",
32 changes: 32 additions & 0 deletions cmd/genesisblock/templates/develop.preRegisteredNodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"AccountAddress": "BCZEGOb3WNx3fDOVf9ZS4EjvOIv_UeW4TVBQJ_6tHKlE",
"AccountBalance": 0,
"NodeSeed": "sprinkled sneak species pork outpost thrift unwind cheesy vexingly dizzy neurology neatness",
"NodePublicKeyB64": "mToyyAc9bOXMMMeRFWN9SzEtdmHbUPL0ZIaQ9iWQ1Yc=",
"LockedBalance": 0,
"NodeAddress": "172.104.34.10:8080",
"ParticipationScore": 0,
"Smithing": true
},
{
"AccountAddress": "OnEYzI-EMV6UTfoUEzpQUjkSlnqB82-SyRN7469lJTWH",
"AccountBalance": 0,
"NodeSeed": "demanding unlined hazard neuter condone anime asleep ascent capitol sitter marathon armband",
"NodePublicKeyB64": "AA4G2qo2PDICQoJ34ut+ywUMmMKqkis/4GV/8T6Yu/8=",
"LockedBalance": 0,
"NodeAddress": "45.79.39.58:8080",
"ParticipationScore": 0,
"Smithing": true
},
{
"AccountAddress": "iSJt3H8wFOzlWKsy_UoEWF_OjF6oymHMqthyUMDKSyxb",
"AccountBalance": 0,
"NodeSeed": "merge plug donor drop cancel inherit extra toss cabbage awkward destroy rail",
"NodePublicKeyB64": "WyTkRmVeuva6BE6OraK7rcpR81yNeJTcKaDQXq6mPs8=",
"LockedBalance": 0,
"NodeAddress": "85.90.246.90:8080",
"ParticipationScore": 0,
"Smithing": true
}
]
12 changes: 12 additions & 0 deletions cmd/genesisblock/templates/staging.preRegisteredNodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"AccountAddress": "iSJt3H8wFOzlWKsy_UoEWF_OjF6oymHMqthyUMDKSyxb",
"AccountBalance": 0,
"NodeSeed": "merge plug donor drop cancel inherit extra toss cabbage awkward destroy rail",
"NodePublicKeyB64": "WyTkRmVeuva6BE6OraK7rcpR81yNeJTcKaDQXq6mPs8=",
"LockedBalance": 0,
"NodeAddress": "172.104.47.168:8080",
"ParticipationScore": 0,
"Smithing": true
}
]
21 changes: 19 additions & 2 deletions cmd/readme.md
Original file line number Diff line number Diff line change
@@ -139,10 +139,27 @@ go run main.go generate account multisig --addresses "BCZnSfqpP5tqFQlMTYkDeBVFWn
```

## Other Commands
### Genesis
```bash
go run main.go genesis generate
Usage:
zoobc genesis generate [flags]

Flags:
-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/")
--deploymentName string nomad task name associated to this deployment (default "zoobc-alpha")
-e, --env-target string env mode indeed a.k.a develop,staging,alpha (default "alpha")
-n, --extraNodes int number of 'extra' autogenerated nodes to be deployed using cluster_config.json
-h, --help help for generate
--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
--logLevels string default log levels for all nodes (for kvConsulScript.sh). example: 'warn info fatal error panic' (default "fatal error panic")
-o, --output string output generated files target (default "resource")
--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")
```
outputs cmd/genesis.go.new and cmd/cluster_config.json

```bash
go run main.go genesis generate -e {local,staging,develop,alpa} -o dist
```
It will generate files such as genesis.go consul script and more, you can check these inside `${-o}/generated/genesis` directory.

```bash
### Genesis Generate From cmd/genesisblock/preRegisteredNodes.json and resource/zoobc.db
23 changes: 11 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
@@ -16,14 +16,14 @@

Table of Contents:

- [Environments](#environments)
- [Install](#install)
- [Build](#build)
- [Run](#run)
- [Tests](#tests)
- [Swagger](#swagger)
- [Contributing](#contributing)
- [GRPC web proxy for browser](#grpc-web-proxy-for-browser)
- [Environments](#environments)
- [Install](#install)
- [Build](#build)
- [Run](#run)
- [Tests](#tests)
- [Swagger](#swagger)
- [Contributing](#contributing)
- [GRPC web proxy for browser](#grpc-web-proxy-for-browser)

### Environments

@@ -52,16 +52,15 @@ Table of Contents:
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.

- Core

note: For cross compilation please install and activate docker.

> note: For cross compilation please install and activate docker.
For:

- host: `go build -o zoobc`
- darwin: `make VERSION=v1.10.1 core-darwin`
- linux (386 & amd64): `make VERSION=v1.10.1 core-linux`
- windows (32 & 64bit): `make VERSION=v1.10.1 core-windows`

> With genesis replacement, you can add argument `genesis=true` and what your target is {develop,staging,alhpa(default),local}, like:
`make build genesis=true gen-target=develop gen-output=dist` for the local target you need create `local.preRegisteredNodes.json`.
- CMD

For: