changed start script for web socket access, modified writeBlock

This commit is contained in:
Dustin Brickwood 2018-05-02 15:14:59 -04:00
parent bd6acd8059
commit 1d93088980
4 changed files with 91 additions and 75 deletions

View file

@ -8,7 +8,7 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
shyftdb "github.com/ethereum/go-ethereum/shyftdb" "github.com/ethereum/go-ethereum/shyftdb"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -48,6 +48,7 @@ func GetAllTransactions(w http.ResponseWriter, r *http.Request) {
return return
} }
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View file

@ -94,8 +94,9 @@ func WriteBlock(sqldb *sql.DB, block *types.Block) error {
panic(qerr) panic(qerr)
} }
if block.Transactions().Len() > 0 { if block.Transactions().Len() > 0 && block.Transactions()[0].To() != nil {
for _, tx := range block.Transactions() { for _, tx := range block.Transactions() {
//WriteMinerRewards(sqldb, block)
WriteTransactions(sqldb, tx, block.Header().Hash()) WriteTransactions(sqldb, tx, block.Header().Hash())
WriteFromBalance(sqldb, tx) WriteFromBalance(sqldb, tx)
} }
@ -233,63 +234,77 @@ func WriteBalanceHelper(sqldb *sql.DB, tx *types.Transaction) (SendAndReceive, s
return sendAndReceiveData, balanceReceiver, balanceSender 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: // @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. // 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. // 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 reward if there are uncles
// @TODO: Calculate mining reward (most likely retrieve higher up in the operations) // @TODO: Calculate mining reward (most likely retrieve higher up in the operations)
// @TODO: Calculate reorg // @TODO: Calculate reorg
// func WriteMinerReward(db *leveldb.DB, block *types.Block) { //func WriteMinerReward(db *leveldb.DB, block *types.Block) {
// var totalGas *big.Int // var totalGas *big.Int
// var txs []string // var txs []string
// key := append([]byte("acc-")[:], block.Coinbase().Hash().Bytes()[:]...) // key := append([]byte("acc-")[:], block.Coinbase().Hash().Bytes()[:]...)
// for _, tx := range block.Transactions() { // for _, tx := range block.Transactions() {
// totalGas.Add(totalGas, new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas()))) // totalGas.Add(totalGas, new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas())))
// } // }
// retrievedData, err := db.Get(key, nil) //// retrievedData, err := db.Get(key, nil)
// if err != nil { // if err != nil {
// // Assume time this account has had a tx // // Assume time this account has had a tx
// // Balacne is exclusively minerreward + total gas from the block b/c no prior evm activity // // 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 // // Txs would be empty because they have not had any transactions on the EVM
// // @TODO: Calc mining reward // // @TODO: Calc mining reward
// //balance := totalGas.Add(totalGas, MINING_REWARD) // //balance := totalGas.Add(totalGas, MINING_REWARD)
// balance := totalGas // balance := totalGas
// accData := ShyftAccountEntry{ // accData := ShyftAccountEntry{
// Balance: balance, // Balance: balance,
// Txs: txs, // Txs: txs,
// } // }
// var encodedData bytes.Buffer // var encodedData bytes.Buffer
// encoder := gob.NewEncoder(&encodedData) // encoder := gob.NewEncoder(&encodedData)
// if err := encoder.Encode(accData); err != nil { // if err := encoder.Encode(accData); err != nil {
// log.Crit("Faild to encode Miner Account data", "err", err) // log.Crit("Faild to encode Miner Account data", "err", err)
// } // }
// if err := db.Put(key, encodedData.Bytes(), nil); err != nil { // if err := db.Put(key, encodedData.Bytes(), nil); err != nil {
// log.Crit("Could not write the miner's first tx", "err", err) // log.Crit("Could not write the miner's first tx", "err", err)
// } // }
// } else { // } else {
// // The account has already have previous data stored due to activity in the EVM // // The account has already have previous data stored due to activity in the EVM
// // Decode the data to update balance // // Decode the data to update balance
// var decodedData ShyftAccountEntry // var decodedData ShyftAccountEntry
// d := gob.NewDecoder(bytes.NewBuffer(retrievedData)) // d := gob.NewDecoder(bytes.NewBuffer(retrievedData))
// if err := d.Decode(&decodedData); err != nil { // if err := d.Decode(&decodedData); err != nil {
// log.Crit("Failed to decode miner data:", "err", err) // log.Crit("Failed to decode miner data:", "err", err)
// } // }
// // Write new balance // // Write new balance
// // @TODO: Calc mining reward // // @TODO: Calc mining reward
// // decodedData.Balance.Add(decodedData.Balance, totalGas.Add(totalGas, MINING_REWARD))) // // decodedData.Balance.Add(decodedData.Balance, totalGas.Add(totalGas, MINING_REWARD)))
// decodedData.Balance.Add(decodedData.Balance, totalGas) // decodedData.Balance.Add(decodedData.Balance, totalGas)
// // Encode the data to be written back to the db // // Encode the data to be written back to the db
// var encodedData bytes.Buffer // var encodedData bytes.Buffer
// encoder := gob.NewEncoder(&encodedData) // encoder := gob.NewEncoder(&encodedData)
// if err := encoder.Encode(decodedData); err != nil { // if err := encoder.Encode(decodedData); err != nil {
// log.Crit("Faild to encode Miner Account data", "err", err) // log.Crit("Faild to encode Miner Account data", "err", err)
// } // }
// // Write newly encoded data back to the db // // Write newly encoded data back to the db
// if err := db.Put(key, encodedData.Bytes(), nil); err != nil { // if err := db.Put(key, encodedData.Bytes(), nil); err != nil {
// log.Crit("Could not update miner account data", "err", err) // log.Crit("Could not update miner account data", "err", err)
// } // }
// } // }
// } //}
/////////// ///////////
// Getters // Getters
@ -410,7 +425,7 @@ func GetAllTransactions(sqldb *sql.DB) string {
//GetTransaction fn returns single tx //GetTransaction fn returns single tx
func GetTransaction(sqldb *sql.DB) string { func GetTransaction(sqldb *sql.DB) string {
sqlStatement := `SELECT sqlStatement := `SELECT
txhash, txhash,
to_addr, to_addr,
from_addr, from_addr,
@ -418,7 +433,7 @@ func GetTransaction(sqldb *sql.DB) string {
amount, amount,
gasprice, gasprice,
gas, gas,
nonce nonce
FROM txs WHERE nonce=$1;` FROM txs WHERE nonce=$1;`
row := sqldb.QueryRow(sqlStatement, 1) row := sqldb.QueryRow(sqlStatement, 1)
var txhash string var txhash string
@ -477,7 +492,7 @@ func GetAllAccounts(sqldb *sql.DB) string {
} }
defer accs.Close() defer accs.Close()
////
for accs.Next() { for accs.Next() {
var addr string var addr string
var balance string var balance string

View file

@ -2,28 +2,28 @@ 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 < 10; i++) { for (var i = 0; i < 1; 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[2], from: web3.eth.accounts[1],
to: web3.eth.accounts[0], to: web3.eth.accounts[2],
value: 5, value: 5,
gas: 50000, gas: 50000,
gasPrice: 20 gasPrice: 20
}); });
web3.eth.sendTransaction({ // web3.eth.sendTransaction({
from: web3.eth.accounts[1], // from: web3.eth.accounts[1],
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
}); // });
} }

View file

@ -1,2 +1,2 @@
#!/bin/sh #!/bin/sh
./build/bin/geth --config config.toml --nat=none --mine --minerthreads 4 --targetgaslimit 80000000 --unlock "0x43EC6d0942f7fAeF069F7F63D0384a27f529B062,0x9e602164C5826ebb5A6B68E4AFD9Cd466043dc4A,0x5Bd738164C61FB50eb12E227846CbaeF2dE965Aa,0xC04eE4131895F1d0C294D508AF65D94060AA42BB,0x07D899C4aC0c1725C35C5f816e60273B33a964F7" --password ./unlockPasswords.txt ./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