code refactor

This commit is contained in:
Dustin Brickwood 2018-07-19 16:24:08 -04:00
parent c17880e21e
commit 71cf9acfd6
6 changed files with 531 additions and 808 deletions

View file

@ -163,7 +163,7 @@ func WriteShyftGen(gen *Genesis, block *types.Block) {
isContract := false
data:= ""
addr := k.String()
txCountAccount := v.Nonce +1
accountNonce := v.Nonce +1
i, err := strconv.ParseInt(block.Time().String(), 10, 64)
if err != nil {
panic(err)
@ -173,15 +173,15 @@ func WriteShyftGen(gen *Genesis, block *types.Block) {
GENESIS := "GENESIS"
txHash := strings.Join(Genesis, addr)
sqlStatement := `INSERT INTO accounts(addr, balance, txCountAccount) VALUES(($1), ($2), ($3)) RETURNING addr`
insertErr := sqldb.QueryRow(sqlStatement, addr, v.Balance.String(), txCountAccount).Scan(&addr)
sqlStatement := `INSERT INTO accounts(addr, balance, accountnonce) VALUES(($1), ($2), ($3)) RETURNING addr`
insertErr := sqldb.QueryRow(sqlStatement, addr, v.Balance.String(), accountNonce).Scan(&addr)
if insertErr != nil {
panic(insertErr)
}
var retNonce string
sqlGenTxStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount,gasPrice, gas, gasLimit,txFee,nonce,txstatus, iscontract,age, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12), ($13), ($14), ($15)) RETURNING nonce`
insertError := sqldb.QueryRow(sqlGenTxStatement, txHash, GENESIS, addr, block.Header().Hash().Hex(), number, v.Balance.String(), gasPrice, gasUsed, gasLimit, txFee,txCountAccount,txStatus, isContract, age, data).Scan(&retNonce)
insertError := sqldb.QueryRow(sqlGenTxStatement, txHash, GENESIS, addr, block.Header().Hash().Hex(), number, v.Balance.String(), gasPrice, gasUsed, gasLimit, txFee,accountNonce,txStatus, isContract, age, data).Scan(&retNonce)
if insertError != nil {
panic(insertError)
}

File diff suppressed because it is too large Load diff

381
core/shyft_get_utils.go Normal file
View file

@ -0,0 +1,381 @@
package core
import (
"encoding/json"
"database/sql"
"fmt"
"time"
)
///////////
// Getters
//////////
func SGetAllBlocks(sqldb *sql.DB) string {
var arr blockRes
var blockArr string
rows, err := sqldb.Query(`SELECT * FROM blocks`)
if err != nil {
fmt.Println("err")
}
defer rows.Close()
for rows.Next() {
var hash, coinbase, age, parentHash, uncleHash, difficulty, size, rewards, num string
var gasUsed, gasLimit, nonce uint64
var txCount, uncleCount int
err = rows.Scan(
&hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num,)
arr.Blocks = append(arr.Blocks, 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,
Rewards: rewards,
Number: num,
})
blocks, _ := json.Marshal(arr.Blocks)
blocksFmt := string(blocks)
blockArr = blocksFmt
}
return blockArr
}
//GetBlock queries to send single block info
//TODO provide blockHash arg passed from handler.go
func SGetBlock(sqldb *sql.DB, blockNumber string) string {
sqlStatement := `SELECT * FROM blocks WHERE number=$1;`
row := sqldb.QueryRow(sqlStatement, blockNumber)
var hash, coinbase, age, parentHash, uncleHash, difficulty, size, rewards, num string
var gasUsed, gasLimit, nonce uint64
var txCount, uncleCount int
row.Scan(
&hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &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,
Rewards: rewards,
Number: num,
}
json, _ := json.Marshal(block)
return string(json)
}
func SGetRecentBlock(sqldb *sql.DB) string {
sqlStatement := `SELECT * FROM blocks WHERE number=(SELECT MAX(number) FROM blocks);`
row := sqldb.QueryRow(sqlStatement)
var hash, coinbase, age, parentHash, uncleHash, difficulty, size, rewards, num string
var gasUsed, gasLimit, nonce uint64
var txCount, uncleCount int
row.Scan(
&hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &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,
Rewards: rewards,
Number: num,
}
json, _ := json.Marshal(block)
return string(json)
}
func SGetAllTransactionsFromBlock(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, to_addr, from_addr, blockhash, blocknumber, amount, status string
var gasprice, gas, gasLimit, txfee, nonce uint64
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
}
func SGetAllBlocksMinedByAddress(sqldb *sql.DB, coinbase string) string {
var arr blockRes
var blockArr string
sqlStatement := `SELECT * FROM blocks WHERE coinbase=$1`
rows, err := sqldb.Query(sqlStatement, coinbase)
if err != nil {
fmt.Println("err")
}
defer rows.Close()
for rows.Next() {
var hash, coinbase, age, parentHash, uncleHash, difficulty, size, rewards, num string
var gasUsed, gasLimit, nonce uint64
var txCount, uncleCount int
err = rows.Scan(
&hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num,)
arr.Blocks = append(arr.Blocks, 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,
Rewards: rewards,
Number: num,
})
blocks, _ := json.Marshal(arr.Blocks)
blocksFmt := string(blocks)
blockArr = blocksFmt
}
return blockArr
}
//GetAllTransactions getter fn for API
func SGetAllTransactions(sqldb *sql.DB) string {
var arr txRes
var txx string
rows, err := sqldb.Query(`SELECT * FROM txs`)
if err != nil {
fmt.Println("err")
}
defer rows.Close()
for rows.Next() {
var txhash, to_addr, from_addr, blockhash, blocknumber, amount, status string
var gasprice, gas, gasLimit, txfee, nonce uint64
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
}
//GetTransaction fn returns single tx
func SGetTransaction(sqldb *sql.DB, txHash string) string {
sqlStatement := `SELECT * FROM txs WHERE txhash=$1;`
row := sqldb.QueryRow(sqlStatement, txHash)
var txhash, to_addr, from_addr, blockhash, blocknumber, amount, status string
var gasprice, gas, gasLimit, txfee, nonce uint64
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,
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,
}
json, _ := json.Marshal(tx)
return string(json)
}
//GetAccount returns account balances
func SGetAccount(sqldb *sql.DB, address string) string {
sqlStatement := `SELECT * FROM accounts WHERE addr=$1;`
row := sqldb.QueryRow(sqlStatement, address)
var addr, balance, accountNonce string
row.Scan(
&addr, &balance, &accountNonce)
account := SAccounts{
Addr: addr,
Balance: balance,
AccountNonce: accountNonce,
}
json, _ := json.Marshal(account)
return string(json)
}
//GetAllAccounts returns all accounts and balances
func SGetAllAccounts(sqldb *sql.DB) string {
var array accountRes
var accountsArr, accountNonce string
accs, err := sqldb.Query(`
SELECT
addr,
balance,
accountNonce
FROM accounts`)
if err != nil {
fmt.Println(err)
}
defer accs.Close()
for accs.Next() {
var addr, balance string
err = accs.Scan(
&addr, &balance, &accountNonce,
)
array.AllAccounts = append(array.AllAccounts, SAccounts{
Addr: addr,
Balance: balance,
AccountNonce: accountNonce,
})
accounts, _ := json.Marshal(array.AllAccounts)
accountsFmt := string(accounts)
accountsArr = accountsFmt
}
return accountsArr
}
//GetAccount returns account balances
func SGetAccountTxs(sqldb *sql.DB, address string) string {
var arr txRes
var txx string
sqlStatement := `SELECT * FROM txs WHERE to_addr=$1 OR from_addr=$1;`
rows, err := sqldb.Query(sqlStatement, address)
if err != nil {
fmt.Println("err", err)
}
defer rows.Close()
for rows.Next() {
var txhash, to_addr, from_addr, blockhash, blocknumber, amount, status string
var gasprice, gas, gasLimit, txfee, nonce uint64
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
}

View file

@ -36,7 +36,7 @@ CREATE TABLE IF NOT EXISTS txs (
CREATE TABLE IF NOT EXISTS accounts (
addr text primary key unique,
balance numeric,
txCountAccount numeric
accountNonce numeric
);
CREATE TABLE IF NOT EXISTS contracts (

View file

@ -36,5 +36,5 @@ CREATE TABLE IF NOT EXISTS txs (
CREATE TABLE IF NOT EXISTS accounts (
addr text primary key unique,
balance numeric,
txCountAccount numeric
accountnonce numeric
);

View file

@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/consensus/ethash"
"strconv"
"fmt"
)
type ShyftTracer struct {}
@ -66,11 +67,12 @@ func TestBlock(t *testing.T) {
receipts := []*types.Receipt{receipt}
block := types.NewBlock(&types.Header{Number: big.NewInt(315)}, txs, nil, receipts)
fmt.Println("++++++HERE")
// Write and verify the block in the database
if err := core.SWriteBlock(block, receipts); err != nil {
t.Fatalf("Failed to write block into database: %v", err)
}
fmt.Println("++++++AND HERE")
sqldb, err := core.DBConnection()
if err != nil {
panic(err)
@ -467,8 +469,8 @@ t.Run("TestAccountsToReturnAccounts",func(t *testing.T) {
if tx.Value().String() != accountDataTo.Balance {
t.Fatalf("To address balance [%v]: To address balance not found", accountDataTo.Balance)
}
if strconv.FormatUint(tx.Nonce(), 10) != accountDataTo.TxCountAccount {
t.Fatalf("To account nonce [%v]: To account nonce not found", accountDataTo.TxCountAccount)
if strconv.FormatUint(tx.Nonce(), 10) != accountDataTo.AccountNonce {
t.Fatalf("To account nonce [%v]: To account nonce not found", accountDataTo.AccountNonce)
}
if getAllAccountTxs := core.SGetAccountTxs(sqldb, tx.To().String()); len(getAllAccountTxs) == 0 {
t.Fatalf("GetAccountTxs [%v]: GetAccountTxs did not return correctly", getAllAccountTxs)