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) { func DBConnection() (*sql.DB, error) {
if (blockExplorerDb == nil) { if blockExplorerDb == nil {
_, err := InitDB() _, err := InitDB()
if(err != nil) { if err != nil {
return nil, err return nil, err
} }
} }
return blockExplorerDb, nil 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 package core
import ( import (
"math/big"
"time"
"strconv"
"database/sql" "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" "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 var IShyftTracer shyfttracerinterface.IShyftTracer
@ -23,22 +24,22 @@ func SetIShyftTracer(st shyfttracerinterface.IShyftTracer) {
//SBlock type //SBlock type
type SBlock struct { type SBlock struct {
Hash string Hash string
Coinbase string Coinbase string
AgeGet string AgeGet string
Age time.Time Age time.Time
ParentHash string ParentHash string
UncleHash string UncleHash string
Difficulty string Difficulty string
Size string Size string
Rewards string Rewards string
Number string Number string
GasUsed uint64 GasUsed uint64
GasLimit uint64 GasLimit uint64
Nonce uint64 Nonce uint64
TxCount int TxCount int
UncleCount int UncleCount int
Blocks []SBlock Blocks []SBlock
} }
type InteralWrite struct { type InteralWrite struct {
@ -63,9 +64,9 @@ type blockRes struct {
} }
type SAccounts struct { type SAccounts struct {
Addr string Addr string
Balance string Balance string
AccountNonce string AccountNonce string
} }
type accountRes struct { type accountRes struct {
@ -79,30 +80,30 @@ type txRes struct {
} }
type ShyftTxEntryPretty struct { type ShyftTxEntryPretty struct {
TxHash string TxHash string
To *common.Address To *common.Address
ToGet string ToGet string
From string From string
BlockHash string BlockHash string
BlockNumber string BlockNumber string
Amount string Amount string
GasPrice uint64 GasPrice uint64
Gas uint64 Gas uint64
GasLimit uint64 GasLimit uint64
Cost uint64 Cost uint64
Nonce uint64 Nonce uint64
Status string Status string
IsContract bool IsContract bool
Age time.Time Age time.Time
Data []byte Data []byte
} }
type SendAndReceive struct { type SendAndReceive struct {
To string To string
From string From string
Amount string Amount string
Address string Address string
Balance string Balance string
AccountNonce uint64 `json:",string"` AccountNonce uint64 `json:",string"`
} }
@ -114,7 +115,7 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error {
} }
//Get miner rewards //Get miner rewards
rewards := swriteMinerRewards(sqldb,block) rewards := swriteMinerRewards(sqldb, block)
//Format block time to be stored //Format block time to be stored
i, err := strconv.ParseInt(block.Time().String(), 10, 64) i, err := strconv.ParseInt(block.Time().String(), 10, 64)
if err != nil { if err != nil {
@ -123,20 +124,20 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error {
age := time.Unix(i, 0) age := time.Unix(i, 0)
blockData := SBlock{ blockData := SBlock{
Hash: block.Header().Hash().Hex(), Hash: block.Header().Hash().Hex(),
Coinbase: block.Header().Coinbase.String(), Coinbase: block.Header().Coinbase.String(),
Number: block.Header().Number.String(), Number: block.Header().Number.String(),
GasUsed: block.Header().GasUsed, GasUsed: block.Header().GasUsed,
GasLimit: block.Header().GasLimit, GasLimit: block.Header().GasLimit,
TxCount: block.Transactions().Len(), TxCount: block.Transactions().Len(),
UncleCount: len(block.Uncles()), UncleCount: len(block.Uncles()),
ParentHash: block.ParentHash().String(), ParentHash: block.ParentHash().String(),
UncleHash: block.UncleHash().String(), UncleHash: block.UncleHash().String(),
Difficulty: block.Difficulty().String(), Difficulty: block.Difficulty().String(),
Size: block.Size().String(), Size: block.Size().String(),
Nonce: block.Nonce(), Nonce: block.Nonce(),
Rewards: rewards, Rewards: rewards,
Age: age, Age: age,
} }
//Inserts block data into DB //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 //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 isContract bool
var statusFromReciept string var statusFromReciept string
var toAddr *common.Address var toAddr *common.Address
@ -191,21 +192,21 @@ func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.H
} }
txData := ShyftTxEntryPretty{ txData := ShyftTxEntryPretty{
TxHash: tx.Hash().Hex(), TxHash: tx.Hash().Hex(),
From: tx.From().Hex(), From: tx.From().Hex(),
To: toAddr, To: toAddr,
BlockHash: blockHash.Hex(), BlockHash: blockHash.Hex(),
BlockNumber: blockNumber, BlockNumber: blockNumber,
Amount: tx.Value().String(), Amount: tx.Value().String(),
Cost: tx.Cost().Uint64(), Cost: tx.Cost().Uint64(),
GasPrice: tx.GasPrice().Uint64(), GasPrice: tx.GasPrice().Uint64(),
GasLimit: gasLimit, GasLimit: gasLimit,
Gas: tx.Gas(), Gas: tx.Gas(),
Nonce: tx.Nonce(), Nonce: tx.Nonce(),
Age: age, Age: age,
Data: tx.Data(), Data: tx.Data(),
Status: statusFromReciept, Status: statusFromReciept,
IsContract: isContract, IsContract: isContract,
} }
//Inserts Tx into DB //Inserts Tx into DB
InsertTx(sqldb, txData) 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 { func swriteContractBalance(sqldb *sql.DB, tx *types.Transaction) error {
sendAndReceiveData := SendAndReceive{ sendAndReceiveData := SendAndReceive{
From: tx.From().Hex(), From: tx.From().Hex(),
Amount: tx.Value().String(), Amount: tx.Value().String(),
AccountNonce: tx.Nonce(), AccountNonce: tx.Nonce(),
} }
fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.From) 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) accountNonce := strconv.FormatUint(tx.Nonce(), 10)
CreateAccount(sqldb, sendAndReceiveData.From, sendAndReceiveData.Amount, accountNonce) CreateAccount(sqldb, sendAndReceiveData.From, sendAndReceiveData.Amount, accountNonce)
default: default:
var newBalanceSender,newAccountNonceSender big.Int var newBalanceSender, newAccountNonceSender big.Int
var nonceIncrement = big.NewInt(1) var nonceIncrement = big.NewInt(1)
fromBalance := new(big.Int) fromBalance := new(big.Int)
@ -249,57 +250,64 @@ func swriteContractBalance(sqldb *sql.DB, tx *types.Transaction) error {
//writeFromBalance writes senders balance to accounts db //writeFromBalance writes senders balance to accounts db
func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error { func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error {
sendAndReceiveData := SendAndReceive{ sendAndReceiveData := SendAndReceive{
To: tx.To().Hex(), To: tx.To().Hex(),
From: tx.From().Hex(), From: tx.From().Hex(),
Amount: tx.Value().String(), Amount: tx.Value().String(),
} }
value := tx.Value()
toAddressBalance, toAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.To) _, _, err := AccountExists(sqldb, sendAndReceiveData.To)
switch { switch {
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)
case err != nil: case err != nil:
log.Fatal(err) log.Fatal(err)
default: default:
fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.From) balanceHelper(sqldb, sendAndReceiveData, value)
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())
} }
return nil 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 { func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amount string) error {
sendAndReceiveData := SendAndReceive{ sendAndReceiveData := SendAndReceive{
To: toAddr, To: toAddr,
From: fromAddr, From: fromAddr,
Amount: amount, Amount: amount,
} }
@ -316,7 +324,7 @@ func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amo
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
var newBalanceReceiver, newBalanceSender, newAccountNonceReceiver, newAccountNonceSender big.Int var newBalanceReceiver, newBalanceSender, newAccountNonceReceiver, newAccountNonceSender big.Int
var nonceIncrement = big.NewInt(1) var nonceIncrement = big.NewInt(1)
//STRING TO BIG INT //STRING TO BIG INT
@ -341,8 +349,8 @@ func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amo
newAccountNonceSender.Add(fromNonce, nonceIncrement) newAccountNonceSender.Add(fromNonce, nonceIncrement)
//UPDATE ACCOUNTS BASED ON NEW BALANCES AND ACCOUNT NONCES //UPDATE ACCOUNTS BASED ON NEW BALANCES AND ACCOUNT NONCES
UpdateAccount(sqldb, sendAndReceiveData.To, newBalanceReceiver.String(), newAccountNonceReceiver.String()) UpdateAccount(sqldb, sendAndReceiveData.To, newBalanceReceiver.String(), newAccountNonceReceiver.String())
UpdateAccount(sqldb, sendAndReceiveData.From, newBalanceSender.String(), newAccountNonceSender.String()) UpdateAccount(sqldb, sendAndReceiveData.From, newBalanceSender.String(), newAccountNonceSender.String())
} }
return nil return nil
} }
@ -366,7 +374,7 @@ func swriteMinerRewards(sqldb *sql.DB, block *types.Block) string {
// https://ethereum.stackexchange.com/questions/27172/different-uncles-reward // https://ethereum.stackexchange.com/questions/27172/different-uncles-reward
// line 551 in consensus.go (shyft_go-ethereum/consensus/ethash/consensus.go) // line 551 in consensus.go (shyft_go-ethereum/consensus/ethash/consensus.go)
// Some weird constants to avoid constant memory allocs for them. // 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 uncleRewards []*big.Int
var uncleAddrs []string var uncleAddrs []string
@ -435,7 +443,7 @@ func sstoreReward(sqldb *sql.DB, address string, reward *big.Int) {
/////////////////////// ///////////////////////
//DB Utility functions //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` sqlStatement := `INSERT INTO accounts(addr, balance, accountNonce) VALUES(($1), ($2), ($3)) RETURNING addr`
insertErr := sqldb.QueryRow(sqlStatement, strings.ToLower(addr), balance, accountNonce).Scan(&addr) insertErr := sqldb.QueryRow(sqlStatement, strings.ToLower(addr), balance, accountNonce).Scan(&addr)
if insertErr != nil { 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 var addressBalance, accountNonce string
sqlExistsStatement := `SELECT balance, accountNonce from accounts WHERE addr = ($1)` sqlExistsStatement := `SELECT balance, accountNonce from accounts WHERE addr = ($1)`
err := sqldb.QueryRow(sqlExistsStatement, strings.ToLower(addr)).Scan(&addressBalance, &accountNonce) 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 var res string
sqlExistsStatement := `SELECT hash from blocks WHERE hash= ($1)` sqlExistsStatement := `SELECT hash from blocks WHERE hash= ($1)`
err := sqldb.QueryRow(sqlExistsStatement, strings.ToLower(hash)).Scan(&res) 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 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` 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) 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) panic(qerr)
} }
} }

View file

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