mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
fix conflicts
This commit is contained in:
parent
8ae73e83c2
commit
53247aaca4
6 changed files with 207 additions and 115 deletions
|
|
@ -54,25 +54,47 @@ func GetAllTransactions(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, txs)
|
fmt.Fprintln(w, txs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBalance gets balance
|
// GetAccount gets balance
|
||||||
func GetBalance(w http.ResponseWriter, r *http.Request) {
|
func GetAccount(w http.ResponseWriter, r *http.Request) {
|
||||||
// vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
// address := vars["address"]
|
address := vars["address"]
|
||||||
//addressBytes := []byte(address)
|
//addressBytes := []byte(address)
|
||||||
|
fmt.Println("ADDRESS FROM ROUTE", address)
|
||||||
|
connStr := "user=postgres dbname=shyftdb sslmode=disable"
|
||||||
|
blockExplorerDb, err := sql.Open("postgres", connStr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
getAccountBalance := shyftdb.GetAccount(blockExplorerDb, address)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
//fmt.Fprintln(w, "Get Balances", addresses)
|
fmt.Fprintln(w, getAccountBalance)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBalances gets balances
|
// GetAllAccounts gets balances
|
||||||
func GetBalances(w http.ResponseWriter, r *http.Request) {
|
func GetAllAccounts(w http.ResponseWriter, r *http.Request) {
|
||||||
|
connStr := "user=postgres dbname=shyftdb sslmode=disable"
|
||||||
|
blockExplorerDb, err := sql.Open("postgres", connStr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
allAccounts := shyftdb.GetAllAccounts(blockExplorerDb)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
//fmt.Fprintln(w, "Get Balances", addresses)
|
fmt.Fprintln(w, allAccounts)
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetBlock returns block json
|
//GetBlock returns block json
|
||||||
|
|
@ -98,23 +120,18 @@ func GetBlock(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// GetAllBlocks response
|
// GetAllBlocks response
|
||||||
func GetAllBlocks(w http.ResponseWriter, r *http.Request) {
|
func GetAllBlocks(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
connStr := "user=postgres dbname=shyftdb sslmode=disable"
|
connStr := "user=postgres dbname=shyftdb sslmode=disable"
|
||||||
blockExplorerDb, err := sql.Open("postgres", connStr)
|
blockExplorerDb, err := sql.Open("postgres", connStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
block3 := shyftdb.GetAllBlocks(blockExplorerDb)
|
block3 := shyftdb.GetAllBlocks(blockExplorerDb)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
fmt.Fprintln(w, block3)
|
fmt.Fprintln(w, block3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,16 +16,16 @@ type Routes []Route
|
||||||
|
|
||||||
var routes = Routes{
|
var routes = Routes{
|
||||||
Route{
|
Route{
|
||||||
"GetBalance",
|
"GetAccount",
|
||||||
"GET",
|
"GET",
|
||||||
"/api/get_balance/",
|
"/api/get_account/{address}",
|
||||||
GetBalance,
|
GetAccount,
|
||||||
},
|
},
|
||||||
Route{
|
Route{
|
||||||
"GetBalances",
|
"GetAllAccounts",
|
||||||
"GET",
|
"GET",
|
||||||
"/api/get_balances/{addresses}",
|
"/api/get_all_accounts",
|
||||||
GetBalances,
|
GetAllAccounts,
|
||||||
},
|
},
|
||||||
Route{
|
Route{
|
||||||
"GetAllBlocks",
|
"GetAllBlocks",
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ CREATE TABLE IF NOT EXISTS blocks (
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS txs (
|
CREATE TABLE IF NOT EXISTS txs (
|
||||||
txHash text primary key,
|
txHash text,
|
||||||
to_addr text references accounts(addr),
|
to_addr text,
|
||||||
from_addr text references accounts(addr),
|
from_addr text,
|
||||||
blockhash text references blocks(hash),
|
blockhash text references blocks(hash),
|
||||||
amount numeric,
|
amount numeric,
|
||||||
gasprice numeric,
|
gasprice numeric,
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
DROP TABLE txs;
|
DROP TABLE txs;
|
||||||
DROP TABLE blocks;
|
DROP TABLE blocks;
|
||||||
|
DROP TABLE accounts;
|
||||||
|
|
@ -10,6 +10,8 @@ import (
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
|
"log"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -28,8 +30,15 @@ type blockRes struct {
|
||||||
Blocks []SBlock
|
Blocks []SBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
type txRes struct {
|
type SAccounts struct {
|
||||||
TxEntry []ShyftTxEntryPretty
|
Addr string
|
||||||
|
Balance string
|
||||||
|
}
|
||||||
|
|
||||||
|
type accountRes struct {
|
||||||
|
addr string
|
||||||
|
balance string
|
||||||
|
AllAccounts []SAccounts
|
||||||
}
|
}
|
||||||
|
|
||||||
//ShyftTxEntry structure
|
//ShyftTxEntry structure
|
||||||
|
|
@ -45,6 +54,10 @@ type ShyftTxEntry struct {
|
||||||
Data []byte
|
Data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type txRes struct {
|
||||||
|
TxEntry []ShyftTxEntryPretty
|
||||||
|
}
|
||||||
|
|
||||||
type ShyftTxEntryPretty struct {
|
type ShyftTxEntryPretty struct {
|
||||||
TxHash string
|
TxHash string
|
||||||
To string
|
To string
|
||||||
|
|
@ -62,9 +75,12 @@ type ShyftAccountEntry struct {
|
||||||
Txs []string
|
Txs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ShyftSenderEntry struct {
|
type SendAndReceive struct {
|
||||||
Address string
|
To string
|
||||||
Balance *big.Int
|
From string
|
||||||
|
Amount string
|
||||||
|
Address string
|
||||||
|
Balance string
|
||||||
}
|
}
|
||||||
|
|
||||||
//WriteBlock writes to block info to sql db
|
//WriteBlock writes to block info to sql db
|
||||||
|
|
@ -131,84 +147,91 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//WriteSenderBalance writes senders balance to accounts db
|
//WriteFromBalance writes senders balance to accounts db
|
||||||
func WriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error {
|
func WriteFromBalance(sqldb *sql.DB, tx *types.Transaction) error {
|
||||||
sender := ShyftSenderEntry{
|
sendAndReceiveData, balanceRec, balanceSen := WriteBalanceHelper(sqldb, tx)
|
||||||
Address: tx.From().Hex(),
|
toAddr := sendAndReceiveData.To
|
||||||
Balance: tx.Value(),
|
fromAddr := sendAndReceiveData.From
|
||||||
}
|
amount := sendAndReceiveData.Amount
|
||||||
|
balanceReceiver := balanceRec
|
||||||
|
balanceSender := balanceSen
|
||||||
|
|
||||||
addr := sender.Address
|
var response string
|
||||||
balance := sender.Balance.String()
|
sqlExistsStatement := `SELECT balance from accounts WHERE addr = ($1)`
|
||||||
fmt.Println("++++++++++++++++++++++", addr)
|
err := sqldb.QueryRow(sqlExistsStatement, toAddr).Scan(&response)
|
||||||
fmt.Println("++++++++++++++++++++++", balance)
|
switch {
|
||||||
sqlStatement := `INSERT INTO accounts(addr, balance) VALUES(($1), ($2)) RETURNING addr`
|
case err == sql.ErrNoRows:
|
||||||
qerr := sqldb.QueryRow(sqlStatement, addr, balance).Scan(&addr)
|
fmt.Println("No rows error :)")
|
||||||
if qerr != nil {
|
|
||||||
panic(qerr)
|
sqlStatement := `INSERT INTO accounts(addr, balance) VALUES(($1), ($2)) RETURNING addr`
|
||||||
|
insertErr := sqldb.QueryRow(sqlStatement, toAddr, amount).Scan(&toAddr)
|
||||||
|
if insertErr != nil {
|
||||||
|
panic(insertErr)
|
||||||
|
}
|
||||||
|
case err != nil:
|
||||||
|
log.Fatal(err)
|
||||||
|
default:
|
||||||
|
|
||||||
|
var newBalanceReceiver big.Int
|
||||||
|
var newBalanceSender big.Int
|
||||||
|
updateSQLStatement := `UPDATE accounts SET balance = ($2) WHERE addr = ($1)`
|
||||||
|
|
||||||
|
r := new(big.Int)
|
||||||
|
_, err := fmt.Sscan(balanceReceiver, r)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error scanning value:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s := new(big.Int)
|
||||||
|
_, error := fmt.Sscan(balanceSender, s)
|
||||||
|
if error != nil {
|
||||||
|
log.Println("error scanning value:", error)
|
||||||
|
}
|
||||||
|
|
||||||
|
newBalanceReceiver.Add(r, tx.Value())
|
||||||
|
newBalanceSender.Sub(s, tx.Value())
|
||||||
|
|
||||||
|
_, err = sqldb.Exec(updateSQLStatement, toAddr, newBalanceReceiver.String())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = sqldb.Exec(updateSQLStatement, fromAddr, newBalanceSender.String())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// key := append([]byte("acc-")[:], tx.From().Hash().Bytes()[:]...)
|
func WriteBalanceHelper(sqldb *sql.DB, tx *types.Transaction) (SendAndReceive, string, string) {
|
||||||
// // The from (sender) addr must have balance. If it fails to retrieve there is a bigger issue.
|
sendAndReceiveData := SendAndReceive{
|
||||||
// retrievedData, err := db.Get(key, nil)
|
To: tx.To().Hex(),
|
||||||
// if err != nil {
|
From: tx.From().Hex(),
|
||||||
// log.Crit("From MUST have eth and no record found", "err", err)
|
Amount: tx.Value().String(),
|
||||||
// }
|
}
|
||||||
// var decodedData ShyftAccountEntry
|
|
||||||
// d := gob.NewDecoder(bytes.NewBuffer(retrievedData))
|
|
||||||
// if err := d.Decode(&decodedData); err != nil {
|
|
||||||
// log.Crit("Failed to decode From data:", "err", err)
|
|
||||||
// }
|
|
||||||
// decodedData.Balance.Sub(decodedData.Balance, tx.Value())
|
|
||||||
// decodedData.Txs = append(decodedData.Txs, tx.Hash().String())
|
|
||||||
// // Encode updated data
|
|
||||||
// var encodedData bytes.Buffer
|
|
||||||
// encoder := gob.NewEncoder(&encodedData)
|
|
||||||
// if err := encoder.Encode(decodedData); err != nil {
|
|
||||||
// log.Crit("Faild to encode From Account data", "err", err)
|
|
||||||
// }
|
|
||||||
// if err := db.Put(key, encodedData.Bytes(), nil); err != nil {
|
|
||||||
// log.Crit("Could not write the From account data", "err", err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func WriteToBalance(db *leveldb.DB, tx *types.Transaction) {
|
toAddr := sendAndReceiveData.To
|
||||||
// key := append([]byte("acc-")[:], tx.To().Hash().Bytes()[:]...)
|
fromAddr := sendAndReceiveData.From
|
||||||
// var txs []string
|
|
||||||
|
|
||||||
// retrievedData, err := db.Get(key, nil)
|
getAccountBalanceReceiver := GetAccount(sqldb, toAddr)
|
||||||
// if err != nil {
|
getAccountBalanceSender:= GetAccount(sqldb, fromAddr)
|
||||||
// accData := ShyftAccountEntry{
|
|
||||||
// Balance: tx.Value(),
|
var receiverBalance SendAndReceive
|
||||||
// Txs: append(txs, tx.Hash().String()),
|
if err := json.Unmarshal([]byte(getAccountBalanceReceiver), &receiverBalance); err != nil {
|
||||||
// }
|
log.Fatal(err)
|
||||||
// var encodedData bytes.Buffer
|
}
|
||||||
// encoder := gob.NewEncoder(&encodedData)
|
|
||||||
// if err := encoder.Encode(accData); err != nil {
|
var senderBalance SendAndReceive
|
||||||
// log.Crit("Faild to encode To Account data", "err", err)
|
if err := json.Unmarshal([]byte(getAccountBalanceSender), &senderBalance); err != nil {
|
||||||
// }
|
log.Fatal(err)
|
||||||
// if err := db.Put(key, encodedData.Bytes(), nil); err != nil {
|
}
|
||||||
// log.Crit("Could not write the TO account's first tx", "err", err)
|
|
||||||
// }
|
balanceReceiver := receiverBalance.Balance
|
||||||
// }
|
balanceSender := senderBalance.Balance
|
||||||
// var decodedData ShyftAccountEntry
|
|
||||||
// d := gob.NewDecoder(bytes.NewBuffer(retrievedData))
|
return sendAndReceiveData, balanceReceiver, balanceSender
|
||||||
// if err := d.Decode(&decodedData); err != nil {
|
}
|
||||||
// log.Crit("Failed to decode To account data:", "err", err)
|
|
||||||
// }
|
|
||||||
// decodedData.Balance.Add(decodedData.Balance, tx.Value())
|
|
||||||
// decodedData.Txs = append(decodedData.Txs, tx.Hash().String())
|
|
||||||
// // Encode updated data
|
|
||||||
// var encodedData bytes.Buffer
|
|
||||||
// encoder := gob.NewEncoder(&encodedData)
|
|
||||||
// if err := encoder.Encode(decodedData); err != nil {
|
|
||||||
// log.Crit("Faild to encode To Account data", "err", err)
|
|
||||||
// }
|
|
||||||
// if err := db.Put(key, encodedData.Bytes(), nil); err != nil {
|
|
||||||
// log.Crit("Could not write the To account data", "err", err)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @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.
|
||||||
|
|
@ -388,14 +411,14 @@ func GetAllTransactions(sqldb *sql.DB) string {
|
||||||
//GetTransaction fn returns single tx
|
//GetTransaction fn returns single tx
|
||||||
func GetTransaction(sqldb *sql.DB) string {
|
func GetTransaction(sqldb *sql.DB) string {
|
||||||
sqlStatement := `SELECT
|
sqlStatement := `SELECT
|
||||||
txhash,
|
txhash,
|
||||||
to_addr,
|
to_addr,
|
||||||
from_addr,
|
from_addr,
|
||||||
blockhash,
|
blockhash,
|
||||||
amount,
|
amount,
|
||||||
gasprice,
|
gasprice,
|
||||||
gas,
|
gas,
|
||||||
nonce
|
nonce
|
||||||
FROM txs WHERE nonce=$1;`
|
FROM txs WHERE nonce=$1;`
|
||||||
row := sqldb.QueryRow(sqlStatement, 1)
|
row := sqldb.QueryRow(sqlStatement, 1)
|
||||||
var txhash string
|
var txhash string
|
||||||
|
|
@ -422,3 +445,55 @@ func GetTransaction(sqldb *sql.DB) string {
|
||||||
|
|
||||||
return string(json)
|
return string(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//GetAccount returns account balances
|
||||||
|
func GetAccount(sqldb *sql.DB, address string) string {
|
||||||
|
sqlStatement := `SELECT * FROM accounts WHERE addr=$1;`
|
||||||
|
row := sqldb.QueryRow(sqlStatement, address)
|
||||||
|
var addr string
|
||||||
|
var balance string
|
||||||
|
|
||||||
|
row.Scan(&addr, &balance)
|
||||||
|
|
||||||
|
account := SAccounts{
|
||||||
|
Addr: addr,
|
||||||
|
Balance: balance,
|
||||||
|
}
|
||||||
|
json, _ := json.Marshal(account)
|
||||||
|
return string(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAllAccounts returns all accounts and balances
|
||||||
|
func GetAllAccounts(sqldb *sql.DB) string {
|
||||||
|
var array accountRes
|
||||||
|
var accountsArr string
|
||||||
|
accs, err := sqldb.Query(`
|
||||||
|
SELECT
|
||||||
|
addr,
|
||||||
|
balance
|
||||||
|
FROM accounts`)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer accs.Close()
|
||||||
|
////
|
||||||
|
for accs.Next() {
|
||||||
|
var addr string
|
||||||
|
var balance string
|
||||||
|
err = accs.Scan(
|
||||||
|
&addr,
|
||||||
|
&balance,
|
||||||
|
)
|
||||||
|
|
||||||
|
array.AllAccounts = append(array.AllAccounts, SAccounts{
|
||||||
|
Addr: addr,
|
||||||
|
Balance: balance,
|
||||||
|
})
|
||||||
|
|
||||||
|
accounts, _ := json.Marshal(array.AllAccounts)
|
||||||
|
accountsFmt := string(accounts)
|
||||||
|
accountsArr = accountsFmt
|
||||||
|
}
|
||||||
|
return accountsArr
|
||||||
|
}
|
||||||
|
|
@ -2,18 +2,17 @@ var firstAccount = web3.eth.accounts[0]
|
||||||
var secondAccount = web3.eth.accounts[1]
|
var secondAccount = web3.eth.accounts[1]
|
||||||
var thirdAccount = web3.eth.accounts[2]
|
var thirdAccount = web3.eth.accounts[2]
|
||||||
|
|
||||||
for (var i = 0; i < 1; i++) {
|
for (var i = 0; i < 10; i++) {
|
||||||
console.log('\t\t' + (i + 1) + ' - Transactions')
|
console.log('\t\t' + (i + 1) + ' - Transactions')
|
||||||
web3.eth.sendTransaction({
|
web3.eth.sendTransaction({
|
||||||
from: web3.eth.accounts[0],
|
from: web3.eth.accounts[2],
|
||||||
to: web3.eth.accounts[1],
|
to: web3.eth.accounts[0],
|
||||||
value: 623,
|
value: 5,
|
||||||
gas: 50000,
|
gas: 50000,
|
||||||
gasPrice: 20
|
gasPrice: 20
|
||||||
});
|
});
|
||||||
|
|
||||||
web3.eth.sendTransaction({
|
web3.eth.sendTransaction({
|
||||||
from: web3.eth.accounts[0],
|
from: web3.eth.accounts[1],
|
||||||
to: web3.eth.accounts[2],
|
to: web3.eth.accounts[2],
|
||||||
value: 291,
|
value: 291,
|
||||||
gas: 50000,
|
gas: 50000,
|
||||||
|
|
@ -21,8 +20,8 @@ for (var i = 0; i < 1; i++) {
|
||||||
});
|
});
|
||||||
|
|
||||||
web3.eth.sendTransaction({
|
web3.eth.sendTransaction({
|
||||||
from: web3.eth.accounts[1],
|
from: web3.eth.accounts[0],
|
||||||
to: web3.eth.accounts[3],
|
to: web3.eth.accounts[1],
|
||||||
value: 53039,
|
value: 53039,
|
||||||
gas: 50000,
|
gas: 50000,
|
||||||
gasPrice: 20
|
gasPrice: 20
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue