From 18bc415b0d9ca7ee6a619b3234d3dc6c1f3c0ca6 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Fri, 20 Apr 2018 16:24:51 -0400 Subject: [PATCH] getAllBlocks logging arrays of SBlock --- blockExplorerApi/handler.go | 41 ++++++++++++++++------ shyftdb/shyft_database_util.go | 63 ++++++++++++++++++++++++---------- 2 files changed, 75 insertions(+), 29 deletions(-) diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go index 33cc098a42..ddc2a5abf4 100644 --- a/blockExplorerApi/handler.go +++ b/blockExplorerApi/handler.go @@ -3,16 +3,14 @@ package main ///@NOTE Shyft handler functions when endpoints are hit import ( "database/sql" + "encoding/json" "fmt" - logger "log" "net/http" _ "github.com/lib/pq" shyftdb "github.com/ethereum/shyft_go-ethereum/shyftdb" "github.com/gorilla/mux" - "github.com/syndtr/goleveldb/leveldb" - "github.com/syndtr/goleveldb/leveldb/opt" ) // GetTransaction gets txs @@ -69,28 +67,49 @@ func GetBlock(w http.ResponseWriter, r *http.Request) { block3 := shyftdb.GetBlock(blockExplorerDb) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + out, err := json.Marshal(block3) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) - fmt.Fprintln(w, "block", block3) + fmt.Fprintln(w, "block", string(out)) } // GetAllBlocks response func GetAllBlocks(w http.ResponseWriter, r *http.Request) { - blockExplorerDb, err := leveldb.OpenFile("./shyftData/geth/blockExplorerDb/", &opt.Options{ - ErrorIfMissing: true, - ReadOnly: true, - }) + + connStr := "user=postgres dbname=shyftdb sslmode=disable" + blockExplorerDb, err := sql.Open("postgres", connStr) if err != nil { - logger.Print(err) return } - blocks := shyftdb.GetAllBlocks(blockExplorerDb) + + block3 := shyftdb.GetAllBlocks(blockExplorerDb) + + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + out, err := json.Marshal(block3) + if err != nil { + http.Error(w, err.Error(), 500) + return + } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) - fmt.Fprintln(w, "blocks", blocks) + fmt.Fprintln(w, "block", string(out)) } //GetInternalTransactions gets internal txs diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 7b3be296b3..3c3a405548 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -19,8 +19,14 @@ import ( //SBlock type type SBlock struct { - hash string - txes []string + hash string + coinbase string + number int +} + +//blockRes struct +type blockRes struct { + Blocks []SBlock } //ShyftTxEntry structure @@ -262,26 +268,47 @@ func WriteMinerReward(db *leveldb.DB, block *types.Block) { /////////// // Getters ////////// - -func GetAllBlocks(db *leveldb.DB) []SBlock { +//GetAllBlocks returns []SBlock blocks for API +func GetAllBlocks(sqldb *sql.DB) []SBlock { var arr []SBlock - iter := db.NewIterator(util.BytesPrefix([]byte("bk-")), nil) - for iter.Next() { - result := iter.Value() - buf := bytes.NewBuffer(result) - strs2 := []string{} - gob.NewDecoder(buf).Decode(&strs2) - //fmt.Println("the key is") - hash := common.BytesToHash(iter.Key()) - hex := hash.Hex() - //fmt.Println(hex) - sblock := SBlock{hex, strs2} - arr = append(arr, sblock) + fmt.Println("HERE+++++++++++") + rows, err := sqldb.Query(` + SELECT + number, + hash, + coinbase + FROM blocks`) + if err != nil { + fmt.Println("err") + + } + fmt.Println("ROWS") + fmt.Println(rows) + defer rows.Close() + + for rows.Next() { + var num int + var hash string + var coinbase string + err = rows.Scan( + &num, + &hash, + &coinbase, + ) + sblock := SBlock{hash: hash, number: num, coinbase: coinbase} + arr := append(arr, sblock) + fmt.Println("++++++++++++++++", arr) + // fmt.Println("++++++++++++++++", hash) + // fmt.Println("++++++++++++++++", coinbase) + + // } + // err = rows.Err() + // if err != nil { + // return err + // } - //fmt.Println("\n ALL BK BK VALUE" + string(result)) } - iter.Release() return arr }