From 3f0cf00b656b2e1428fab548f2eb3b615a4ee2a5 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Thu, 10 May 2018 15:03:49 -0400 Subject: [PATCH 1/4] added missing block fields and modified blocks table --- shyftDb/postgres_setup/create_tables.psql | 6 +++++- shyftdb/shyft_database_util.go | 13 ++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/shyftDb/postgres_setup/create_tables.psql b/shyftDb/postgres_setup/create_tables.psql index 16506fa497..d2be84603b 100644 --- a/shyftDb/postgres_setup/create_tables.psql +++ b/shyftDb/postgres_setup/create_tables.psql @@ -1,6 +1,10 @@ CREATE TABLE IF NOT EXISTS blocks ( hash text primary key, - coinbase text, + coinbase text, + gasUsed numeric, + gasLimit numeric, + txCount numeric, + uncleCount numeric, number bigint ); diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 13ee5e2252..4ee853e39e 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -87,8 +87,15 @@ type SendAndReceive struct { func WriteBlock(sqldb *sql.DB, block *types.Block) error { coinbase := block.Header().Coinbase.String() number := block.Header().Number.String() - sqlStatement := `INSERT INTO blocks(hash, coinbase, number) VALUES(($1), ($2), ($3)) RETURNING number` - qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number).Scan(&number) + gasUsed := block.Header().GasUsed + gasLimit := block.Header().GasLimit + txCount := block.Transactions().Len() + uncleCount := len(block.Uncles()) + age := block.Time() + fmt.Println("TIMESTAMP+++++++", age) + + sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txcount, uncleCount) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7)) RETURNING number` + qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number, gasUsed, gasLimit, txCount, uncleCount).Scan(&number) if qerr != nil { panic(qerr) } @@ -126,7 +133,7 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha gas := txData.Gas data := txData.Data to := txData.To - if(to == nil){ + if to == nil{ var retNonce string sqlStatement := `INSERT INTO txs(txhash, from_addr, blockhash, amount, gasprice, gas, nonce, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8)) RETURNING nonce` qerr := sqldb.QueryRow(sqlStatement, txHash, from, blockHasher, amount, gasPrice, gas, nonce, data).Scan(&retNonce) From 361841e460122679fb93b9346758969083bbb7f7 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Thu, 10 May 2018 15:19:24 -0400 Subject: [PATCH 2/4] added timestamp of blocks to postgres db --- shyftDb/postgres_setup/create_tables.psql | 1 + shyftdb/shyft_database_util.go | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/shyftDb/postgres_setup/create_tables.psql b/shyftDb/postgres_setup/create_tables.psql index d2be84603b..e80c608779 100644 --- a/shyftDb/postgres_setup/create_tables.psql +++ b/shyftDb/postgres_setup/create_tables.psql @@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS blocks ( gasLimit numeric, txCount numeric, uncleCount numeric, + age timestamp, number bigint ); diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 4ee853e39e..a99596c802 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -6,8 +6,8 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - - + "time" + "strconv" "database/sql" "log" @@ -91,11 +91,16 @@ func WriteBlock(sqldb *sql.DB, block *types.Block) error { gasLimit := block.Header().GasLimit txCount := block.Transactions().Len() uncleCount := len(block.Uncles()) - age := block.Time() - fmt.Println("TIMESTAMP+++++++", age) - - sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txcount, uncleCount) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7)) RETURNING number` - qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number, gasUsed, gasLimit, txCount, uncleCount).Scan(&number) + + i, err := strconv.ParseInt(block.Time().String(), 10, 64) + if err != nil { + panic(err) + } + age := time.Unix(i, 0) + fmt.Println("TIME++++++++++++++", age) + + sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txcount, uncleCount, age) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8)) RETURNING number` + qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number, gasUsed, gasLimit, txCount, uncleCount, age).Scan(&number) if qerr != nil { panic(qerr) } From 2e39356a71b90f8b870d6f093d1dccb7145e22d6 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Thu, 10 May 2018 16:55:02 -0400 Subject: [PATCH 3/4] updated GetBlock handlers for API --- shyftdb/shyft_database_util.go | 50 ++++++++++++++++++++++++++++++--- simulations/sendTransactions.js | 32 ++++++++++----------- 2 files changed, 62 insertions(+), 20 deletions(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index a99596c802..18b4da9f5a 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -20,6 +20,11 @@ type SBlock struct { Hash string Coinbase string Number string + GasUsed string + GasLimit string + TxCount string + UncleCount string + Age string } //blockRes struct @@ -97,9 +102,8 @@ func WriteBlock(sqldb *sql.DB, block *types.Block) error { panic(err) } age := time.Unix(i, 0) - fmt.Println("TIME++++++++++++++", age) - sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txcount, uncleCount, age) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8)) RETURNING number` + sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txCount, uncleCount, age) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8)) RETURNING number` qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number, gasUsed, gasLimit, txCount, uncleCount, age).Scan(&number) if qerr != nil { panic(qerr) @@ -325,11 +329,18 @@ func WriteBalanceHelper(sqldb *sql.DB, tx *types.Transaction) (SendAndReceive, s func GetAllBlocks(sqldb *sql.DB) string { var arr blockRes var blockArr string + rows, err := sqldb.Query(` SELECT number, hash, - coinbase + coinbase, + gasused, + gaslimit, + txcount, + unclecount, + age + FROM blocks`) if err != nil { fmt.Println("err") @@ -340,16 +351,32 @@ func GetAllBlocks(sqldb *sql.DB) string { var num string var hash string var coinbase string + var gasUsed string + var gasLimit string + var txCount string + var uncleCount string + var age string + err = rows.Scan( &num, &hash, &coinbase, + &gasUsed, + &gasLimit, + &txCount, + &uncleCount, + &age, ) arr.Blocks = append(arr.Blocks, SBlock{ Hash: hash, Number: num, Coinbase: coinbase, + GasUsed: gasUsed, + GasLimit: gasLimit, + TxCount: txCount, + UncleCount: uncleCount, + Age: age, }) blocks, _ := json.Marshal(arr.Blocks) @@ -367,12 +394,27 @@ func GetBlock(sqldb *sql.DB) string { var num string var hash string var coinbase string - row.Scan(&num, &hash, &coinbase) + var gasUsed string + var gasLimit string + var txCount string + var uncleCount string + var age string + + row.Scan(&num, &hash, &coinbase, &gasUsed, + &gasLimit, + &txCount, + &uncleCount, + &age,) block := SBlock{ Hash: hash, Number: num, Coinbase: coinbase, + GasUsed: gasUsed, + GasLimit: gasLimit, + TxCount: txCount, + UncleCount: uncleCount, + Age: age, } json, _ := json.Marshal(block) return string(json) diff --git a/simulations/sendTransactions.js b/simulations/sendTransactions.js index c3a302d699..b338e7ba94 100644 --- a/simulations/sendTransactions.js +++ b/simulations/sendTransactions.js @@ -2,7 +2,7 @@ var firstAccount = web3.eth.accounts[0] var secondAccount = web3.eth.accounts[1] var thirdAccount = web3.eth.accounts[2] -for (var i = 0; i < 1; i++) { +for (var i = 0; i < 10; i++) { console.log('\t\t' + (i + 1) + ' - Transactions') web3.eth.sendTransaction({ from: web3.eth.accounts[1], @@ -11,19 +11,19 @@ for (var i = 0; i < 1; i++) { gas: 50000, gasPrice: 20 }); - // web3.eth.sendTransaction({ - // from: web3.eth.accounts[1], - // to: web3.eth.accounts[2], - // value: 291, - // gas: 50000, - // gasPrice: 20 - // }); - // - // web3.eth.sendTransaction({ - // from: web3.eth.accounts[0], - // to: web3.eth.accounts[1], - // value: 53039, - // gas: 50000, - // gasPrice: 20 - // }); + web3.eth.sendTransaction({ + from: web3.eth.accounts[0], + to: web3.eth.accounts[2], + value: 291, + gas: 50000, + gasPrice: 20 + }); + + web3.eth.sendTransaction({ + from: web3.eth.accounts[0], + to: web3.eth.accounts[1], + value: 53039, + gas: 50000, + gasPrice: 20 + }); } \ No newline at end of file From 7f70a3e7693d9d7fb210f747ef73e23f68c392fa Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Fri, 11 May 2018 10:51:10 -0400 Subject: [PATCH 4/4] fixed issues as per merge comments --- shyftdb/shyft_database_util.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 18b4da9f5a..8525204936 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -142,7 +142,7 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha gas := txData.Gas data := txData.Data to := txData.To - if to == nil{ + if (to == nil){ var retNonce string sqlStatement := `INSERT INTO txs(txhash, from_addr, blockhash, amount, gasprice, gas, nonce, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8)) RETURNING nonce` qerr := sqldb.QueryRow(sqlStatement, txHash, from, blockHasher, amount, gasPrice, gas, nonce, data).Scan(&retNonce) @@ -329,7 +329,6 @@ func WriteBalanceHelper(sqldb *sql.DB, tx *types.Transaction) (SendAndReceive, s func GetAllBlocks(sqldb *sql.DB) string { var arr blockRes var blockArr string - rows, err := sqldb.Query(` SELECT number, @@ -340,7 +339,6 @@ func GetAllBlocks(sqldb *sql.DB) string { txcount, unclecount, age - FROM blocks`) if err != nil { fmt.Println("err") @@ -400,7 +398,11 @@ func GetBlock(sqldb *sql.DB) string { var uncleCount string var age string - row.Scan(&num, &hash, &coinbase, &gasUsed, + row.Scan( + &num, + &hash, + &coinbase, + &gasUsed, &gasLimit, &txCount, &uncleCount,