getTransaction in place

This commit is contained in:
Dustin Brickwood 2018-04-23 12:22:52 -04:00
parent d35259e1c5
commit de9228da27
3 changed files with 127 additions and 86 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

@ -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
@ -114,10 +126,6 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha
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) {
@ -302,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
} }