From 1d9308898037ff14700dfb5b809823a50d6a7016 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Wed, 2 May 2018 15:14:59 -0400 Subject: [PATCH] changed start script for web socket access, modified writeBlock --- blockExplorerApi/handler.go | 3 +- shyftdb/shyft_database_util.go | 125 ++++++++++++++++++-------------- simulations/sendTransactions.js | 36 ++++----- startShyftGeth.sh | 2 +- 4 files changed, 91 insertions(+), 75 deletions(-) diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go index 7303aed137..1b5f3d8129 100644 --- a/blockExplorerApi/handler.go +++ b/blockExplorerApi/handler.go @@ -8,7 +8,7 @@ import ( _ "github.com/lib/pq" - shyftdb "github.com/ethereum/go-ethereum/shyftdb" + "github.com/ethereum/go-ethereum/shyftdb" "github.com/gorilla/mux" ) @@ -48,6 +48,7 @@ func GetAllTransactions(w http.ResponseWriter, r *http.Request) { return } + w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 03e7be7694..49c58cdc19 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -94,8 +94,9 @@ func WriteBlock(sqldb *sql.DB, block *types.Block) error { panic(qerr) } - if block.Transactions().Len() > 0 { + if block.Transactions().Len() > 0 && block.Transactions()[0].To() != nil { for _, tx := range block.Transactions() { + //WriteMinerRewards(sqldb, block) WriteTransactions(sqldb, tx, block.Header().Hash()) WriteFromBalance(sqldb, tx) } @@ -233,63 +234,77 @@ func WriteBalanceHelper(sqldb *sql.DB, tx *types.Transaction) (SendAndReceive, s return sendAndReceiveData, balanceReceiver, balanceSender } +//func WriteMinerRewards(sqldb *sql.DB, block *types.Block) { +// var totalGas big.Int +// //var txs []string +// +// fmt.Println("this is BLOCK.UNCLE", block.Uncles()) +// fmt.Println("this is BLOCK UNCLE HASH", block.UncleHash().String()) +// fmt.Println("this is BLOCK.TRANSACTIONS", block.Transactions()) +// fmt.Println("this is BLOCK TOTAL GAS", block.GasUsed()) +// +// for _, tx := range block.Transactions() { +// totalGas.Add(&totalGas, new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas()))) +// } +//} + // @NOTE: This function is extremely complex and requires heavy testing and knowdlege of edge cases: // uncle blocks, account balance updates based on reorgs, diverges that get dropped. // Reason for this is because the accounts are not deterministic like the block and tx hashes. // @TODO: Calculate reward if there are uncles // @TODO: Calculate mining reward (most likely retrieve higher up in the operations) // @TODO: Calculate reorg -// func WriteMinerReward(db *leveldb.DB, block *types.Block) { -// var totalGas *big.Int -// var txs []string -// key := append([]byte("acc-")[:], block.Coinbase().Hash().Bytes()[:]...) -// for _, tx := range block.Transactions() { -// totalGas.Add(totalGas, new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas()))) -// } -// retrievedData, err := db.Get(key, nil) -// if err != nil { -// // Assume time this account has had a tx -// // Balacne is exclusively minerreward + total gas from the block b/c no prior evm activity -// // Txs would be empty because they have not had any transactions on the EVM -// // @TODO: Calc mining reward -// //balance := totalGas.Add(totalGas, MINING_REWARD) -// balance := totalGas -// accData := ShyftAccountEntry{ -// Balance: balance, -// Txs: txs, -// } -// var encodedData bytes.Buffer -// encoder := gob.NewEncoder(&encodedData) -// if err := encoder.Encode(accData); err != nil { -// log.Crit("Faild to encode Miner Account data", "err", err) -// } -// if err := db.Put(key, encodedData.Bytes(), nil); err != nil { -// log.Crit("Could not write the miner's first tx", "err", err) -// } -// } else { -// // The account has already have previous data stored due to activity in the EVM -// // Decode the data to update balance -// var decodedData ShyftAccountEntry -// d := gob.NewDecoder(bytes.NewBuffer(retrievedData)) -// if err := d.Decode(&decodedData); err != nil { -// log.Crit("Failed to decode miner data:", "err", err) -// } -// // Write new balance -// // @TODO: Calc mining reward -// // decodedData.Balance.Add(decodedData.Balance, totalGas.Add(totalGas, MINING_REWARD))) -// decodedData.Balance.Add(decodedData.Balance, totalGas) -// // Encode the data to be written back to the db -// var encodedData bytes.Buffer -// encoder := gob.NewEncoder(&encodedData) -// if err := encoder.Encode(decodedData); err != nil { -// log.Crit("Faild to encode Miner Account data", "err", err) -// } -// // Write newly encoded data back to the db -// if err := db.Put(key, encodedData.Bytes(), nil); err != nil { -// log.Crit("Could not update miner account data", "err", err) -// } -// } -// } +//func WriteMinerReward(db *leveldb.DB, block *types.Block) { +// var totalGas *big.Int +// var txs []string +// key := append([]byte("acc-")[:], block.Coinbase().Hash().Bytes()[:]...) +// for _, tx := range block.Transactions() { +// totalGas.Add(totalGas, new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas()))) +// } +//// retrievedData, err := db.Get(key, nil) +// if err != nil { +// // Assume time this account has had a tx +// // Balacne is exclusively minerreward + total gas from the block b/c no prior evm activity +// // Txs would be empty because they have not had any transactions on the EVM +// // @TODO: Calc mining reward +// //balance := totalGas.Add(totalGas, MINING_REWARD) +// balance := totalGas +// accData := ShyftAccountEntry{ +// Balance: balance, +// Txs: txs, +// } +// var encodedData bytes.Buffer +// encoder := gob.NewEncoder(&encodedData) +// if err := encoder.Encode(accData); err != nil { +// log.Crit("Faild to encode Miner Account data", "err", err) +// } +// if err := db.Put(key, encodedData.Bytes(), nil); err != nil { +// log.Crit("Could not write the miner's first tx", "err", err) +// } +// } else { +// // The account has already have previous data stored due to activity in the EVM +// // Decode the data to update balance +// var decodedData ShyftAccountEntry +// d := gob.NewDecoder(bytes.NewBuffer(retrievedData)) +// if err := d.Decode(&decodedData); err != nil { +// log.Crit("Failed to decode miner data:", "err", err) +// } +// // Write new balance +// // @TODO: Calc mining reward +// // decodedData.Balance.Add(decodedData.Balance, totalGas.Add(totalGas, MINING_REWARD))) +// decodedData.Balance.Add(decodedData.Balance, totalGas) +// // Encode the data to be written back to the db +// var encodedData bytes.Buffer +// encoder := gob.NewEncoder(&encodedData) +// if err := encoder.Encode(decodedData); err != nil { +// log.Crit("Faild to encode Miner Account data", "err", err) +// } +// // Write newly encoded data back to the db +// if err := db.Put(key, encodedData.Bytes(), nil); err != nil { +// log.Crit("Could not update miner account data", "err", err) +// } +// } +//} /////////// // Getters @@ -410,7 +425,7 @@ func GetAllTransactions(sqldb *sql.DB) string { //GetTransaction fn returns single tx func GetTransaction(sqldb *sql.DB) string { - sqlStatement := `SELECT + sqlStatement := `SELECT txhash, to_addr, from_addr, @@ -418,7 +433,7 @@ func GetTransaction(sqldb *sql.DB) string { amount, gasprice, gas, - nonce + nonce FROM txs WHERE nonce=$1;` row := sqldb.QueryRow(sqlStatement, 1) var txhash string @@ -477,7 +492,7 @@ func GetAllAccounts(sqldb *sql.DB) string { } defer accs.Close() - //// + for accs.Next() { var addr string var balance string diff --git a/simulations/sendTransactions.js b/simulations/sendTransactions.js index 73aeba9e9e..c3a302d699 100644 --- a/simulations/sendTransactions.js +++ b/simulations/sendTransactions.js @@ -2,28 +2,28 @@ var firstAccount = web3.eth.accounts[0] var secondAccount = web3.eth.accounts[1] var thirdAccount = web3.eth.accounts[2] -for (var i = 0; i < 10; i++) { +for (var i = 0; i < 1; i++) { console.log('\t\t' + (i + 1) + ' - Transactions') web3.eth.sendTransaction({ - from: web3.eth.accounts[2], - to: web3.eth.accounts[0], + from: web3.eth.accounts[1], + to: web3.eth.accounts[2], value: 5, 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[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 + // }); } \ No newline at end of file diff --git a/startShyftGeth.sh b/startShyftGeth.sh index 7247e8e37a..9085f628d3 100755 --- a/startShyftGeth.sh +++ b/startShyftGeth.sh @@ -1,2 +1,2 @@ #!/bin/sh -./build/bin/geth --config config.toml --nat=none --mine --minerthreads 4 --targetgaslimit 80000000 --unlock "0x43EC6d0942f7fAeF069F7F63D0384a27f529B062,0x9e602164C5826ebb5A6B68E4AFD9Cd466043dc4A,0x5Bd738164C61FB50eb12E227846CbaeF2dE965Aa,0xC04eE4131895F1d0C294D508AF65D94060AA42BB,0x07D899C4aC0c1725C35C5f816e60273B33a964F7" --password ./unlockPasswords.txt \ No newline at end of file +./build/bin/geth --config config.toml --ws --wsaddr="0.0.0.0" --wsorigins "*" --nat=none --mine --minerthreads 4 --targetgaslimit 80000000 --unlock "0x43EC6d0942f7fAeF069F7F63D0384a27f529B062,0x9e602164C5826ebb5A6B68E4AFD9Cd466043dc4A,0x5Bd738164C61FB50eb12E227846CbaeF2dE965Aa,0xC04eE4131895F1d0C294D508AF65D94060AA42BB,0x07D899C4aC0c1725C35C5f816e60273B33a964F7" --password ./unlockPasswords.txt