Interim - Adjust Scripts & PG Connection Params to facilitate Docker DB option

This commit is contained in:
David Krett 2018-07-26 18:47:50 -04:00
parent 905021b5e2
commit 122c62ca81
6 changed files with 86 additions and 49 deletions

View file

@ -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 {

View file

@ -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
@ -161,9 +162,9 @@ func WriteShyftGen(gen *Genesis, block *types.Block) {
txFee := 0
txStatus := ""
isContract := false
data:= ""
data := ""
addr := k.String()
txCountAccount := v.Nonce +1
txCountAccount := v.Nonce + 1
i, err := strconv.ParseInt(block.Time().String(), 10, 64)
if err != nil {
panic(err)
@ -181,14 +182,16 @@ func WriteShyftGen(gen *Genesis, block *types.Block) {
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)
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")
}}}
}
}
}
func WriteShyftBlockZero(block *types.Block, gen *Genesis) error {
@ -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 {

View file

@ -21,6 +21,7 @@ services:
- shyftnet
command: >
sh -c '/go/src/ShyftNetwork/go-empyrean/wait-for.sh pg:5432 &&
DBENV=docker export DBENV &&
/initShyftGeth.sh &&
/startShyftGeth.sh'
pg:
@ -28,7 +29,7 @@ services:
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:

View file

@ -1,2 +1,6 @@
#!/bin/sh
/bin/geth --identity "ShyftTestnetNode" --keystore ./ --datadir "./shyftData" init ShyftNetwork.json
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

View file

@ -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

View file

@ -1,5 +1,5 @@
#!/bin/sh
# https://github.com/eficode/wait-for.git
log() {
echo "[wait-for] [`date +'%Y%m%d%H%M%S'`] $@"
}