From d995a67b9cee1a2a854a4638cdefa47d724b31db Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Mon, 23 Apr 2018 11:31:32 -0400 Subject: [PATCH 1/3] updated tables --- shyftDb/postgres_setup/create_tables.psql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shyftDb/postgres_setup/create_tables.psql b/shyftDb/postgres_setup/create_tables.psql index 4205273740..d501365a54 100644 --- a/shyftDb/postgres_setup/create_tables.psql +++ b/shyftDb/postgres_setup/create_tables.psql @@ -9,8 +9,8 @@ CREATE TABLE IF NOT EXISTS txs ( to_addr text, from_addr text, blockhash text, - amount bigint, - gasprice bigint, + amount numeric, + gasprice numeric, gas numeric, nonce numeric, data bytea, From d35259e1c5565680df6173896f0f4cd2b9cedfa6 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Mon, 23 Apr 2018 11:44:39 -0400 Subject: [PATCH 2/3] write txs successfully writing to db --- shyftdb/shyft_database_util.go | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 2381be3dfa..71c56357a1 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -95,32 +95,19 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha Nonce: tx.Nonce(), Data: tx.Data(), } - // var encodedData bytes.Buffer - // encoder := gob.NewEncoder(&encodedData) - // if err := encoder.Encode(txData); err != nil { - // log.Crit("Faild to encode TX data", "err", err) - // } - txHash := txData.TxHash - from := txData.From - to := txData.To + + txHash := txData.TxHash.Hex() + from := txData.From.Hex() + to := txData.To.Hex() blockHasher := txData.BlockHash - amount := txData.Amount - gasPrice := txData.GasPrice + amount := txData.Amount.String() + gasPrice := txData.GasPrice.String() nonce := txData.Nonce gas := txData.Gas 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` - qerr := sqldb.QueryRow(sqlStatement, txHash, from, to, blockHasher, amount, gasPrice, nonce, gas, data).Scan(&nonce) //.Scan(&fun) + 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, to, from, blockHasher, amount, gasPrice, gas, nonce, data).Scan(&nonce) if qerr != nil { panic(qerr) } From de9228da277113f3ba1bc29ee7308679f4e7620c Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Mon, 23 Apr 2018 12:22:52 -0400 Subject: [PATCH 3/3] getTransaction in place --- blockExplorerApi/handler.go | 33 ++++-- blockExplorerApi/routes.go | 2 +- shyftdb/shyft_database_util.go | 178 ++++++++++++++++++--------------- 3 files changed, 127 insertions(+), 86 deletions(-) diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go index de61956f15..1c55261cd5 100644 --- a/blockExplorerApi/handler.go +++ b/blockExplorerApi/handler.go @@ -14,24 +14,44 @@ import ( // GetTransaction gets txs func GetTransaction(w http.ResponseWriter, r *http.Request) { - // vars := mux.Vars(r) - // address := vars["address"] + connStr := "user=postgres dbname=shyftdb sslmode=disable" + 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.WriteHeader(http.StatusOK) - //fmt.Fprintln(w, "Get All Transactions", addresses) + fmt.Fprintln(w, getTxResponse) } // GetAllTransactions gets txs 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.WriteHeader(http.StatusOK) - //fmt.Fprintln(w, "Get All Transactions", address) + fmt.Fprintln(w, txs) } // GetBalance gets balance @@ -57,7 +77,6 @@ func GetBalances(w http.ResponseWriter, r *http.Request) { //GetBlock returns block json func GetBlock(w http.ResponseWriter, r *http.Request) { - connStr := "user=postgres dbname=shyftdb sslmode=disable" blockExplorerDb, err := sql.Open("postgres", connStr) if err != nil { diff --git a/blockExplorerApi/routes.go b/blockExplorerApi/routes.go index fadd46ea94..dcd5831d8b 100644 --- a/blockExplorerApi/routes.go +++ b/blockExplorerApi/routes.go @@ -48,7 +48,7 @@ var routes = Routes{ Route{ "GetTransaction", "GET", - "/api/get_transaction/{address}", + "/api/get_transaction", GetTransaction, }, Route{ diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 71c56357a1..88288a8714 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -10,7 +10,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" "github.com/syndtr/goleveldb/leveldb" - "github.com/syndtr/goleveldb/leveldb/util" "database/sql" @@ -32,6 +31,19 @@ type blockRes struct { 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 type ShyftTxEntry struct { TxHash common.Hash @@ -50,8 +62,8 @@ type ShyftTxEntryPretty struct { To string From string BlockHash string - Amount *big.Int - GasPrice *big.Int + Amount string + GasPrice string Gas uint64 Nonce uint64 Data []byte @@ -114,10 +126,6 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha 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) // func WriteFromBalance(db *leveldb.DB, tx *types.Transaction) { @@ -302,78 +310,92 @@ func GetBlock(sqldb *sql.DB) []SBlock { return arr.Blocks } -func GetAllTransactions(db *leveldb.DB) []ShyftTxEntryPretty { - var txs []ShyftTxEntryPretty - iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) - for iter.Next() { - var txData ShyftTxEntry - d := gob.NewDecoder(bytes.NewBuffer(iter.Value())) - if err := d.Decode(&txData); err != nil { - log.Crit("Failed to decode tx:", "err", err) - } - prettyFormat := ShyftTxEntryPretty{ - TxHash: txData.TxHash.Hex(), - From: txData.From.Hex(), - To: txData.To.Hex(), - BlockHash: txData.BlockHash, - Amount: txData.Amount, - Gas: txData.Gas, - GasPrice: txData.GasPrice, - Nonce: txData.Nonce, - Data: txData.Data, - } - txs = append(txs, prettyFormat) - // 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) +//GetAllTransactions getter fn for API +func GetAllTransactions(sqldb *sql.DB) []ShyftTxEntryPretty { + var arr txRes + rows, err := sqldb.Query(` + SELECT + txhash, + to_addr, + from_addr, + blockhash, + amount, + gasprice, + gas, + nonce + FROM txs`) + if err != nil { + fmt.Println("err") } - iter.Release() - return txs + defer rows.Close() + + for rows.Next() { + var txhash string + var to_addr string + var from_addr string + var blockhash string + var amount string + var gasprice string + 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, + }) + } + return arr.TxEntry } -func GetTransaction(db *leveldb.DB, tx *types.Transaction) ShyftTxEntryPretty { - var prettyFormat ShyftTxEntryPretty - key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) - data, err := db.Get(key, nil) - if err != nil { - log.Crit("Could not retrieve TX", "err", err) - } - if len(data) > 0 { - var txData ShyftTxEntry - d := gob.NewDecoder(bytes.NewBuffer(data)) - if err := d.Decode(&txData); err != nil { - log.Crit("Failed to decode tx:", "err", err) - } - prettyFormat = ShyftTxEntryPretty{ - TxHash: txData.TxHash.Hex(), - From: txData.From.Hex(), - To: txData.To.Hex(), - BlockHash: txData.BlockHash, - Amount: txData.Amount, - Gas: txData.Gas, - GasPrice: txData.GasPrice, - Nonce: txData.Nonce, - Data: txData.Data, - } - // 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) - } - return prettyFormat +//GetTransaction fn returns single tx +func GetTransaction(sqldb *sql.DB) []ShyftTxEntryPretty { + var arr txRes + sqlStatement := `SELECT * FROM txs WHERE nonce=$1;` + row := sqldb.QueryRow(sqlStatement, 7) + var txhash string + var to_addr string + var from_addr string + var blockhash string + var amount string + var gasprice string + var gas uint64 + var nonce uint64 + _ = row.Scan(&txhash, &to_addr, &from_addr, &blockhash, &amount, &gasprice, &gas, &nonce) + + // switch err { + // case sql.ErrNoRows: + // fmt.Println("No rows were returned!") + // case nil: + // fmt.Println(txhash, to_addr, from_addr, blockhash, amount, gasprice, gas, nonce) + // default: + // panic(err) + // } + + arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{ + TxHash: txhash, + To: to_addr, + From: from_addr, + BlockHash: blockhash, + Amount: amount, + GasPrice: gasprice, + Gas: gas, + Nonce: nonce, + }) + + return arr.TxEntry }