mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
getBlock written need to test
This commit is contained in:
parent
bb58ba3eec
commit
d826794404
3 changed files with 51 additions and 15 deletions
|
|
@ -2,10 +2,13 @@ package main
|
||||||
|
|
||||||
///@NOTE Shyft handler functions when endpoints are hit
|
///@NOTE Shyft handler functions when endpoints are hit
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
logger "log"
|
logger "log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
_ "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"
|
||||||
|
|
@ -57,12 +60,19 @@ func GetBalances(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
//GetBlock returns block json
|
//GetBlock returns block json
|
||||||
func GetBlock(w http.ResponseWriter, r *http.Request) {
|
func GetBlock(w http.ResponseWriter, r *http.Request) {
|
||||||
//block := shyftdb.GetBlock()
|
|
||||||
|
connStr := "user=postgres dbname=shyftdb sslmode=disable"
|
||||||
|
blockExplorerDb, err := sql.Open("postgres", connStr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
block3 := shyftdb.GetBlock(blockExplorerDb)
|
||||||
|
|
||||||
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", block)
|
fmt.Fprintln(w, "block", block3)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllBlocks response
|
// GetAllBlocks response
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,12 @@ var routes = Routes{
|
||||||
"/api/get_all_blocks",
|
"/api/get_all_blocks",
|
||||||
GetAllBlocks,
|
GetAllBlocks,
|
||||||
},
|
},
|
||||||
|
Route{
|
||||||
|
"GetBlock",
|
||||||
|
"GET",
|
||||||
|
"/api/get_block",
|
||||||
|
GetBlock,
|
||||||
|
},
|
||||||
Route{
|
Route{
|
||||||
"GetAllTransactions",
|
"GetAllTransactions",
|
||||||
"GET",
|
"GET",
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,13 @@ import (
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//SBlock type
|
||||||
type SBlock struct {
|
type SBlock struct {
|
||||||
hash string
|
hash string
|
||||||
txes []string
|
txes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ShyftTxEntry structure
|
||||||
type ShyftTxEntry struct {
|
type ShyftTxEntry struct {
|
||||||
TxHash common.Hash
|
TxHash common.Hash
|
||||||
To *common.Address
|
To *common.Address
|
||||||
|
|
@ -98,11 +100,16 @@ func WriteBlock(sqldb *sql.DB, block *types.Block) error {
|
||||||
|
|
||||||
//sqldb.Exec("INSERT INTO block(hash, miner) VALUES ($1)", block)
|
//sqldb.Exec("INSERT INTO block(hash, miner) VALUES ($1)", block)
|
||||||
//qerr := sqldb.QueryRow(`INSERT INTO block(hash, miner) VALUES('bark', 'willow')`).Scan(&fun)
|
//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)
|
sqlStatement := `INSERT INTO blocks(hash, coinbase, number) VALUES(($1), ($2), ($3)) RETURNING number`
|
||||||
fmt.Println("insert ERROR")
|
qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number).Scan(&number) //.Scan(&fun)
|
||||||
fmt.Println(qerr)
|
if qerr != nil {
|
||||||
fmt.Println(res)
|
panic(qerr)
|
||||||
fmt.Println(number)
|
}
|
||||||
|
fmt.Println("New record ID is:", number)
|
||||||
|
// fmt.Println("insert ERROR")
|
||||||
|
// fmt.Println(qerr)
|
||||||
|
// fmt.Println(res)
|
||||||
|
// fmt.Println(number)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -278,15 +285,28 @@ func GetAllBlocks(db *leveldb.DB) []SBlock {
|
||||||
return arr
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBlock(db *leveldb.DB, block *types.Block) []byte {
|
//GetBlock queries to send single block info
|
||||||
hash := block.Header().Hash().Bytes()
|
//TODO provide blockHash arg passed from handler.go
|
||||||
key := append([]byte("bk-")[:], hash[:]...)
|
func GetBlock(sqldb *sql.DB) []SBlock {
|
||||||
data, err := db.Get(key, nil)
|
var block []SBlock
|
||||||
if err != nil {
|
var number string
|
||||||
log.Crit("Could not retrieve block", "err", err)
|
var hash string
|
||||||
|
var coinbase string
|
||||||
|
|
||||||
|
sqlStatement := `SELECT * FROM blocks WHERE number=$1;`
|
||||||
|
row := sqldb.QueryRow(sqlStatement, 3)
|
||||||
|
err := row.Scan(&number, &hash, &coinbase)
|
||||||
|
switch err {
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
fmt.Println("No rows were returned")
|
||||||
|
return block
|
||||||
|
case nil:
|
||||||
|
fmt.Println("nil")
|
||||||
|
default:
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println("\nBLOCK Value: " + string(data))
|
fmt.Println("this is a query", row)
|
||||||
return data
|
return block
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllTransactions(db *leveldb.DB) []ShyftTxEntryPretty {
|
func GetAllTransactions(db *leveldb.DB) []ShyftTxEntryPretty {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue