Merge pull request #10 from ShyftNetwork/switch-to-sql

create initializing scripts for postgres tables and modify write bloc…
This commit is contained in:
greg 2018-04-17 21:42:59 -04:00 committed by GitHub
commit e1bf6c7135
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 79 additions and 37 deletions

View file

@ -28,8 +28,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -59,6 +57,9 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
// @shyft
"database/sql"
) )
var ( var (
@ -1250,7 +1251,13 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
cache.TrieNodeLimit = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100 cache.TrieNodeLimit = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
} }
vmcfg := vm.Config{EnablePreimageRecording: ctx.GlobalBool(VMEnableDebugFlag.Name)} vmcfg := vm.Config{EnablePreimageRecording: ctx.GlobalBool(VMEnableDebugFlag.Name)}
blockExplorerDb, _ := leveldb.OpenFile("./foo_data/", nil)
// @NOTE:shyft instantiate BlockExplorerDB here?
connStr := "user=postgres dbname=shyftdb sslmode=disable"
blockExplorerDb, err := sql.Open("postgres", connStr)
if err != nil {
return nil, nil
}
fmt.Println("Calling NewBlock CHAIN in flags.go ******************************") fmt.Println("Calling NewBlock CHAIN in flags.go ******************************")
chain, err = core.NewBlockChain(chainDb, blockExplorerDb,cache, config, engine, vmcfg) chain, err = core.NewBlockChain(chainDb, blockExplorerDb,cache, config, engine, vmcfg)
if err != nil { if err != nil {

View file

@ -29,8 +29,6 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/mclock" "github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus"
@ -48,6 +46,9 @@ import (
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/hashicorp/golang-lru" "github.com/hashicorp/golang-lru"
"gopkg.in/karalabe/cookiejar.v2/collections/prque" "gopkg.in/karalabe/cookiejar.v2/collections/prque"
// @shyft
"database/sql"
) )
var ( var (
@ -95,7 +96,7 @@ type BlockChain struct {
cacheConfig *CacheConfig // Cache configuration for pruning cacheConfig *CacheConfig // Cache configuration for pruning
db ethdb.Database // Low level persistent database to store final content in db ethdb.Database // Low level persistent database to store final content in
blockExplorerDb *leveldb.DB blockExplorerDb *sql.DB
triegc *prque.Prque // Priority queue mapping block numbers to tries to gc triegc *prque.Prque // Priority queue mapping block numbers to tries to gc
gcproc time.Duration // Accumulates canonical block processing for trie dumping gcproc time.Duration // Accumulates canonical block processing for trie dumping
@ -140,7 +141,8 @@ type BlockChain struct {
// NewBlockChain returns a fully initialised block chain using information // NewBlockChain returns a fully initialised block chain using information
// available in the database. It initialises the default Ethereum Validator and // available in the database. It initialises the default Ethereum Validator and
// Processor. // Processor.
func NewBlockChain(db ethdb.Database, blockExplorerDb *leveldb.DB, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) { func NewBlockChain(db ethdb.Database, blockExplorerDb *sql.DB, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) {
fmt.Printf("+++++++++++++++++core/blockchain.GO+++++++++++++++++++++++++NewBlockChain()")
if cacheConfig == nil { if cacheConfig == nil {
cacheConfig = &CacheConfig{ cacheConfig = &CacheConfig{
TrieNodeLimit: 256 * 1024 * 1024, TrieNodeLimit: 256 * 1024 * 1024,

View file

@ -20,8 +20,6 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/consensus/misc"
@ -30,6 +28,9 @@ import (
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
// @shyft
"database/sql"
) )
// So we can deterministically seed different blockchains // So we can deterministically seed different blockchains
@ -168,7 +169,8 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
genblock := func(i int, parent *types.Block, statedb *state.StateDB) (*types.Block, types.Receipts) { genblock := func(i int, parent *types.Block, statedb *state.StateDB) (*types.Block, types.Receipts) {
// TODO(karalabe): This is needed for clique, which depends on multiple blocks. // TODO(karalabe): This is needed for clique, which depends on multiple blocks.
// It's nonetheless ugly to spin up a blockchain here. Get rid of this somehow. // It's nonetheless ugly to spin up a blockchain here. Get rid of this somehow.
blockExplorerDb, _ := leveldb.OpenFile("./foo_data/", nil) connStr := "user=postgres dbname=shyftdb sslmode=disable"
blockExplorerDb, _ := sql.Open("postgres", connStr)
blockchain, _ := NewBlockChain(db, blockExplorerDb,nil, config, engine, vm.Config{}) blockchain, _ := NewBlockChain(db, blockExplorerDb,nil, config, engine, vm.Config{})
defer blockchain.Stop() defer blockchain.Stop()
@ -251,7 +253,8 @@ func newCanonical(engine consensus.Engine, n int, full bool) (ethdb.Database, *B
gspec := new(Genesis) gspec := new(Genesis)
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
genesis := gspec.MustCommit(db) genesis := gspec.MustCommit(db)
blockExplorerDb, err := leveldb.OpenFile("./foo_data/", nil) connStr := "user=postgres dbname=shyftdb sslmode=disable"
blockExplorerDb, _ := sql.Open("postgres", connStr)
blockchain, _ := NewBlockChain(db, blockExplorerDb, nil, params.AllEthashProtocolChanges, engine, vm.Config{}) blockchain, _ := NewBlockChain(db, blockExplorerDb, nil, params.AllEthashProtocolChanges, engine, vm.Config{})
// Create and inject the requested chain // Create and inject the requested chain
if n == 0 { if n == 0 {

View file

@ -24,7 +24,6 @@ import (
"runtime" "runtime"
"sync" "sync"
"sync/atomic" "sync/atomic"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -48,6 +47,9 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
// @shyft
"database/sql"
) )
type LesServer interface { type LesServer interface {
@ -74,7 +76,7 @@ type Ethereum struct {
// DB interfaces // DB interfaces
chainDb ethdb.Database // Block chain database chainDb ethdb.Database // Block chain database
blockExplorerDb *leveldb.DB blockExplorerDb *sql.DB
eventMux *event.TypeMux eventMux *event.TypeMux
engine consensus.Engine engine consensus.Engine
@ -116,7 +118,8 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
} }
// @NOTE:shyft instantiate BlockExplorerDB here? // @NOTE:shyft instantiate BlockExplorerDB here?
blockExplorerDb, err := leveldb.OpenFile("./shyftData/geth/blockExplorerDb/", nil) connStr := "user=postgres dbname=shyftdb sslmode=disable"
blockExplorerDb, err := sql.Open("postgres", connStr)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -0,0 +1 @@
CREATE DATABASE shyftdb

View file

@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS blocks (
hash text primary key,
coinbase text,
number bigint
);
CREATE TABLE IF NOT EXISTS txs (
from_addr text,
blockhash text,
amount bigint,
gasprice bigint,
gas numeric,
nonce numeric,
data bytea,
block text references blocks(hash)
);

View file

@ -0,0 +1 @@
psql -U postgres -d shyftdb -f create_tables.psql

View file

@ -0,0 +1 @@
psql -U postgres -f create_shyftdb.psql

View file

@ -11,6 +11,9 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"database/sql"
_ "github.com/lib/pq"
) )
@ -43,31 +46,36 @@ type ShyftTxEntryPretty struct {
Data []byte Data []byte
} }
func WriteBlock(db *leveldb.DB, block *types.Block) error {
leng := block.Transactions().Len()
var tx_strs = make([]string, leng)
//var tx_bytes = make([]byte, leng)
hash := block.Header().Hash().Bytes() func WriteBlock(sqldb *sql.DB, block *types.Block) error {
if block.Transactions().Len() > 0 {
for i, tx := range block.Transactions() {
tx_strs[i] = WriteTransactions(db, tx, block.Header().Hash())
//tx_bytes[i] = tx.Hash().Bytes()
}
}
fmt.Println("The tx_strs is") //hash := block.Header().Hash().Bytes()
fmt.Println(tx_strs) coinbase := block.Header().Coinbase.String()
//strs := []string{"foo", "bar"} number := block.Header().Number.String()
buf := &bytes.Buffer{}
gob.NewEncoder(buf).Encode(tx_strs) // if block.Transactions().Len() > 0 {
bs := buf.Bytes() // for i, tx := range block.Transactions() {
// tx_strs[i] = WriteTransactions(db, tx, block.Header().Hash())
// //tx_bytes[i] = tx.Hash().Bytes()
// }
// }
//connStr := "user=postgres dbname=shyftdb sslmode=disable"
//sqldb, err := sql.Open("postgres", connStr)
//if merr := sqldb.Ping(); merr != nil {
// fmt.Println("ping ERROR")
// fmt.Println(merr)
//}
//sqldb.Exec("INSERT INTO block(hash, miner) VALUES ($1)", block)
//qerr := sqldb.QueryRow(`INSERT INTO block(hash, miner) VALUES('bark', 'willow')`).Scan(&fun)
res, qerr := sqldb.Exec(`INSERT INTO blocks(hash, coinbase, number) VALUES(($1), ($2), ($3))`, block.Header().Hash().Hex(), coinbase, number) //.Scan(&fun)
fmt.Println("insert ERROR")
fmt.Println(qerr)
fmt.Println(res)
fmt.Println(number)
key := append([]byte("bk-")[:], hash[:]...)
if err := db.Put(key, bs, nil); err != nil {
log.Crit("Failed to store block", "err", err)
return nil // Do we want to force an exit here?
}
return nil return nil
} }