need to fix gen write before internals

This commit is contained in:
Dustin Brickwood 2018-07-25 16:36:16 -04:00
parent 8c95ce84d4
commit dc379b811c
3 changed files with 124 additions and 48 deletions

View file

@ -25,7 +25,7 @@ import (
"math/big" "math/big"
"strings" "strings"
_ "github.com/lib/pq" "database/sql"
"github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common"
"github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/common/hexutil"
"github.com/ShyftNetwork/go-empyrean/common/math" "github.com/ShyftNetwork/go-empyrean/common/math"
@ -35,7 +35,7 @@ import (
"github.com/ShyftNetwork/go-empyrean/log" "github.com/ShyftNetwork/go-empyrean/log"
"github.com/ShyftNetwork/go-empyrean/params" "github.com/ShyftNetwork/go-empyrean/params"
"github.com/ShyftNetwork/go-empyrean/rlp" "github.com/ShyftNetwork/go-empyrean/rlp"
"database/sql" _ "github.com/lib/pq"
"strconv" "strconv"
"time" "time"
) )
@ -143,52 +143,59 @@ func (e *GenesisMismatchError) Error() string {
//WriteShyftGen writes the genesis block to Shyft db //WriteShyftGen writes the genesis block to Shyft db
//@NOTE:SHYFT //@NOTE:SHYFT
func WriteShyftGen(gen *Genesis, block *types.Block) { func WriteShyftGen(gen *Genesis, block *types.Block) {
fmt.Println("IN WRITE GEN")
sqldb, _ := DBConnection() sqldb, _ := DBConnection()
for k := range gen.Alloc { for k, v := range gen.Alloc {
addr := k.String() addr := k.String()
var response string
sqlExistsStatement := `SELECT balance from accounts WHERE addr = ($1)` //var response string
err := sqldb.QueryRow(sqlExistsStatement, addr).Scan(&response) //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 { switch {
case err == sql.ErrNoRows: case err == sql.ErrNoRows:
for k, v := range gen.Alloc { //number := block.Header().Number.String()
number := block.Header().Number.String() //gasUsed := block.Header().GasUsed
gasUsed := block.Header().GasUsed //gasLimit := block.Header().GasLimit
gasLimit := block.Header().GasLimit //gasPrice := 0
gasPrice := 0 //txFee := 0
txFee := 0 //txStatus := ""
txStatus := "" //isContract := false
isContract := false //data := ""
data:= ""
addr := k.String() addr := k.String()
accountNonce := v.Nonce +1 accountNonce := v.Nonce + 1
i, err := strconv.ParseInt(block.Time().String(), 10, 64) //i, err := strconv.ParseInt(block.Time().String(), 10, 64)
if err != nil { //if err != nil {
panic(err) // panic(err)
} //}
age := time.Unix(i, 0) //age := time.Unix(i, 0)
Genesis := []string{"GENESIS_", addr} //Genesis := []string{"GENESIS_", addr}
GENESIS := "GENESIS" //GENESIS := "GENESIS"
txHash := strings.Join(Genesis, addr) //txHash := strings.Join(Genesis, addr)
sqlStatement := `INSERT INTO accounts(addr, balance, accountnonce) VALUES(($1), ($2), ($3)) RETURNING addr` //sqlStatement := `INSERT INTO accounts(addr, balance, accountnonce) VALUES(($1), ($2), ($3)) RETURNING addr`
insertErr := sqldb.QueryRow(sqlStatement, addr, v.Balance.String(), accountNonce).Scan(&addr) //insertErr := sqldb.QueryRow(sqlStatement, strings.ToLower(addr), v.Balance.String(), accountNonce).Scan(&addr)
if insertErr != nil { //if insertErr != nil {
panic(insertErr) // panic(insertErr)
} //}
var retNonce string accountNoncee := strconv.FormatUint(accountNonce, 10)
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` CreateAccount(sqldb, addr, v.Balance.String(), accountNoncee)
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 { //var retNonce string
panic(insertError) //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: default:
log.Info("Found Genesis Block") log.Info("Found Genesis Block")
}}} }
}
}
func WriteShyftBlockZero(block *types.Block, gen *Genesis) error { func WriteShyftBlockZero(block *types.Block, gen *Genesis) error {
@ -229,6 +236,7 @@ func WriteShyftBlockZero(block *types.Block, gen *Genesis) error {
} }
return nil return nil
} }
// SetupGenesisBlock writes or updates the genesis block in db. // SetupGenesisBlock writes or updates the genesis block in db.
// The block that will be used is: // The block that will be used is:
// //
@ -315,6 +323,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
return params.AllEthashProtocolChanges return params.AllEthashProtocolChanges
} }
} }
// ToBlock creates the genesis block and writes state of a genesis specification // ToBlock creates the genesis block and writes state of a genesis specification
// to the given database (or discards it if nil). // to the given database (or discards it if nil).
func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
@ -439,6 +448,7 @@ func DefaultRinkebyGenesisBlock() *Genesis {
Alloc: decodePrealloc(rinkebyAllocData), Alloc: decodePrealloc(rinkebyAllocData),
} }
} }
// DeveloperGenesisBlock returns the 'geth --dev' genesis block. Note, this must // DeveloperGenesisBlock returns the 'geth --dev' genesis block. Note, this must
// be seeded with the // be seeded with the
func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis { func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {

View file

@ -11,6 +11,8 @@ import (
"github.com/ShyftNetwork/go-empyrean/core/types" "github.com/ShyftNetwork/go-empyrean/core/types"
Rewards "github.com/ShyftNetwork/go-empyrean/consensus/ethash" Rewards "github.com/ShyftNetwork/go-empyrean/consensus/ethash"
"github.com/ShyftNetwork/go-empyrean/shyfttracerinterface" "github.com/ShyftNetwork/go-empyrean/shyfttracerinterface"
"fmt"
"strings"
) )
var IShyftTracer shyfttracerinterface.IShyftTracer var IShyftTracer shyfttracerinterface.IShyftTracer
@ -290,6 +292,64 @@ func swriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error {
return nil 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: // @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. // 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. // 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) addressBalance, accountNonce, err := AccountExists(sqldb, address)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
fmt.Println("this ran", address)
// Addr does not exist, thus create new entry // Addr does not exist, thus create new entry
// We convert totalReward into a string and postgres converts into number // We convert totalReward into a string and postgres converts into number
CreateAccount(sqldb, address, reward.String(), "1") CreateAccount(sqldb, address, reward.String(), "1")
@ -379,11 +440,12 @@ func sstoreReward(sqldb *sql.DB, address string, reward *big.Int) {
//DB Utility functions //DB Utility functions
////////////////////// //////////////////////
func CreateAccount (sqldb *sql.DB, addr string, balance string, accountNonce string) { func CreateAccount (sqldb *sql.DB, addr string, balance string, accountNonce string) {
sqlStatement := `INSERT INTO accounts(addr, balance, accountNonce) VALUES(($1), ($2), ($3)) RETURNING addr` fmt.Println("[CREATE ACCOUNT]", strings.ToLower(addr))
insertErr := sqldb.QueryRow(sqlStatement, addr, balance, accountNonce).Scan(&addr) //sqlStatement := `INSERT INTO accounts(addr, balance, accountNonce) VALUES(($1), ($2), ($3)) RETURNING addr`
if insertErr != nil { //insertErr := sqldb.QueryRow(sqlStatement, strings.ToLower(addr), balance, accountNonce).Scan(&addr)
panic(insertErr) //if insertErr != nil {
} // panic(insertErr)
//}
} }
func AccountExists (sqldb *sql.DB, addr string) (string, string, error) { func AccountExists (sqldb *sql.DB, addr string) (string, string, error) {

View file

@ -603,6 +603,10 @@ func (i *Internals) SWriteInteralTxs(hash common.Hash) {
value, _ := hexutil.DecodeUint64(i.Value) value, _ := hexutil.DecodeUint64(i.Value)
amount := strconv.FormatUint(value, 10) amount := strconv.FormatUint(value, 10)
//@TODO WRITE OVER TRANSACTION STRUCT
core.SWriteInternalTxBalances(sqldb, i.To, i.From, amount)
var returnValue string 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` 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) qerr := sqldb.QueryRow(sqlStatement, i.Type, hash.Hex(), i.From, i.To, amount, gas, gasUsed, i.Time, i.Input, i.Output).Scan(&returnValue)