diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 9f5b2bc033..6b2c76c0af 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -28,8 +28,6 @@ import ( "strconv" "strings" - "github.com/syndtr/goleveldb/leveldb" - "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" @@ -59,6 +57,9 @@ import ( "github.com/ethereum/go-ethereum/params" whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" "gopkg.in/urfave/cli.v1" + + // @shyft + "database/sql" ) 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 } 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 ******************************") chain, err = core.NewBlockChain(chainDb, blockExplorerDb,cache, config, engine, vmcfg) if err != nil { diff --git a/core/blockchain.go b/core/blockchain.go index 8c72431cd8..c5676eacd2 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -29,8 +29,6 @@ import ( "sync/atomic" "time" - "github.com/syndtr/goleveldb/leveldb" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/mclock" "github.com/ethereum/go-ethereum/consensus" @@ -48,6 +46,9 @@ import ( "github.com/ethereum/go-ethereum/trie" "github.com/hashicorp/golang-lru" "gopkg.in/karalabe/cookiejar.v2/collections/prque" + + // @shyft + "database/sql" ) var ( @@ -95,7 +96,7 @@ type BlockChain struct { cacheConfig *CacheConfig // Cache configuration for pruning 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 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 // available in the database. It initialises the default Ethereum Validator and // 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 { cacheConfig = &CacheConfig{ TrieNodeLimit: 256 * 1024 * 1024, diff --git a/core/chain_makers.go b/core/chain_makers.go index f831a0518a..7e05bdbe4e 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -20,8 +20,6 @@ import ( "fmt" "math/big" - "github.com/syndtr/goleveldb/leveldb" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc" @@ -30,6 +28,9 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/params" + + // @shyft + "database/sql" ) // 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) { // 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. - 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{}) defer blockchain.Stop() @@ -251,7 +253,8 @@ func newCanonical(engine consensus.Engine, n int, full bool) (ethdb.Database, *B gspec := new(Genesis) db, _ := ethdb.NewMemDatabase() 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{}) // Create and inject the requested chain if n == 0 { diff --git a/eth/backend.go b/eth/backend.go index 37b4549868..fd31e5aabd 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -24,7 +24,6 @@ import ( "runtime" "sync" "sync/atomic" - "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -48,6 +47,9 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" + + // @shyft + "database/sql" ) type LesServer interface { @@ -74,7 +76,7 @@ type Ethereum struct { // DB interfaces chainDb ethdb.Database // Block chain database - blockExplorerDb *leveldb.DB + blockExplorerDb *sql.DB eventMux *event.TypeMux engine consensus.Engine @@ -116,7 +118,9 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { // @NOTE:shyft instantiate BlockExplorerDB here // @TODO: Create Genesis Block - blockExplorerDb, err := leveldb.OpenFile("./shyftData/geth/blockExplorerDb/", nil) + // @NOTE:shyft instantiate BlockExplorerDB here? + connStr := "user=postgres dbname=shyftdb sslmode=disable" + blockExplorerDb, err := sql.Open("postgres", connStr) if err != nil { return nil, err } diff --git a/shyftDb/postgres_setup/create_shyftdb.psql b/shyftDb/postgres_setup/create_shyftdb.psql new file mode 100644 index 0000000000..2c029ee772 --- /dev/null +++ b/shyftDb/postgres_setup/create_shyftdb.psql @@ -0,0 +1 @@ +CREATE DATABASE shyftdb diff --git a/shyftDb/postgres_setup/create_tables.psql b/shyftDb/postgres_setup/create_tables.psql new file mode 100644 index 0000000000..cfe220d080 --- /dev/null +++ b/shyftDb/postgres_setup/create_tables.psql @@ -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) +); diff --git a/shyftDb/postgres_setup/init_tables.sh b/shyftDb/postgres_setup/init_tables.sh new file mode 100644 index 0000000000..988ad56700 --- /dev/null +++ b/shyftDb/postgres_setup/init_tables.sh @@ -0,0 +1 @@ +psql -U postgres -d shyftdb -f create_tables.psql diff --git a/shyftDb/postgres_setup/initdb.sh b/shyftDb/postgres_setup/initdb.sh new file mode 100644 index 0000000000..1fb7d829c5 --- /dev/null +++ b/shyftDb/postgres_setup/initdb.sh @@ -0,0 +1 @@ +psql -U postgres -f create_shyftdb.psql diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 4ed1b89a23..5179d77fa6 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -11,6 +11,9 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" + + "database/sql" + _ "github.com/lib/pq" ) @@ -48,29 +51,59 @@ type ShyftAccountEntry struct { Txs []string } -func WriteBlock(db *leveldb.DB, block *types.Block) error { - fmt.Println("+++++++++++++++++++++++++++ BLOCK NUMBER", block.Number()) - fmt.Println("+++++++++++++++++++++++++++ # of TX", len(block.Transactions())) - leng := block.Transactions().Len() - var tx_strs = make([]string, leng) - hash := block.Header().Hash().Bytes() +//func WriteBlock(db *leveldb.DB, block *types.Block) error { +// fmt.Println("+++++++++++++++++++++++++++ BLOCK NUMBER", block.Number()) +// fmt.Println("+++++++++++++++++++++++++++ # of TX", len(block.Transactions())) +// leng := block.Transactions().Len() +// var tx_strs = make([]string, leng) +// hash := block.Header().Hash().Bytes() +// +// buf := &bytes.Buffer{} +// gob.NewEncoder(buf).Encode(tx_strs) +// bs := buf.Bytes() +// +// 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? +// } +// WriteMinerReward(db, block) +// +// if block.Transactions().Len() > 0 { +// for i, tx := range block.Transactions() { +// tx_strs[i] = WriteTransactions(db, tx, block.Header().Hash()) +// } +// } - buf := &bytes.Buffer{} - gob.NewEncoder(buf).Encode(tx_strs) - bs := buf.Bytes() +func WriteBlock(sqldb *sql.DB, block *types.Block) error { - 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? - } - WriteMinerReward(db, block) + //hash := block.Header().Hash().Bytes() + coinbase := block.Header().Coinbase.String() + number := block.Header().Number.String() + +// 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() +// } +// } + + //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) - if block.Transactions().Len() > 0 { - for i, tx := range block.Transactions() { - tx_strs[i] = WriteTransactions(db, tx, block.Header().Hash()) - } - } return nil }