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"
"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 {

View file

@ -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) {

View file

@ -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)