Merge pull request #24 from ShyftNetwork/writeContracts

Updated writeTransactions
This commit is contained in:
Tim Williams 2018-05-17 11:11:38 -04:00 committed by GitHub
commit 573cb28a43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 125 additions and 124 deletions

View file

@ -16,7 +16,6 @@ import (
func GetTransaction(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
txHash := vars["txHash"]
fmt.Println(txHash)
connStr := "user=postgres dbname=shyftdb sslmode=disable"
blockExplorerDb, err := sql.Open("postgres", connStr)
if err != nil {
@ -24,7 +23,6 @@ func GetTransaction(w http.ResponseWriter, r *http.Request) {
}
getTxResponse := shyftdb.GetTransaction(blockExplorerDb, txHash)
fmt.Println(getTxResponse)
if err != nil {
http.Error(w, err.Error(), 500)

View file

@ -24,7 +24,7 @@ class BlocksTable extends Component {
render() {
const table = this.state.data.map((data, i) => {
return <BlockTable
key={data.Hash[i]}
key={`${data.TxHash}${i}`}
Hash={data.Hash}
Number={data.Number}
Coinbase={data.Coinbase}

View file

@ -5,7 +5,7 @@ import { Link } from 'react-router-dom'
const BlockTable = (props) => {
return (
<tbody key={props.key}>
<tbody>
<tr>
<td><Link to="/blocks/detail" onClick={() => props.detailBlockHandler(props.Number)}>
{props.Number}

View file

@ -8,6 +8,7 @@ class DetailBlockTable extends Component {
let combinedClasses = ['responsive-table', classes.table];
return (
<table className={combinedClasses.join(' ')}>
<tbody>
<tr>
<th scope="col">Height:</th>
<td>{data.Number}</td>
@ -68,6 +69,7 @@ class DetailBlockTable extends Component {
<th scope="col">Reward:</th>
<td>TBD</td>
</tr>
</tbody>
</table>
);
}

View file

@ -13,6 +13,11 @@ th {
padding-right: 10px;
}
td {
padding-left: 5px;
font-size: .5rem;
}
.addressTag {
display: inline-block;
vertical-align: bottom;

View file

@ -8,66 +8,60 @@ class DetailTransactionTable extends Component {
let combinedClasses = ['responsive-table', classes.table];
return (
<table className={combinedClasses.join(' ')}>
<tbody>
<tr>
<th scope="col">TxHash:</th>
<td>{data.TxHash}</td>
</tr>
<tr>
<th scope="col">Age:</th>
<th scope="col">TxReceipt Status:</th>
<td>{data.Status}</td>
</tr>
<tr>
<th scope="col">Block Height:</th>
<td>{data.BlockNumber}</td>
</tr>
<tr>
<th scope="col">TimeStamp:</th>
<td>{data.Age}</td>
</tr>
<tr>
<th scope="col">Txn:</th>
<td>{data.TxCount} transactions</td>
<th scope="col">From:</th>
<td>{data.From}</td>
</tr>
<tr>
<th scope="col">Block Hash:</th>
<td>{data.Hash}</td>
<th scope="col">To:</th>
<td>{ `${data.IsContract}` ? `${data.To} (Contract)` : `${data.To}` }</td>
</tr>
<tr>
<th scope="col">Parent Hash:</th>
<td>{data.ParentHash}</td>
<th scope="col">Value:</th>
<td>{data.Amount}</td>
</tr>
<tr>
<th scope="col">Uncle Hash:</th>
<td>{data.UncleHash}</td>
</tr>
<tr>
<th scope="col">Uncle Count:</th>
<td>{data.UncleCount}</td>
</tr>
<tr>
<th scope="col">Coinbase:</th>
<td>{data.Coinbase}</td>
</tr>
<tr>
<th scope="col">Difficulty:</th>
<td>{data.Difficulty}</td>
</tr>
<tr>
<th scope="col">GasUsed:</th>
<td>{data.GasUsed}</td>
</tr>
<tr>
<th scope="col">Size:</th>
<td>{data.Size}ytes</td>
</tr>
<tr>
<th scope="col">GasUsed:</th>
<td>{data.GasUsed}</td>
</tr>
<tr>
<th scope="col">GasLimit:</th>
<th scope="col">Gas Limit:</th>
<td>{data.GasLimit}</td>
</tr>
<tr>
<th scope="col">Gas Used By Txn:</th>
<td>{data.Gas}</td>
</tr>
<tr>
<th scope="col">Gas Price:</th>
<td>{data.GasPrice}</td>
</tr>
<tr>
<th scope="col">Actual Tx Cost/Fee:</th>
<td>{data.Cost}</td>
</tr>
<tr>
<th scope="col">Nonce:</th>
<td>{data.Nonce}</td>
</tr>
<tr>
<th scope="col">Reward:</th>
<td>TBD</td>
<th scope="col">Input Data:</th>
<td>{data.Data}</td>
</tr>
</tbody>
</table>
);
}

View file

@ -26,25 +26,28 @@ class TransactionTable extends Component {
const table = this.state.data.map((data, i) => {
return <TransactionsTable
key={data.TxHash[i]}
key={`${data.TxHash}${i}`}
age={data.Age}
txHash={data.TxHash}
blockNumber={data.BlockNumber}
to={data.To}
from={data.From}
value={data.Amount}
cost={data.Cost}
detailTransactionHandler={this.props.detailTransactionHandler}
/>
})
let combinedClasses = ['responsive-table', classes.table];
return (
<table className={combinedClasses.join(' ')}>
<table key={this.state.data.TxHash} className={combinedClasses.join(' ')}>
<thead className={classes.tHead}>
<tr>
<th scope="col">TxHash</th>
<th scope="col">Block</th>
<th scope="col">Age</th>
<th scope="col">From</th>
<th scope="col"></th>
<th scope="col">To</th>
<th scope="col">Value</th>
<th scope="col">TxFee</th>

View file

@ -12,10 +12,10 @@ const TransactionTable = (props) => {
{props.txHash}</Link>
</td>
<td>{props.blockNumber}</td>
<td>30 secs ago</td>
<td>{props.age}</td>
<td className={classes.fromTag}>{props.from}</td>
<img className={classes.arrow} src={arrow}/>
<td>{props.to}</td>
<td><img className={classes.arrow} src={arrow}/></td>
<td className={classes.toTag}>{props.to}</td>
<td>{props.value}</td>
<td>{props.cost}</td>
</tr>

View file

@ -23,9 +23,12 @@ CREATE TABLE IF NOT EXISTS txs (
amount numeric,
gasprice numeric,
gas numeric,
gasLimit numeric,
txFee numeric,
nonce numeric,
txStatus text,
isContract bool,
age timestamp,
data bytea
);

View file

@ -78,8 +78,12 @@ type ShyftTxEntryPretty struct {
Amount uint64
GasPrice uint64
Gas uint64
GasLimit uint64
Cost uint64
Nonce uint64
Status string
IsContract bool
Age time.Time
Data []byte
}
@ -98,13 +102,7 @@ type SendAndReceive struct {
}
//WriteBlock writes to block info to sql db
func WriteBlock(sqldb *sql.DB, block *types.Block, receipts []*types.Receipt) error {
//Need to create field in postgres db isContract : True || False
//Need to fix nonce numeric issue (attempt to reproduce and record)
//Need to update tx To Field where null with Contract Address
//Need to update account table with that Contract Address
//Need to update AccountNonce and Balance of Tx To field || Contract Address
func WriteBlock(sqldb *sql.DB, block *types.Block, receipts []*types.Receipt) error {
coinbase := block.Header().Coinbase.String()
number := block.Header().Number.String()
gasUsed := block.Header().GasUsed
@ -117,33 +115,6 @@ type SendAndReceive struct {
blockSize := block.Size().String()
blockNonce := block.Nonce()
// Convert the receipts into their storage form and serialize them
storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
for i, receipt := range receipts {
storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
var txHashFromReciept = (*types.ReceiptForStorage)(receipt).TxHash
var statusFromReciept = (*types.ReceiptForStorage)(receipt).Status
var contractAddressFromReciept = (*types.ReceiptForStorage)(receipt).ContractAddress
if statusFromReciept == 1 {
fmt.Println("THIS IS statusFromReciept", "SUCCESS", statusFromReciept)
}
if statusFromReciept == 0 {
fmt.Println("THIS IS statusFromReciept", "FAIL", statusFromReciept)
}
if block.Transactions()[0].To() == nil {
updateSQLStatement := `UPDATE txs SET to_addr = ($2) WHERE txHash = ($1)`
_, error := sqldb.Exec(updateSQLStatement, txHashFromReciept.String(), contractAddressFromReciept.String())
if error != nil {
panic(error)
}
}
fmt.Println("THIS IS txHashFromReciept", txHashFromReciept.String())
fmt.Println("THIS IS contractAddressFromReciept", contractAddressFromReciept.String())
}
i, err := strconv.ParseInt(block.Time().String(), 10, 64)
if err != nil {
panic(err)
@ -159,7 +130,7 @@ type SendAndReceive struct {
if block.Transactions().Len() > 0 {
for _, tx := range block.Transactions() {
//WriteMinerRewards(sqldb, block)
WriteTransactions(sqldb, tx, block.Header().Hash(), block.Header().Number.String())
WriteTransactions(sqldb, tx, block.Header().Hash(), block.Header().Number.String(), receipts, age, gasLimit)
if block.Transactions()[0].To() != nil {
WriteFromBalance(sqldb, tx)
}
@ -173,7 +144,7 @@ type SendAndReceive struct {
}
//WriteTransactions writes to sqldb
func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Hash, blockNumber string) error {
func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Hash, blockNumber string, receipts []*types.Receipt, age time.Time, gasLimit uint64) error {
txData := ShyftTxEntry{
TxHash: tx.Hash(),
From: tx.From(),
@ -186,7 +157,6 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha
Nonce: tx.Nonce(),
Data: tx.Data(),
}
txHash := txData.TxHash.Hex()
from := txData.From.Hex()
blockHasher := txData.BlockHash
@ -198,11 +168,25 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha
data := txData.Data
to := txData.To
var isContract bool
var statusFromReciept string
if (to == nil){
var contractAddressFromReciept string
for _, receipt := range receipts {
statusReciept := (*types.ReceiptForStorage)(receipt).Status
contractAddressFromReciept = (*types.ReceiptForStorage)(receipt).ContractAddress.String()
if statusReciept == 1 {
statusFromReciept = "SUCCESS"
}
if statusReciept == 0 {
statusFromReciept = "FAIL"
}
}
var retNonce string
isContract = true
sqlStatement := `INSERT INTO txs(txhash, from_addr, blockhash, blockNumber, amount, gasprice, gas, txfee, nonce, isContract, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11)) RETURNING nonce`
qerr := sqldb.QueryRow(sqlStatement, txHash, from, blockHasher, blockNumber, amount, gasPrice, gas, txFee, nonce, isContract, data).Scan(&retNonce)
sqlStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount, gasprice, gas, gasLimit,txfee, nonce, isContract, txStatus, age, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12), ($13), ($14), ($15)) RETURNING nonce`
qerr := sqldb.QueryRow(sqlStatement, txHash, from, contractAddressFromReciept, blockHasher, blockNumber, amount, gasPrice, gas, gasLimit, txFee, nonce, isContract, statusFromReciept, age,data).Scan(&retNonce)
if qerr != nil {
panic(qerr)
@ -210,8 +194,18 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha
} else {
var retNonce string
isContract = false
sqlStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount, gasprice, gas, txfee, nonce, isContract, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12)) RETURNING nonce`
qerr := sqldb.QueryRow(sqlStatement, txHash, from, to.Hex(), blockHasher, blockNumber, amount, gasPrice, gas, txFee, nonce, isContract, data).Scan(&retNonce)
for _, receipt := range receipts {
statusReciept := (*types.ReceiptForStorage)(receipt).Status
if statusReciept == 1 {
statusFromReciept = "SUCCESS"
}
if statusReciept == 0 {
statusFromReciept = "FAIL"
}
}
sqlStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount, gasprice, gas, gasLimit, txfee, nonce, isContract, txStatus, age, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12), ($13), ($14), ($15)) RETURNING nonce`
qerr := sqldb.QueryRow(sqlStatement, txHash, from, to.Hex(), blockHasher, blockNumber, amount, gasPrice, gas, gasLimit, txFee, nonce, isContract, statusFromReciept, age,data).Scan(&retNonce)
if qerr != nil {
panic(qerr)
@ -243,13 +237,6 @@ func WriteContractBalance(sqldb *sql.DB, tx *types.Transaction) error {
err := sqldb.QueryRow(sqlExistsStatement, fromAddr).Scan(&response)
switch {
case err == sql.ErrNoRows:
fmt.Println("NO ROWS RAN")
//i, err := strconv.Atoi(accountNonceSen)
//if err != nil {
// fmt.Println(err)
//}
//fmt.Println("accountnonce", i)
//fmt.Println(reflect.TypeOf(i))
sqlStatement := `INSERT INTO accounts(addr, balance, txCountAccount) VALUES(($1), ($2), ($3)) RETURNING addr`
insertErr := sqldb.QueryRow(sqlStatement, fromAddr, amount, accountNonceSen).Scan(&fromAddr)
if insertErr != nil {
@ -307,8 +294,6 @@ func WriteContractBalanceHelper(sqldb *sql.DB, tx *types.Transaction) (SendAndRe
//WriteFromBalance writes senders balance to accounts db
func WriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error {
//IF to address is nil (which means its contract creation)
//Need to create a condition (flag) check and then change how the nonce increment works
sendAndReceiveData, balanceRec, balanceSen, accountNonceRec, accountNonceSen := WriteBalanceHelper(sqldb, tx)
toAddr := sendAndReceiveData.To
fromAddr := sendAndReceiveData.From
@ -321,13 +306,10 @@ func WriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error {
err := sqldb.QueryRow(sqlExistsStatement, toAddr).Scan(&response)
switch {
case err == sql.ErrNoRows:
fmt.Println("NO ROWS RAN")
i, err := strconv.Atoi(accountNonceRec)
if err != nil {
fmt.Println(err)
}
//fmt.Println("accountnonce", i)
//fmt.Println(reflect.TypeOf(i))
sqlStatement := `INSERT INTO accounts(addr, balance, txCountAccount) VALUES(($1), ($2), ($3)) RETURNING addr`
insertErr := sqldb.QueryRow(sqlStatement, toAddr, amount, i).Scan(&toAddr)
if insertErr != nil {
@ -611,19 +593,7 @@ func GetAllTransactions(sqldb *sql.DB) string {
var arr txRes
var txx string
rows, err := sqldb.Query(`
SELECT
txhash,
to_addr,
from_addr,
blockhash,
blocknumber,
amount,
gasprice,
gas,
txfee,
nonce,
data
FROM txs`)
SELECT * FROM txs`)
if err != nil {
fmt.Println("err")
}
@ -637,8 +607,12 @@ func GetAllTransactions(sqldb *sql.DB) 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,
@ -649,8 +623,12 @@ func GetAllTransactions(sqldb *sql.DB) string {
&amount,
&gasprice,
&gas,
&gasLimit,
&txfee,
&nonce,
&status,
&isContract,
&age,
&data,
)
@ -663,8 +641,12 @@ func GetAllTransactions(sqldb *sql.DB) string {
Amount: amount,
GasPrice: gasprice,
Gas: gas,
GasLimit: gasLimit,
Cost: txfee,
Nonce: nonce,
Status: status,
IsContract: isContract,
Age: age,
Data: data,
})
@ -679,28 +661,38 @@ func GetAllTransactions(sqldb *sql.DB) string {
func GetTransaction(sqldb *sql.DB, txHash string) string {
sqlStatement := `SELECT * FROM txs WHERE txhash=$1;`
row := sqldb.QueryRow(sqlStatement, txHash)
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 txfee uint64
var nonce uint64
var data []byte
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
row.Scan(
&txhash,
&to_addr,
&from_addr,
&blockhash,
&blocknumber,
&amount,
&gasprice,
&gas,
&gasLimit,
&txfee,
&nonce,
&status,
&isContract,
&age,
&data)
tx := ShyftTxEntryPretty{
TxHash: txhash,
To: to_addr,
@ -710,8 +702,12 @@ func GetTransaction(sqldb *sql.DB, txHash string) string {
Amount: amount,
GasPrice: gasprice,
Gas: gas,
GasLimit: gasLimit,
Cost: txfee,
Nonce: nonce,
Status: status,
IsContract: isContract,
Age: age,
Data: data,
}
json, _ := json.Marshal(tx)