From 122c62ca81e76706fe9f967527214059adf089f0 Mon Sep 17 00:00:00 2001 From: David Krett Date: Thu, 26 Jul 2018 18:47:50 -0400 Subject: [PATCH] Interim - Adjust Scripts & PG Connection Params to facilitate Docker DB option --- core/db.go | 21 ++++++++-- core/genesis.go | 84 ++++++++++++++++++++----------------- docker-compose.yml | 16 +++++-- shyft-cli/initShyftGeth.sh | 6 ++- shyft-cli/startShyftGeth.sh | 6 ++- wait-for.sh | 2 +- 6 files changed, 86 insertions(+), 49 deletions(-) diff --git a/core/db.go b/core/db.go index 4c69bfa338..4686de61fd 100644 --- a/core/db.go +++ b/core/db.go @@ -3,18 +3,23 @@ package core import ( "database/sql" "fmt" + "os" ) var blockExplorerDb *sql.DB -// @NOTE:SHYFT - TODO: Move connection parameters into an env file +// @NOTE: SHYFT - could be refactored to add a test db environment const ( - connStr = "user=postgres dbname=shyftdb host=pg password=docker sslmode=disable" - // connStr = "postgresql://postgres:docker@pg:5432/shyftdb" connStrTest = "user=postgres dbname=shyftdbtest password=docker sslmode=disable" ) +var connStr = connectionStr() + +// InitDB - initalizes a Postgresql Database for use by the Blockexplorer func InitDB() (*sql.DB, error) { + // To set the environment you can run the program with an ENV variable DBENV. + // DBENV defaults to local for purposes of running the correct local + // database connection parameters but will use docker connection parameters if DBENV=docker db, err := sql.Open("postgres", connStr) if err != nil { fmt.Println("ERROR OPENING DB, NOT INITIALIZING") @@ -26,6 +31,16 @@ func InitDB() (*sql.DB, error) { } } +func connectionStr() string { + dbEnv := os.Getenv("DBENV") + switch dbEnv { + default: + return "user=postgres dbname=shyftdb host=localhost sslmode=disable" + case "docker": + return "user=postgres dbname=shyftdb host=pg password=docker sslmode=disable" + } +} + func InitDBTest() (*sql.DB, error) { db, err := sql.Open("postgres", connStrTest) if err != nil { diff --git a/core/genesis.go b/core/genesis.go index 398b38d5f6..e029ceaec4 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -25,7 +25,10 @@ import ( "math/big" "strings" - _ "github.com/lib/pq" + "database/sql" + "strconv" + "time" + "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/common/math" @@ -35,9 +38,7 @@ import ( "github.com/ShyftNetwork/go-empyrean/log" "github.com/ShyftNetwork/go-empyrean/params" "github.com/ShyftNetwork/go-empyrean/rlp" - "database/sql" - "strconv" - "time" + _ "github.com/lib/pq" ) //go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go @@ -151,47 +152,49 @@ func WriteShyftGen(gen *Genesis, block *types.Block) { var response string sqlExistsStatement := `SELECT balance from accounts WHERE addr = ($1)` err := sqldb.QueryRow(sqlExistsStatement, addr).Scan(&response) - switch { - case err == sql.ErrNoRows: - for k, v := range gen.Alloc { - number := block.Header().Number.String() - gasUsed := block.Header().GasUsed - gasLimit := block.Header().GasLimit - gasPrice := 0 - txFee := 0 - txStatus := "" - isContract := false - data:= "" - addr := k.String() - txCountAccount := v.Nonce +1 - i, err := strconv.ParseInt(block.Time().String(), 10, 64) - if err != nil { - panic(err) - } - age := time.Unix(i, 0) - Genesis := []string{"GENESIS_", addr} - GENESIS := "GENESIS" - txHash := strings.Join(Genesis, addr) + switch { + case err == sql.ErrNoRows: + for k, v := range gen.Alloc { + number := block.Header().Number.String() + gasUsed := block.Header().GasUsed + gasLimit := block.Header().GasLimit + gasPrice := 0 + txFee := 0 + txStatus := "" + isContract := false + data := "" + addr := k.String() + txCountAccount := v.Nonce + 1 + i, err := strconv.ParseInt(block.Time().String(), 10, 64) + if err != nil { + panic(err) + } + age := time.Unix(i, 0) + Genesis := []string{"GENESIS_", addr} + GENESIS := "GENESIS" + txHash := strings.Join(Genesis, addr) - sqlStatement := `INSERT INTO accounts(addr, balance, txCountAccount) VALUES(($1), ($2), ($3)) RETURNING addr` - insertErr := sqldb.QueryRow(sqlStatement, addr, v.Balance.String(), txCountAccount).Scan(&addr) - if insertErr != nil { - panic(insertErr) - } + sqlStatement := `INSERT INTO accounts(addr, balance, txCountAccount) VALUES(($1), ($2), ($3)) RETURNING addr` + insertErr := sqldb.QueryRow(sqlStatement, addr, v.Balance.String(), txCountAccount).Scan(&addr) + if insertErr != nil { + panic(insertErr) + } - var retNonce string - sqlGenTxStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount,gasPrice, gas, gasLimit,txFee,nonce,txstatus, iscontract,age, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12), ($13), ($14), ($15)) RETURNING nonce` - insertError := sqldb.QueryRow(sqlGenTxStatement, txHash, GENESIS, addr, block.Header().Hash().Hex(), number, v.Balance.String(), gasPrice, gasUsed, gasLimit, txFee,txCountAccount,txStatus, isContract, age, data).Scan(&retNonce) - if insertError != nil { - panic(insertError) + var retNonce string + sqlGenTxStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount,gasPrice, gas, gasLimit,txFee,nonce,txstatus, iscontract,age, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12), ($13), ($14), ($15)) RETURNING nonce` + insertError := sqldb.QueryRow(sqlGenTxStatement, txHash, GENESIS, addr, block.Header().Hash().Hex(), number, v.Balance.String(), gasPrice, gasUsed, gasLimit, txFee, txCountAccount, txStatus, isContract, age, data).Scan(&retNonce) + if insertError != nil { + panic(insertError) + } } + default: + log.Info("Found Genesis Block") } - default: - log.Info("Found Genesis Block") -}}} + } +} func WriteShyftBlockZero(block *types.Block, gen *Genesis) error { - + sqldb, _ := DBConnection() coinbase := block.Header().Coinbase.String() @@ -229,6 +232,7 @@ func WriteShyftBlockZero(block *types.Block, gen *Genesis) error { } return nil } + // SetupGenesisBlock writes or updates the genesis block in db. // The block that will be used is: // @@ -315,6 +319,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig { return params.AllEthashProtocolChanges } } + // ToBlock creates the genesis block and writes state of a genesis specification // to the given database (or discards it if nil). func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { @@ -439,6 +444,7 @@ func DefaultRinkebyGenesisBlock() *Genesis { Alloc: decodePrealloc(rinkebyAllocData), } } + // DeveloperGenesisBlock returns the 'geth --dev' genesis block. Note, this must // be seeded with the func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis { diff --git a/docker-compose.yml b/docker-compose.yml index bd0281fc6b..59297e58dd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,14 +21,15 @@ services: - shyftnet command: > sh -c '/go/src/ShyftNetwork/go-empyrean/wait-for.sh pg:5432 && - /initShyftGeth.sh && + DBENV=docker export DBENV && + /initShyftGeth.sh && /startShyftGeth.sh' pg: build: context: $PWD dockerfile: containers/docker/develop-alpine/pg/Dockerfile volumes: - - ./pg-data/data:/var/lib/postgresql/data + - ./pg-data:/var/lib/postgresql/ ports: - "8001:5432" networks: @@ -36,12 +37,19 @@ services: environment: - POSTGRES_USER=postgres - POSGRES_PASSWORD=docker - - PGDATA=/pg-data + # api: + # build: + # context: $PWD + # dockerfile: containers/docker/develop-alpine/shyftApi/Dockerfile + # volumes: - + + networks: shyftnet: driver: bridge - +# volumes: +# pg-data: # api: # build: diff --git a/shyft-cli/initShyftGeth.sh b/shyft-cli/initShyftGeth.sh index eccb4303f8..08277e2c0e 100755 --- a/shyft-cli/initShyftGeth.sh +++ b/shyft-cli/initShyftGeth.sh @@ -1,2 +1,6 @@ #!/bin/sh -/bin/geth --identity "ShyftTestnetNode" --keystore ./ --datadir "./shyftData" init ShyftNetwork.json \ No newline at end of file +if [[ -z "${DBENV}" ]]; then + ./build/bin/geth --identity "ShyftTestnetNode" --keystore ./ --datadir "./shyftData" init ShyftNetwork.json +else + /bin/geth --identity "ShyftTestnetNode" --keystore ./ --datadir "./shyftData" init ShyftNetwork.json +fi diff --git a/shyft-cli/startShyftGeth.sh b/shyft-cli/startShyftGeth.sh index 19a5376e76..5a782d97fb 100755 --- a/shyft-cli/startShyftGeth.sh +++ b/shyft-cli/startShyftGeth.sh @@ -1,2 +1,6 @@ #!/bin/sh -/bin/geth --config config.toml --ws --wsaddr="0.0.0.0" --wsorigins "*" --nat=none --mine --minerthreads 4 --targetgaslimit 80000000 --unlock "0x43EC6d0942f7fAeF069F7F63D0384a27f529B062,0x9e602164C5826ebb5A6B68E4AFD9Cd466043dc4A,0x5Bd738164C61FB50eb12E227846CbaeF2dE965Aa,0xC04eE4131895F1d0C294D508AF65D94060AA42BB,0x07D899C4aC0c1725C35C5f816e60273B33a964F7" --password ./unlockPasswords.txt +if [[ -z "${DBENV}" ]]; then + ./build/bin/geth --config config.toml --ws --wsaddr="0.0.0.0" --wsorigins "*" --nat=none --mine --minerthreads 4 --targetgaslimit 80000000 --unlock "0x43EC6d0942f7fAeF069F7F63D0384a27f529B062,0x9e602164C5826ebb5A6B68E4AFD9Cd466043dc4A,0x5Bd738164C61FB50eb12E227846CbaeF2dE965Aa,0xC04eE4131895F1d0C294D508AF65D94060AA42BB,0x07D899C4aC0c1725C35C5f816e60273B33a964F7" --password ./unlockPasswords.txt +else + /bin/geth --config config.toml --ws --wsaddr="0.0.0.0" --wsorigins "*" --nat=none --mine --minerthreads 4 --targetgaslimit 80000000 --unlock "0x43EC6d0942f7fAeF069F7F63D0384a27f529B062,0x9e602164C5826ebb5A6B68E4AFD9Cd466043dc4A,0x5Bd738164C61FB50eb12E227846CbaeF2dE965Aa,0xC04eE4131895F1d0C294D508AF65D94060AA42BB,0x07D899C4aC0c1725C35C5f816e60273B33a964F7" --password ./unlockPasswords.txt +fi diff --git a/wait-for.sh b/wait-for.sh index daca546968..7b05f7e3f3 100755 --- a/wait-for.sh +++ b/wait-for.sh @@ -1,5 +1,5 @@ #!/bin/sh - +# https://github.com/eficode/wait-for.git log() { echo "[wait-for] [`date +'%Y%m%d%H%M%S'`] $@" }