Merge pull request #22 from ShyftNetwork/writeGenesis

WriteBlock and GetBlock Function Modification
This commit is contained in:
greg 2018-05-11 10:54:21 -04:00 committed by GitHub
commit 5c59ac75a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 24 deletions

View file

@ -1,6 +1,11 @@
CREATE TABLE IF NOT EXISTS blocks ( CREATE TABLE IF NOT EXISTS blocks (
hash text primary key, hash text primary key,
coinbase text, coinbase text,
gasUsed numeric,
gasLimit numeric,
txCount numeric,
uncleCount numeric,
age timestamp,
number bigint number bigint
); );

View file

@ -6,8 +6,8 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"time"
"strconv"
"database/sql" "database/sql"
"log" "log"
@ -20,6 +20,11 @@ type SBlock struct {
Hash string Hash string
Coinbase string Coinbase string
Number string Number string
GasUsed string
GasLimit string
TxCount string
UncleCount string
Age string
} }
//blockRes struct //blockRes struct
@ -87,8 +92,19 @@ type SendAndReceive struct {
func WriteBlock(sqldb *sql.DB, block *types.Block) error { func WriteBlock(sqldb *sql.DB, block *types.Block) error {
coinbase := block.Header().Coinbase.String() coinbase := block.Header().Coinbase.String()
number := block.Header().Number.String() number := block.Header().Number.String()
sqlStatement := `INSERT INTO blocks(hash, coinbase, number) VALUES(($1), ($2), ($3)) RETURNING number` gasUsed := block.Header().GasUsed
qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number).Scan(&number) gasLimit := block.Header().GasLimit
txCount := block.Transactions().Len()
uncleCount := len(block.Uncles())
i, err := strconv.ParseInt(block.Time().String(), 10, 64)
if err != nil {
panic(err)
}
age := time.Unix(i, 0)
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 { if qerr != nil {
panic(qerr) panic(qerr)
} }
@ -126,7 +142,7 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha
gas := txData.Gas gas := txData.Gas
data := txData.Data data := txData.Data
to := txData.To to := txData.To
if(to == nil){ if (to == nil){
var retNonce string 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` 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) qerr := sqldb.QueryRow(sqlStatement, txHash, from, blockHasher, amount, gasPrice, gas, nonce, data).Scan(&retNonce)
@ -317,7 +333,12 @@ func GetAllBlocks(sqldb *sql.DB) string {
SELECT SELECT
number, number,
hash, hash,
coinbase coinbase,
gasused,
gaslimit,
txcount,
unclecount,
age
FROM blocks`) FROM blocks`)
if err != nil { if err != nil {
fmt.Println("err") fmt.Println("err")
@ -328,16 +349,32 @@ func GetAllBlocks(sqldb *sql.DB) string {
var num string var num string
var hash string var hash string
var coinbase string var coinbase string
var gasUsed string
var gasLimit string
var txCount string
var uncleCount string
var age string
err = rows.Scan( err = rows.Scan(
&num, &num,
&hash, &hash,
&coinbase, &coinbase,
&gasUsed,
&gasLimit,
&txCount,
&uncleCount,
&age,
) )
arr.Blocks = append(arr.Blocks, SBlock{ arr.Blocks = append(arr.Blocks, SBlock{
Hash: hash, Hash: hash,
Number: num, Number: num,
Coinbase: coinbase, Coinbase: coinbase,
GasUsed: gasUsed,
GasLimit: gasLimit,
TxCount: txCount,
UncleCount: uncleCount,
Age: age,
}) })
blocks, _ := json.Marshal(arr.Blocks) blocks, _ := json.Marshal(arr.Blocks)
@ -355,12 +392,31 @@ func GetBlock(sqldb *sql.DB) string {
var num string var num string
var hash string var hash string
var coinbase 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{ block := SBlock{
Hash: hash, Hash: hash,
Number: num, Number: num,
Coinbase: coinbase, Coinbase: coinbase,
GasUsed: gasUsed,
GasLimit: gasLimit,
TxCount: txCount,
UncleCount: uncleCount,
Age: age,
} }
json, _ := json.Marshal(block) json, _ := json.Marshal(block)
return string(json) return string(json)

View file

@ -2,7 +2,7 @@ var firstAccount = web3.eth.accounts[0]
var secondAccount = web3.eth.accounts[1] var secondAccount = web3.eth.accounts[1]
var thirdAccount = web3.eth.accounts[2] 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') console.log('\t\t' + (i + 1) + ' - Transactions')
web3.eth.sendTransaction({ web3.eth.sendTransaction({
from: web3.eth.accounts[1], from: web3.eth.accounts[1],
@ -11,19 +11,19 @@ for (var i = 0; i < 1; i++) {
gas: 50000, gas: 50000,
gasPrice: 20 gasPrice: 20
}); });
// web3.eth.sendTransaction({ web3.eth.sendTransaction({
// from: web3.eth.accounts[1], from: web3.eth.accounts[0],
// to: web3.eth.accounts[2], to: web3.eth.accounts[2],
// value: 291, value: 291,
// gas: 50000, gas: 50000,
// gasPrice: 20 gasPrice: 20
// }); });
//
// web3.eth.sendTransaction({ web3.eth.sendTransaction({
// from: web3.eth.accounts[0], from: web3.eth.accounts[0],
// to: web3.eth.accounts[1], to: web3.eth.accounts[1],
// value: 53039, value: 53039,
// gas: 50000, gas: 50000,
// gasPrice: 20 gasPrice: 20
// }); });
} }