From dc379b811c08f9b151f4c869cc24e0cbff40fb99 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Wed, 25 Jul 2018 16:36:16 -0400 Subject: [PATCH 01/15] need to fix gen write before internals --- core/genesis.go | 96 ++++++++++++++++++++----------------- core/shyft_database_util.go | 72 ++++++++++++++++++++++++++-- eth/tracers/tracer.go | 4 ++ 3 files changed, 124 insertions(+), 48 deletions(-) diff --git a/core/genesis.go b/core/genesis.go index dd1106ddb5..d13fe558c1 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -25,7 +25,7 @@ import ( "math/big" "strings" - _ "github.com/lib/pq" + "database/sql" "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/common/math" @@ -35,7 +35,7 @@ import ( "github.com/ShyftNetwork/go-empyrean/log" "github.com/ShyftNetwork/go-empyrean/params" "github.com/ShyftNetwork/go-empyrean/rlp" - "database/sql" + _ "github.com/lib/pq" "strconv" "time" ) @@ -143,55 +143,62 @@ func (e *GenesisMismatchError) Error() string { //WriteShyftGen writes the genesis block to Shyft db //@NOTE:SHYFT func WriteShyftGen(gen *Genesis, block *types.Block) { - + fmt.Println("IN WRITE GEN") sqldb, _ := DBConnection() - for k := range gen.Alloc { + for k, v := range gen.Alloc { addr := k.String() - var response string - sqlExistsStatement := `SELECT balance from accounts WHERE addr = ($1)` - err := sqldb.QueryRow(sqlExistsStatement, addr).Scan(&response) - switch { - case err == sql.ErrNoRows: - for k, v := range gen.Alloc { - number := block.Header().Number.String() - gasUsed := block.Header().GasUsed - gasLimit := block.Header().GasLimit - gasPrice := 0 - txFee := 0 - txStatus := "" - isContract := false - data:= "" - addr := k.String() - accountNonce := v.Nonce +1 - i, err := strconv.ParseInt(block.Time().String(), 10, 64) - if err != nil { - panic(err) - } - age := time.Unix(i, 0) - Genesis := []string{"GENESIS_", addr} - GENESIS := "GENESIS" - txHash := strings.Join(Genesis, addr) - sqlStatement := `INSERT INTO accounts(addr, balance, accountnonce) VALUES(($1), ($2), ($3)) RETURNING addr` - insertErr := sqldb.QueryRow(sqlStatement, addr, v.Balance.String(), accountNonce).Scan(&addr) - if insertErr != nil { - panic(insertErr) - } + //var response string + //sqlExistsStatement := `SELECT balance from accounts WHERE addr = ($1)` + //err := sqldb.QueryRow(sqlExistsStatement, addr).Scan(&response) + fmt.Println("[GENESIS]", strings.ToLower(addr)) + _, _, err := AccountExists(sqldb, addr) + fmt.Println("[]ACCOUNT EXISTS ERR", err) + switch { + case err == sql.ErrNoRows: + //number := block.Header().Number.String() + //gasUsed := block.Header().GasUsed + //gasLimit := block.Header().GasLimit + //gasPrice := 0 + //txFee := 0 + //txStatus := "" + //isContract := false + //data := "" + addr := k.String() + accountNonce := v.Nonce + 1 + //i, err := strconv.ParseInt(block.Time().String(), 10, 64) + //if err != nil { + // panic(err) + //} + //age := time.Unix(i, 0) + //Genesis := []string{"GENESIS_", addr} + //GENESIS := "GENESIS" + //txHash := strings.Join(Genesis, addr) - var retNonce string - sqlGenTxStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount,gasPrice, gas, gasLimit,txFee,nonce,txstatus, iscontract,age, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12), ($13), ($14), ($15)) RETURNING nonce` - insertError := sqldb.QueryRow(sqlGenTxStatement, txHash, GENESIS, addr, block.Header().Hash().Hex(), number, v.Balance.String(), gasPrice, gasUsed, gasLimit, txFee,accountNonce,txStatus, isContract, age, data).Scan(&retNonce) - if insertError != nil { - panic(insertError) - } + //sqlStatement := `INSERT INTO accounts(addr, balance, accountnonce) VALUES(($1), ($2), ($3)) RETURNING addr` + //insertErr := sqldb.QueryRow(sqlStatement, strings.ToLower(addr), v.Balance.String(), accountNonce).Scan(&addr) + //if insertErr != nil { + // panic(insertErr) + //} + + accountNoncee := strconv.FormatUint(accountNonce, 10) + CreateAccount(sqldb, addr, v.Balance.String(), accountNoncee) + + //var retNonce string + //sqlGenTxStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount,gasPrice, gas, gasLimit,txFee,nonce,txstatus, iscontract,age, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12), ($13), ($14), ($15)) RETURNING nonce` + //insertError := sqldb.QueryRow(sqlGenTxStatement, txHash, GENESIS, addr, block.Header().Hash().Hex(), number, v.Balance.String(), gasPrice, gasUsed, gasLimit, txFee, accountNonce, txStatus, isContract, age, data).Scan(&retNonce) + //if insertError != nil { + // panic(insertError) + //} + default: + log.Info("Found Genesis Block") } - default: - log.Info("Found Genesis Block") -}}} + } +} func WriteShyftBlockZero(block *types.Block, gen *Genesis) error { - + sqldb, _ := DBConnection() coinbase := block.Header().Coinbase.String() @@ -229,6 +236,7 @@ func WriteShyftBlockZero(block *types.Block, gen *Genesis) error { } return nil } + // SetupGenesisBlock writes or updates the genesis block in db. // The block that will be used is: // @@ -315,6 +323,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig { return params.AllEthashProtocolChanges } } + // ToBlock creates the genesis block and writes state of a genesis specification // to the given database (or discards it if nil). func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { @@ -439,6 +448,7 @@ func DefaultRinkebyGenesisBlock() *Genesis { Alloc: decodePrealloc(rinkebyAllocData), } } + // DeveloperGenesisBlock returns the 'geth --dev' genesis block. Note, this must // be seeded with the func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis { diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 8632723615..011318c3c9 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -11,6 +11,8 @@ import ( "github.com/ShyftNetwork/go-empyrean/core/types" Rewards "github.com/ShyftNetwork/go-empyrean/consensus/ethash" "github.com/ShyftNetwork/go-empyrean/shyfttracerinterface" + "fmt" + "strings" ) var IShyftTracer shyfttracerinterface.IShyftTracer @@ -290,6 +292,64 @@ func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error { return nil } +func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amount string) error { + sendAndReceiveData := SendAndReceive{ + To: toAddr, + From: fromAddr, + Amount: amount, + } + + toAddressBalance, toAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.To) + + switch { + case err == sql.ErrNoRows: + accountNonce := "1" + CreateAccount(sqldb, sendAndReceiveData.To, sendAndReceiveData.Amount, accountNonce) + case err != nil: + log.Fatal(err) + default: + fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.From) + if err == sql.ErrNoRows { + fmt.Println("NOT IN ACCOUNTS TABLE", sendAndReceiveData.From) + fmt.Println("THIS RAN NO ERR FROM ADDR") + //accountNonce := "1" + //CreateAccount(sqldb, sendAndReceiveData.From, sendAndReceiveData.Amount, accountNonce) + } + 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) + amountValue := new(big.Int) + amountValue, _ = amountValue.SetString(amount, 10) + + //ACCOUNT NONCES + toNonce := new(big.Int) + toNonce, _ = toNonce.SetString(toAccountNonce, 10) + fromNonce := new(big.Int) + fromNonce, _ = fromNonce.SetString(fromAccountNonce, 10) + + newBalanceReceiver.Add(toBalance, amountValue) + newBalanceSender.Sub(fromBalance, amountValue) + + 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 +} + // @NOTE: This function is extremely complex and requires heavy testing and knowdlege of edge cases: // uncle blocks, account balance updates based on reorgs, diverges that get dropped. // Reason for this is because the accounts are not deterministic like the block and tx hashes. @@ -344,6 +404,7 @@ func sstoreReward(sqldb *sql.DB, address string, reward *big.Int) { addressBalance, accountNonce, err := AccountExists(sqldb, address) if err == sql.ErrNoRows { + fmt.Println("this ran", address) // Addr does not exist, thus create new entry // We convert totalReward into a string and postgres converts into number CreateAccount(sqldb, address, reward.String(), "1") @@ -379,11 +440,12 @@ func sstoreReward(sqldb *sql.DB, address string, reward *big.Int) { //DB Utility functions ////////////////////// 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, addr, balance, accountNonce).Scan(&addr) - if insertErr != nil { - panic(insertErr) - } + fmt.Println("[CREATE ACCOUNT]", strings.ToLower(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) + //if insertErr != nil { + // panic(insertErr) + //} } func AccountExists (sqldb *sql.DB, addr string) (string, string, error) { diff --git a/eth/tracers/tracer.go b/eth/tracers/tracer.go index e674d817ba..6ca7bc95de 100644 --- a/eth/tracers/tracer.go +++ b/eth/tracers/tracer.go @@ -603,6 +603,10 @@ func (i *Internals) SWriteInteralTxs(hash common.Hash) { value, _ := hexutil.DecodeUint64(i.Value) amount := strconv.FormatUint(value, 10) + //@TODO WRITE OVER TRANSACTION STRUCT + + core.SWriteInternalTxBalances(sqldb, i.To, i.From, amount) + var returnValue string sqlStatement := `INSERT INTO internaltxs(type, txhash, from_addr, to_addr, amount, gas, gasUsed, time, input, output) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10)) RETURNING txHash` qerr := sqldb.QueryRow(sqlStatement, i.Type, hash.Hex(), i.From, i.To, amount, gas, gasUsed, i.Time, i.Input, i.Output).Scan(&returnValue) From 3f6bdfc0e4e403093a82b1b2da4005fe59765d0d Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Thu, 26 Jul 2018 15:46:48 -0400 Subject: [PATCH 02/15] refactored gen write functions for internals --- core/genesis.go | 133 +++++++++++++++++------------------- core/shyft_database_util.go | 119 +++++++++++++++----------------- 2 files changed, 120 insertions(+), 132 deletions(-) diff --git a/core/genesis.go b/core/genesis.go index d13fe558c1..84a449b971 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -24,7 +24,6 @@ import ( "fmt" "math/big" "strings" - "database/sql" "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common/hexutil" @@ -143,92 +142,88 @@ func (e *GenesisMismatchError) Error() string { //WriteShyftGen writes the genesis block to Shyft db //@NOTE:SHYFT func WriteShyftGen(gen *Genesis, block *types.Block) { - fmt.Println("IN WRITE GEN") sqldb, _ := DBConnection() - for k, v := range gen.Alloc { - addr := k.String() - - //var response string - //sqlExistsStatement := `SELECT balance from accounts WHERE addr = ($1)` - //err := sqldb.QueryRow(sqlExistsStatement, addr).Scan(&response) - fmt.Println("[GENESIS]", strings.ToLower(addr)) - _, _, err := AccountExists(sqldb, addr) - fmt.Println("[]ACCOUNT EXISTS ERR", err) + _, _, err := AccountExists(sqldb, k.String()) switch { case err == sql.ErrNoRows: - //number := block.Header().Number.String() - //gasUsed := block.Header().GasUsed - //gasLimit := block.Header().GasLimit - //gasPrice := 0 - //txFee := 0 - //txStatus := "" - //isContract := false - //data := "" - addr := k.String() - accountNonce := v.Nonce + 1 - //i, err := strconv.ParseInt(block.Time().String(), 10, 64) - //if err != nil { - // panic(err) - //} - //age := time.Unix(i, 0) - //Genesis := []string{"GENESIS_", addr} - //GENESIS := "GENESIS" - //txHash := strings.Join(Genesis, addr) + var toAddr *common.Address + var data []byte + var cost, gasPrice uint64 + //Initializing proper types for tx struct + toAddr = &k + cost = 0 + gasPrice = 0 + //Appending GENESIS to address stored as txHash and From Addr + Genesis := []string{"GENESIS_", k.String()} + GENESIS := "GENESIS" + txHash := strings.Join(Genesis, k.String()) + //Create the accountNonce, set to 1 (1 incoming tx), format type + accountNonce := v.Nonce + 1 + accountNoncee := strconv.FormatUint(accountNonce, 10) - //sqlStatement := `INSERT INTO accounts(addr, balance, accountnonce) VALUES(($1), ($2), ($3)) RETURNING addr` - //insertErr := sqldb.QueryRow(sqlStatement, strings.ToLower(addr), v.Balance.String(), accountNonce).Scan(&addr) - //if insertErr != nil { - // panic(insertErr) - //} + i, err := strconv.ParseInt(block.Time().String(), 10, 64) + if err != nil { + panic(err) + } + age := time.Unix(i, 0) - accountNoncee := strconv.FormatUint(accountNonce, 10) - CreateAccount(sqldb, addr, v.Balance.String(), accountNoncee) + txData := ShyftTxEntryPretty { + TxHash: txHash, + From: GENESIS, + To: toAddr, + BlockHash: block.Header().Hash().Hex(), + BlockNumber:block.Header().Number.String(), + Amount: v.Balance.String(), + Cost: cost, + GasPrice: gasPrice, + GasLimit: block.GasLimit(), + Gas: block.GasUsed(), + Nonce: accountNonce, + Age: age, + Data: data, + Status: "SUCCESS", + IsContract: false, + } + //Create account and store tx + CreateAccount(sqldb, k.String(), v.Balance.String(), accountNoncee) + InsertTx(sqldb, txData) - //var retNonce string - //sqlGenTxStatement := `INSERT INTO txs(txhash, from_addr, to_addr, blockhash, blockNumber, amount,gasPrice, gas, gasLimit,txFee,nonce,txstatus, iscontract,age, data) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12), ($13), ($14), ($15)) RETURNING nonce` - //insertError := sqldb.QueryRow(sqlGenTxStatement, txHash, GENESIS, addr, block.Header().Hash().Hex(), number, v.Balance.String(), gasPrice, gasUsed, gasLimit, txFee, accountNonce, txStatus, isContract, age, data).Scan(&retNonce) - //if insertError != nil { - // panic(insertError) - //} default: - log.Info("Found Genesis Block") - } - } + log.Info("Found Genesis Block") + }} } func WriteShyftBlockZero(block *types.Block, gen *Genesis) error { - sqldb, _ := DBConnection() - coinbase := block.Header().Coinbase.String() - number := block.Header().Number.String() - gasUsed := block.Header().GasUsed - gasLimit := block.Header().GasLimit - uncleCount := len(block.Uncles()) - parentHash := block.ParentHash().String() - uncleHash := block.UncleHash().String() - blockDifficulty := block.Difficulty().String() - blockSize := block.Size().String() - blockNonce := block.Nonce() - genesisTxCount := len(gen.Alloc) - - i, err := strconv.ParseInt(block.Time().String(), 10, 64) - if err != nil { - panic(err) + i, error := strconv.ParseInt(block.Time().String(), 10, 64) + if error != nil { + panic(error) } age := time.Unix(i, 0) - var response string - sqlExistsStatement := `SELECT hash from blocks WHERE hash= ($1)` - err = sqldb.QueryRow(sqlExistsStatement, block.Header().Hash().Hex()).Scan(&response) + 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: len(gen.Alloc), + UncleCount: len(block.Uncles()), + Age: age, + ParentHash: block.ParentHash().String(), + UncleHash: block.UncleHash().String(), + Difficulty: block.Difficulty().String(), + Size: block.Size().String(), + Nonce: block.Nonce(), + Rewards: "0", + } + + err := BlockExists(sqldb, blockData.Hash) switch { case err == sql.ErrNoRows: - sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txCount, uncleCount, age, parentHash, uncleHash, difficulty, size, nonce) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12),($13)) RETURNING number` - qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number, gasUsed, gasLimit, genesisTxCount, uncleCount, age, parentHash, uncleHash, blockDifficulty, blockSize, blockNonce).Scan(&number) - if qerr != nil { - panic(qerr) - } + InsertBlock(sqldb, blockData) case err != nil: panic(err) default: diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 011318c3c9..6f5c5484be 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -11,7 +11,6 @@ import ( "github.com/ShyftNetwork/go-empyrean/core/types" Rewards "github.com/ShyftNetwork/go-empyrean/consensus/ethash" "github.com/ShyftNetwork/go-empyrean/shyfttracerinterface" - "fmt" "strings" ) @@ -100,7 +99,14 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error { panic(err) } + //Get miner rewards rewards := swriteMinerRewards(sqldb,block) + //Format block time to be stored + i, err := strconv.ParseInt(block.Time().String(), 10, 64) + if err != nil { + panic(err) + } + age := time.Unix(i, 0) blockData := SBlock{ Hash: block.Header().Hash().Hex(), @@ -116,20 +122,12 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error { Size: block.Size().String(), Nonce: block.Nonce(), Rewards: rewards, + Age: age, } - i, err := strconv.ParseInt(block.Time().String(), 10, 64) - if err != nil { - panic(err) - } - age := time.Unix(i, 0) - - blockAge := SBlock { - Age: age, - } //Inserts block data into DB - InsertBlock(sqldb, blockData, blockAge) + InsertBlock(sqldb, blockData) if block.Transactions().Len() > 0 { for _, tx := range block.Transactions() { @@ -149,24 +147,9 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) 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 var contractAddressFromReciept common.Address - txData := ShyftTxEntryPretty{ - TxHash: tx.Hash().Hex(), - From: tx.From().Hex(), - To: tx.To(), - 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(), - } - if tx.To() == nil { for _, receipt := range receipts { statusReciept := (*types.ReceiptForStorage)(receipt).Status @@ -179,13 +162,7 @@ func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.H } } isContract = true - contractData := ShyftTxEntryPretty{ - Status: statusFromReciept, - IsContract: isContract, - To: &contractAddressFromReciept, - } - //Insert Tx into DB - InsertTx(sqldb, txData, contractData) + toAddr = &contractAddressFromReciept } else { isContract = false for _, receipt := range receipts { @@ -197,14 +174,28 @@ func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.H statusFromReciept = "SUCCESS" } } - data := ShyftTxEntryPretty{ - Status: statusFromReciept, - IsContract: isContract, - To: tx.To(), - } - //Insert Tx into DB - InsertTx(sqldb, txData, data) + toAddr = tx.To() } + + txData := ShyftTxEntryPretty{ + 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, + } + //Inserts Tx into DB + InsertTx(sqldb, txData) //Runs necessary functions for tracing internal transactions through tracers.go IShyftTracer.GetTracerToRun(tx.Hash()) @@ -309,16 +300,9 @@ func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amo log.Fatal(err) default: fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.From) - if err == sql.ErrNoRows { - fmt.Println("NOT IN ACCOUNTS TABLE", sendAndReceiveData.From) - fmt.Println("THIS RAN NO ERR FROM ADDR") - //accountNonce := "1" - //CreateAccount(sqldb, sendAndReceiveData.From, sendAndReceiveData.Amount, accountNonce) - } if err != nil { log.Fatal(err) } - var newBalanceReceiver, newBalanceSender, newAccountNonceReceiver, newAccountNonceSender big.Int var nonceIncrement = big.NewInt(1) @@ -404,7 +388,6 @@ func sstoreReward(sqldb *sql.DB, address string, reward *big.Int) { addressBalance, accountNonce, err := AccountExists(sqldb, address) if err == sql.ErrNoRows { - fmt.Println("this ran", address) // Addr does not exist, thus create new entry // We convert totalReward into a string and postgres converts into number CreateAccount(sqldb, address, reward.String(), "1") @@ -440,19 +423,17 @@ func sstoreReward(sqldb *sql.DB, address string, reward *big.Int) { //DB Utility functions ////////////////////// func CreateAccount (sqldb *sql.DB, addr string, balance string, accountNonce string) { - fmt.Println("[CREATE ACCOUNT]", strings.ToLower(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) - //if insertErr != nil { - // panic(insertErr) - //} + 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 { + panic(insertErr) + } } 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, addr).Scan(&addressBalance, &accountNonce) - + err := sqldb.QueryRow(sqlExistsStatement, strings.ToLower(addr)).Scan(&addressBalance, &accountNonce) switch { case err == sql.ErrNoRows: return addressBalance, accountNonce, err @@ -463,27 +444,39 @@ func AccountExists (sqldb *sql.DB, addr string) (string, 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) + switch { + case err == sql.ErrNoRows: + return err + panic(err) + default: + return err + } +} + func UpdateAccount(sqldb *sql.DB, addr string, balance string, accountNonce string) { updateSQLStatement := `UPDATE accounts SET balance = ($2), accountNonce = ($3) WHERE addr = ($1)` - _, updateErr := sqldb.Exec(updateSQLStatement, addr, balance, accountNonce) + _, updateErr := sqldb.Exec(updateSQLStatement, strings.ToLower(addr), balance, accountNonce) if updateErr != nil { panic(updateErr) } } -func InsertBlock(sqldb *sql.DB, blockData SBlock, blockAge SBlock) { +func InsertBlock(sqldb *sql.DB, blockData SBlock) { sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txCount, uncleCount, age, parentHash, uncleHash, difficulty, size, rewards, nonce) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12),($13), ($14)) RETURNING number` - qerr := sqldb.QueryRow(sqlStatement, blockData.Hash, blockData.Coinbase, blockData.Number, blockData.GasUsed, blockData.GasLimit, blockData.TxCount, blockData.UncleCount, blockAge.Age, blockData.ParentHash, blockData.UncleHash, blockData.Difficulty, blockData.Size, blockData.Rewards, blockData.Nonce).Scan(&blockData.Number) + qerr := sqldb.QueryRow(sqlStatement, strings.ToLower(blockData.Hash), blockData.Coinbase, blockData.Number, blockData.GasUsed, blockData.GasLimit, blockData.TxCount, blockData.UncleCount, blockData.Age, blockData.ParentHash, blockData.UncleHash, blockData.Difficulty, blockData.Size, blockData.Rewards, blockData.Nonce).Scan(&blockData.Number) if qerr != nil { panic(qerr) } } - -func InsertTx (sqldb *sql.DB, txData ShyftTxEntryPretty, data 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, txData.TxHash, txData.From, data.To.String(), txData.BlockHash, txData.BlockNumber, txData.Amount, txData.GasPrice, txData.Gas, txData.GasLimit, txData.Cost, txData.Nonce, data.IsContract, data.Status, txData.Age, txData.Data).Scan(&retNonce) + err := sqldb.QueryRow(sqlStatement, strings.ToLower(txData.TxHash), txData.From, 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) if err != nil { panic(err) } From ab8d98ad913638a190ce0de5d4dfdffd18a6e23d Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Thu, 26 Jul 2018 17:03:50 -0400 Subject: [PATCH 03/15] updating accounts from internal txs --- core/genesis.go | 21 +++++++++++++++------ core/shyft_database_util.go | 24 ++++++++++++++++++++++++ eth/tracers/tracer.go | 26 +++++++++++++++----------- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/core/genesis.go b/core/genesis.go index 84a449b971..c9d049d4fc 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -190,7 +190,7 @@ func WriteShyftGen(gen *Genesis, block *types.Block) { InsertTx(sqldb, txData) default: - log.Info("Found Genesis Block") + log.Info("Found Genesis Block") }} } @@ -259,11 +259,20 @@ func SetupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig log.Info("Writing custom genesis block") } block, err := genesis.Commit(db) - //@NOTE:SHYFT WRITE TO BLOCK ZERO DB - WriteShyftBlockZero(block, genesis) - //@NOTE:SHYFT WRITE TO DB - WriteShyftGen(genesis, block) - + //@NOTE:SHYFT SWITCH CASE ENSURES SHYFT GENESIS FUNCTIONS ARE ONLY CALLED ONCE + sqldb, _ := DBConnection() + serror := BlockExists(sqldb, block.Hash().String()) + switch { + case serror == sql.ErrNoRows: + //@NOTE:SHYFT WRITE TO BLOCK ZERO DB + WriteShyftBlockZero(block, genesis) + //@NOTE:SHYFT WRITE TO DB + WriteShyftGen(genesis, block) + case serror != nil: + panic(serror) + default: + log.Info("Genesis Block Written") + } return genesis.Config, block.Hash(), err } diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 6f5c5484be..ecf318db67 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -12,6 +12,7 @@ import ( Rewards "github.com/ShyftNetwork/go-empyrean/consensus/ethash" "github.com/ShyftNetwork/go-empyrean/shyfttracerinterface" "strings" + "fmt" ) var IShyftTracer shyfttracerinterface.IShyftTracer @@ -40,6 +41,19 @@ type SBlock struct { Blocks []SBlock } +type InteralWrite struct { + Hash string + Type string + From string + To string + Value string + Gas uint64 + GasUsed uint64 + Input string + Output string + Time string +} + //blockRes struct type blockRes struct { hash string @@ -482,4 +496,14 @@ func InsertTx (sqldb *sql.DB, txData ShyftTxEntryPretty) { } } +func InsertInternalTx(sqldb *sql.DB, i InteralWrite) { + var returnValue string + sqlStatement := `INSERT INTO internaltxs(type, txhash, from_addr, to_addr, amount, gas, gasUsed, time, input, output) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10)) RETURNING txHash` + qerr := sqldb.QueryRow(sqlStatement, i.Type, i.Hash, i.From, i.To, i.Value, i.Gas, i.GasUsed, i.Time, i.Input, i.Output).Scan(&returnValue) + if qerr != nil { + fmt.Println(qerr) + panic(qerr) + } +} + diff --git a/eth/tracers/tracer.go b/eth/tracers/tracer.go index 6ca7bc95de..f419834a37 100644 --- a/eth/tracers/tracer.go +++ b/eth/tracers/tracer.go @@ -603,18 +603,22 @@ func (i *Internals) SWriteInteralTxs(hash common.Hash) { value, _ := hexutil.DecodeUint64(i.Value) amount := strconv.FormatUint(value, 10) - //@TODO WRITE OVER TRANSACTION STRUCT - - core.SWriteInternalTxBalances(sqldb, i.To, i.From, amount) - - var returnValue string - sqlStatement := `INSERT INTO internaltxs(type, txhash, from_addr, to_addr, amount, gas, gasUsed, time, input, output) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10)) RETURNING txHash` - qerr := sqldb.QueryRow(sqlStatement, i.Type, hash.Hex(), i.From, i.To, amount, gas, gasUsed, i.Time, i.Input, i.Output).Scan(&returnValue) - - if qerr != nil { - fmt.Println(qerr) - panic(qerr) + iTx := core.InteralWrite{ + Hash: hash.Hex(), + Type: i.Type, + From: i.From, + To: i.To, + Value: amount, + Gas: gas, + GasUsed: gasUsed, + Input: i.Input, + Output: i.Output, + Time: i.Time, } + + //@TODO WRITE OVER TRANSACTION STRUCT + core.SWriteInternalTxBalances(sqldb, i.To, i.From, amount) + core.InsertInternalTx(sqldb, iTx) } //@NOTE:SHYFT From f066a8e461eebfae9ba09b161e90f3e691734f2a Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Mon, 30 Jul 2018 11:37:18 -0400 Subject: [PATCH 04/15] all hashes stored as lower case --- core/shyft_database_util.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index ecf318db67..4e87f95c68 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -139,7 +139,6 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error { Age: age, } - //Inserts block data into DB InsertBlock(sqldb, blockData) @@ -490,7 +489,7 @@ func InsertBlock(sqldb *sql.DB, blockData SBlock) { 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), txData.From, 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) if err != nil { panic(err) } @@ -499,7 +498,7 @@ func InsertTx (sqldb *sql.DB, txData ShyftTxEntryPretty) { func InsertInternalTx(sqldb *sql.DB, i InteralWrite) { var returnValue string sqlStatement := `INSERT INTO internaltxs(type, txhash, from_addr, to_addr, amount, gas, gasUsed, time, input, output) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10)) RETURNING txHash` - qerr := sqldb.QueryRow(sqlStatement, i.Type, i.Hash, i.From, i.To, i.Value, i.Gas, i.GasUsed, i.Time, i.Input, i.Output).Scan(&returnValue) + qerr := sqldb.QueryRow(sqlStatement, i.Type, strings.ToLower(i.Hash), strings.ToLower(i.From), strings.ToLower(i.To), i.Value, i.Gas, i.GasUsed, i.Time, i.Input, i.Output).Scan(&returnValue) if qerr != nil { fmt.Println(qerr) panic(qerr) From d299d2baeba134d6830d1b785b8cec4757630551 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Wed, 1 Aug 2018 15:17:38 -0400 Subject: [PATCH 05/15] unit testing for account balances --- core/db.go | 29 +- core/shyft_database_util.go | 266 ++++++------- shyftDb/shyft_database_util_test.go | 563 ++++++++++++++-------------- 3 files changed, 439 insertions(+), 419 deletions(-) diff --git a/core/db.go b/core/db.go index 48f86ac7a1..366eafff95 100644 --- a/core/db.go +++ b/core/db.go @@ -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) + } +} diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 4e87f95c68..8ba92e7d05 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -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) } } - - diff --git a/shyftDb/shyft_database_util_test.go b/shyftDb/shyft_database_util_test.go index b7aa7092d9..eda8b56d54 100644 --- a/shyftDb/shyft_database_util_test.go +++ b/shyftDb/shyft_database_util_test.go @@ -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 := ð.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() } - From 112c47bf293ad6577f2971515ee1072bb254ce06 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Wed, 1 Aug 2018 15:44:18 -0400 Subject: [PATCH 06/15] unit tests for account balances wip --- core/shyft_database_util.go | 32 ++++++++++++++++++++++++++--- shyftDb/shyft_database_util_test.go | 23 +++++++++++---------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 8ba92e7d05..1448b7cc35 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -260,7 +260,7 @@ func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error { case err == sql.ErrNoRows: accountNonce := strconv.FormatUint(tx.Nonce(), 10) CreateAccount(sqldb, sendAndReceiveData.To, sendAndReceiveData.Amount, accountNonce) - balanceHelper(sqldb, sendAndReceiveData, value) + adjustBalanceFromAddr(sqldb, sendAndReceiveData, value) case err != nil: log.Fatal(err) default: @@ -269,6 +269,32 @@ func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error { 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) { fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From) 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 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) diff --git a/shyftDb/shyft_database_util_test.go b/shyftDb/shyft_database_util_test.go index eda8b56d54..c94e10130f 100644 --- a/shyftDb/shyft_database_util_test.go +++ b/shyftDb/shyft_database_util_test.go @@ -2,7 +2,6 @@ package shyftdb import ( "encoding/json" - "fmt" "math/big" "strconv" "strings" @@ -68,7 +67,6 @@ func TestBlock(t *testing.T) { receipts := []*types.Receipt{receipt} block := types.NewBlock(&types.Header{Number: big.NewInt(315)}, txs, nil, receipts) - // Write and verify the block in the database if err := core.SWriteBlock(block, receipts); err != nil { 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 { t.Fatalf("GetAllBlocksMinedByAddress [%v]: GetAllBlocksMinedByAddress did not return correctly", getAllBlocksMinedByAddress) } - fmt.Println("passed") core.ClearTables() }) @@ -215,7 +212,6 @@ func TestBlock(t *testing.T) { if allTxsFromBlock := core.SGetAllTransactionsFromBlock(sqldb, block2.Number().String()); len(allTxsFromBlock) == 0 { t.Fatalf("GetAllTransactionsFromBlock [%v]: GetAllTransactionsFromBlock did not return correctly", allTxsFromBlock) } - fmt.Println("Passed 2") core.ClearTables() }) @@ -316,7 +312,6 @@ func TestBlock(t *testing.T) { t.Fatalf("isContract [%v]: isContract bool is incorrect", isContract) } } - fmt.Println("Passed 3") core.ClearTables() }) // @@ -420,7 +415,6 @@ func TestBlock(t *testing.T) { if getAllTx := core.SGetAllTransactions(sqldb); len(getAllTx) == 0 { t.Fatalf("GetAllTransactions [%v]: GetAllTransactions did not return correctly", getAllTx) } - fmt.Println("Passed 4") core.ClearTables() }) // @@ -434,6 +428,9 @@ func TestBlock(t *testing.T) { } fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" + fromAddrEndBalance := "35" + fromAddrEndNonce := "4" + core.CreateAccount(sqldb, fromAddr, "50", "1") toAddr := common.BytesToAddress([]byte{0x11}) @@ -486,19 +483,23 @@ func TestBlock(t *testing.T) { var accountDataFrom core.SAccounts 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 { 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) } - - fmt.Println("Passed 5") core.ClearTables() }) - core.ClearTables() } From c121072d0fcaf83d3b3ad67bc5c5e8293f35d0ae Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Wed, 1 Aug 2018 15:58:14 -0400 Subject: [PATCH 07/15] added create from account for each test --- shyftDb/shyft_database_util_test.go | 55 +++++++++++++++++------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/shyftDb/shyft_database_util_test.go b/shyftDb/shyft_database_util_test.go index c94e10130f..dca93af92b 100644 --- a/shyftDb/shyft_database_util_test.go +++ b/shyftDb/shyft_database_util_test.go @@ -45,11 +45,11 @@ func TestBlock(t *testing.T) { 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}) + 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(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22}) + 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(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33}) + 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} @@ -67,15 +67,18 @@ func TestBlock(t *testing.T) { receipts := []*types.Receipt{receipt} block := types.NewBlock(&types.Header{Number: big.NewInt(315)}, txs, nil, receipts) - // Write and verify the block in the database - 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) } + fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" + core.CreateAccount(sqldb, fromAddr, "50", "1") + + // Write and verify the block in the database + if err := core.SWriteBlock(block, receipts); err != nil { + t.Fatalf("Failed to write block into database: %v", err) + } entry := core.SGetBlock(sqldb, block.Number().String()) byt := []byte(entry) @@ -132,11 +135,11 @@ func TestBlock(t *testing.T) { 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}) + 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(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22}) + 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(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33}) + 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} txs1 := []*types.Transaction{mytx3} @@ -158,6 +161,14 @@ func TestBlock(t *testing.T) { block2 := types.NewBlock(&types.Header{Number: big.NewInt(320)}, txs1, nil, receipts) blocks := []*types.Block{block, block2} + sqldb, err := core.DBConnection() + if err != nil { + panic(err) + } + + fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" + core.CreateAccount(sqldb, fromAddr, "50", "1") + for _, bc := range blocks { // Write and verify the block in the database if err := core.SWriteBlock(bc, receipts); err != nil { @@ -165,11 +176,6 @@ func TestBlock(t *testing.T) { } } - sqldb, err := core.DBConnection() - if err != nil { - panic(err) - } - response := core.SGetRecentBlock(sqldb) byteRes := []byte(response) var recentBlock core.SBlock @@ -320,11 +326,11 @@ func TestBlock(t *testing.T) { 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}) + 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(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22}) + 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(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33}) + 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} @@ -343,14 +349,18 @@ func TestBlock(t *testing.T) { 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) } + fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" + core.CreateAccount(sqldb, fromAddr, "50", "1") + + if err := core.SWriteBlock(block, receipts); err != nil { + t.Fatalf("Failed to write block into database: %v", err) + } + for _, tx := range txs { txn := core.SGetTransaction(sqldb, tx.Hash().String()) byt := []byte(txn) @@ -417,7 +427,7 @@ func TestBlock(t *testing.T) { } core.ClearTables() }) - // + t.Run("TestAccountsToReturnAccounts", func(t *testing.T) { key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") signer := types.NewEIP155Signer(big.NewInt(2147483647)) @@ -492,7 +502,6 @@ func TestBlock(t *testing.T) { 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 { t.Fatalf("GetAccountTxs [%v]: GetAccountTxs did not return correctly", getAllAccountTxs) } From 494e62c69ab50992a5b0fa1db6027299d626acd3 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Wed, 1 Aug 2018 15:59:38 -0400 Subject: [PATCH 08/15] wip --- core/shyft_database_util.go | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 1448b7cc35..1aa75f1119 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -271,28 +271,24 @@ func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error { 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: + if 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()) } + + 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) { From 2abd54c29e7d206c2f03b9336b9318791d24a9e1 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Thu, 2 Aug 2018 14:47:11 -0400 Subject: [PATCH 09/15] refactored unit tests to include set up and tear down --- core/shyft_database_util.go | 5 +- .../create_tables_test.psql | 20 +- .../postgres_setup_test/drop_tables_test.psql | 1 + shyftDb/shyft_database_util_test.go | 430 ++++++------------ 4 files changed, 164 insertions(+), 292 deletions(-) diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 1aa75f1119..b430e04274 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -271,6 +271,10 @@ func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error { func adjustBalanceFromAddr(sqldb *sql.DB, s SendAndReceive, value *big.Int) { fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From) + switch { + case err == sql.ErrNoRows: + fmt.Println("wtf", s.From) + } if err != nil { log.Fatal(err) } @@ -288,7 +292,6 @@ func adjustBalanceFromAddr(sqldb *sql.DB, s SendAndReceive, value *big.Int) { newAccountNonceSender.Add(fromNonce, nonceIncrement) UpdateAccount(sqldb, s.From, newBalanceSender.String(), newAccountNonceSender.String()) - //} } func balanceHelper(sqldb *sql.DB, s SendAndReceive, value *big.Int) { diff --git a/shyftDb/postgres_setup_test/create_tables_test.psql b/shyftDb/postgres_setup_test/create_tables_test.psql index 9d0a553cbf..98665c6bc9 100644 --- a/shyftDb/postgres_setup_test/create_tables_test.psql +++ b/shyftDb/postgres_setup_test/create_tables_test.psql @@ -16,7 +16,7 @@ CREATE TABLE IF NOT EXISTS blocks ( ); CREATE TABLE IF NOT EXISTS txs ( - txHash text, + txHash text primary key unique, to_addr text, from_addr text, blockhash text references blocks(hash), @@ -36,5 +36,19 @@ CREATE TABLE IF NOT EXISTS txs ( CREATE TABLE IF NOT EXISTS accounts ( addr text primary key unique, balance numeric, - accountnonce numeric -); \ No newline at end of file + accountNonce numeric +); + +CREATE TABLE IF NOT EXISTS internalTxs ( + id SERIAL PRIMARY KEY, + txHash text references txs(txHash), + type text, + to_addr text, + from_addr text, + amount text, + gas numeric, + gasUsed numeric, + time text, + input text, + output text +) \ No newline at end of file diff --git a/shyftDb/postgres_setup_test/drop_tables_test.psql b/shyftDb/postgres_setup_test/drop_tables_test.psql index 80625a022b..94390c17a7 100644 --- a/shyftDb/postgres_setup_test/drop_tables_test.psql +++ b/shyftDb/postgres_setup_test/drop_tables_test.psql @@ -1,3 +1,4 @@ +DROP TABLE internalTxs; DROP TABLE txs; DROP TABLE blocks; DROP TABLE accounts; \ No newline at end of file diff --git a/shyftDb/shyft_database_util_test.go b/shyftDb/shyft_database_util_test.go index dca93af92b..5e7fd39ad9 100644 --- a/shyftDb/shyft_database_util_test.go +++ b/shyftDb/shyft_database_util_test.go @@ -22,6 +22,7 @@ const ( ) func TestBlock(t *testing.T) { + //SET UP FOR TEST FUNCTIONS eth.NewShyftTestLDB() core.InitDBTest() shyftTracer := new(eth.ShyftTracer) @@ -40,135 +41,54 @@ func TestBlock(t *testing.T) { eth.InitTracerEnv() core.ClearTables() + 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(5), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11}) + mytx1, _ := 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{mytx1, mytx2} + txs1 := []*types.Transaction{mytx3} + + //Nonce,Value, GasLimit, Gasprice, data + contractCreation := types.NewContractCreation(1, big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11}) + mytx4, _ := types.SignTx(contractCreation, signer, key) + txs2 := []*types.Transaction{mytx4} + + receipt := &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{receipt} + + block1 := types.NewBlock(&types.Header{Number: big.NewInt(323)}, txs, nil, receipts) + block2 := types.NewBlock(&types.Header{Number: big.NewInt(320)}, txs1, nil, receipts) + block3 := types.NewBlock(&types.Header{Number: big.NewInt(322)}, txs2, nil, receipts) + blocks := []*types.Block{block1, block2, block3} + + sqldb, err := core.DBConnection() + if err != nil { + panic(err) + } + + fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" + fromAddrEndBalance := "75" + fromAddrEndNonce := "5" + toAddr := common.BytesToAddress([]byte{0x11}) + core.CreateAccount(sqldb, fromAddr, "201", "1") + t.Run("TestBlockToReturnBlock", 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(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} - - receipt := &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{receipt} - block := types.NewBlock(&types.Header{Number: big.NewInt(315)}, txs, nil, receipts) - - sqldb, err := core.DBConnection() - if err != nil { - panic(err) - } - fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" - core.CreateAccount(sqldb, fromAddr, "50", "1") - - // Write and verify the block in the database - if err := core.SWriteBlock(block, receipts); err != nil { - t.Fatalf("Failed to write block into database: %v", err) - } - - entry := core.SGetBlock(sqldb, block.Number().String()) - byt := []byte(entry) - var data core.SBlock - json.Unmarshal(byt, &data) - - //TODO Difficulty, rewards, age - if block.Hash().String() != data.Hash { - t.Fatalf("Block Hash [%v]: Block hash not found", block.Hash().String()) - } - if block.Coinbase().String() != data.Coinbase { - t.Fatalf("Block coinbase [%v]: Block coinbase not found", block.Coinbase().String()) - } - if block.Number().String() != data.Number { - t.Fatalf("Block number [%v]: Block number not found", block.Number().String()) - } - if block.GasUsed() != data.GasUsed { - t.Fatalf("Gas Used [%v]: Gas used not found", block.GasUsed()) - } - if block.GasLimit() != data.GasLimit { - t.Fatalf("Gas Limit [%v]: Gas limit not found", block.GasLimit()) - } - if block.Transactions().Len() != data.TxCount { - t.Fatalf("Tx Count [%v]: Tx Count not found", block.Transactions().Len()) - } - if len(block.Uncles()) != data.UncleCount { - t.Fatalf("Uncle count [%v]: Uncle count not found", len(block.Uncles())) - } - if block.ParentHash().String() != data.ParentHash { - t.Fatalf("Parent hash [%v]: Parent hash not found", block.ParentHash().String()) - } - if block.UncleHash().String() != data.UncleHash { - t.Fatalf("Uncle hash [%v]: Uncle hash not found", block.UncleHash().String()) - } - if block.Size().String() != data.Size { - t.Fatalf("Size [%v]: Size not found", block.Size().String()) - } - if block.Nonce() != data.Nonce { - t.Fatalf("Block nonce [%v]: Block nonce not found", block.Nonce()) - } - - if getAllBlocks := core.SGetAllBlocks(sqldb); len(getAllBlocks) == 0 { - t.Fatalf("GetAllBlocks [%v]: GetAllBlocks did not return correctly", getAllBlocks) - } - - if getAllBlocksMinedByAddress := core.SGetAllBlocksMinedByAddress(sqldb, block.Coinbase().String()); len(getAllBlocksMinedByAddress) == 0 { - t.Fatalf("GetAllBlocksMinedByAddress [%v]: GetAllBlocksMinedByAddress did not return correctly", getAllBlocksMinedByAddress) - } - core.ClearTables() - }) - - t.Run("TestGetRecentBlock", 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(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} - txs1 := []*types.Transaction{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(322)}, txs, nil, receipts) - block2 := types.NewBlock(&types.Header{Number: big.NewInt(320)}, txs1, nil, receipts) - blocks := []*types.Block{block, block2} - - sqldb, err := core.DBConnection() - if err != nil { - panic(err) - } - - fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" - core.CreateAccount(sqldb, fromAddr, "50", "1") - for _, bc := range blocks { // Write and verify the block in the database if err := core.SWriteBlock(bc, receipts); err != nil { @@ -176,90 +96,107 @@ func TestBlock(t *testing.T) { } } + entry := core.SGetBlock(sqldb, block1.Number().String()) + byt := []byte(entry) + var data core.SBlock + json.Unmarshal(byt, &data) + + //TODO Difficulty, rewards, age + if block1.Hash().String() != data.Hash { + t.Fatalf("Block Hash [%v]: Block hash not found", block1.Hash().String()) + } + if block1.Coinbase().String() != data.Coinbase { + t.Fatalf("Block coinbase [%v]: Block coinbase not found", block1.Coinbase().String()) + } + if block1.Number().String() != data.Number { + t.Fatalf("Block number [%v]: Block number not found", block1.Number().String()) + } + if block1.GasUsed() != data.GasUsed { + t.Fatalf("Gas Used [%v]: Gas used not found", block1.GasUsed()) + } + if block1.GasLimit() != data.GasLimit { + t.Fatalf("Gas Limit [%v]: Gas limit not found", block1.GasLimit()) + } + if block1.Transactions().Len() != data.TxCount { + t.Fatalf("Tx Count [%v]: Tx Count not found", block1.Transactions().Len()) + } + if len(block1.Uncles()) != data.UncleCount { + t.Fatalf("Uncle count [%v]: Uncle count not found", len(block1.Uncles())) + } + if block1.ParentHash().String() != data.ParentHash { + t.Fatalf("Parent hash [%v]: Parent hash not found", block1.ParentHash().String()) + } + if block1.UncleHash().String() != data.UncleHash { + t.Fatalf("Uncle hash [%v]: Uncle hash not found", block1.UncleHash().String()) + } + if block1.Size().String() != data.Size { + t.Fatalf("Size [%v]: Size not found", block1.Size().String()) + } + if block1.Nonce() != data.Nonce { + t.Fatalf("Block nonce [%v]: Block nonce not found", block1.Nonce()) + } + + if getAllBlocks := core.SGetAllBlocks(sqldb); len(getAllBlocks) == 0 { + t.Fatalf("GetAllBlocks [%v]: GetAllBlocks did not return correctly", getAllBlocks) + } + + if getAllBlocksMinedByAddress := core.SGetAllBlocksMinedByAddress(sqldb, block1.Coinbase().String()); len(getAllBlocksMinedByAddress) == 0 { + t.Fatalf("GetAllBlocksMinedByAddress [%v]: GetAllBlocksMinedByAddress did not return correctly", getAllBlocksMinedByAddress) + } + }) + + t.Run("TestGetRecentBlock", func(t *testing.T) { response := core.SGetRecentBlock(sqldb) byteRes := []byte(response) var recentBlock core.SBlock json.Unmarshal(byteRes, &recentBlock) - if block.Hash().String() != recentBlock.Hash { - t.Fatalf("Block Hash [%v]: Block hash not found", block.Hash().String()) + if block1.Hash().String() != recentBlock.Hash { + t.Fatalf("Block Hash [%v]: Block hash not found", block1.Hash().String()) } - if block.Coinbase().String() != recentBlock.Coinbase { - t.Fatalf("Block coinbase [%v]: Block coinbase not found", block.Coinbase().String()) + if block1.Coinbase().String() != recentBlock.Coinbase { + t.Fatalf("Block coinbase [%v]: Block coinbase not found", block1.Coinbase().String()) } - if block.Number().String() != recentBlock.Number { - t.Fatalf("Block number [%v]: Block number not found", block.Number().String()) + if block1.Number().String() != recentBlock.Number { + t.Fatalf("Block number [%v]: Block number not found", block1.Number().String()) } - if block.GasUsed() != recentBlock.GasUsed { - t.Fatalf("Gas Used [%v]: Gas used not found", block.GasUsed()) + if block1.GasUsed() != recentBlock.GasUsed { + t.Fatalf("Gas Used [%v]: Gas used not found", block1.GasUsed()) } - if block.GasLimit() != recentBlock.GasLimit { - t.Fatalf("Gas Limit [%v]: Gas limit not found", block.GasLimit()) + if block1.GasLimit() != recentBlock.GasLimit { + t.Fatalf("Gas Limit [%v]: Gas limit not found", block1.GasLimit()) } - if block.Transactions().Len() != recentBlock.TxCount { - t.Fatalf("Tx Count [%v]: Tx Count not found", block.Transactions().Len()) + if block1.Transactions().Len() != recentBlock.TxCount { + t.Fatalf("Tx Count [%v]: Tx Count not found", block1.Transactions().Len()) } - if len(block.Uncles()) != recentBlock.UncleCount { - t.Fatalf("Uncle count [%v]: Uncle count not found", len(block.Uncles())) + if len(block1.Uncles()) != recentBlock.UncleCount { + t.Fatalf("Uncle count [%v]: Uncle count not found", len(block1.Uncles())) } - if block.ParentHash().String() != recentBlock.ParentHash { - t.Fatalf("Parent hash [%v]: Parent hash not found", block.ParentHash().String()) + if block1.ParentHash().String() != recentBlock.ParentHash { + t.Fatalf("Parent hash [%v]: Parent hash not found", block1.ParentHash().String()) } - if block.UncleHash().String() != recentBlock.UncleHash { - t.Fatalf("Uncle hash [%v]: Uncle hash not found", block.UncleHash().String()) + if block1.UncleHash().String() != recentBlock.UncleHash { + t.Fatalf("Uncle hash [%v]: Uncle hash not found", block1.UncleHash().String()) } - if block.Size().String() != recentBlock.Size { - t.Fatalf("Size [%v]: Size not found", block.Size().String()) + if block1.Size().String() != recentBlock.Size { + t.Fatalf("Size [%v]: Size not found", block1.Size().String()) } - if block.Nonce() != recentBlock.Nonce { - t.Fatalf("Block nonce [%v]: Block nonce not found", block.Nonce()) + if block1.Nonce() != recentBlock.Nonce { + t.Fatalf("Block nonce [%v]: Block nonce not found", block1.Nonce()) } if allTxsFromBlock := core.SGetAllTransactionsFromBlock(sqldb, block2.Number().String()); len(allTxsFromBlock) == 0 { t.Fatalf("GetAllTransactionsFromBlock [%v]: GetAllTransactionsFromBlock did not return correctly", allTxsFromBlock) } - core.ClearTables() }) 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} - - 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) - - 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() } - sqldb, err := core.DBConnection() - if err != nil { - panic(err) - } - - for _, tx := range txs { + for _, tx := range txs2 { txn := core.SGetTransaction(sqldb, tx.Hash().String()) byt := []byte(txn) var data core.ShyftTxEntryPretty @@ -283,14 +220,14 @@ func TestBlock(t *testing.T) { 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 block1.GasLimit() != data.GasLimit { + t.Fatalf("Gas Limit [%v]: Gas limit not found", block1.GasLimit()) } - if block.Hash().String() != data.BlockHash { - t.Fatalf("Block Hash [%v]: Block hash not found", block.Hash().String()) + if block3.Hash().String() != data.BlockHash { + t.Fatalf("Block Hash [%v]: Block hash not found", block1.Hash().String()) } - if block.Number().String() != data.BlockNumber { - t.Fatalf("Block Number [%v]: Block number not found", block.Number().String()) + if block3.Number().String() != data.BlockNumber { + t.Fatalf("Block Number [%v]: Block number not found", block1.Number().String()) } if tx.Value().String() != data.Amount { t.Fatalf("Amount [%v]: Amount not found", tx.Value().String()) @@ -299,10 +236,10 @@ func TestBlock(t *testing.T) { t.Fatalf("Cost [%v]: Cost not found", tx.Cost().String()) } var status string - if receipt2.Status == 1 { + if receipt.Status == 1 { status = "SUCCESS" } - if receipt2.Status == 0 { + if receipt.Status == 0 { status = "FAIL" } if status != data.Status { @@ -318,49 +255,9 @@ func TestBlock(t *testing.T) { t.Fatalf("isContract [%v]: isContract bool is incorrect", isContract) } } - core.ClearTables() }) - // + 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(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} - - 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) - - sqldb, err := core.DBConnection() - if err != nil { - panic(err) - } - - fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" - core.CreateAccount(sqldb, fromAddr, "50", "1") - - if err := core.SWriteBlock(block, receipts); err != nil { - t.Fatalf("Failed to write block into database: %v", err) - } - for _, tx := range txs { txn := core.SGetTransaction(sqldb, tx.Hash().String()) byt := []byte(txn) @@ -386,14 +283,14 @@ func TestBlock(t *testing.T) { 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 block1.GasLimit() != data.GasLimit { + t.Fatalf("Gas Limit [%v]: Gas limit not found", block1.GasLimit()) } - if block.Hash().String() != data.BlockHash { - t.Fatalf("Block Hash [%v]: Block hash not found", block.Hash().String()) + if block1.Hash().String() != data.BlockHash { + t.Fatalf("Block Hash [%v]: Block hash not found", block1.Hash().String()) } - if block.Number().String() != data.BlockNumber { - t.Fatalf("Block Number [%v]: Block number not found", block.Number().String()) + if block1.Number().String() != data.BlockNumber { + t.Fatalf("Block Number [%v]: Block number not found", block1.Number().String()) } if tx.Value().String() != data.Amount { t.Fatalf("Amount [%v]: Amount not found", tx.Value().String()) @@ -402,10 +299,10 @@ func TestBlock(t *testing.T) { t.Fatalf("Cost [%v]: Cost not found", tx.Cost().String()) } var status string - if receipt1.Status == 1 { + if receipt.Status == 1 { status = "SUCCESS" } - if receipt1.Status == 0 { + if receipt.Status == 0 { status = "FAIL" } if status != data.Status { @@ -425,53 +322,8 @@ func TestBlock(t *testing.T) { if getAllTx := core.SGetAllTransactions(sqldb); len(getAllTx) == 0 { t.Fatalf("GetAllTransactions [%v]: GetAllTransactions did not return correctly", getAllTx) } - core.ClearTables() }) - 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) - } - - fromAddr := "0x71562b71999873db5b286df957af199ec94617f7" - fromAddrEndBalance := "35" - fromAddrEndNonce := "4" - - core.CreateAccount(sqldb, fromAddr, "50", "1") - toAddr := common.BytesToAddress([]byte{0x11}) - - //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} - - 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(319)}, txs, nil, receipts) - - if err := core.SWriteBlock(block, receipts); err != nil { - t.Fatalf("Failed to write block into database: %v", err) - } - for _, tx := range txs { accountAddrTo := core.SGetAccount(sqldb, tx.To().String()) byts := []byte(accountAddrTo) @@ -508,7 +360,9 @@ func TestBlock(t *testing.T) { if getAllAccounts := core.SGetAllAccounts(sqldb); len(getAllAccounts) == 0 { t.Fatalf("GetAllAccounts [%v]: GetAllAccounts did not return correctly", getAllAccounts) } - core.ClearTables() }) - core.ClearTables() } + +//Genesis.go functions +// WriteBlockZero +// WriteShyftGen From 3acdd3f5c5fd73777fc41448771c38080abb6c58 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Mon, 6 Aug 2018 17:38:01 -0400 Subject: [PATCH 10/15] 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 From d760a7caa76a8383b59ca9f923fb28e49b73d033 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Tue, 7 Aug 2018 13:38:08 -0400 Subject: [PATCH 11/15] refactored account balance updating --- core/shyft_database_util.go | 123 +++++++----------------------------- 1 file changed, 22 insertions(+), 101 deletions(-) diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index f9c4a01a75..5fdb04ae67 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -146,12 +146,12 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error { if block.Transactions().Len() > 0 { for _, tx := range block.Transactions() { swriteTransactions(sqldb, tx, block.Header().Hash(), blockData.Number, receipts, age, blockData.GasLimit) - if block.Transactions()[0].To() != nil { - swriteBalance(sqldb, tx) - } - if block.Transactions()[0].To() == nil { - swriteContractBalance(sqldb, tx) - } + // if block.Transactions()[0].To() != nil { + // swriteBalance(sqldb, tx) + // } + // if block.Transactions()[0].To() == nil { + // swriteContractBalance(sqldb, tx) + // } } } return nil @@ -216,55 +216,25 @@ func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.H return nil } -func swriteContractBalance(sqldb *sql.DB, tx *types.Transaction) error { +//SWriteInternalTxBalances Writes internal txs and updates balances +func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amount string) error { sendAndReceiveData := SendAndReceive{ - From: tx.From().Hex(), - Amount: tx.Value().String(), - AccountNonce: tx.Nonce(), + To: toAddr, + From: fromAddr, + Amount: amount, } - - fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, sendAndReceiveData.From) - - switch { - case err == sql.ErrNoRows: - accountNonce := strconv.FormatUint(tx.Nonce(), 10) - CreateAccount(sqldb, sendAndReceiveData.From, sendAndReceiveData.Amount, accountNonce) - 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, tx.Value()) - newAccountNonceSender.Add(fromNonce, nonceIncrement) - - UpdateAccount(sqldb, sendAndReceiveData.From, newBalanceSender.String(), newAccountNonceSender.String()) - } - return nil -} - -//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(), - Amount: tx.Value().String(), - } - value := tx.Value() _, _, err := AccountExists(sqldb, sendAndReceiveData.To) + value := new(big.Int) + value, _ = value.SetString(amount, 10) switch { case err == sql.ErrNoRows: - accountNonce := strconv.FormatUint(tx.Nonce(), 10) + accountNonce := "1" CreateAccount(sqldb, sendAndReceiveData.To, sendAndReceiveData.Amount, accountNonce) adjustBalanceFromAddr(sqldb, sendAndReceiveData, value) case err != nil: log.Fatal(err) default: - balanceHelper(sqldb, sendAndReceiveData, value) + balanceHelper(sqldb, sendAndReceiveData, amount) } return nil } @@ -293,7 +263,7 @@ func adjustBalanceFromAddr(sqldb *sql.DB, s SendAndReceive, value *big.Int) { 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, amount string) { fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From) toAddressBalance, toAccountNonce, err := AccountExists(sqldb, s.To) if err != nil { @@ -310,14 +280,18 @@ func balanceHelper(sqldb *sql.DB, s SendAndReceive, value *big.Int) { fromBalance := new(big.Int) fromBalance, _ = fromBalance.SetString(fromAddressBalance, 10) + amountValue := new(big.Int) + amountValue, _ = amountValue.SetString(amount, 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) + newBalanceReceiver.Add(toBalance, amountValue) + newBalanceSender.Sub(fromBalance, amountValue) newAccountNonceReceiver.Add(toNonce, nonceIncrement) newAccountNonceSender.Add(fromNonce, nonceIncrement) @@ -327,59 +301,6 @@ func balanceHelper(sqldb *sql.DB, s SendAndReceive, value *big.Int) { 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, - 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: - 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) - amountValue := new(big.Int) - amountValue, _ = amountValue.SetString(amount, 10) - - //ACCOUNT NONCES - toNonce := new(big.Int) - toNonce, _ = toNonce.SetString(toAccountNonce, 10) - fromNonce := new(big.Int) - fromNonce, _ = fromNonce.SetString(fromAccountNonce, 10) - - newBalanceReceiver.Add(toBalance, amountValue) - newBalanceSender.Sub(fromBalance, amountValue) - - 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 -} - // @NOTE: This function is extremely complex and requires heavy testing and knowdlege of edge cases: // uncle blocks, account balance updates based on reorgs, diverges that get dropped. // Reason for this is because the accounts are not deterministic like the block and tx hashes. From ba48ad13e9fc99a702c0e81d997ea0e2724dcbf2 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Tue, 7 Aug 2018 16:48:08 -0400 Subject: [PATCH 12/15] updating balances working correctly, test suite incomplete for accounts --- core/blockchain.go | 7 +++---- core/database_util.go | 9 +-------- core/shyft_database_util.go | 8 +------- eth/api_tracer.go | 4 +--- eth/shyft_tracer.go | 20 +++++++++++--------- eth/tracers/tracer.go | 24 ++++++++++++------------ shyftDb/shyft_database_util_test.go | 3 ++- 7 files changed, 31 insertions(+), 44 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 36d7a0a120..d7a8f65aff 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -29,7 +29,6 @@ import ( "sync/atomic" "time" - "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common/mclock" "github.com/ShyftNetwork/go-empyrean/consensus" @@ -92,10 +91,10 @@ type BlockChain struct { chainConfig *params.ChainConfig // Chain & network configuration cacheConfig *CacheConfig // Cache configuration for pruning - db ethdb.Database // Low level persistent database to store final content in + db ethdb.Database // Low level persistent database to store final content in - triegc *prque.Prque // Priority queue mapping block numbers to tries to gc - gcproc time.Duration // Accumulates canonical block processing for trie dumping + triegc *prque.Prque // Priority queue mapping block numbers to tries to gc + gcproc time.Duration // Accumulates canonical block processing for trie dumping hc *HeaderChain rmLogsFeed event.Feed diff --git a/core/database_util.go b/core/database_util.go index 256a2edd5d..9eea4863b4 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -21,8 +21,8 @@ import ( "encoding/binary" "encoding/json" "errors" - "math/big" "fmt" + "math/big" "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/core/types" @@ -283,14 +283,7 @@ func GetTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, // its added positional metadata. func GetTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { // Retrieve the lookup metadata and resolve the transaction from the body - //fmt.Println("INSIDE GET TRANSACTION ++++++++") - //fmt.Println("The hash and db reader are") - //fmt.Println(hash) - //fmt.Println(db) blockHash, blockNumber, txIndex := GetTxLookupEntry(db, hash) - //fmt.Println(blockHash) - //fmt.Println(blockNumber) - if blockHash != (common.Hash{}) { body := GetBody(db, blockHash, blockNumber) if body == nil || len(body.Transactions) <= int(txIndex) { diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 5fdb04ae67..ad6f6872cc 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -146,12 +146,6 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error { if block.Transactions().Len() > 0 { for _, tx := range block.Transactions() { swriteTransactions(sqldb, tx, block.Header().Hash(), blockData.Number, receipts, age, blockData.GasLimit) - // if block.Transactions()[0].To() != nil { - // swriteBalance(sqldb, tx) - // } - // if block.Transactions()[0].To() == nil { - // swriteContractBalance(sqldb, tx) - // } } } return nil @@ -243,7 +237,7 @@ func adjustBalanceFromAddr(sqldb *sql.DB, s SendAndReceive, value *big.Int) { fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From) switch { case err == sql.ErrNoRows: - fmt.Println("wtf", s.From) + fmt.Println("Need to write From", s.From) } if err != nil { log.Fatal(err) diff --git a/eth/api_tracer.go b/eth/api_tracer.go index 983f5913a9..13ca84fcf1 100644 --- a/eth/api_tracer.go +++ b/eth/api_tracer.go @@ -631,8 +631,6 @@ func (api *PrivateDebugAPI) StraceTx(ctx context.Context, message core.Message, } } - - // traceTx configures a new tracer according to the provided configuration, and // executes the given message in the provided environment. The return value will // be tracer dependent. @@ -646,7 +644,7 @@ func (api *PrivateDebugAPI) traceTx(ctx context.Context, message core.Message, v case config != nil && config.Tracer != nil: // Define a meaningful timeout of a single transaction trace - timeout := defaultTraceTimeout + timeout := defaultTraceTimeout if config.Timeout != nil { if timeout, err = time.ParseDuration(*config.Timeout); err != nil { diff --git a/eth/shyft_tracer.go b/eth/shyft_tracer.go index 550062c7b2..bfcc9ad653 100644 --- a/eth/shyft_tracer.go +++ b/eth/shyft_tracer.go @@ -1,15 +1,16 @@ package eth - import ( + "context" + "fmt" + "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/params" - "context" ) var EthereumObject interface{} -type ShyftTracer struct {} +type ShyftTracer struct{} var PrivateAPI *PrivateDebugAPI var Context context.Context @@ -23,21 +24,22 @@ func InitTracerEnv() { Context = ctx2 config := &TraceConfig{ LogConfig: nil, - Tracer: &jsTracer, // needs to be non-nil - Timeout: nil, - Reexec: nil, + Tracer: &jsTracer, // needs to be non-nil + Timeout: nil, + Reexec: nil, } TracerConfig = config fullNode, _ := SNew(Global_config) + fmt.Println("FULL NODE", fullNode) privateAPI := NewPrivateDebugAPI(config2, fullNode) PrivateAPI = privateAPI } -func (st ShyftTracer) GetTracerToRun (hash common.Hash) (interface{}, error) { +func (st ShyftTracer) GetTracerToRun(hash common.Hash) (interface{}, error) { return PrivateAPI.STraceTransaction(Context, hash, TracerConfig) } -func setEthObject(ethobj interface{}){ +func setEthObject(ethobj interface{}) { EthereumObject = ethobj } @@ -45,4 +47,4 @@ var Global_config *Config func SetGlobalConfig(c *Config) { Global_config = c -} \ No newline at end of file +} diff --git a/eth/tracers/tracer.go b/eth/tracers/tracer.go index f419834a37..4b81984a35 100644 --- a/eth/tracers/tracer.go +++ b/eth/tracers/tracer.go @@ -25,6 +25,8 @@ import ( "time" "unsafe" + "strconv" + "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/core" @@ -32,7 +34,6 @@ import ( "github.com/ShyftNetwork/go-empyrean/crypto" "github.com/ShyftNetwork/go-empyrean/log" "gopkg.in/olebedev/go-duktape.v3" - "strconv" ) // bigIntegerJS is the minified version of https://github.com/peterolson/BigInteger.js. @@ -604,18 +605,17 @@ func (i *Internals) SWriteInteralTxs(hash common.Hash) { amount := strconv.FormatUint(value, 10) iTx := core.InteralWrite{ - Hash: hash.Hex(), - Type: i.Type, - From: i.From, - To: i.To, - Value: amount, - Gas: gas, - GasUsed: gasUsed, - Input: i.Input, - Output: i.Output, - Time: i.Time, + Hash: hash.Hex(), + Type: i.Type, + From: i.From, + To: i.To, + Value: amount, + Gas: gas, + GasUsed: gasUsed, + Input: i.Input, + Output: i.Output, + Time: i.Time, } - //@TODO WRITE OVER TRANSACTION STRUCT core.SWriteInternalTxBalances(sqldb, i.To, i.From, amount) core.InsertInternalTx(sqldb, iTx) diff --git a/shyftDb/shyft_database_util_test.go b/shyftDb/shyft_database_util_test.go index d1ef772a4f..3151c00505 100644 --- a/shyftDb/shyft_database_util_test.go +++ b/shyftDb/shyft_database_util_test.go @@ -2,6 +2,7 @@ package shyftdb import ( "encoding/json" + "fmt" "math/big" "strconv" "strings" @@ -318,13 +319,13 @@ func TestBlock(t *testing.T) { 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) } }) t.Run("TestAccountsToReturnAccounts", func(t *testing.T) { for _, tx := range txs { + fmt.Println("test account", tx.To().String()) accountAddrTo := core.SGetAccount(sqldb, tx.To().String()) byts := []byte(accountAddrTo) var accountDataTo core.SAccounts From a37f60d6a39f84b7f264cb1033a6e48c372fc4c9 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Wed, 8 Aug 2018 17:43:56 -0400 Subject: [PATCH 13/15] removed fmt prints, created types file and fixed UI --- core/genesis.go | 76 +++--- core/sTypes/stypes.go | 92 +++++++ core/shyft_database_util.go | 186 +++++++------ core/shyft_get_utils.go | 252 +++++++++--------- eth/tracers/tracer.go | 3 +- .../components/table/accounts/accountRows.js | 3 +- .../table/accounts/accountsTable.js | 2 +- .../table/accounts/detailAccountsRow.js | 2 +- .../src/components/table/blocks/blockRows.js | 2 +- .../src/components/table/blocks/blockTable.js | 2 +- .../table/blocks/blocksDetailsRow.js | 2 +- .../components/table/blocks/blocksMined.js | 2 +- .../table/blocks/blocksMinedTable.js | 2 +- .../transactions/transactionDetailsRow.js | 2 +- .../table/transactions/transactionRow.js | 2 +- 15 files changed, 370 insertions(+), 260 deletions(-) create mode 100644 core/sTypes/stypes.go diff --git a/core/genesis.go b/core/genesis.go index c9d049d4fc..97cf009241 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -18,16 +18,20 @@ package core import ( "bytes" + "database/sql" "encoding/hex" "encoding/json" "errors" "fmt" "math/big" + "strconv" "strings" - "database/sql" + "time" + "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/common/math" + stypes "github.com/ShyftNetwork/go-empyrean/core/sTypes" "github.com/ShyftNetwork/go-empyrean/core/state" "github.com/ShyftNetwork/go-empyrean/core/types" "github.com/ShyftNetwork/go-empyrean/ethdb" @@ -35,8 +39,6 @@ import ( "github.com/ShyftNetwork/go-empyrean/params" "github.com/ShyftNetwork/go-empyrean/rlp" _ "github.com/lib/pq" - "strconv" - "time" ) //go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go @@ -157,7 +159,7 @@ func WriteShyftGen(gen *Genesis, block *types.Block) { //Appending GENESIS to address stored as txHash and From Addr Genesis := []string{"GENESIS_", k.String()} GENESIS := "GENESIS" - txHash := strings.Join(Genesis, k.String()) + txHash := strings.Join(Genesis, k.String()) //Create the accountNonce, set to 1 (1 incoming tx), format type accountNonce := v.Nonce + 1 accountNoncee := strconv.FormatUint(accountNonce, 10) @@ -168,22 +170,22 @@ func WriteShyftGen(gen *Genesis, block *types.Block) { } age := time.Unix(i, 0) - txData := ShyftTxEntryPretty { - TxHash: txHash, - From: GENESIS, - To: toAddr, - BlockHash: block.Header().Hash().Hex(), - BlockNumber:block.Header().Number.String(), - Amount: v.Balance.String(), - Cost: cost, - GasPrice: gasPrice, - GasLimit: block.GasLimit(), - Gas: block.GasUsed(), - Nonce: accountNonce, - Age: age, - Data: data, - Status: "SUCCESS", - IsContract: false, + txData := stypes.ShyftTxEntryPretty{ + TxHash: txHash, + From: GENESIS, + To: toAddr, + BlockHash: block.Header().Hash().Hex(), + BlockNumber: block.Header().Number.String(), + Amount: v.Balance.String(), + Cost: cost, + GasPrice: gasPrice, + GasLimit: block.GasLimit(), + Gas: block.GasUsed(), + Nonce: accountNonce, + Age: age, + Data: data, + Status: "SUCCESS", + IsContract: false, } //Create account and store tx CreateAccount(sqldb, k.String(), v.Balance.String(), accountNoncee) @@ -191,9 +193,11 @@ func WriteShyftGen(gen *Genesis, block *types.Block) { default: log.Info("Found Genesis Block") - }} + } + } } +//WriteShyftBlockZero writes block 0 to postgres db func WriteShyftBlockZero(block *types.Block, gen *Genesis) error { sqldb, _ := DBConnection() @@ -203,21 +207,21 @@ func WriteShyftBlockZero(block *types.Block, gen *Genesis) 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: len(gen.Alloc), - UncleCount: len(block.Uncles()), - Age: age, - ParentHash: block.ParentHash().String(), - UncleHash: block.UncleHash().String(), - Difficulty: block.Difficulty().String(), - Size: block.Size().String(), - Nonce: block.Nonce(), - Rewards: "0", + blockData := stypes.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: len(gen.Alloc), + UncleCount: len(block.Uncles()), + Age: age, + ParentHash: block.ParentHash().String(), + UncleHash: block.UncleHash().String(), + Difficulty: block.Difficulty().String(), + Size: block.Size().String(), + Nonce: block.Nonce(), + Rewards: "0", } err := BlockExists(sqldb, blockData.Hash) diff --git a/core/sTypes/stypes.go b/core/sTypes/stypes.go new file mode 100644 index 0000000000..5954403edf --- /dev/null +++ b/core/sTypes/stypes.go @@ -0,0 +1,92 @@ +package stypes + +import ( + "time" + + "github.com/ShyftNetwork/go-empyrean/common" +) + +//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 +} + +type InteralWrite struct { + Hash string + Type string + From string + To string + Value string + Gas uint64 + GasUsed uint64 + Input string + Output string + Time string +} + +//blockRes struct +type BlockRes struct { + hash string + coinbase string + number string + Blocks []SBlock +} + +type SAccounts struct { + Addr string + Balance string + AccountNonce string +} + +type AccountRes struct { + addr string + balance string + AllAccounts []SAccounts +} + +type TxRes struct { + TxEntry []ShyftTxEntryPretty +} + +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 +} + +type SendAndReceive struct { + To string + From string + Amount string + Address string + Balance string + AccountNonce uint64 `json:",string"` +} diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index ad6f6872cc..87750e46e6 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -11,103 +11,106 @@ import ( "github.com/ShyftNetwork/go-empyrean/common" Rewards "github.com/ShyftNetwork/go-empyrean/consensus/ethash" + stypes "github.com/ShyftNetwork/go-empyrean/core/sTypes" "github.com/ShyftNetwork/go-empyrean/core/types" "github.com/ShyftNetwork/go-empyrean/shyfttracerinterface" _ "github.com/lib/pq" ) +//IShyftTracer Used to initialize ShyftTracer var IShyftTracer shyfttracerinterface.IShyftTracer +//SetIShyftTracer sets tracer type func SetIShyftTracer(st shyfttracerinterface.IShyftTracer) { IShyftTracer = st } -//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 -} +// //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 +// } -type InteralWrite struct { - Hash string - Type string - From string - To string - Value string - Gas uint64 - GasUsed uint64 - Input string - Output string - Time string -} +// type InteralWrite struct { +// Hash string +// Type string +// From string +// To string +// Value string +// Gas uint64 +// GasUsed uint64 +// Input string +// Output string +// Time string +// } -//blockRes struct -type blockRes struct { - hash string - coinbase string - number string - Blocks []SBlock -} +// //blockRes struct +// // type blockRes struct { +// // hash string +// // coinbase string +// // number string +// // Blocks []SBlock +// // } -type SAccounts struct { - Addr string - Balance string - AccountNonce string -} +// type SAccounts struct { +// Addr string +// Balance string +// AccountNonce string +// } -type accountRes struct { - addr string - balance string - AllAccounts []SAccounts -} +// type accountRes struct { +// addr string +// balance string +// AllAccounts []SAccounts +// } -type txRes struct { - TxEntry []ShyftTxEntryPretty -} +// type txRes struct { +// TxEntry []ShyftTxEntryPretty +// } -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 -} +// 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 +// } -type SendAndReceive struct { - To string - From string - Amount string - Address string - Balance string - AccountNonce uint64 `json:",string"` -} +// type SendAndReceive struct { +// To string +// From string +// Amount string +// Address string +// Balance string +// AccountNonce uint64 `json:",string"` +// } -//WriteBlock writes to block info to sql db +//SWriteBlock writes to block info to sql db func SWriteBlock(block *types.Block, receipts []*types.Receipt) error { sqldb, err := DBConnection() if err != nil { @@ -123,7 +126,7 @@ func SWriteBlock(block *types.Block, receipts []*types.Receipt) error { } age := time.Unix(i, 0) - blockData := SBlock{ + blockData := stypes.SBlock{ Hash: block.Header().Hash().Hex(), Coinbase: block.Header().Coinbase.String(), Number: block.Header().Number.String(), @@ -185,7 +188,7 @@ func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.H toAddr = tx.To() } - txData := ShyftTxEntryPretty{ + txData := stypes.ShyftTxEntryPretty{ TxHash: tx.Hash().Hex(), From: tx.From().Hex(), To: toAddr, @@ -212,7 +215,7 @@ func swriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.H //SWriteInternalTxBalances Writes internal txs and updates balances func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amount string) error { - sendAndReceiveData := SendAndReceive{ + sendAndReceiveData := stypes.SendAndReceive{ To: toAddr, From: fromAddr, Amount: amount, @@ -233,11 +236,12 @@ func SWriteInternalTxBalances(sqldb *sql.DB, toAddr string, fromAddr string, amo return nil } -func adjustBalanceFromAddr(sqldb *sql.DB, s SendAndReceive, value *big.Int) { +func adjustBalanceFromAddr(sqldb *sql.DB, s stypes.SendAndReceive, value *big.Int) { fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From) switch { case err == sql.ErrNoRows: - fmt.Println("Need to write From", s.From) + CreateAccount(sqldb, s.From, "0", "1") + fmt.Println("New From account created") } if err != nil { log.Fatal(err) @@ -257,7 +261,7 @@ func adjustBalanceFromAddr(sqldb *sql.DB, s SendAndReceive, value *big.Int) { UpdateAccount(sqldb, s.From, newBalanceSender.String(), newAccountNonceSender.String()) } -func balanceHelper(sqldb *sql.DB, s SendAndReceive, amount string) { +func balanceHelper(sqldb *sql.DB, s stypes.SendAndReceive, amount string) { fromAddressBalance, fromAccountNonce, err := AccountExists(sqldb, s.From) toAddressBalance, toAccountNonce, err := AccountExists(sqldb, s.To) if err != nil { @@ -383,6 +387,8 @@ func sstoreReward(sqldb *sql.DB, address string, reward *big.Int) { /////////////////////// //DB Utility functions ////////////////////// + +//CreateAccount writes new account to Postgres Db 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) @@ -391,6 +397,7 @@ func CreateAccount(sqldb *sql.DB, addr string, balance string, accountNonce stri } } +//AccountExists checks if account exists in Postgres Db func AccountExists(sqldb *sql.DB, addr string) (string, string, error) { var addressBalance, accountNonce string sqlExistsStatement := `SELECT balance, accountNonce from accounts WHERE addr = ($1)` @@ -405,6 +412,7 @@ func AccountExists(sqldb *sql.DB, addr string) (string, string, error) { } } +//BlockExists checks if block exists in Postgres Db func BlockExists(sqldb *sql.DB, hash string) error { var res string sqlExistsStatement := `SELECT hash from blocks WHERE hash= ($1)` @@ -418,6 +426,7 @@ func BlockExists(sqldb *sql.DB, hash string) error { } } +//UpdateAccount updates account in Postgres Db func UpdateAccount(sqldb *sql.DB, addr string, balance string, accountNonce string) { updateSQLStatement := `UPDATE accounts SET balance = ($2), accountNonce = ($3) WHERE addr = ($1)` _, updateErr := sqldb.Exec(updateSQLStatement, strings.ToLower(addr), balance, accountNonce) @@ -426,7 +435,8 @@ func UpdateAccount(sqldb *sql.DB, addr string, balance string, accountNonce stri } } -func InsertBlock(sqldb *sql.DB, blockData SBlock) { +//InsertBlock writes block to Postgres Db +func InsertBlock(sqldb *sql.DB, blockData stypes.SBlock) { sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txCount, uncleCount, age, parentHash, uncleHash, difficulty, size, rewards, nonce) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10), ($11), ($12),($13), ($14)) RETURNING number` qerr := sqldb.QueryRow(sqlStatement, strings.ToLower(blockData.Hash), blockData.Coinbase, blockData.Number, blockData.GasUsed, blockData.GasLimit, blockData.TxCount, blockData.UncleCount, blockData.Age, blockData.ParentHash, blockData.UncleHash, blockData.Difficulty, blockData.Size, blockData.Rewards, blockData.Nonce).Scan(&blockData.Number) if qerr != nil { @@ -434,7 +444,8 @@ func InsertBlock(sqldb *sql.DB, blockData SBlock) { } } -func InsertTx(sqldb *sql.DB, txData ShyftTxEntryPretty) { +//InsertTx writes tx to Postgres Db +func InsertTx(sqldb *sql.DB, txData stypes.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) @@ -443,7 +454,8 @@ func InsertTx(sqldb *sql.DB, txData ShyftTxEntryPretty) { } } -func InsertInternalTx(sqldb *sql.DB, i InteralWrite) { +//InsertInternalTx writes internal tx to Postgres Db +func InsertInternalTx(sqldb *sql.DB, i stypes.InteralWrite) { var returnValue string sqlStatement := `INSERT INTO internaltxs(type, txhash, from_addr, to_addr, amount, gas, gasUsed, time, input, output) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10)) RETURNING txHash` qerr := sqldb.QueryRow(sqlStatement, i.Type, strings.ToLower(i.Hash), strings.ToLower(i.From), strings.ToLower(i.To), i.Value, i.Gas, i.GasUsed, i.Time, i.Input, i.Output).Scan(&returnValue) diff --git a/core/shyft_get_utils.go b/core/shyft_get_utils.go index c9fbeac3c3..9a650c5bff 100644 --- a/core/shyft_get_utils.go +++ b/core/shyft_get_utils.go @@ -1,17 +1,19 @@ package core import ( - "encoding/json" "database/sql" + "encoding/json" "fmt" "time" + + stypes "github.com/ShyftNetwork/go-empyrean/core/sTypes" ) /////////// // Getters ////////// func SGetAllBlocks(sqldb *sql.DB) string { - var arr blockRes + var arr stypes.BlockRes var blockArr string rows, err := sqldb.Query(`SELECT * FROM blocks`) if err != nil { @@ -25,23 +27,23 @@ func SGetAllBlocks(sqldb *sql.DB) string { var txCount, uncleCount int err = rows.Scan( - &hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num,) + &hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num) - arr.Blocks = append(arr.Blocks, SBlock{ - Hash: hash, - Coinbase: coinbase, - GasUsed: gasUsed, - GasLimit: gasLimit, - TxCount: txCount, - UncleCount: uncleCount, - AgeGet: age, - ParentHash: parentHash, - UncleHash: uncleHash, - Difficulty: difficulty, - Size: size, - Nonce: nonce, - Rewards: rewards, - Number: num, + arr.Blocks = append(arr.Blocks, stypes.SBlock{ + Hash: hash, + Coinbase: coinbase, + GasUsed: gasUsed, + GasLimit: gasLimit, + TxCount: txCount, + UncleCount: uncleCount, + AgeGet: age, + ParentHash: parentHash, + UncleHash: uncleHash, + Difficulty: difficulty, + Size: size, + Nonce: nonce, + Rewards: rewards, + Number: num, }) blocks, _ := json.Marshal(arr.Blocks) @@ -61,23 +63,23 @@ func SGetBlock(sqldb *sql.DB, blockNumber string) string { var txCount, uncleCount int row.Scan( - &hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num,) + &hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num) - block := SBlock{ - Hash: hash, - Coinbase: coinbase, - GasUsed: gasUsed, - GasLimit: gasLimit, - TxCount: txCount, + block := stypes.SBlock{ + Hash: hash, + Coinbase: coinbase, + GasUsed: gasUsed, + GasLimit: gasLimit, + TxCount: txCount, UncleCount: uncleCount, - AgeGet: age, - ParentHash: parentHash, - UncleHash: uncleHash, - Difficulty: difficulty, - Size: size, - Nonce: nonce, - Rewards: rewards, - Number: num, + AgeGet: age, + ParentHash: parentHash, + UncleHash: uncleHash, + Difficulty: difficulty, + Size: size, + Nonce: nonce, + Rewards: rewards, + Number: num, } json, _ := json.Marshal(block) return string(json) @@ -91,30 +93,30 @@ func SGetRecentBlock(sqldb *sql.DB) string { var txCount, uncleCount int row.Scan( - &hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num,) + &hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num) - block := SBlock{ - Hash: hash, - Coinbase: coinbase, - GasUsed: gasUsed, - GasLimit: gasLimit, - TxCount: txCount, + block := stypes.SBlock{ + Hash: hash, + Coinbase: coinbase, + GasUsed: gasUsed, + GasLimit: gasLimit, + TxCount: txCount, UncleCount: uncleCount, - AgeGet: age, - ParentHash: parentHash, - UncleHash: uncleHash, - Difficulty: difficulty, - Size: size, - Nonce: nonce, - Rewards: rewards, - Number: num, + AgeGet: age, + ParentHash: parentHash, + UncleHash: uncleHash, + Difficulty: difficulty, + Size: size, + Nonce: nonce, + Rewards: rewards, + Number: num, } json, _ := json.Marshal(block) return string(json) } func SGetAllTransactionsFromBlock(sqldb *sql.DB, blockNumber string) string { - var arr txRes + var arr stypes.TxRes var txx string sqlStatement := `SELECT * FROM txs WHERE blocknumber=$1` rows, err := sqldb.Query(sqlStatement, blockNumber) @@ -133,22 +135,22 @@ func SGetAllTransactionsFromBlock(sqldb *sql.DB, blockNumber string) string { &txhash, &to_addr, &from_addr, &blockhash, &blocknumber, &amount, &gasprice, &gas, &gasLimit, &txfee, &nonce, &status, &isContract, &age, &data, ) - arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{ - TxHash: txhash, + arr.TxEntry = append(arr.TxEntry, stypes.ShyftTxEntryPretty{ + TxHash: txhash, ToGet: to_addr, - From: from_addr, - BlockHash: blockhash, + From: from_addr, + BlockHash: blockhash, BlockNumber: blocknumber, - Amount: amount, - GasPrice: gasprice, - Gas: gas, - GasLimit: gasLimit, - Cost: txfee, - Nonce: nonce, - Status: status, + Amount: amount, + GasPrice: gasprice, + Gas: gas, + GasLimit: gasLimit, + Cost: txfee, + Nonce: nonce, + Status: status, IsContract: isContract, - Age: age, - Data: data, + Age: age, + Data: data, }) tx, _ := json.Marshal(arr.TxEntry) @@ -159,7 +161,7 @@ func SGetAllTransactionsFromBlock(sqldb *sql.DB, blockNumber string) string { } func SGetAllBlocksMinedByAddress(sqldb *sql.DB, coinbase string) string { - var arr blockRes + var arr stypes.BlockRes var blockArr string sqlStatement := `SELECT * FROM blocks WHERE coinbase=$1` rows, err := sqldb.Query(sqlStatement, coinbase) @@ -174,23 +176,23 @@ func SGetAllBlocksMinedByAddress(sqldb *sql.DB, coinbase string) string { var txCount, uncleCount int err = rows.Scan( - &hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num,) + &hash, &coinbase, &gasUsed, &gasLimit, &txCount, &uncleCount, &age, &parentHash, &uncleHash, &difficulty, &size, &nonce, &rewards, &num) - arr.Blocks = append(arr.Blocks, SBlock{ - Hash: hash, - Coinbase: coinbase, - GasUsed: gasUsed, - GasLimit: gasLimit, - TxCount: txCount, + arr.Blocks = append(arr.Blocks, stypes.SBlock{ + Hash: hash, + Coinbase: coinbase, + GasUsed: gasUsed, + GasLimit: gasLimit, + TxCount: txCount, UncleCount: uncleCount, - AgeGet: age, - ParentHash: parentHash, - UncleHash: uncleHash, - Difficulty: difficulty, - Size: size, - Nonce: nonce, - Rewards: rewards, - Number: num, + AgeGet: age, + ParentHash: parentHash, + UncleHash: uncleHash, + Difficulty: difficulty, + Size: size, + Nonce: nonce, + Rewards: rewards, + Number: num, }) blocks, _ := json.Marshal(arr.Blocks) @@ -202,7 +204,7 @@ func SGetAllBlocksMinedByAddress(sqldb *sql.DB, coinbase string) string { //GetAllTransactions getter fn for API func SGetAllTransactions(sqldb *sql.DB) string { - var arr txRes + var arr stypes.TxRes var txx string rows, err := sqldb.Query(`SELECT * FROM txs`) if err != nil { @@ -220,22 +222,22 @@ func SGetAllTransactions(sqldb *sql.DB) string { &txhash, &to_addr, &from_addr, &blockhash, &blocknumber, &amount, &gasprice, &gas, &gasLimit, &txfee, &nonce, &status, &isContract, &age, &data, ) - arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{ - TxHash: txhash, + arr.TxEntry = append(arr.TxEntry, stypes.ShyftTxEntryPretty{ + TxHash: txhash, ToGet: to_addr, - From: from_addr, - BlockHash: blockhash, + From: from_addr, + BlockHash: blockhash, BlockNumber: blocknumber, - Amount: amount, - GasPrice: gasprice, - Gas: gas, - GasLimit: gasLimit, - Cost: txfee, - Nonce: nonce, - Status: status, + Amount: amount, + GasPrice: gasprice, + Gas: gas, + GasLimit: gasLimit, + Cost: txfee, + Nonce: nonce, + Status: status, IsContract: isContract, - Age: age, - Data: data, + Age: age, + Data: data, }) tx, _ := json.Marshal(arr.TxEntry) @@ -258,39 +260,39 @@ func SGetTransaction(sqldb *sql.DB, txHash string) string { row.Scan( &txhash, &to_addr, &from_addr, &blockhash, &blocknumber, &amount, &gasprice, &gas, &gasLimit, &txfee, &nonce, &status, &isContract, &age, &data) - tx := ShyftTxEntryPretty{ + tx := stypes.ShyftTxEntryPretty{ TxHash: txhash, ToGet: to_addr, - From: from_addr, - BlockHash: blockhash, + From: from_addr, + BlockHash: blockhash, BlockNumber: blocknumber, Amount: amount, GasPrice: gasprice, Gas: gas, - GasLimit: gasLimit, - Cost: txfee, - Nonce: nonce, - Status: status, + GasLimit: gasLimit, + Cost: txfee, + Nonce: nonce, + Status: status, IsContract: isContract, - Age: age, - Data: data, + Age: age, + Data: data, } json, _ := json.Marshal(tx) return string(json) } -func InnerSGetAccount(sqldb *sql.DB, address string) (SAccounts, bool) { +func InnerSGetAccount(sqldb *sql.DB, address string) (stypes.SAccounts, bool) { sqlStatement := `SELECT * FROM accounts WHERE addr=$1;` var addr, balance, accountNonce string err := sqldb.QueryRow(sqlStatement, address).Scan(&addr, &balance, &accountNonce) if err == sql.ErrNoRows { - return SAccounts{}, false + return stypes.SAccounts{}, false } else { - account := SAccounts{ - Addr: addr, - Balance: balance, - AccountNonce: accountNonce, + account := stypes.SAccounts{ + Addr: addr, + Balance: balance, + AccountNonce: accountNonce, } return account, true } @@ -305,7 +307,7 @@ func SGetAccount(sqldb *sql.DB, address string) string { //GetAllAccounts returns all accounts and balances func SGetAllAccounts(sqldb *sql.DB) string { - var array accountRes + var array stypes.AccountRes var accountsArr, accountNonce string accs, err := sqldb.Query(` @@ -326,10 +328,10 @@ func SGetAllAccounts(sqldb *sql.DB) string { &addr, &balance, &accountNonce, ) - array.AllAccounts = append(array.AllAccounts, SAccounts{ - Addr: addr, - Balance: balance, - AccountNonce: accountNonce, + array.AllAccounts = append(array.AllAccounts, stypes.SAccounts{ + Addr: addr, + Balance: balance, + AccountNonce: accountNonce, }) accounts, _ := json.Marshal(array.AllAccounts) @@ -341,7 +343,7 @@ func SGetAllAccounts(sqldb *sql.DB) string { //GetAccount returns account balances func SGetAccountTxs(sqldb *sql.DB, address string) string { - var arr txRes + var arr stypes.TxRes var txx string sqlStatement := `SELECT * FROM txs WHERE to_addr=$1 OR from_addr=$1;` rows, err := sqldb.Query(sqlStatement, address) @@ -360,22 +362,22 @@ func SGetAccountTxs(sqldb *sql.DB, address string) string { &txhash, &to_addr, &from_addr, &blockhash, &blocknumber, &amount, &gasprice, &gas, &gasLimit, &txfee, &nonce, &status, &isContract, &age, &data, ) - arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{ - TxHash: txhash, + arr.TxEntry = append(arr.TxEntry, stypes.ShyftTxEntryPretty{ + TxHash: txhash, ToGet: to_addr, - From: from_addr, + From: from_addr, BlockHash: blockhash, BlockNumber: blocknumber, - Amount: amount, - GasPrice: gasprice, - Gas: gas, - GasLimit: gasLimit, - Cost: txfee, - Nonce: nonce, - Status: status, + Amount: amount, + GasPrice: gasprice, + Gas: gas, + GasLimit: gasLimit, + Cost: txfee, + Nonce: nonce, + Status: status, IsContract: isContract, - Age: age, - Data: data, + Age: age, + Data: data, }) tx, _ := json.Marshal(arr.TxEntry) diff --git a/eth/tracers/tracer.go b/eth/tracers/tracer.go index 4b81984a35..a6ee995de2 100644 --- a/eth/tracers/tracer.go +++ b/eth/tracers/tracer.go @@ -30,6 +30,7 @@ import ( "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/core" + stypes "github.com/ShyftNetwork/go-empyrean/core/sTypes" "github.com/ShyftNetwork/go-empyrean/core/vm" "github.com/ShyftNetwork/go-empyrean/crypto" "github.com/ShyftNetwork/go-empyrean/log" @@ -604,7 +605,7 @@ func (i *Internals) SWriteInteralTxs(hash common.Hash) { value, _ := hexutil.DecodeUint64(i.Value) amount := strconv.FormatUint(value, 10) - iTx := core.InteralWrite{ + iTx := stypes.InteralWrite{ Hash: hash.Hex(), Type: i.Type, From: i.From, diff --git a/shyftBlockExplorerUI/src/components/table/accounts/accountRows.js b/shyftBlockExplorerUI/src/components/table/accounts/accountRows.js index ecb160a04b..403eeaa2e5 100644 --- a/shyftBlockExplorerUI/src/components/table/accounts/accountRows.js +++ b/shyftBlockExplorerUI/src/components/table/accounts/accountRows.js @@ -21,7 +21,6 @@ class AccountTable extends Component { } render() { - let startNum = 1; const sorted = [...this.state.data]; sorted.sort((a, b) => Number(a.Balance) > Number(b.Balance)); @@ -38,7 +37,7 @@ class AccountTable extends Component { Percentage={percentage.toFixed(2)} Addr={data.Addr} Balance={conversion} - TxCountAccount={data.TxCountAccount} + AccountNonce={data.AccountNonce} detailAccountHandler={this.props.detailAccountHandler} /> }); diff --git a/shyftBlockExplorerUI/src/components/table/accounts/accountsTable.js b/shyftBlockExplorerUI/src/components/table/accounts/accountsTable.js index 62451059dd..2f708d449c 100644 --- a/shyftBlockExplorerUI/src/components/table/accounts/accountsTable.js +++ b/shyftBlockExplorerUI/src/components/table/accounts/accountsTable.js @@ -12,7 +12,7 @@ const AccountsTable = (props) => { {props.Balance} {props.Percentage}% - {props.TxCountAccount} + {props.AccountNonce} ) diff --git a/shyftBlockExplorerUI/src/components/table/accounts/detailAccountsRow.js b/shyftBlockExplorerUI/src/components/table/accounts/detailAccountsRow.js index 98ccdf8d27..3e9e532980 100644 --- a/shyftBlockExplorerUI/src/components/table/accounts/detailAccountsRow.js +++ b/shyftBlockExplorerUI/src/components/table/accounts/detailAccountsRow.js @@ -17,7 +17,7 @@ class AccountTransactionTable extends Component { age={data.Age} txHash={data.TxHash} blockNumber={data.BlockNumber} - to={data.To} + to={data.ToGet} from={data.From} value={amountConversion} cost={costConversion} diff --git a/shyftBlockExplorerUI/src/components/table/blocks/blockRows.js b/shyftBlockExplorerUI/src/components/table/blocks/blockRows.js index 378e48ecfb..dca2922e04 100644 --- a/shyftBlockExplorerUI/src/components/table/blocks/blockRows.js +++ b/shyftBlockExplorerUI/src/components/table/blocks/blockRows.js @@ -30,7 +30,7 @@ class BlocksTable extends Component { Hash={data.Hash} Number={data.Number} Coinbase={data.Coinbase} - Age={data.Age} + AgeGet={data.AgeGet} GasUsed={data.GasUsed} GasLimit={data.GasLimit} UncleCount={data.UncleCount} diff --git a/shyftBlockExplorerUI/src/components/table/blocks/blockTable.js b/shyftBlockExplorerUI/src/components/table/blocks/blockTable.js index 0f0a025832..174acc3b68 100644 --- a/shyftBlockExplorerUI/src/components/table/blocks/blockTable.js +++ b/shyftBlockExplorerUI/src/components/table/blocks/blockTable.js @@ -10,7 +10,7 @@ const BlockTable = (props) => { {props.Number} {props.Hash} - {props.Age} + {props.AgeGet} {props.TxCount} {props.UncleCount} props.getBlocksMined(props.Coinbase)}>{props.Coinbase} diff --git a/shyftBlockExplorerUI/src/components/table/blocks/blocksDetailsRow.js b/shyftBlockExplorerUI/src/components/table/blocks/blocksDetailsRow.js index 12d06bc0b6..64639c9b46 100644 --- a/shyftBlockExplorerUI/src/components/table/blocks/blocksDetailsRow.js +++ b/shyftBlockExplorerUI/src/components/table/blocks/blocksDetailsRow.js @@ -16,7 +16,7 @@ class DetailBlockTable extends Component { Age: - {data.Age} + {data.AgeGet} Txn: diff --git a/shyftBlockExplorerUI/src/components/table/blocks/blocksMined.js b/shyftBlockExplorerUI/src/components/table/blocks/blocksMined.js index 782da7fe42..63ccb3eac3 100644 --- a/shyftBlockExplorerUI/src/components/table/blocks/blocksMined.js +++ b/shyftBlockExplorerUI/src/components/table/blocks/blocksMined.js @@ -19,7 +19,7 @@ class BlocksMinedTable extends Component { Hash={data.Hash} Number={data.Number} Coinbase={data.Coinbase} - Age={data.Age} + AgeGet={data.AgeGet} GasUsed={data.GasUsed} GasLimit={data.GasLimit} UncleCount={data.UncleCount} diff --git a/shyftBlockExplorerUI/src/components/table/blocks/blocksMinedTable.js b/shyftBlockExplorerUI/src/components/table/blocks/blocksMinedTable.js index 503dc9322c..8f30d218dd 100644 --- a/shyftBlockExplorerUI/src/components/table/blocks/blocksMinedTable.js +++ b/shyftBlockExplorerUI/src/components/table/blocks/blocksMinedTable.js @@ -10,7 +10,7 @@ const MinedBlockTable = (props) => { {props.Number} {props.Hash} - {props.Age} + {props.AgeGet} {props.TxCount} {props.UncleCount} {props.Coinbase} diff --git a/shyftBlockExplorerUI/src/components/table/transactions/transactionDetailsRow.js b/shyftBlockExplorerUI/src/components/table/transactions/transactionDetailsRow.js index e130feb577..7578781fb7 100644 --- a/shyftBlockExplorerUI/src/components/table/transactions/transactionDetailsRow.js +++ b/shyftBlockExplorerUI/src/components/table/transactions/transactionDetailsRow.js @@ -32,7 +32,7 @@ class DetailTransactionTable extends Component { To: - { `${data.IsContract}` ? `${data.To} (Contract)` : `${data.To}` } + { `${data.IsContract}` ? `${data.ToGet} (Contract)` : `${data.ToGet}` } Value: diff --git a/shyftBlockExplorerUI/src/components/table/transactions/transactionRow.js b/shyftBlockExplorerUI/src/components/table/transactions/transactionRow.js index af3f4f0b94..c0d7e617cc 100644 --- a/shyftBlockExplorerUI/src/components/table/transactions/transactionRow.js +++ b/shyftBlockExplorerUI/src/components/table/transactions/transactionRow.js @@ -31,7 +31,7 @@ class TransactionTable extends Component { age={data.Age} txHash={data.TxHash} blockNumber={data.BlockNumber} - to={data.To} + to={data.ToGet} from={data.From} value={data.Amount} cost={conversion} From de13a538f6836b92ff63dc226ad0ad30ad579440 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Wed, 8 Aug 2018 17:59:26 -0400 Subject: [PATCH 14/15] resolved conflicts --- core/shyft_database_util.go | 85 ------------------------------------- 1 file changed, 85 deletions(-) diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 87750e46e6..3e955d5f97 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -25,91 +25,6 @@ func SetIShyftTracer(st shyfttracerinterface.IShyftTracer) { IShyftTracer = st } -// //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 -// } - -// type InteralWrite struct { -// Hash string -// Type string -// From string -// To string -// Value string -// Gas uint64 -// GasUsed uint64 -// Input string -// Output string -// Time string -// } - -// //blockRes struct -// // type blockRes struct { -// // hash string -// // coinbase string -// // number string -// // Blocks []SBlock -// // } - -// type SAccounts struct { -// Addr string -// Balance string -// AccountNonce string -// } - -// type accountRes struct { -// addr string -// balance string -// AllAccounts []SAccounts -// } - -// type txRes struct { -// TxEntry []ShyftTxEntryPretty -// } - -// 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 -// } - -// type SendAndReceive struct { -// To string -// From string -// Amount string -// Address string -// Balance string -// AccountNonce uint64 `json:",string"` -// } - //SWriteBlock writes to block info to sql db func SWriteBlock(block *types.Block, receipts []*types.Receipt) error { sqldb, err := DBConnection() From a908f3fd02ece6b7f8e3371fd312c9ae4ba72b8a Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 10 Aug 2018 21:23:21 +0300 Subject: [PATCH 15/15] remove fmt --- eth/shyft_tracer.go | 1 - 1 file changed, 1 deletion(-) diff --git a/eth/shyft_tracer.go b/eth/shyft_tracer.go index bfcc9ad653..083b21a06b 100644 --- a/eth/shyft_tracer.go +++ b/eth/shyft_tracer.go @@ -30,7 +30,6 @@ func InitTracerEnv() { } TracerConfig = config fullNode, _ := SNew(Global_config) - fmt.Println("FULL NODE", fullNode) privateAPI := NewPrivateDebugAPI(config2, fullNode) PrivateAPI = privateAPI }