mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
create initializing scripts for postgres tables and modify write block so it now writes to postgres
This commit is contained in:
parent
7b3802a1b2
commit
1363c69454
9 changed files with 79 additions and 37 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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,8 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
}
|
||||
|
||||
// @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 {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
1
shyftDb/postgres_setup/create_shyftdb.psql
Normal file
1
shyftDb/postgres_setup/create_shyftdb.psql
Normal file
|
|
@ -0,0 +1 @@
|
|||
CREATE DATABASE shyftdb
|
||||
16
shyftDb/postgres_setup/create_tables.psql
Normal file
16
shyftDb/postgres_setup/create_tables.psql
Normal 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)
|
||||
);
|
||||
1
shyftDb/postgres_setup/init_tables.sh
Normal file
1
shyftDb/postgres_setup/init_tables.sh
Normal file
|
|
@ -0,0 +1 @@
|
|||
psql -U postgres -d shyftdb -f create_tables.psql
|
||||
1
shyftDb/postgres_setup/initdb.sh
Normal file
1
shyftDb/postgres_setup/initdb.sh
Normal file
|
|
@ -0,0 +1 @@
|
|||
psql -U postgres -f create_shyftdb.psql
|
||||
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -43,31 +46,36 @@ type ShyftTxEntryPretty struct {
|
|||
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()
|
||||
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()
|
||||
}
|
||||
}
|
||||
func WriteBlock(sqldb *sql.DB, block *types.Block) error {
|
||||
|
||||
fmt.Println("The tx_strs is")
|
||||
fmt.Println(tx_strs)
|
||||
//strs := []string{"foo", "bar"}
|
||||
buf := &bytes.Buffer{}
|
||||
gob.NewEncoder(buf).Encode(tx_strs)
|
||||
bs := buf.Bytes()
|
||||
//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)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue