unit tests for account balances wip

This commit is contained in:
Dustin Brickwood 2018-08-01 15:44:18 -04:00
parent d299d2baeb
commit 112c47bf29
2 changed files with 41 additions and 14 deletions

View file

@ -260,7 +260,7 @@ func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error {
case err == sql.ErrNoRows: case err == sql.ErrNoRows:
accountNonce := strconv.FormatUint(tx.Nonce(), 10) accountNonce := strconv.FormatUint(tx.Nonce(), 10)
CreateAccount(sqldb, sendAndReceiveData.To, sendAndReceiveData.Amount, accountNonce) CreateAccount(sqldb, sendAndReceiveData.To, sendAndReceiveData.Amount, accountNonce)
balanceHelper(sqldb, sendAndReceiveData, value) adjustBalanceFromAddr(sqldb, sendAndReceiveData, value)
case err != nil: case err != nil:
log.Fatal(err) log.Fatal(err)
default: default:
@ -269,6 +269,32 @@ func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error {
return nil return nil
} }
func adjustBalanceFromAddr(sqldb *sql.DB, s SendAndReceive, value *big.Int) {
fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From)
switch {
case err == sql.ErrNoRows:
accountNonce := "1"
//s.Amount should be 0?
CreateAccount(sqldb, s.From, s.Amount, accountNonce)
case err != nil:
log.Fatal(err)
default:
var newBalanceSender, newAccountNonceSender big.Int
var nonceIncrement = big.NewInt(1)
fromBalance := new(big.Int)
fromBalance, _ = fromBalance.SetString(fromAddressBalance, 10)
fromNonce := new(big.Int)
fromNonce, _ = fromNonce.SetString(fromAccountNonce, 10)
newBalanceSender.Sub(fromBalance, value)
newAccountNonceSender.Add(fromNonce, nonceIncrement)
UpdateAccount(sqldb, s.From, newBalanceSender.String(), newAccountNonceSender.String())
}
}
func balanceHelper(sqldb *sql.DB, s SendAndReceive, value *big.Int) { func balanceHelper(sqldb *sql.DB, s SendAndReceive, value *big.Int) {
fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From) fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From)
toAddressBalance, toAccountNonce, err := AccountExists(sqldb, s.To) toAddressBalance, toAccountNonce, err := AccountExists(sqldb, s.To)
@ -277,12 +303,12 @@ func balanceHelper(sqldb *sql.DB, s SendAndReceive, value *big.Int) {
} }
var newBalanceReceiver, newBalanceSender, newAccountNonceReceiver, newAccountNonceSender big.Int var newBalanceReceiver, newBalanceSender, newAccountNonceReceiver, newAccountNonceSender big.Int
var nonceIncrement = big.NewInt(1) var nonceIncrement = big.NewInt(1)
fmt.Println(toAddressBalance)
//STRING TO BIG INT //STRING TO BIG INT
//BALANCES TO AND FROM ADDR //BALANCES TO AND FROM ADDR
toBalance := new(big.Int) toBalance := new(big.Int)
toBalance, _ = toBalance.SetString(toAddressBalance, 10) toBalance, _ = toBalance.SetString(toAddressBalance, 10)
fmt.Println(toBalance)
fromBalance := new(big.Int) fromBalance := new(big.Int)
fromBalance, _ = fromBalance.SetString(fromAddressBalance, 10) fromBalance, _ = fromBalance.SetString(fromAddressBalance, 10)

View file

