unit testing for account balances

This commit is contained in:
Dustin Brickwood 2018-08-01 15:17:38 -04:00
parent f066a8e461
commit d299d2baeb
3 changed files with 439 additions and 419 deletions

View file

@ -37,11 +37,36 @@ func InitDBTest() (*sql.DB, error){
}
func DBConnection() (*sql.DB, error) {
if (blockExplorerDb == nil) {
if blockExplorerDb == nil {
_, err := InitDB()
if(err != nil) {
if err != nil {
return nil, err
}
}
return blockExplorerDb, nil
}
func ClearTables() {
sqldb, err := DBConnection()
if err != nil {
panic(err)
}
sqlStatementTx:= `DELETE FROM txs`
_, err = sqldb.Exec(sqlStatementTx)
if err != nil {
panic(err)
}
sqlStatementAcc:= `DELETE FROM accounts`
_, err = sqldb.Exec(sqlStatementAcc)
if err != nil {
panic(err)
}
sqlStatement := `DELETE FROM blocks`
_, err = sqldb.Exec(sqlStatement)
if err != nil {
panic(err)
}
}

View file

@ -1,18 +1,19 @@
package core
import (
"math/big"
"time"
"strconv"
"database/sql"
"log"
_ "github.com/lib/pq"
"github.com/ShyftNetwork/go-empyrean/common"
"github.com/ShyftNetwork/go-empyrean/core/types"
Rewards "github.com/ShyftNetwork/go-empyrean/consensus/ethash"
"github.com/ShyftNetwork/go-empyrean/shyfttracerinterface"
"strings"
"fmt"
"log"
"math/big"
"strconv"
"strings"
"time"
"github.com/ShyftNetwork/go-empyrean/common"
Rewards "github.com/ShyftNetwork/go-empyrean/consensus/ethash"
"github.com/ShyftNetwork/go-empyrean/core/types"
"github.com/ShyftNetwork/go-empyrean/shyfttracerinterface"
_ "github.com/lib/pq"
)
var IShyftTracer shyfttracerinterface.IShyftTracer
@ -23,22 +24,22 @@ func SetIShyftTracer(st shyfttracerinterface.IShyftTracer) {
//SBlock type
type SBlock struct {
Hash string
Coinbase string
AgeGet string
Age time.Time
ParentHash string
UncleHash string
Difficulty string
Size string
Rewards string
Number string
GasUsed uint64
GasLimit uint64
Nonce uint64
TxCount int
UncleCount int
Blocks []SBlock
Hash string
Coinbase string
AgeGet string
Age time.Time
ParentHash string
UncleHash string
Difficulty string
Size string
Rewards string
Number string
GasUsed uint64
GasLimit uint64
Nonce uint64
TxCount int
UncleCount int
Blocks []SBlock
}
type InteralWrite struct {
@ -63,9 +64,9 @@ type blockRes struct {
}
type SAccounts struct {
Addr string
Balance string
AccountNonce string
Addr string
Balance string
AccountNonce string
}
type accountRes struct {
@ -79,30 +80,30 @@ type txRes struct {
}
type ShyftTxEntryPretty struct {
TxHash string
To *common.Address
ToGet string
From string
BlockHash string
BlockNumber string
Amount string
GasPrice uint64
Gas uint64
GasLimit uint64
Cost uint64
Nonce uint64
Status string
IsContract bool
Age time.Time
Data []byte
TxHash string
To *common.Address
ToGet string
From string
BlockHash string
BlockNumber string
Amount string
GasPrice uint64
Gas uint64
GasLimit uint64
Cost uint64
Nonce uint64
Status string
IsContract bool
Age time.Time
Data []byte
}
type SendAndReceive struct {
To string
From string
Amount string
Address string
Balance string
To string
From string
Amount string
Address string
Balance string
AccountNonce uint64 `json:",string"`
}
@ -114,7 +115,7 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error {
}
//Get miner rewards
rewards := swriteMinerRewards(sqldb,block)
rewards := swriteMinerRewards(sqldb, block)
//Format block time to be stored
i, err := strconv.ParseInt(block.Time().String(), 10, 64)
if err != nil {
@ -123,20 +124,20 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error {
age := time.Unix(i, 0)
blockData := SBlock{
Hash: block.Header().Hash().Hex(),
Coinbase: block.Header().Coinbase.String(),
Number: block.Header().Number.String(),
GasUsed: block.Header().GasUsed,
GasLimit: block.Header().GasLimit,
TxCount: block.Transactions().Len(),
UncleCount: len(block.Uncles()),
ParentHash: block.ParentHash().String(),
UncleHash: block.UncleHash().String(),
Difficulty: block.Difficulty().String(),
Size: block.Size().String(),
Nonce: block.Nonce(),
Rewards: rewards,
Age: age,
Hash: block.Header().Hash().Hex(),
Coinbase: block.Header().Coinbase.String(),
Number: block.Header().Number.String(),
GasUsed: block.Header().GasUsed,
GasLimit: block.Header().GasLimit,
TxCount: block.Transactions().Len(),
UncleCount: len(block.Uncles()),
ParentHash: block.ParentHash().String(),
UncleHash: block.UncleHash().String(),
Difficulty: block.Difficulty().String(),
Size: block.Size().String(),
Nonce: block.Nonce(),
Rewards: rewards,
Age: age,
}
//Inserts block data into DB
@ -157,7 +158,7 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error {
}
//swriteTransactions writes to sqldb, a SHYFT postgres instance
func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Hash, blockNumber string, receipts []*types.Receipt, age time.Time, gasLimit uint64) error {
func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Hash, blockNumber string, receipts []*types.Receipt, age time.Time, gasLimit uint64) error {
var isContract bool
var statusFromReciept string
var toAddr *common.Address
@ -191,21 +192,21 @@ func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.H
}
txData := ShyftTxEntryPretty{
TxHash: tx.Hash().Hex(),
From: tx.From().Hex(),
To: toAddr,
BlockHash: blockHash.Hex(),
TxHash: tx.Hash().Hex(),
From: tx.From().Hex(),
To: toAddr,
BlockHash: blockHash.Hex(),
BlockNumber: blockNumber,
Amount: tx.Value().String(),
Cost: tx.Cost().Uint64(),
GasPrice: tx.GasPrice().Uint64(),
GasLimit: gasLimit,
Gas: tx.Gas(),
Nonce: tx.Nonce(),
Age: age,
Data: tx.Data(),
Status: statusFromReciept,
IsContract: isContract,
Amount: tx.Value().String(),
Cost: tx.Cost().Uint64(),
GasPrice: tx.GasPrice().Uint64(),
GasLimit: gasLimit,
Gas: tx.Gas(),
Nonce: tx.Nonce(),
Age: age,
Data: tx.Data(),
Status: statusFromReciept,
IsContract: isContract,
}
//Inserts Tx into DB
InsertTx(sqldb, txData)
@ -217,9 +218,9 @@ func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.H
func swriteContractBalance(sqldb *sql.DB, tx *types.Transaction) error {
sendAndReceiveData := SendAndReceive{
From: tx.From().Hex(),
Amount: tx.Value().String(),
AccountNonce: tx.Nonce(),
From: tx.From().Hex(),
Amount: tx.Value().String(),
AccountNonce: tx.Nonce(),
}
fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.From)
@ -229,7 +230,7 @@ func swriteContractBalance(sqldb *sql.DB, tx *types.Transaction) error {
accountNonce := strconv.FormatUint(tx.Nonce(), 10)
CreateAccount(sqldb, sendAndReceiveData.From, sendAndReceiveData.Amount, accountNonce)
default:
var newBalanceSender,newAccountNonceSender big.Int
var newBalanceSender, newAccountNonceSender big.Int
var nonceIncrement = big.NewInt(1)
fromBalance := new(big.Int)
@ -249,57 +250,64 @@ func swriteContractBalance(sqldb *sql.DB, tx *types.Transaction) error {
//writeFromBalance writes senders balance to accounts db
func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error {
sendAndReceiveData := SendAndReceive{
To: tx.To().Hex(),
From: tx.From().Hex(),
To: tx.To().Hex(),
From: tx.From().Hex(),
Amount: tx.Value().String(),
}
toAddressBalance, toAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.To)
value := tx.Value()
_, _, err := AccountExists(sqldb, sendAndReceiveData.To)
switch {
case err == sql.ErrNoRows:
accountNonce := strconv.FormatUint(tx.Nonce(), 10)
CreateAccount(sqldb, sendAndReceiveData.To, sendAndReceiveData.Amount, accountNonce)
balanceHelper(sqldb, sendAndReceiveData, value)
case err != nil:
log.Fatal(err)
default:
fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.From)
if err != nil {
log.Fatal(err)
}
var newBalanceReceiver, newBalanceSender, newAccountNonceReceiver, newAccountNonceSender big.Int
var nonceIncrement = big.NewInt(1)
//STRING TO BIG INT
//BALANCES TO AND FROM ADDR
toBalance := new(big.Int)
toBalance, _ = toBalance.SetString(toAddressBalance, 10)
fromBalance := new(big.Int)
fromBalance, _ = fromBalance.SetString(fromAddressBalance, 10)
//ACCOUNT NONCES
toNonce := new(big.Int)
toNonce, _ = toNonce.SetString(toAccountNonce, 10)
fromNonce := new(big.Int)
fromNonce, _ = fromNonce.SetString(fromAccountNonce, 10)
newBalanceReceiver.Add(toBalance, tx.Value())
newBalanceSender.Sub(fromBalance, tx.Value())
newAccountNonceReceiver.Add(toNonce, nonceIncrement)
newAccountNonceSender.Add(fromNonce, nonceIncrement)
//UPDATE ACCOUNTS BASED ON NEW BALANCES AND ACCOUNT NONCES
UpdateAccount(sqldb, sendAndReceiveData.To, newBalanceReceiver.String(), newAccountNonceReceiver.String())
UpdateAccount(sqldb, sendAndReceiveData.From, newBalanceSender.String(), newAccountNonceSender.String())
balanceHelper(sqldb, sendAndReceiveData, value)
}
return nil
}
func balanceHelper(sqldb *sql.DB, s SendAndReceive, value *big.Int) {
fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From)
toAddressBalance, toAccountNonce, err := AccountExists(sqldb, s.To)
if err != nil {
log.Fatal(err)
}
var newBalanceReceiver, newBalanceSender, newAccountNonceReceiver, newAccountNonceSender big.Int
var nonceIncrement = big.NewInt(1)
fmt.Println(toAddressBalance)
//STRING TO BIG INT
//BALANCES TO AND FROM ADDR
toBalance := new(big.Int)
toBalance, _ = toBalance.SetString(toAddressBalance, 10)
fmt.Println(toBalance)
fromBalance := new(big.Int)
fromBalance, _ = fromBalance.SetString(fromAddressBalance, 10)
//ACCOUNT NONCES
toNonce := new(big.Int)
toNonce, _ = toNonce.SetString(toAccountNonce, 10)
fromNonce := new(big.Int)
fromNonce, _ = fromNonce.SetString(fromAccountNonce, 10)
newBalanceReceiver.Add(toBalance, value)
newBalanceSender.Sub(fromBalance, value)
newAccountNonceReceiver.Add(toNonce, nonceIncrement)
newAccountNonceSender.Add(fromNonce, nonceIncrement)
//UPDATE ACCOUNTS BASED ON NEW BALANCES AND ACCOUNT NONCES
UpdateAccount(sqldb, s.To, newBalanceReceiver.String(), newAccountNonceReceiver.String())
UpdateAccount(sqldb, s.From, newBalanceSender.String(), newAccountNonceSender.String())
}
//SWriteInternalTxBalances Writes internal txs and updates balances
func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amount string) error {
sendAndReceiveData := SendAndReceive{
To: toAddr,
From: fromAddr,
To: toAddr,
From: fromAddr,
Amount: amount,
}
@ -316,7 +324,7 @@ func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amo
if err != nil {
log.Fatal(err)
}
var newBalanceReceiver, newBalanceSender, newAccountNonceReceiver, newAccountNonceSender big.Int
var newBalanceReceiver, newBalanceSender, newAccountNonceReceiver, newAccountNonceSender big.Int
var nonceIncrement = big.NewInt(1)
//STRING TO BIG INT
@ -341,8 +349,8 @@ func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amo
newAccountNonceSender.Add(fromNonce, nonceIncrement)
//UPDATE ACCOUNTS BASED ON NEW BALANCES AND ACCOUNT NONCES
UpdateAccount(sqldb, sendAndReceiveData.To, newBalanceReceiver.String(), newAccountNonceReceiver.String())
UpdateAccount(sqldb, sendAndReceiveData.From, newBalanceSender.String(), newAccountNonceSender.String())
UpdateAccount(sqldb, sendAndReceiveData.To, newBalanceReceiver.String(), newAccountNonceReceiver.String())
UpdateAccount(sqldb, sendAndReceiveData.From, newBalanceSender.String(), newAccountNonceSender.String())
}
return nil
}
@ -366,7 +374,7 @@ func swriteMinerRewards(sqldb *sql.DB, block *types.Block) string {
// https://ethereum.stackexchange.com/questions/27172/different-uncles-reward
// line 551 in consensus.go (shyft_go-ethereum/consensus/ethash/consensus.go)
// Some weird constants to avoid constant memory allocs for them.
var big8 = big.NewInt(8)
var big8 = big.NewInt(8)
var uncleRewards []*big.Int
var uncleAddrs []string
@ -435,7 +443,7 @@ func sstoreReward(sqldb *sql.DB, address string, reward *big.Int) {
///////////////////////
//DB Utility functions
//////////////////////
func CreateAccount (sqldb *sql.DB, addr string, balance string, accountNonce string) {
func CreateAccount(sqldb *sql.DB, addr string, balance string, accountNonce string) {
sqlStatement := `INSERT INTO accounts(addr, balance, accountNonce) VALUES(($1), ($2), ($3)) RETURNING addr`
insertErr := sqldb.QueryRow(sqlStatement, strings.ToLower(addr), balance, accountNonce).Scan(&addr)
if insertErr != nil {
@ -443,7 +451,7 @@ func CreateAccount (sqldb *sql.DB, addr string, balance string, accountNonce str
}
}
func AccountExists (sqldb *sql.DB, addr string) (string, string, error) {
func AccountExists(sqldb *sql.DB, addr string) (string, string, error) {
var addressBalance, accountNonce string
sqlExistsStatement := `SELECT balance, accountNonce from accounts WHERE addr = ($1)`
err := sqldb.QueryRow(sqlExistsStatement, strings.ToLower(addr)).Scan(&addressBalance, &accountNonce)
@ -457,7 +465,7 @@ func AccountExists (sqldb *sql.DB, addr string) (string, string, error) {
}
}
func BlockExists (sqldb *sql.DB, hash string) (error) {
func BlockExists(sqldb *sql.DB, hash string) error {
var res string
sqlExistsStatement := `SELECT hash from blocks WHERE hash= ($1)`
err := sqldb.QueryRow(sqlExistsStatement, strings.ToLower(hash)).Scan(&res)
@ -486,7 +494,7 @@ func InsertBlock(sqldb *sql.DB, blockData SBlock) {
}
}
func InsertTx (sqldb *sql.DB, txData ShyftTxEntryPretty) {
func InsertTx(sqldb *sql.DB, txData ShyftTxEntryPretty) {
var retNonce string
sqlStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount, gasprice, gas, gasLimit, txfee, nonce, isContract, txStatus, age, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12), ($13), ($14), ($15)) RETURNING nonce`
err := sqldb.QueryRow(sqlStatement, strings.ToLower(txData.TxHash), strings.ToLower(txData.From), strings.ToLower(txData.To.String()), strings.ToLower(txData.BlockHash), txData.BlockNumber, txData.Amount, txData.GasPrice, txData.Gas, txData.GasLimit, txData.Cost, txData.Nonce, txData.IsContract, txData.Status, txData.Age, txData.Data).Scan(&retNonce)
@ -504,5 +512,3 @@ func InsertInternalTx(sqldb *sql.DB, i InteralWrite) {
panic(qerr)
}
}

View file

@ -1,30 +1,32 @@
package shyftdb
import (
"encoding/json"
"fmt"
"math/big"
"strconv"
"strings"
"testing"
"github.com/ShyftNetwork/go-empyrean/common"
"github.com/ShyftNetwork/go-empyrean/consensus/ethash"
"github.com/ShyftNetwork/go-empyrean/core"
"github.com/ShyftNetwork/go-empyrean/core/types"
"github.com/ShyftNetwork/go-empyrean/eth"
"math/big"
//"time"
"encoding/json"
"github.com/ShyftNetwork/go-empyrean/crypto"
"github.com/ShyftNetwork/go-empyrean/consensus/ethash"
"strconv"
"github.com/ShyftNetwork/go-empyrean/eth"
)
type ShyftTracer struct {}
type ShyftTracer struct{}
const (
testAddress = "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
testAddress = "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
)
func TestBlock(t *testing.T) {
eth.NewShyftTestLDB()
core.InitDBTest()
shyft_tracer := new(eth.ShyftTracer)
core.SetIShyftTracer(shyft_tracer)
shyftTracer := new(eth.ShyftTracer)
core.SetIShyftTracer(shyftTracer)
ethConf := &eth.Config{
Genesis: core.DeveloperGenesisBlock(15, common.Address{}),
@ -37,18 +39,19 @@ func TestBlock(t *testing.T) {
eth.SetGlobalConfig(ethConf)
eth.InitTracerEnv()
core.ClearTables()
t.Run("TestBlockToReturnBlock", func(t *testing.T) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
//Nonce, To Address,Value, GasLimit, Gasprice, data
tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
mytx,_ := types.SignTx(tx1, signer, key)
mytx, _ := types.SignTx(tx1, signer, key)
tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
mytx2,_ := types.SignTx(tx2, signer, key)
mytx2, _ := types.SignTx(tx2, signer, key)
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
mytx3,_ := types.SignTx(tx3, signer, key)
mytx3, _ := types.SignTx(tx3, signer, key)
txs := []*types.Transaction{mytx, mytx2, mytx3}
receipt := &types.Receipt{
@ -123,22 +126,22 @@ func TestBlock(t *testing.T) {
if getAllBlocksMinedByAddress := core.SGetAllBlocksMinedByAddress(sqldb, block.Coinbase().String()); len(getAllBlocksMinedByAddress) == 0 {
t.Fatalf("GetAllBlocksMinedByAddress [%v]: GetAllBlocksMinedByAddress did not return correctly", getAllBlocksMinedByAddress)
}
ClearTables()
fmt.Println("passed")
core.ClearTables()
})
t.Run("TestGetRecentBlock", func(t *testing.T) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
//Nonce, To Address,Value, GasLimit, Gasprice, data
tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
mytx,_ := types.SignTx(tx1, signer, key)
mytx, _ := types.SignTx(tx1, signer, key)
tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
mytx2,_ := types.SignTx(tx2, signer, key)
mytx2, _ := types.SignTx(tx2, signer, key)
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
mytx3,_ := types.SignTx(tx3, signer, key)
txs := []*types.Transaction{mytx, mytx2}
mytx3, _ := types.SignTx(tx3, signer, key)
txs := []*types.Transaction{mytx, mytx2}
txs1 := []*types.Transaction{mytx3}
receipt1 := &types.Receipt{
@ -166,7 +169,7 @@ func TestBlock(t *testing.T) {
}
sqldb, err := core.DBConnection()
if (err != nil) {
if err != nil {
panic(err)
}
@ -209,307 +212,293 @@ func TestBlock(t *testing.T) {
t.Fatalf("Block nonce [%v]: Block nonce not found", block.Nonce())
}
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)
}
ClearTables()
fmt.Println("Passed 2")
core.ClearTables()
})
//
t.Run("TestContractCreationTx", func (t *testing.T) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
t.Run("TestContractCreationTx", func(t *testing.T) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
//Nonce,Value, GasLimit, Gasprice, data
contractCreation := types.NewContractCreation(1, big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
mytx,_ := types.SignTx(contractCreation, signer, key)
txs := []*types.Transaction{mytx}
//Nonce,Value, GasLimit, Gasprice, data
contractCreation := types.NewContractCreation(1, big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
mytx, _ := types.SignTx(contractCreation, signer, key)
txs := []*types.Transaction{mytx}
receipt2 := &types.Receipt{
Status: types.ReceiptStatusSuccessful,
CumulativeGasUsed: 1,
Logs: []*types.Log{
{Address: common.BytesToAddress([]byte{0x11})},
{Address: common.BytesToAddress([]byte{0x01, 0x11})},
},
TxHash: common.BytesToHash([]byte{0x11, 0x11}),
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
GasUsed: 111111,
}
receipt2 := &types.Receipt{
Status: types.ReceiptStatusSuccessful,
CumulativeGasUsed: 1,
Logs: []*types.Log{
{Address: common.BytesToAddress([]byte{0x11})},
{Address: common.BytesToAddress([]byte{0x01, 0x11})},
},
TxHash: common.BytesToHash([]byte{0x11, 0x11}),
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
GasUsed: 111111,
}
receipts := []*types.Receipt{receipt2}
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, receipts)
receipts := []*types.Receipt{receipt2}
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, receipts)
if err := core.SWriteBlock(block, receipts); err != nil {
t.Fatalf("Failed to write block into database: %v", err)
}
if err := core.SWriteBlock(block, receipts); err != nil {
t.Fatalf("Failed to write block into database: %v", err)
}
var contractAddressFromReciept string
for _, receipt := range receipts {
contractAddressFromReciept = (*types.ReceiptForStorage)(receipt).ContractAddress.String()
}
var contractAddressFromReciept string
for _, receipt := range receipts {
contractAddressFromReciept = (*types.ReceiptForStorage)(receipt).ContractAddress.String()
}
sqldb, err := core.DBConnection()
if (err != nil) {
panic(err)
}
sqldb, err := core.DBConnection()
if err != nil {
panic(err)
}
for _, tx := range txs {
txn := core.SGetTransaction(sqldb, tx.Hash().String())
byt := []byte(txn)
var data core.ShyftTxEntryPretty
json.Unmarshal(byt, &data)
for _, tx := range txs {
txn := core.SGetTransaction(sqldb, tx.Hash().String())
byt := []byte(txn)
var data core.ShyftTxEntryPretty
json.Unmarshal(byt, &data)
if tx.Hash().String() != data.TxHash {
t.Fatalf("txHash [%v]: tx Hash not found", tx.Hash().String())
if tx.Hash().String() != data.TxHash {
t.Fatalf("txHash [%v]: tx Hash not found", tx.Hash().String())
}
if contractAddressFromReciept != data.ToGet {
t.Fatalf("Contract Addr [%v]: Contract addr not found", contractAddressFromReciept)
}
if strings.ToLower(tx.From().String()) != data.From {
t.Fatalf("From Addr [%v]: From addr not found", tx.From().String())
}
if tx.Nonce() != data.Nonce {
t.Fatalf("Nonce [%v]: Nonce not found", tx.Nonce())
}
if tx.Gas() != data.Gas {
t.Fatalf("Gas [%v]: Gas not found", tx.Gas())
}
if tx.GasPrice().Uint64() != data.GasPrice {
t.Fatalf("Gas Price [%v]: Gas price not found", tx.GasPrice().String())
}
if block.GasLimit() != data.GasLimit {
t.Fatalf("Gas Limit [%v]: Gas limit not found", block.GasLimit())
}
if block.Hash().String() != data.BlockHash {
t.Fatalf("Block Hash [%v]: Block hash not found", block.Hash().String())
}
if block.Number().String() != data.BlockNumber {
t.Fatalf("Block Number [%v]: Block number not found", block.Number().String())
}
if tx.Value().String() != data.Amount {
t.Fatalf("Amount [%v]: Amount not found", tx.Value().String())
}
if tx.Cost().Uint64() != data.Cost {
t.Fatalf("Cost [%v]: Cost not found", tx.Cost().String())
}
var status string
if receipt2.Status == 1 {
status = "SUCCESS"
}
if receipt2.Status == 0 {
status = "FAIL"
}
if status != data.Status {
t.Fatalf("Receipt status [%v]: Receipt status not found", status)
}
var isContract bool
if tx.To() != nil {
isContract = false
} else {
isContract = true
}
if isContract != data.IsContract {
t.Fatalf("isContract [%v]: isContract bool is incorrect", isContract)
}
}
if contractAddressFromReciept != data.ToGet {
t.Fatalf("Contract Addr [%v]: Contract addr not found", contractAddressFromReciept)
}
if tx.From().String() != data.From {
t.Fatalf("From Addr [%v]: From addr not found", tx.From().String())
}
if tx.Nonce() != data.Nonce {
t.Fatalf("Nonce [%v]: Nonce not found", tx.Nonce())
}
if tx.Gas() != data.Gas {
t.Fatalf("Gas [%v]: Gas not found", tx.Gas())
}
if tx.GasPrice().Uint64() != data.GasPrice {
t.Fatalf("Gas Price [%v]: Gas price not found", tx.GasPrice().String())
}
if block.GasLimit() != data.GasLimit {
t.Fatalf("Gas Limit [%v]: Gas limit not found", block.GasLimit())
}
if block.Hash().String() != data.BlockHash {
t.Fatalf("Block Hash [%v]: Block hash not found", block.Hash().String())
}
if block.Number().String() != data.BlockNumber {
t.Fatalf("Block Number [%v]: Block number not found", block.Number().String())
}
if tx.Value().String() != data.Amount {
t.Fatalf("Amount [%v]: Amount not found", tx.Value().String())
}
if tx.Cost().Uint64() != data.Cost {
t.Fatalf("Cost [%v]: Cost not found", tx.Cost().String())
}
var status string
if receipt2.Status == 1 {
status = "SUCCESS"
}
if receipt2.Status == 0 {
status = "FAIL"
}
if status != data.Status {
t.Fatalf("Receipt status [%v]: Receipt status not found", status)
}
var isContract bool
if tx.To() != nil {
isContract = false
} else {
isContract = true
}
if isContract != data.IsContract {
t.Fatalf("isContract [%v]: isContract bool is incorrect", isContract)
}
}
ClearTables()
})
fmt.Println("Passed 3")
core.ClearTables()
})
//
t.Run("TestTransactionsToReturnTransactions", func(t *testing.T) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
t.Run("TestTransactionsToReturnTransactions", func(t *testing.T) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
//Nonce, To Address,Value, GasLimit, Gasprice, data
tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
mytx, _ := types.SignTx(tx1, signer, key)
tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
mytx2, _ := types.SignTx(tx2, signer, key)
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
mytx3, _ := types.SignTx(tx3, signer, key)
txs := []*types.Transaction{mytx, mytx2, mytx3}
//Nonce, To Address,Value, GasLimit, Gasprice, data
tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
mytx,_ := types.SignTx(tx1, signer, key)
tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
mytx2,_ := types.SignTx(tx2, signer, key)
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
mytx3,_ := types.SignTx(tx3, signer, key)
txs := []*types.Transaction{mytx, mytx2, mytx3}
receipt1 := &types.Receipt{
Status: types.ReceiptStatusSuccessful,
CumulativeGasUsed: 1,
Logs: []*types.Log{
{Address: common.BytesToAddress([]byte{0x11})},
{Address: common.BytesToAddress([]byte{0x01, 0x11})},
},
TxHash: common.BytesToHash([]byte{0x11, 0x11}),
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
GasUsed: 111111,
}
receipt1 := &types.Receipt{
Status: types.ReceiptStatusSuccessful,
CumulativeGasUsed: 1,
Logs: []*types.Log{
{Address: common.BytesToAddress([]byte{0x11})},
{Address: common.BytesToAddress([]byte{0x01, 0x11})},
},
TxHash: common.BytesToHash([]byte{0x11, 0x11}),
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
GasUsed: 111111,
}
receipts := []*types.Receipt{receipt1}
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, receipts)
receipts := []*types.Receipt{receipt1}
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, receipts)
if err := core.SWriteBlock(block, receipts); err != nil {
t.Fatalf("Failed to write block into database: %v", err)
}
sqldb, err := core.DBConnection()
if err != nil {
panic(err)
}
if err := core.SWriteBlock(block, receipts); err != nil {
t.Fatalf("Failed to write block into database: %v", err)
}
sqldb, err := core.DBConnection()
if (err != nil) {
panic(err)
}
for _, tx := range txs {
txn := core.SGetTransaction(sqldb, tx.Hash().String())
for _, tx := range txs {
txn := core.SGetTransaction(sqldb, tx.Hash().String())
byt := []byte(txn)
var data core.ShyftTxEntryPretty
json.Unmarshal(byt, &data)
//TODO age, data
if tx.Hash().String() != data.TxHash {
t.Fatalf("txHash [%v]: tx Hash not found", tx.Hash().String())
if strings.ToLower(tx.Hash().String()) != data.TxHash {
t.Fatalf("txHash [%v]: tx Hash not found", tx.Hash().String())
}
if strings.ToLower(tx.From().String()) != data.From {
t.Fatalf("From Addr [%v]: From addr not found", tx.From().String())
}
if strings.ToLower(tx.To().String()) != data.ToGet {
t.Fatalf("To Addr [%v]: To addr not found", tx.To().String())
}
if tx.Nonce() != data.Nonce {
t.Fatalf("Nonce [%v]: Nonce not found", tx.Nonce())
}
if tx.Gas() != data.Gas {
t.Fatalf("Gas [%v]: Gas not found", tx.Gas())
}
if tx.GasPrice().Uint64() != data.GasPrice {
t.Fatalf("Gas Price [%v]: Gas price not found", tx.GasPrice().String())
}
if block.GasLimit() != data.GasLimit {
t.Fatalf("Gas Limit [%v]: Gas limit not found", block.GasLimit())
}
if block.Hash().String() != data.BlockHash {
t.Fatalf("Block Hash [%v]: Block hash not found", block.Hash().String())
}
if block.Number().String() != data.BlockNumber {
t.Fatalf("Block Number [%v]: Block number not found", block.Number().String())
}
if tx.Value().String() != data.Amount {
t.Fatalf("Amount [%v]: Amount not found", tx.Value().String())
}
if tx.Cost().Uint64() != data.Cost {
t.Fatalf("Cost [%v]: Cost not found", tx.Cost().String())
}
var status string
if receipt1.Status == 1 {
status = "SUCCESS"
}
if receipt1.Status == 0 {
status = "FAIL"
}
if status != data.Status {
t.Fatalf("Receipt status [%v]: Receipt status not found", status)
}
var isContract bool
if tx.To() != nil {
isContract = false
} else {
isContract = true
}
if isContract != data.IsContract {
t.Fatalf("isContract [%v]: isContract bool is incorrect", isContract)
}
}
if tx.From().String() != data.From {
t.Fatalf("From Addr [%v]: From addr not found", tx.From().String())
}
if tx.To().String() != data.ToGet {
t.Fatalf("To Addr [%v]: To addr not found", tx.To().String())
}
if tx.Nonce() != data.Nonce {
t.Fatalf("Nonce [%v]: Nonce not found", tx.Nonce())
}
if tx.Gas() != data.Gas {
t.Fatalf("Gas [%v]: Gas not found", tx.Gas())
}
if tx.GasPrice().Uint64() != data.GasPrice {
t.Fatalf("Gas Price [%v]: Gas price not found", tx.GasPrice().String())
}
if block.GasLimit() != data.GasLimit {
t.Fatalf("Gas Limit [%v]: Gas limit not found", block.GasLimit())
}
if block.Hash().String() != data.BlockHash {
t.Fatalf("Block Hash [%v]: Block hash not found", block.Hash().String())
}
if block.Number().String() != data.BlockNumber {
t.Fatalf("Block Number [%v]: Block number not found", block.Number().String())
}
if tx.Value().String() != data.Amount {
t.Fatalf("Amount [%v]: Amount not found", tx.Value().String())
}
if tx.Cost().Uint64() != data.Cost {
t.Fatalf("Cost [%v]: Cost not found", tx.Cost().String())
}
var status string
if receipt1.Status == 1 {
status = "SUCCESS"
}
if receipt1.Status == 0 {
status = "FAIL"
}
if status != data.Status {
t.Fatalf("Receipt status [%v]: Receipt status not found", status)
}
var isContract bool
if tx.To() != nil {
isContract = false
} else {
isContract = true
}
if isContract != data.IsContract {
t.Fatalf("isContract [%v]: isContract bool is incorrect", isContract)
}
}
if getAllTx := core.SGetAllTransactions(sqldb); len(getAllTx) == 0 {
t.Fatalf("GetAllTransactions [%v]: GetAllTransactions did not return correctly", getAllTx)
}
ClearTables()
})
if getAllTx := core.SGetAllTransactions(sqldb); len(getAllTx) == 0 {
t.Fatalf("GetAllTransactions [%v]: GetAllTransactions did not return correctly", getAllTx)
}
fmt.Println("Passed 4")
core.ClearTables()
})
//
t.Run("TestAccountsToReturnAccounts", func(t *testing.T) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
t.Run("TestAccountsToReturnAccounts",func(t *testing.T) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer := types.NewEIP155Signer(big.NewInt(2147483647))
sqldb, err := core.DBConnection()
if err != nil {
panic(err)
}
toAddr1 := common.BytesToAddress([]byte{0x11})
toAddr2 := common.BytesToAddress([]byte{0x22})
toAddr3 := common.BytesToAddress([]byte{0x33})
fromAddr := "0x71562b71999873db5b286df957af199ec94617f7"
core.CreateAccount(sqldb, fromAddr, "50", "1")
toAddr := common.BytesToAddress([]byte{0x11})
toAmount1 := big.NewInt(111)
var toAmountPrev1 string = "3968686868"
//Nonce, To Address,Value, GasLimit, Gasprice, data
tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(5), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
mytx, _ := types.SignTx(tx1, signer, key)
tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(5), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
mytx2, _ := types.SignTx(tx2, signer, key)
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(5), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
mytx3, _ := types.SignTx(tx3, signer, key)
txs := []*types.Transaction{mytx, mytx2, mytx3}
sqldb, err := core.DBConnection()
if (err != nil) {
panic(err)
}
receipt1 := &types.Receipt{
Status: types.ReceiptStatusSuccessful,
CumulativeGasUsed: 1,
Logs: []*types.Log{
{Address: common.BytesToAddress([]byte{0x11})},
{Address: common.BytesToAddress([]byte{0x01, 0x11})},
},
TxHash: common.BytesToHash([]byte{0x11, 0x11}),
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
GasUsed: 111111,
}
core.CreateAccount(sqldb, toAddr1.Hex(), toAmountPrev1, "1")
core.CreateAccount(sqldb, toAddr2.Hex(), "423798729847", "1")
core.CreateAccount(sqldb, toAddr3.Hex(), "0", "1")
core.CreateAccount(sqldb, "0x71562b71999873DB5b286dF957af199Ec94617F7", "3968686868", "1")
receipts := []*types.Receipt{receipt1}
block := types.NewBlock(&types.Header{Number: big.NewInt(319)}, txs, nil, receipts)
//Nonce, To Address,Value, GasLimit, Gasprice, data
tx1 := types.NewTransaction(1, toAddr1, toAmount1, 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
mytx,_ := types.SignTx(tx1, signer, key)
tx2 := types.NewTransaction(2, toAddr2, big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
mytx2,_ := types.SignTx(tx2, signer, key)
tx3 := types.NewTransaction(3, toAddr3, big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
mytx3,_ := types.SignTx(tx3, signer, key)
txs := []*types.Transaction{mytx, mytx2, mytx3}
receipt1 := &types.Receipt{
Status: types.ReceiptStatusSuccessful,
CumulativeGasUsed: 1,
Logs: []*types.Log{
{Address: common.BytesToAddress([]byte{0x11})},
{Address: common.BytesToAddress([]byte{0x01, 0x11})},
},
TxHash: common.BytesToHash([]byte{0x11, 0x11}),
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
GasUsed: 111111,
}
receipts := []*types.Receipt{receipt1}
block := types.NewBlock(&types.Header{Number: big.NewInt(315)}, txs, nil, receipts)
if err := core.SWriteBlock(block, receipts); err != nil {
t.Fatalf("Failed to write block into database: %v", err)
}
if toAddr1.String() != tx1.To().String() {
t.Fatalf("To address [%v]: To address not found", toAddr1.String())
}
accountAddrTo, _ := core.InnerSGetAccount(sqldb, toAddr1.String())
//ewAccountNonceReceiver.Add(accountR, nonceIncrement)
addedAmount := new(big.Int)
toAmountPrevious1, _ := strconv.ParseUint(toAmountPrev1, 10, 64)
b := new(big.Int).SetUint64(toAmountPrevious1)
addedAmount.Add(toAmount1, b)
toBalance := new(big.Int)
toBalance, _ = toBalance.SetString(accountAddrTo.Balance, 10)
for _, tx := range txs {
accountAddrTo := core.SGetAccount(sqldb, tx.To().String())
byts := []byte(accountAddrTo)
var accountDataTo core.SAccounts
json.Unmarshal(byts, &accountDataTo)
if toBalance.Cmp(addedAmount) != 0 {
t.Fatalf("To address balance [%v]: To address balance not correct FFO", toBalance)
}
if strings.ToLower(tx.To().String()) != accountDataTo.Addr {
t.Fatalf("To address [%v]: To address not found", accountDataTo.Addr)
}
if tx.Value().String() != accountDataTo.Balance {
t.Fatalf("To address balance [%v]: To address balance not found", accountDataTo.Balance)
}
if strconv.FormatUint(tx.Nonce(), 10) != accountDataTo.AccountNonce {
t.Fatalf("To account nonce [%v]: To account nonce not found", accountDataTo.AccountNonce)
}
}
accountAddrFrom := core.SGetAccount(sqldb, fromAddr)
byts := []byte(accountAddrFrom)
var accountDataFrom core.SAccounts
json.Unmarshal(byts, &accountDataFrom)
//for _, tx := range txs {
// accountAddrTo := core.SGetAccount(sqldb, tx.To().String())
// byts := []byte(accountAddrTo)
// var accountDataTo core.SAccounts
// json.Unmarshal(byts, &accountDataTo)
//
// if tx.To().String() != accountDataTo.Addr {
// t.Fatalf("To address [%v]: To address not found", accountDataTo.Addr)
// }
// if tx.Value().String() != accountDataTo.Balance {
// t.Fatalf("To address balance [%v]: To address balance not found", accountDataTo.Balance)
// }
// if strconv.FormatUint(tx.Nonce(), 10) != accountDataTo.AccountNonce {
// t.Fatalf("To account nonce [%v]: To account nonce not found", accountDataTo.AccountNonce)
// }
//}
fmt.Println("FROM", accountDataFrom)
if getAllAccountTxs := core.SGetAccountTxs(sqldb, toAddr1.String()); len(getAllAccountTxs) == 0 {
t.Fatalf("GetAccountTxs [%v]: GetAccountTxs did not return correctly", getAllAccountTxs)
}
if getAllAccountTxs := core.SGetAccountTxs(sqldb, toAddr.String()); len(getAllAccountTxs) == 0 {
t.Fatalf("GetAccountTxs [%v]: GetAccountTxs did not return correctly", getAllAccountTxs)
}
if getAllAccounts := core.SGetAllAccounts(sqldb); len(getAllAccounts) == 0 {
t.Fatalf("GetAllAccounts [%v]: GetAllAccounts did not return correctly", getAllAccounts)
}
ClearTables()
})
if getAllAccounts := core.SGetAllAccounts(sqldb); len(getAllAccounts) == 0 {
t.Fatalf("GetAllAccounts [%v]: GetAllAccounts did not return correctly", getAllAccounts)
}
ClearTables()
fmt.Println("Passed 5")
core.ClearTables()
})
core.ClearTables()
}