Merge pull request #13 from ShyftNetwork/shyftBlockExp

Shyft block exp
This commit is contained in:
Tim Williams 2018-04-23 12:33:18 -04:00 committed by GitHub
commit b4ecde074c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 137 additions and 109 deletions

View file

@ -14,24 +14,44 @@ import (
// GetTransaction gets txs // GetTransaction gets txs
func GetTransaction(w http.ResponseWriter, r *http.Request) { func GetTransaction(w http.ResponseWriter, r *http.Request) {
// vars := mux.Vars(r) connStr := "user=postgres dbname=shyftdb sslmode=disable"
// address := vars["address"] blockExplorerDb, err := sql.Open("postgres", connStr)
if err != nil {
return
}
getTxResponse := shyftdb.GetTransaction(blockExplorerDb)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
//tx := shyftdb.GetTransaction()
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, "Get All Transactions", addresses) fmt.Fprintln(w, getTxResponse)
} }
// GetAllTransactions gets txs // GetAllTransactions gets txs
func GetAllTransactions(w http.ResponseWriter, r *http.Request) { func GetAllTransactions(w http.ResponseWriter, r *http.Request) {
//txs := shyftdb.GetAllTransactions() connStr := "user=postgres dbname=shyftdb sslmode=disable"
blockExplorerDb, err := sql.Open("postgres", connStr)
if err != nil {
return
}
txs := shyftdb.GetAllTransactions(blockExplorerDb)
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, "Get All Transactions", address) fmt.Fprintln(w, txs)
} }
// GetBalance gets balance // GetBalance gets balance
@ -57,7 +77,6 @@ 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) {
connStr := "user=postgres dbname=shyftdb sslmode=disable" connStr := "user=postgres dbname=shyftdb sslmode=disable"
blockExplorerDb, err := sql.Open("postgres", connStr) blockExplorerDb, err := sql.Open("postgres", connStr)
if err != nil { if err != nil {

View file

@ -48,7 +48,7 @@ var routes = Routes{
Route{ Route{
"GetTransaction", "GetTransaction",
"GET", "GET",
"/api/get_transaction/{address}", "/api/get_transaction",
GetTransaction, GetTransaction,
}, },
Route{ Route{

View file

@ -9,8 +9,8 @@ CREATE TABLE IF NOT EXISTS txs (
to_addr text, to_addr text,
from_addr text, from_addr text,
blockhash text, blockhash text,
amount bigint, amount numeric,
gasprice bigint, gasprice numeric,
gas numeric, gas numeric,
nonce numeric, nonce numeric,
data bytea, data bytea,

View file

@ -10,7 +10,6 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
"database/sql" "database/sql"
@ -32,6 +31,19 @@ type blockRes struct {
Blocks []SBlock Blocks []SBlock
} }
type txRes struct {
txHash string
to_addr string
from_addr string
blockHash string
amount string
gasPrice string
gas uint64
nonce uint64
data []byte
TxEntry []ShyftTxEntryPretty
}
//ShyftTxEntry structure //ShyftTxEntry structure
type ShyftTxEntry struct { type ShyftTxEntry struct {
TxHash common.Hash TxHash common.Hash
@ -50,8 +62,8 @@ type ShyftTxEntryPretty struct {
To string To string
From string From string
BlockHash string BlockHash string
Amount *big.Int Amount string
GasPrice *big.Int GasPrice string
Gas uint64 Gas uint64
Nonce uint64 Nonce uint64
Data []byte Data []byte
@ -95,42 +107,25 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha
Nonce: tx.Nonce(), Nonce: tx.Nonce(),
Data: tx.Data(), Data: tx.Data(),
} }
// var encodedData bytes.Buffer
// encoder := gob.NewEncoder(&encodedData) txHash := txData.TxHash.Hex()
// if err := encoder.Encode(txData); err != nil { from := txData.From.Hex()
// log.Crit("Faild to encode TX data", "err", err) to := txData.To.Hex()
// }
txHash := txData.TxHash
from := txData.From
to := txData.To
blockHasher := txData.BlockHash blockHasher := txData.BlockHash
amount := txData.Amount amount := txData.Amount.String()
gasPrice := txData.GasPrice gasPrice := txData.GasPrice.String()
nonce := txData.Nonce nonce := txData.Nonce
gas := txData.Gas gas := txData.Gas
data := txData.Data data := txData.Data
fmt.Println("+++++++++txHash", txHash)
fmt.Println("+++++++++from", from)
fmt.Println("+++++++++to", to)
fmt.Println("+++++++++BLOCKHASHER", blockHasher)
fmt.Println("+++++++++amount", amount)
fmt.Println("+++++++++gas Price", gasPrice)
fmt.Println("+++++++++nonce", nonce)
fmt.Println("+++++++++Gas", gas)
fmt.Println("+++++++++Data", data)
sqlStatement := `INSERT INTO txs(blockHasher, amount, gasPrice, none, gas, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6) RETURNING nonce` sqlStatement := `INSERT INTO txs(txhash, to_addr, from_addr, blockhash, amount, gasprice, gas, nonce, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9)) RETURNING nonce`
qerr := sqldb.QueryRow(sqlStatement, txHash, from, to, blockHasher, amount, gasPrice, nonce, gas, data).Scan(&nonce) //.Scan(&fun) qerr := sqldb.QueryRow(sqlStatement, txHash, to, from, blockHasher, amount, gasPrice, gas, nonce, data).Scan(&nonce)
if qerr != nil { if qerr != nil {
panic(qerr) panic(qerr)
} }
return nil return nil
} }
// key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...)
// if err := db.Put(key, encodedData.Bytes(), nil); err != nil {
// log.Crit("Failed to store TX", "err", err)
// }
//WriteAccountBalances(db, tx) //WriteAccountBalances(db, tx)
// func WriteFromBalance(db *leveldb.DB, tx *types.Transaction) { // func WriteFromBalance(db *leveldb.DB, tx *types.Transaction) {
@ -315,78 +310,92 @@ func GetBlock(sqldb *sql.DB) []SBlock {
return arr.Blocks return arr.Blocks
} }
func GetAllTransactions(db *leveldb.DB) []ShyftTxEntryPretty { //GetAllTransactions getter fn for API
var txs []ShyftTxEntryPretty func GetAllTransactions(sqldb *sql.DB) []ShyftTxEntryPretty {
iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) var arr txRes
for iter.Next() { rows, err := sqldb.Query(`
var txData ShyftTxEntry SELECT
d := gob.NewDecoder(bytes.NewBuffer(iter.Value())) txhash,
if err := d.Decode(&txData); err != nil { to_addr,
log.Crit("Failed to decode tx:", "err", err) from_addr,
blockhash,
amount,
gasprice,
gas,
nonce
FROM txs`)
if err != nil {
fmt.Println("err")
} }
prettyFormat := ShyftTxEntryPretty{ defer rows.Close()
TxHash: txData.TxHash.Hex(),
From: txData.From.Hex(), for rows.Next() {
To: txData.To.Hex(), var txhash string
BlockHash: txData.BlockHash, var to_addr string
Amount: txData.Amount, var from_addr string
Gas: txData.Gas, var blockhash string
GasPrice: txData.GasPrice, var amount string
Nonce: txData.Nonce, var gasprice string
Data: txData.Data, var gas uint64
var nonce uint64
err = rows.Scan(
&txhash,
&to_addr,
&from_addr,
&blockhash,
&amount,
&gasprice,
&gas,
&nonce,
)
arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{
TxHash: txhash,
To: to_addr,
From: from_addr,
BlockHash: blockhash,
Amount: amount,
GasPrice: gasprice,
Gas: gas,
Nonce: nonce,
})
} }
txs = append(txs, prettyFormat) return arr.TxEntry
// Uncomment to view "pretty" version of data
//fmt.Println("DECODED TX")
//fmt.Println("Tx Hash: ", txData.TxHash.Hex())
//fmt.Println("From: ", txData.From.Hex())
//fmt.Println("To: ", txData.To.Hex())
//fmt.Println("BlockHash: ", txData.BlockHash.Hex())
//fmt.Println("Amount: ", txData.Amount)
//fmt.Println("Gas: ", txData.Gas)
//fmt.Println("GasPrice: ", txData.GasPrice)
//fmt.Println("Nonce: ", txData.Nonce)
//fmt.Println("Data: ", txData.Data)
}
iter.Release()
return txs
} }
func GetTransaction(db *leveldb.DB, tx *types.Transaction) ShyftTxEntryPretty { //GetTransaction fn returns single tx
var prettyFormat ShyftTxEntryPretty func GetTransaction(sqldb *sql.DB) []ShyftTxEntryPretty {
key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) var arr txRes
data, err := db.Get(key, nil) sqlStatement := `SELECT * FROM txs WHERE nonce=$1;`
if err != nil { row := sqldb.QueryRow(sqlStatement, 7)
log.Crit("Could not retrieve TX", "err", err) var txhash string
} var to_addr string
if len(data) > 0 { var from_addr string
var txData ShyftTxEntry var blockhash string
d := gob.NewDecoder(bytes.NewBuffer(data)) var amount string
if err := d.Decode(&txData); err != nil { var gasprice string
log.Crit("Failed to decode tx:", "err", err) var gas uint64
} var nonce uint64
prettyFormat = ShyftTxEntryPretty{ _ = row.Scan(&txhash, &to_addr, &from_addr, &blockhash, &amount, &gasprice, &gas, &nonce)
TxHash: txData.TxHash.Hex(),
From: txData.From.Hex(), // switch err {
To: txData.To.Hex(), // case sql.ErrNoRows:
BlockHash: txData.BlockHash, // fmt.Println("No rows were returned!")
Amount: txData.Amount, // case nil:
Gas: txData.Gas, // fmt.Println(txhash, to_addr, from_addr, blockhash, amount, gasprice, gas, nonce)
GasPrice: txData.GasPrice, // default:
Nonce: txData.Nonce, // panic(err)
Data: txData.Data, // }
}
// Uncomment to view "pretty" version of data arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{
//fmt.Println("DECODED TX") TxHash: txhash,
//fmt.Println("Tx Hash: ", txData.TxHash.Hex()) To: to_addr,
//fmt.Println("From: ", txData.From.Hex()) From: from_addr,
//fmt.Println("To: ", txData.To.Hex()) BlockHash: blockhash,
//fmt.Println("BlockHash: ", txData.BlockHash.Hex()) Amount: amount,
//fmt.Println("Amount: ", txData.Amount) GasPrice: gasprice,
//fmt.Println("Gas: ", txData.Gas) Gas: gas,
//fmt.Println("GasPrice: ", txData.GasPrice) Nonce: nonce,
//fmt.Println("Nonce: ", txData.Nonce) })
//fmt.Println("Data: ", txData.Data)
} return arr.TxEntry
return prettyFormat
} }