@ -2,7 +2,6 @@ package shyftdb
import ( import (
"encoding/json" "encoding/json"
"fmt"
"math/big" "math/big"
"strconv" "strconv"
"strings" "strings"
@ -68,7 +67,6 @@ func TestBlock(t *testing.T) {
receipts := []*types.Receipt{receipt} receipts := []*types.Receipt{receipt}
block := types.NewBlock(&types.Header{Number: big.NewInt(315)}, txs, nil, receipts) block := types.NewBlock(&types.Header{Number: big.NewInt(315)}, txs, nil, receipts)
// Write and verify the block in the database // Write and verify the block in the database
if err := core.SWriteBlock(block, receipts); err != nil { if err := core.SWriteBlock(block, receipts); err != nil {
t.Fatalf("Failed to write block into database: %v", err) t.Fatalf("Failed to write block into database: %v", err)
@ -126,7 +124,6 @@ func TestBlock(t *testing.T) {
if getAllBlocksMinedByAddress := core.SGetAllBlocksMinedByAddress(sqldb, block.Coinbase().String()); len(getAllBlocksMinedByAddress) == 0 { if getAllBlocksMinedByAddress := core.SGetAllBlocksMinedByAddress(sqldb, block.Coinbase().String()); len(getAllBlocksMinedByAddress) == 0 {
t.Fatalf("GetAllBlocksMinedByAddress [%v]: GetAllBlocksMinedByAddress did not return correctly", getAllBlocksMinedByAddress) t.Fatalf("GetAllBlocksMinedByAddress [%v]: GetAllBlocksMinedByAddress did not return correctly", getAllBlocksMinedByAddress)
} }
fmt.Println("passed")
core.ClearTables() core.ClearTables()
}) })
@ -215,7 +212,6 @@ func TestBlock(t *testing.T) {
if allTxsFromBlock := core.SGetAllTransactionsFromBlock(sqldb, block2.Number().String()); len(allTxsFromBlock) == 0 { if allTxsFromBlock := core.SGetAllTransactionsFromBlock(sqldb, block2.Number().String()); len(allTxsFromBlock) == 0 {
t.Fatalf("GetAllTransactionsFromBlock [%v]: GetAllTransactionsFromBlock did not return correctly", allTxsFromBlock) t.Fatalf("GetAllTransactionsFromBlock [%v]: GetAllTransactionsFromBlock did not return correctly", allTxsFromBlock)
} }
fmt.Println("Passed 2")
core.ClearTables() core.ClearTables()
}) })
@ -316,7 +312,6 @@ func TestBlock(t *testing.T) {
t.Fatalf("isContract [%v]: isContract bool is incorrect", isContract) t.Fatalf("isContract [%v]: isContract bool is incorrect", isContract)
} }
} }
fmt.Println("Passed 3")
core.ClearTables() core.ClearTables()
}) })
// //
@ -420,7 +415,6 @@ func TestBlock(t *testing.T) {
if getAllTx := core.SGetAllTransactions(sqldb); len(getAllTx) == 0 { if getAllTx := core.SGetAllTransactions(sqldb); len(getAllTx) == 0 {
t.Fatalf("GetAllTransactions [%v]: GetAllTransactions did not return correctly", getAllTx) t.Fatalf("GetAllTransactions [%v]: GetAllTransactions did not return correctly", getAllTx)
} }
fmt.Println("Passed 4")
core.ClearTables() core.ClearTables()
}) })
// //
@ -434,6 +428,9 @@ func TestBlock(t *testing.T) {
} }
fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" fromAddr := "0x71562b71999873db5b286df957af199ec94617f7"
fromAddrEndBalance := "35"
fromAddrEndNonce := "4"
core.CreateAccount(sqldb, fromAddr, "50", "1") core.CreateAccount(sqldb, fromAddr, "50", "1")
toAddr := common.BytesToAddress([]byte{0x11}) toAddr := common.BytesToAddress([]byte{0x11})
@ -486,19 +483,23 @@ func TestBlock(t *testing.T) {
var accountDataFrom core.SAccounts var accountDataFrom core.SAccounts
json.Unmarshal(byts, &accountDataFrom) json.Unmarshal(byts, &accountDataFrom)
fmt.Println("FROM", accountDataFrom) if fromAddr != accountDataFrom.Addr {
t.Fatalf("To address [%v]: To address not found", accountDataFrom.Addr)
}
if fromAddrEndBalance != accountDataFrom.Balance {
t.Fatalf("To address balance [%v]: To address balance not found", accountDataFrom.Balance)
}
if fromAddrEndNonce != accountDataFrom.AccountNonce {
t.Fatalf("To account nonce [%v]: To account nonce not found", accountDataFrom.AccountNonce)
}
if getAllAccountTxs := core.SGetAccountTxs(sqldb, toAddr.String()); len(getAllAccountTxs) == 0 { if getAllAccountTxs := core.SGetAccountTxs(sqldb, toAddr.String()); len(getAllAccountTxs) == 0 {
t.Fatalf("GetAccountTxs [%v]: GetAccountTxs did not return correctly", getAllAccountTxs) t.Fatalf("GetAccountTxs [%v]: GetAccountTxs did not return correctly", getAllAccountTxs)
} }
if getAllAccounts := core.SGetAllAccounts(sqldb); len(getAllAccounts) == 0 { if getAllAccounts := core.SGetAllAccounts(sqldb); len(getAllAccounts) == 0 {
t.Fatalf("GetAllAccounts [%v]: GetAllAccounts did not return correctly", getAllAccounts) t.Fatalf("GetAllAccounts [%v]: GetAllAccounts did not return correctly", getAllAccounts)
} }
fmt.Println("Passed 5")
core.ClearTables() core.ClearTables()
}) })
core.ClearTables() core.ClearTables()
} }