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 ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"os"
) )
var blockExplorerDb *sql.DB 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 ( 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" 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) { 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) db, err := sql.Open("postgres", connStr)
if err != nil { if err != nil {
fmt.Println("ERROR OPENING DB, NOT INITIALIZING") 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) { func InitDBTest() (*sql.DB, error) {
db, err := sql.Open("postgres", connStrTest) db, err := sql.Open("postgres", connStrTest)
if err != nil { if err != nil {

View file

@ -25,7 +25,10 @@ import (
"math/big" "math/big"
"strings" "strings"
_ "github.com/lib/pq" "database/sql"
"strconv"
"time"
"github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common"
"github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/common/hexutil"
"github.com/ShyftNetwork/go-empyrean/common/math" "github.com/ShyftNetwork/go-empyrean/common/math"
@ -35,9 +38,7 @@ import (
"github.com/ShyftNetwork/go-empyrean/log" "github.com/ShyftNetwork/go-empyrean/log"
"github.com/ShyftNetwork/go-empyrean/params" "github.com/ShyftNetwork/go-empyrean/params"
"github.com/ShyftNetwork/go-empyrean/rlp" "github.com/ShyftNetwork/go-empyrean/rlp"
"database/sql" _ "github.com/lib/pq"
"strconv"
"time"
) )
//go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go //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 txFee := 0
txStatus := "" txStatus := ""
isContract := false isContract := false
data:= "" data := ""
addr := k.String() addr := k.String()
txCountAccount := v.Nonce +1 txCountAccount := v.Nonce + 1
i, err := strconv.ParseInt(block.Time().String(), 10, 64) i, err := strconv.ParseInt(block.Time().String(), 10, 64)
if err != nil { if err != nil {
panic(err) panic(err)
@ -181,14 +182,16 @@ func WriteShyftGen(gen *Genesis, block *types.Block) {
var retNonce string 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` 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 { if insertError != nil {
panic(insertError) panic(insertError)
} }
} }
default: default:
log.Info("Found Genesis Block") log.Info("Found Genesis Block")
}}} }
}
}
func WriteShyftBlockZero(block *types.Block, gen *Genesis) error { func WriteShyftBlockZero(block *types.Block, gen *Genesis) error {
@ -229,6 +232,7 @@ func WriteShyftBlockZero(block *types.Block, gen *Genesis) error {
} }
return nil return nil
} }
// SetupGenesisBlock writes or updates the genesis block in db. // SetupGenesisBlock writes or updates the genesis block in db.
// The block that will be used is: // The block that will be used is:
// //
@ -315,6 +319,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
return params.AllEthashProtocolChanges return params.AllEthashProtocolChanges
} }
} }
// ToBlock creates the genesis block and writes state of a genesis specification // ToBlock creates the genesis block and writes state of a genesis specification
// to the given database (or discards it if nil). // to the given database (or discards it if nil).
func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
@ -439,6 +444,7 @@ func DefaultRinkebyGenesisBlock() *Genesis {
Alloc: decodePrealloc(rinkebyAllocData), Alloc: decodePrealloc(rinkebyAllocData),
} }
} }
// DeveloperGenesisBlock returns the 'geth --dev' genesis block. Note, this must // DeveloperGenesisBlock returns the 'geth --dev' genesis block. Note, this must
// be seeded with the // be seeded with the
func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis { func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {

View file

@ -21,6 +21,7 @@ services:
- shyftnet - shyftnet
command: > command: >
sh -c '/go/src/ShyftNetwork/go-empyrean/wait-for.sh pg:5432 && sh -c '/go/src/ShyftNetwork/go-empyrean/wait-for.sh pg:5432 &&
DBENV=docker export DBENV &&
/initShyftGeth.sh && /initShyftGeth.sh &&
/startShyftGeth.sh' /startShyftGeth.sh'
pg: pg:
@ -28,7 +29,7 @@ services:
context: $PWD context: $PWD
dockerfile: containers/docker/develop-alpine/pg/Dockerfile dockerfile: containers/docker/develop-alpine/pg/Dockerfile
volumes: volumes:
- ./pg-data/data:/var/lib/postgresql/data - ./pg-data:/var/lib/postgresql/
ports: ports:
- "8001:5432" - "8001:5432"
networks: networks:
@ -36,12 +37,19 @@ services:
environment: environment:
- POSTGRES_USER=postgres - POSTGRES_USER=postgres
- POSGRES_PASSWORD=docker - POSGRES_PASSWORD=docker
- PGDATA=/pg-data # api:
# build:
# context: $PWD
# dockerfile: containers/docker/develop-alpine/shyftApi/Dockerfile
# volumes: -
networks: networks:
shyftnet: shyftnet:
driver: bridge driver: bridge
# volumes:
# pg-data:
# api: # api:
# build: # build:

View file

@ -1,2 +1,6 @@
#!/bin/sh #!/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/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 #!/bin/sh
# https://github.com/eficode/wait-for.git
log() { log() {
echo "[wait-for] [`date +'%Y%m%d%H%M%S'`] $@" echo "[wait-for] [`date +'%Y%m%d%H%M%S'`] $@"
} }