From d7b426e9ca4cb3259396259d9b8d8613fb3f9781 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Fri, 18 May 2018 13:19:42 -0400 Subject: [PATCH] updated API requests --- blockExplorerApi/handler.go | 49 ++++++-- blockExplorerApi/routes.go | 18 ++- core/genesis.go | 1 - .../components/table/transactions/table.css | 5 + .../table/transactions/transactionTable.js | 9 +- shyftdb/shyft_database_util.go | 118 ++++++++++++++++++ 6 files changed, 183 insertions(+), 17 deletions(-) diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go index 3ba6e5d672..a030a6b07a 100644 --- a/blockExplorerApi/handler.go +++ b/blockExplorerApi/handler.go @@ -57,6 +57,30 @@ func GetAllTransactions(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, txs) } +// GetAllTransactions gets txs +func GetAllTransactionsFromBlock(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + blockNumber := vars["blockNumber"] + connStr := "user=postgres dbname=shyftdb sslmode=disable" + blockExplorerDb, err := sql.Open("postgres", connStr) + if err != nil { + return + } + + txsFromBlock := shyftdb.GetAllTransactionsFromBlock(blockExplorerDb, blockNumber) + + 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, txsFromBlock) +} + // GetAccount gets balance func GetAccount(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -161,15 +185,22 @@ func GetAllBlocks(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, block3) } -//func GetRecentBlock(w http.ResponseWriter, r *http.Request) { -// connStr := "user=postgres dbname=shyftdb sslmode=disable" -// blockExplorerDb, err := sql.Open("postgres", connStr) -// if err != nil { -// return -// } -// -// -//} +func GetRecentBlock(w http.ResponseWriter, r *http.Request) { + connStr := "user=postgres dbname=shyftdb sslmode=disable" + blockExplorerDb, err := sql.Open("postgres", connStr) + if err != nil { + return + } + mostRecentBlock := shyftdb.GetRecentBlock(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, mostRecentBlock) + +} //GetInternalTransactions gets internal txs func GetInternalTransactions(w http.ResponseWriter, r *http.Request) { diff --git a/blockExplorerApi/routes.go b/blockExplorerApi/routes.go index 578efc3a23..a08e207f6e 100644 --- a/blockExplorerApi/routes.go +++ b/blockExplorerApi/routes.go @@ -57,12 +57,18 @@ var routes = Routes{ "/api/get_transaction/{txHash}", GetTransaction, }, - //Route{ - // Name: "GetRecentBlock", - // Method: "GET", - // Pattern: "/api/get_recent_block", - // HandlerFunc: GetRecentBlock, - //}, + Route{ + Name: "GetRecentBlock", + Method: "GET", + Pattern: "/api/get_recent_block", + HandlerFunc: GetRecentBlock, + }, + Route{ + Name: "GetAllTransactionsFromBlock", + Method: "GET", + Pattern: "/api/get_all_transactions_from_block/{blockNumber}", + HandlerFunc: GetAllTransactionsFromBlock, + }, Route{ "GetInternalTransactions", "GET", diff --git a/core/genesis.go b/core/genesis.go index b7aac23d53..1e39bfa094 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -249,7 +249,6 @@ func SetupGenesisBlock(db ethdb.Database, genesis *Genesis, sqldb *sql.DB) (*par if genesis != nil && genesis.Config == nil { return params.AllEthashProtocolChanges, common.Hash{}, errGenesisNoConfig } - // Just commit the new block if there is no stored genesis block. stored := GetCanonicalHash(db, 0) if (stored == common.Hash{}) { diff --git a/shyftBlockExplorerUI/src/components/table/transactions/table.css b/shyftBlockExplorerUI/src/components/table/transactions/table.css index 38f3d87fed..7b1f0858f0 100644 --- a/shyftBlockExplorerUI/src/components/table/transactions/table.css +++ b/shyftBlockExplorerUI/src/components/table/transactions/table.css @@ -57,4 +57,9 @@ th { width: 25px; height: 25px; margin-left: 15px; +} + +.disabled { + pointer-events: none; + color: black; } \ No newline at end of file diff --git a/shyftBlockExplorerUI/src/components/table/transactions/transactionTable.js b/shyftBlockExplorerUI/src/components/table/transactions/transactionTable.js index d978a82ed5..a4b399efc5 100644 --- a/shyftBlockExplorerUI/src/components/table/transactions/transactionTable.js +++ b/shyftBlockExplorerUI/src/components/table/transactions/transactionTable.js @@ -4,11 +4,18 @@ import arrow from '../../assets/arrow_right.png'; import { Link } from 'react-router-dom' const TransactionTable = (props) => { + let flag; + if(props.txHash.indexOf("GENESIS") != -1) { + flag = false + }else { + flag = true + } + return ( - props.detailTransactionHandler(props.txHash)}> + props.detailTransactionHandler(props.txHash)}> {props.txHash} {props.blockNumber} diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 88b19df03e..b8e3903edc 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -588,6 +588,124 @@ func GetBlock(sqldb *sql.DB, blockNumber string) string { return string(json) } +func GetRecentBlock(sqldb *sql.DB) string { + sqlStatement := `SELECT * FROM blocks WHERE number=(SELECT MAX(number) FROM blocks);` + row := sqldb.QueryRow(sqlStatement) + var hash string + var coinbase string + var gasUsed string + var gasLimit string + var txCount string + var uncleCount string + var age string + var parentHash string + var uncleHash string + var difficulty string + var size string + var nonce string + var num string + row.Scan( + &hash, + &coinbase, + &gasUsed, + &gasLimit, + &txCount, + &uncleCount, + &age, + &parentHash, + &uncleHash, + &difficulty, + &size, + &nonce, + &num,) + + block := SBlock{ + Hash: hash, + Coinbase: coinbase, + GasUsed: gasUsed, + GasLimit: gasLimit, + TxCount: txCount, + UncleCount: uncleCount, + Age: age, + ParentHash:parentHash, + UncleHash:uncleHash, + Difficulty:difficulty, + Size: size, + Nonce:nonce, + Number: num, + } + json, _ := json.Marshal(block) + return string(json) +} + +func GetAllTransactionsFromBlock(sqldb *sql.DB, blockNumber string) string { + var arr txRes + var txx string + sqlStatement := `SELECT * FROM txs WHERE blockNumber=$1` + rows, err := sqldb.Query(sqlStatement, blockNumber) + if err != nil { + fmt.Println("err") + } + defer rows.Close() + for rows.Next() { + var txhash string + var to_addr string + var from_addr string + var blockhash string + var blocknumber string + var amount uint64 + var gasprice uint64 + var gas uint64 + var gasLimit uint64 + var txfee uint64 + var nonce uint64 + var status string + var isContract bool + var age time.Time + var data []byte + err = rows.Scan( + &txhash, + &to_addr, + &from_addr, + &blockhash, + &blocknumber, + &amount, + &gasprice, + &gas, + &gasLimit, + &txfee, + &nonce, + &status, + &isContract, + &age, + &data, + ) + + arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{ + TxHash: txhash, + To: to_addr, + From: from_addr, + BlockHash: blockhash, + BlockNumber: blocknumber, + Amount: amount, + GasPrice: gasprice, + Gas: gas, + GasLimit: gasLimit, + Cost: txfee, + Nonce: nonce, + Status: status, + IsContract: isContract, + Age: age, + Data: data, + }) + + tx, _ := json.Marshal(arr.TxEntry) + newtx := string(tx) + txx = newtx + } + return txx +} + //GetAllTransactions getter fn for API func GetAllTransactions(sqldb *sql.DB) string { var arr txRes