mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
getAllBlocks logging arrays of SBlock
This commit is contained in:
parent
d826794404
commit
18bc415b0d
2 changed files with 75 additions and 29 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue