From 3acdd3f5c5fd73777fc41448771c38080abb6c58 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Mon, 6 Aug 2018 17:38:01 -0400 Subject: [PATCH] need to clean up updating account balances functions --- core/shyft_database_util.go | 12 ++++++------ shyftBlockExplorerApi/handler.go | 11 ++++++----- shyftDb/shyft_database_util_test.go | 4 ---- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index b430e04274..f9c4a01a75 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -147,7 +147,7 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error { for _, tx := range block.Transactions() { swriteTransactions(sqldb, tx, block.Header().Hash(), blockData.Number, receipts, age, blockData.GasLimit) if block.Transactions()[0].To() != nil { - swriteFromBalance(sqldb, tx) + swriteBalance(sqldb, tx) } if block.Transactions()[0].To() == nil { swriteContractBalance(sqldb, tx) @@ -247,8 +247,8 @@ func swriteContractBalance(sqldb *sql.DB, tx *types.Transaction) error { return nil } -//writeFromBalance writes senders balance to accounts db -func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error { +//writeBalance writes senders balance to accounts db +func swriteBalance(sqldb *sql.DB, tx *types.Transaction) error { sendAndReceiveData := SendAndReceive{ To: tx.To().Hex(), From: tx.From().Hex(), @@ -278,7 +278,6 @@ func adjustBalanceFromAddr(sqldb *sql.DB, s SendAndReceive, value *big.Int) { if err != nil { log.Fatal(err) } - var newBalanceSender, newAccountNonceSender big.Int var nonceIncrement = big.NewInt(1) @@ -335,13 +334,14 @@ func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amo From: fromAddr, Amount: amount, } - toAddressBalance, toAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.To) - + value := new(big.Int) + value, _ = value.SetString(amount, 10) switch { case err == sql.ErrNoRows: accountNonce := "1" CreateAccount(sqldb, sendAndReceiveData.To, sendAndReceiveData.Amount, accountNonce) + adjustBalanceFromAddr(sqldb, sendAndReceiveData, value) case err != nil: log.Fatal(err) default: diff --git a/shyftBlockExplorerApi/handler.go b/shyftBlockExplorerApi/handler.go index a6636f8e52..4f59f7675b 100644 --- a/shyftBlockExplorerApi/handler.go +++ b/shyftBlockExplorerApi/handler.go @@ -7,11 +7,12 @@ import ( _ "github.com/lib/pq" + "bytes" + "encoding/json" + "io/ioutil" + "github.com/ShyftNetwork/go-empyrean/core" "github.com/gorilla/mux" - "bytes" - "io/ioutil" - "encoding/json" ) // GetTransaction gets txs @@ -239,7 +240,7 @@ func BroadcastTx(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "ERROR parsing json") } tx_hash := dat["result"] - if(tx_hash == nil) { + if tx_hash == nil { errMap := dat["error"].(map[string]interface{}) w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) @@ -249,4 +250,4 @@ func BroadcastTx(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintln(w, "Transaction Hash:", tx_hash) } -} \ No newline at end of file +} diff --git a/shyftDb/shyft_database_util_test.go b/shyftDb/shyft_database_util_test.go index 5e7fd39ad9..d1ef772a4f 100644 --- a/shyftDb/shyft_database_util_test.go +++ b/shyftDb/shyft_database_util_test.go @@ -362,7 +362,3 @@ func TestBlock(t *testing.T) { } }) } - -//Genesis.go functions -// WriteBlockZero -// WriteShyftGen