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
|
///@NOTE Shyft handler functions when endpoints are hit
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
logger "log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
|
|
||||||
shyftdb "github.com/ethereum/shyft_go-ethereum/shyftdb"
|
shyftdb "github.com/ethereum/shyft_go-ethereum/shyftdb"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
|
||||||
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetTransaction gets txs
|
// GetTransaction gets txs
|
||||||
|
|
@ -69,28 +67,49 @@ func GetBlock(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
block3 := shyftdb.GetBlock(blockExplorerDb)
|
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.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
fmt.Fprintln(w, "block", block3)
|
fmt.Fprintln(w, "block", string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllBlocks response
|
// GetAllBlocks response
|
||||||
func GetAllBlocks(w http.ResponseWriter, r *http.Request) {
|
func GetAllBlocks(w http.ResponseWriter, r *http.Request) {
|
||||||
blockExplorerDb, err := leveldb.OpenFile("./shyftData/geth/blockExplorerDb/", &opt.Options{
|
|
||||||
ErrorIfMissing: true,
|
connStr := "user=postgres dbname=shyftdb sslmode=disable"
|
||||||
ReadOnly: true,
|
blockExplorerDb, err := sql.Open("postgres", connStr)
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Print(err)
|
|
||||||
return
|
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.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
fmt.Fprintln(w, "blocks", blocks)
|
fmt.Fprintln(w, "block", string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetInternalTransactions gets internal txs
|
//GetInternalTransactions gets internal txs
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,13 @@ import (
|
||||||
//SBlock type
|
//SBlock type
|
||||||
type SBlock struct {
|
type SBlock struct {
|
||||||
hash string
|
hash string
|
||||||
txes []string
|
coinbase string
|
||||||
|
number int
|
||||||
|
}
|
||||||
|
|
||||||
|
//blockRes struct
|
||||||
|
type blockRes struct {
|
||||||
|
Blocks []SBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
//ShyftTxEntry structure
|
//ShyftTxEntry structure
|
||||||
|
|
@ -262,26 +268,47 @@ func WriteMinerReward(db *leveldb.DB, block *types.Block) {
|
||||||
///////////
|
///////////
|
||||||
// Getters
|
// Getters
|
||||||
//////////
|
//////////
|
||||||
|
//GetAllBlocks returns []SBlock blocks for API
|
||||||
func GetAllBlocks(db *leveldb.DB) []SBlock {
|
func GetAllBlocks(sqldb *sql.DB) []SBlock {
|
||||||
var arr []SBlock
|
var arr []SBlock
|
||||||
iter := db.NewIterator(util.BytesPrefix([]byte("bk-")), nil)
|
fmt.Println("HERE+++++++++++")
|
||||||
for iter.Next() {
|
rows, err := sqldb.Query(`
|
||||||
result := iter.Value()
|
SELECT
|
||||||
buf := bytes.NewBuffer(result)
|
number,
|
||||||
strs2 := []string{}
|
hash,
|
||||||
gob.NewDecoder(buf).Decode(&strs2)
|
coinbase
|
||||||
//fmt.Println("the key is")
|
FROM blocks`)
|
||||||
hash := common.BytesToHash(iter.Key())
|
if err != nil {
|
||||||
hex := hash.Hex()
|
fmt.Println("err")
|
||||||
//fmt.Println(hex)
|
|
||||||
sblock := SBlock{hex, strs2}
|
}
|
||||||
arr = append(arr, sblock)
|
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
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue