mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
Merge branch 'staging' into dockerShyftBlockApi
* staging: (27 commits) reformat code refactored code to minimize lines resolving conflicts need to pop stash tmp code refactor cleaned up code and removed msg type respond back to client with sig fix var names receiving messages from wallet correctly add in additional message sent over tcp add intArrToByteArr function for testing change test keys and test message log out hash of message as hex more work send message back to client rm printlns rm Printlns rm comments and printlns ecrecover plain text message ... # Conflicts: # core/genesis.go
This commit is contained in:
commit
3a856529f3
8 changed files with 862 additions and 951 deletions
|
|
@ -25,10 +25,7 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"database/sql"
|
_ "github.com/lib/pq"
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"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"
|
||||||
|
|
@ -38,7 +35,9 @@ 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"
|
||||||
_ "github.com/lib/pq"
|
"database/sql"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go
|
//go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go
|
||||||
|
|
@ -164,7 +163,7 @@ func WriteShyftGen(gen *Genesis, block *types.Block) {
|
||||||
isContract := false
|
isContract := false
|
||||||
data:= ""
|
data:= ""
|
||||||
addr := k.String()
|
addr := k.String()
|
||||||
txCountAccount := 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)
|
||||||
|
|
@ -174,24 +173,22 @@ func WriteShyftGen(gen *Genesis, block *types.Block) {
|
||||||
GENESIS := "GENESIS"
|
GENESIS := "GENESIS"
|
||||||
txHash := strings.Join(Genesis, addr)
|
txHash := strings.Join(Genesis, addr)
|
||||||
|
|
||||||
sqlStatement := `INSERT INTO accounts(addr, balance, txCountAccount) 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(), txCountAccount).Scan(&addr)
|
insertErr := sqldb.QueryRow(sqlStatement, addr, v.Balance.String(), accountNonce).Scan(&addr)
|
||||||
if insertErr != nil {
|
if insertErr != nil {
|
||||||
panic(insertErr)
|
panic(insertErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
var retNonce string
|
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`
|
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, txCountAccount, txStatus, isContract, age, data).Scan(&retNonce)
|
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 {
|
if insertError != nil {
|
||||||
panic(insertError)
|
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 {
|
||||||
|
|
||||||
|
|
@ -232,7 +229,6 @@ 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:
|
||||||
//
|
//
|
||||||
|
|
@ -319,7 +315,6 @@ 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 {
|
||||||
|
|
@ -444,7 +439,6 @@ 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 {
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
386
core/shyft_get_utils.go
Normal file
386
core/shyft_get_utils.go
Normal file
|
|
@ -0,0 +1,386 @@
|
||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
///////////
|
||||||
|
// Getters
|
||||||
|
//////////
|
||||||
|
func SGetAllBlocks(sqldb *sql.DB) string {
|
||||||
|
var arr blockRes
|
||||||
|
var blockArr string
|
||||||
|
rows, err := sqldb.Query(`SELECT * FROM blocks`)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("err")
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var hash, coinbase, age, parentHash, uncleHash, difficulty, size, rewards, num string
|
||||||
|
var gasUsed, gasLimit, nonce uint64
|
||||||
|
var txCount, uncleCount int
|
||||||
|
|
||||||
|
err = rows.Scan(
|
||||||
|
&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,
|
||||||
|
})
|
||||||
|
|
||||||
|
blocks, _ := json.Marshal(arr.Blocks)
|
||||||
|
blocksFmt := string(blocks)
|
||||||
|
blockArr = blocksFmt
|
||||||
|
}
|
||||||
|
return blockArr
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetBlock queries to send single block info
|
||||||
|
//TODO provide blockHash arg passed from handler.go
|
||||||
|
func SGetBlock(sqldb *sql.DB, blockNumber string) string {
|
||||||
|
sqlStatement := `SELECT * FROM blocks WHERE number=$1;`
|
||||||
|
row := sqldb.QueryRow(sqlStatement, blockNumber)
|
||||||
|
var hash, coinbase, age, parentHash, uncleHash, difficulty, size, rewards, num string
|
||||||
|
var gasUsed, gasLimit, nonce uint64
|
||||||
|
var txCount, uncleCount int
|
||||||
|
|
||||||
|
row.Scan(
|
||||||
|
&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,
|
||||||
|
UncleCount: uncleCount,
|
||||||
|
AgeGet: age,
|
||||||
|
ParentHash: parentHash,
|
||||||
|
UncleHash: uncleHash,
|
||||||
|
Difficulty: difficulty,
|
||||||
|
Size: size,
|
||||||
|
Nonce: nonce,
|
||||||
|
Rewards: rewards,
|
||||||
|
Number: num,
|
||||||
|
}
|
||||||
|
json, _ := json.Marshal(block)
|
||||||
|
return string(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SGetRecentBlock(sqldb *sql.DB) string {
|
||||||
|
sqlStatement := `SELECT * FROM blocks WHERE number=(SELECT MAX(number) FROM blocks);`
|
||||||
|
row := sqldb.QueryRow(sqlStatement)
|
||||||
|
var hash, coinbase, age, parentHash, uncleHash, difficulty, size, rewards, num string
|
||||||
|
var gasUsed, gasLimit, nonce uint64
|
||||||
|
var txCount, uncleCount int
|
||||||
|
|
||||||
|
row.Scan(
|
||||||
|
&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,
|
||||||
|
UncleCount: uncleCount,
|
||||||
|
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 txx string
|
||||||
|
sqlStatement := `SELECT * FROM txs WHERE blocknumber=$1`
|
||||||
|
rows, err := sqldb.Query(sqlStatement, blockNumber)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("err")
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var txhash, to_addr, from_addr, blockhash, blocknumber, amount, status string
|
||||||
|
var gasprice, gas, gasLimit, txfee, nonce uint64
|
||||||
|
var isContract bool
|
||||||
|
var age time.Time
|
||||||
|
var data []byte
|
||||||
|
|
||||||
|
err = rows.Scan(
|
||||||
|
&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,
|
||||||
|
ToGet: to_addr,
|
||||||
|
From: from_addr,
|
||||||
|
BlockHash: blockhash,
|
||||||
|
BlockNumber: blocknumber,
|
||||||
|
Amount: amount,
|
||||||
|
GasPrice: gasprice,
|
||||||
|
Gas: gas,
|
||||||
|
GasLimit: gasLimit,
|
||||||
|
Cost: txfee,
|
||||||
|
Nonce: nonce,
|
||||||
|
Status: status,
|
||||||
|
IsContract: isContract,
|
||||||
|
Age: age,
|
||||||
|
Data: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
tx, _ := json.Marshal(arr.TxEntry)
|
||||||
|
newtx := string(tx)
|
||||||
|
txx = newtx
|
||||||
|
}
|
||||||
|
return txx
|
||||||
|
}
|
||||||
|
|
||||||
|
func SGetAllBlocksMinedByAddress(sqldb *sql.DB, coinbase string) string {
|
||||||
|
var arr blockRes
|
||||||
|
var blockArr string
|
||||||
|
sqlStatement := `SELECT * FROM blocks WHERE coinbase=$1`
|
||||||
|
rows, err := sqldb.Query(sqlStatement, coinbase)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("err")
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var hash, coinbase, age, parentHash, uncleHash, difficulty, size, rewards, num string
|
||||||
|
var gasUsed, gasLimit, nonce uint64
|
||||||
|
var txCount, uncleCount int
|
||||||
|
|
||||||
|
err = rows.Scan(
|
||||||
|
&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,
|
||||||
|
})
|
||||||
|
|
||||||
|
blocks, _ := json.Marshal(arr.Blocks)
|
||||||
|
blocksFmt := string(blocks)
|
||||||
|
blockArr = blocksFmt
|
||||||
|
}
|
||||||
|
return blockArr
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAllTransactions getter fn for API
|
||||||
|
func SGetAllTransactions(sqldb *sql.DB) string {
|
||||||
|
var arr txRes
|
||||||
|
var txx string
|
||||||
|
rows, err := sqldb.Query(`SELECT * FROM txs`)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("err")
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var txhash, to_addr, from_addr, blockhash, blocknumber, amount, status string
|
||||||
|
var gasprice, gas, gasLimit, txfee, nonce uint64
|
||||||
|
var isContract bool
|
||||||
|
var age time.Time
|
||||||
|
var data []byte
|
||||||
|
|
||||||
|
err = rows.Scan(
|
||||||
|
&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,
|
||||||
|
ToGet: to_addr,
|
||||||
|
From: from_addr,
|
||||||
|
BlockHash: blockhash,
|
||||||
|
BlockNumber: blocknumber,
|
||||||
|
Amount: amount,
|
||||||
|
GasPrice: gasprice,
|
||||||
|
Gas: gas,
|
||||||
|
GasLimit: gasLimit,
|
||||||
|
Cost: txfee,
|
||||||
|
Nonce: nonce,
|
||||||
|
Status: status,
|
||||||
|
IsContract: isContract,
|
||||||
|
Age: age,
|
||||||
|
Data: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
tx, _ := json.Marshal(arr.TxEntry)
|
||||||
|
newtx := string(tx)
|
||||||
|
txx = newtx
|
||||||
|
}
|
||||||
|
return txx
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetTransaction fn returns single tx
|
||||||
|
func SGetTransaction(sqldb *sql.DB, txHash string) string {
|
||||||
|
sqlStatement := `SELECT * FROM txs WHERE txhash=$1;`
|
||||||
|
row := sqldb.QueryRow(sqlStatement, txHash)
|
||||||
|
var txhash, to_addr, from_addr, blockhash, blocknumber, amount, status string
|
||||||
|
var gasprice, gas, gasLimit, txfee, nonce uint64
|
||||||
|
var isContract bool
|
||||||
|
var age time.Time
|
||||||
|
var data []byte
|
||||||
|
|
||||||
|
row.Scan(
|
||||||
|
&txhash, &to_addr, &from_addr, &blockhash, &blocknumber, &amount, &gasprice, &gas, &gasLimit, &txfee, &nonce, &status, &isContract, &age, &data)
|
||||||
|
|
||||||
|
tx := ShyftTxEntryPretty{
|
||||||
|
TxHash: txhash,
|
||||||
|
ToGet: to_addr,
|
||||||
|
From: from_addr,
|
||||||
|
BlockHash: blockhash,
|
||||||
|
BlockNumber: blocknumber,
|
||||||
|
Amount: amount,
|
||||||
|
GasPrice: gasprice,
|
||||||
|
Gas: gas,
|
||||||
|
GasLimit: gasLimit,
|
||||||
|
Cost: txfee,
|
||||||
|
Nonce: nonce,
|
||||||
|
Status: status,
|
||||||
|
IsContract: isContract,
|
||||||
|
Age: age,
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
json, _ := json.Marshal(tx)
|
||||||
|
|
||||||
|
return string(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
func InnerSGetAccount(sqldb *sql.DB, address string) (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
|
||||||
|
} else {
|
||||||
|
account := SAccounts{
|
||||||
|
Addr: addr,
|
||||||
|
Balance: balance,
|
||||||
|
AccountNonce: accountNonce,
|
||||||
|
}
|
||||||
|
return account, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAccount returns account balances
|
||||||
|
func SGetAccount(sqldb *sql.DB, address string) string {
|
||||||
|
var account, _ = InnerSGetAccount(sqldb, address)
|
||||||
|
json, _ := json.Marshal(account)
|
||||||
|
return string(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAllAccounts returns all accounts and balances
|
||||||
|
func SGetAllAccounts(sqldb *sql.DB) string {
|
||||||
|
var array accountRes
|
||||||
|
var accountsArr, accountNonce string
|
||||||
|
|
||||||
|
accs, err := sqldb.Query(`
|
||||||
|
SELECT
|
||||||
|
addr,
|
||||||
|
balance,
|
||||||
|
accountNonce
|
||||||
|
FROM accounts`)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer accs.Close()
|
||||||
|
|
||||||
|
for accs.Next() {
|
||||||
|
var addr, balance string
|
||||||
|
err = accs.Scan(
|
||||||
|
&addr, &balance, &accountNonce,
|
||||||
|
)
|
||||||
|
|
||||||
|
array.AllAccounts = append(array.AllAccounts, SAccounts{
|
||||||
|
Addr: addr,
|
||||||
|
Balance: balance,
|
||||||
|
AccountNonce: accountNonce,
|
||||||
|
})
|
||||||
|
|
||||||
|
accounts, _ := json.Marshal(array.AllAccounts)
|
||||||
|
accountsFmt := string(accounts)
|
||||||
|
accountsArr = accountsFmt
|
||||||
|
}
|
||||||
|
return accountsArr
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAccount returns account balances
|
||||||
|
func SGetAccountTxs(sqldb *sql.DB, address string) string {
|
||||||
|
var arr txRes
|
||||||
|
var txx string
|
||||||
|
sqlStatement := `SELECT * FROM txs WHERE to_addr=$1 OR from_addr=$1;`
|
||||||
|
rows, err := sqldb.Query(sqlStatement, address)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("err", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var txhash, to_addr, from_addr, blockhash, blocknumber, amount, status string
|
||||||
|
var gasprice, gas, gasLimit, txfee, nonce uint64
|
||||||
|
var isContract bool
|
||||||
|
var age time.Time
|
||||||
|
var data []byte
|
||||||
|
|
||||||
|
err = rows.Scan(
|
||||||
|
&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,
|
||||||
|
ToGet: to_addr,
|
||||||
|
From: from_addr,
|
||||||
|
BlockHash: blockhash,
|
||||||
|
BlockNumber: blocknumber,
|
||||||
|
Amount: amount,
|
||||||
|
GasPrice: gasprice,
|
||||||
|
Gas: gas,
|
||||||
|
GasLimit: gasLimit,
|
||||||
|
Cost: txfee,
|
||||||
|
Nonce: nonce,
|
||||||
|
Status: status,
|
||||||
|
IsContract: isContract,
|
||||||
|
Age: age,
|
||||||
|
Data: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
tx, _ := json.Marshal(arr.TxEntry)
|
||||||
|
newtx := string(tx)
|
||||||
|
txx = newtx
|
||||||
|
}
|
||||||
|
return txx
|
||||||
|
}
|
||||||
|
|
@ -32,6 +32,7 @@ import (
|
||||||
"github.com/ShyftNetwork/go-empyrean/crypto"
|
"github.com/ShyftNetwork/go-empyrean/crypto"
|
||||||
"github.com/ShyftNetwork/go-empyrean/log"
|
"github.com/ShyftNetwork/go-empyrean/log"
|
||||||
"gopkg.in/olebedev/go-duktape.v3"
|
"gopkg.in/olebedev/go-duktape.v3"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// bigIntegerJS is the minified version of https://github.com/peterolson/BigInteger.js.
|
// bigIntegerJS is the minified version of https://github.com/peterolson/BigInteger.js.
|
||||||
|
|
@ -599,16 +600,19 @@ func (i *Internals) SWriteInteralTxs(hash common.Hash) {
|
||||||
|
|
||||||
gas, _ := hexutil.DecodeUint64(i.Gas)
|
gas, _ := hexutil.DecodeUint64(i.Gas)
|
||||||
gasUsed, _ := hexutil.DecodeUint64(i.GasUsed)
|
gasUsed, _ := hexutil.DecodeUint64(i.GasUsed)
|
||||||
|
value, _ := hexutil.DecodeUint64(i.Value)
|
||||||
|
amount := strconv.FormatUint(value, 10)
|
||||||
|
|
||||||
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, i.Value, 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)
|
||||||
|
|
||||||
if qerr != nil {
|
if qerr != nil {
|
||||||
fmt.Println(qerr)
|
fmt.Println(qerr)
|
||||||
panic(qerr)
|
panic(qerr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//@NOTE:SHYFT
|
//@NOTE:SHYFT
|
||||||
func (i *Internals) InternalRecursive(hash common.Hash) {
|
func (i *Internals) InternalRecursive(hash common.Hash) {
|
||||||
i.SWriteInteralTxs(hash)
|
i.SWriteInteralTxs(hash)
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ CREATE TABLE IF NOT EXISTS txs (
|
||||||
CREATE TABLE IF NOT EXISTS accounts (
|
CREATE TABLE IF NOT EXISTS accounts (
|
||||||
addr text primary key unique,
|
addr text primary key unique,
|
||||||
balance numeric,
|
balance numeric,
|
||||||
txCountAccount numeric
|
accountNonce numeric
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS contracts (
|
CREATE TABLE IF NOT EXISTS contracts (
|
||||||
|
|
|
||||||
|
|
@ -36,5 +36,5 @@ CREATE TABLE IF NOT EXISTS txs (
|
||||||
CREATE TABLE IF NOT EXISTS accounts (
|
CREATE TABLE IF NOT EXISTS accounts (
|
||||||
addr text primary key unique,
|
addr text primary key unique,
|
||||||
balance numeric,
|
balance numeric,
|
||||||
txCountAccount numeric
|
accountnonce numeric
|
||||||
);
|
);
|
||||||
|
|
@ -263,7 +263,7 @@ t.Run("TestContractCreationTx", func (t *testing.T) {
|
||||||
if tx.Hash().String() != data.TxHash {
|
if tx.Hash().String() != data.TxHash {
|
||||||
t.Fatalf("txHash [%v]: tx Hash not found", tx.Hash().String())
|
t.Fatalf("txHash [%v]: tx Hash not found", tx.Hash().String())
|
||||||
}
|
}
|
||||||
if contractAddressFromReciept != data.To {
|
if contractAddressFromReciept != data.ToGet {
|
||||||
t.Fatalf("Contract Addr [%v]: Contract addr not found", contractAddressFromReciept)
|
t.Fatalf("Contract Addr [%v]: Contract addr not found", contractAddressFromReciept)
|
||||||
}
|
}
|
||||||
if tx.From().String() != data.From {
|
if tx.From().String() != data.From {
|
||||||
|
|
@ -365,7 +365,7 @@ t.Run("TestTransactionsToReturnTransactions", func(t *testing.T) {
|
||||||
if tx.From().String() != data.From {
|
if tx.From().String() != data.From {
|
||||||
t.Fatalf("From Addr [%v]: From addr not found", tx.From().String())
|
t.Fatalf("From Addr [%v]: From addr not found", tx.From().String())
|
||||||
}
|
}
|
||||||
if tx.To().String() != data.To {
|
if tx.To().String() != data.ToGet {
|
||||||
t.Fatalf("To Addr [%v]: To addr not found", tx.To().String())
|
t.Fatalf("To Addr [%v]: To addr not found", tx.To().String())
|
||||||
}
|
}
|
||||||
if tx.Nonce() != data.Nonce {
|
if tx.Nonce() != data.Nonce {
|
||||||
|
|
@ -423,12 +423,29 @@ t.Run("TestAccountsToReturnAccounts",func(t *testing.T) {
|
||||||
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
signer := types.NewEIP155Signer(big.NewInt(2147483647))
|
signer := types.NewEIP155Signer(big.NewInt(2147483647))
|
||||||
|
|
||||||
|
toAddr1 := common.BytesToAddress([]byte{0x11})
|
||||||
|
toAddr2 := common.BytesToAddress([]byte{0x22})
|
||||||
|
toAddr3 := common.BytesToAddress([]byte{0x33})
|
||||||
|
|
||||||
|
toAmount1 := big.NewInt(111)
|
||||||
|
var toAmountPrev1 string = "3968686868"
|
||||||
|
|
||||||
|
sqldb, err := core.DBConnection()
|
||||||
|
if (err != nil) {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
//Nonce, To Address,Value, GasLimit, Gasprice, data
|
//Nonce, To Address,Value, GasLimit, Gasprice, data
|
||||||
tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
|
tx1 := types.NewTransaction(1, toAddr1, toAmount1, 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
|
||||||
mytx,_ := types.SignTx(tx1, signer, key)
|
mytx,_ := types.SignTx(tx1, signer, key)
|
||||||
tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
|
tx2 := types.NewTransaction(2, toAddr2, big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
|
||||||
mytx2,_ := types.SignTx(tx2, signer, key)
|
mytx2,_ := types.SignTx(tx2, signer, key)
|
||||||
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
|
tx3 := types.NewTransaction(3, toAddr3, big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
|
||||||
mytx3,_ := types.SignTx(tx3, signer, key)
|
mytx3,_ := types.SignTx(tx3, signer, key)
|
||||||
txs := []*types.Transaction{mytx, mytx2, mytx3}
|
txs := []*types.Transaction{mytx, mytx2, mytx3}
|
||||||
|
|
||||||
|
|
@ -450,30 +467,42 @@ t.Run("TestAccountsToReturnAccounts",func(t *testing.T) {
|
||||||
t.Fatalf("Failed to write block into database: %v", err)
|
t.Fatalf("Failed to write block into database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sqldb, err := core.DBConnection()
|
if toAddr1.String() != tx1.To().String() {
|
||||||
if (err != nil) {
|
t.Fatalf("To address [%v]: To address not found", toAddr1.String())
|
||||||
panic(err)
|
}
|
||||||
|
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)
|
||||||
|
|
||||||
|
if toBalance.Cmp(addedAmount) != 0 {
|
||||||
|
t.Fatalf("To address balance [%v]: To address balance not correct FFO", toBalance)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tx := range txs {
|
//for _, tx := range txs {
|
||||||
accountAddrTo := core.SGetAccount(sqldb, tx.To().String())
|
// accountAddrTo := core.SGetAccount(sqldb, tx.To().String())
|
||||||
byts := []byte(accountAddrTo)
|
// byts := []byte(accountAddrTo)
|
||||||
var accountDataTo core.SAccounts
|
// var accountDataTo core.SAccounts
|
||||||
json.Unmarshal(byts, &accountDataTo)
|
// json.Unmarshal(byts, &accountDataTo)
|
||||||
|
//
|
||||||
|
// if tx.To().String() != accountDataTo.Addr {
|
||||||
|
// t.Fatalf("To address [%v]: To address not found", accountDataTo.Addr)
|
||||||
|
// }
|
||||||
|
// if tx.Value().String() != accountDataTo.Balance {
|
||||||
|
// t.Fatalf("To address balance [%v]: To address balance not found", accountDataTo.Balance)
|
||||||
|
// }
|
||||||
|
// if strconv.FormatUint(tx.Nonce(), 10) != accountDataTo.AccountNonce {
|
||||||
|
// t.Fatalf("To account nonce [%v]: To account nonce not found", accountDataTo.AccountNonce)
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
if tx.To().String() != accountDataTo.Addr {
|
if getAllAccountTxs := core.SGetAccountTxs(sqldb, toAddr1.String()); len(getAllAccountTxs) == 0 {
|
||||||
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.TxCountAccount {
|
|
||||||
t.Fatalf("To account nonce [%v]: To account nonce not found", accountDataTo.TxCountAccount)
|
|
||||||
}
|
|
||||||
if getAllAccountTxs := core.SGetAccountTxs(sqldb, tx.To().String()); len(getAllAccountTxs) == 0 {
|
|
||||||
t.Fatalf("GetAccountTxs [%v]: GetAccountTxs did not return correctly", getAllAccountTxs)
|
t.Fatalf("GetAccountTxs [%v]: GetAccountTxs did not return correctly", getAllAccountTxs)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if getAllAccounts := core.SGetAllAccounts(sqldb); len(getAllAccounts) == 0 {
|
if getAllAccounts := core.SGetAllAccounts(sqldb); len(getAllAccounts) == 0 {
|
||||||
t.Fatalf("GetAllAccounts [%v]: GetAllAccounts did not return correctly", getAllAccounts)
|
t.Fatalf("GetAllAccounts [%v]: GetAllAccounts did not return correctly", getAllAccounts)
|
||||||
|
|
|
||||||
140
shyftRingWalletConn/app.go
Normal file
140
shyftRingWalletConn/app.go
Normal file
|
|
@ -0,0 +1,140 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
//@NOTE SHYFT main func for api, sets up router and spins up a server
|
||||||
|
//to run server 'go run shyftRingWalletConn/*.go'
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
//"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
"bytes"
|
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CONN_HOST = "localhost"
|
||||||
|
CONN_PORT = "3333"
|
||||||
|
CONN_TYPE = "tcp"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testAddrHex = "14791697260E4c9A71f18484C9f997B308e59325"
|
||||||
|
var testPrivHex = "0123456789012345678901234567890123456789012345678901234567890123"
|
||||||
|
|
||||||
|
// This gives context to the signed message and prevents signing of transactions.
|
||||||
|
func signHash(data []byte) []byte {
|
||||||
|
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
|
||||||
|
return crypto.Keccak256([]byte(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error listening:", err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer l.Close()
|
||||||
|
|
||||||
|
for {
|
||||||
|
// Listen for an incoming connection.
|
||||||
|
conn, err := l.Accept()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error accepting: ", err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
// Handle connections in a new goroutine.
|
||||||
|
go handleRequest(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles incoming requests.
|
||||||
|
func handleRequest(conn net.Conn) {
|
||||||
|
// Make a buffer to hold incoming data.
|
||||||
|
// Read the incoming connection into the buffer.
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
msgBuf := make([]byte, 0)
|
||||||
|
var prevMsg []byte
|
||||||
|
var addressOfClient []byte
|
||||||
|
var signatureFromClient []byte
|
||||||
|
var msgFromClient []byte
|
||||||
|
|
||||||
|
for {
|
||||||
|
msg, err := conn.Read(buf)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
msgBuf = append(msgBuf, buf[:msg]...)
|
||||||
|
}
|
||||||
|
index := bytes.IndexByte(msgBuf, 0x0a)
|
||||||
|
for index != -1 {
|
||||||
|
newMsg := msgBuf[:index]
|
||||||
|
rest := msgBuf[(index + 1):len(msgBuf)]
|
||||||
|
msgBuf = rest
|
||||||
|
index = bytes.IndexByte(msgBuf, 0x0a)
|
||||||
|
if prevMsg != nil {
|
||||||
|
s := string(prevMsg[:])
|
||||||
|
if s == "-- ADDRESS --" {
|
||||||
|
addressOfClient = newMsg
|
||||||
|
}
|
||||||
|
if s == "-- SIGNATURE --" {
|
||||||
|
signatureFromClient = newMsg
|
||||||
|
}
|
||||||
|
if s == "-- MESSAGE --" {
|
||||||
|
msgFromClient = newMsg
|
||||||
|
}
|
||||||
|
prevMsg = nil
|
||||||
|
} else {
|
||||||
|
prevMsg = newMsg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil {
|
||||||
|
sig := string(signatureFromClient[:])
|
||||||
|
var sigByteArr, error = hexutil.Decode(sig)
|
||||||
|
|
||||||
|
if error != nil {
|
||||||
|
fmt.Println(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
var sigHex = hexutil.Bytes(sigByteArr)
|
||||||
|
sigHex[64] -= 27
|
||||||
|
|
||||||
|
signedMsgHash := signHash(msgFromClient)
|
||||||
|
|
||||||
|
var rpk, err = crypto.Ecrecover(signedMsgHash, sigHex)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKey := crypto.ToECDSAPub(rpk)
|
||||||
|
recoveredAddr := crypto.PubkeyToAddress(*pubKey)
|
||||||
|
fmt.Println("ADDRESS IS ::", recoveredAddr.Hex())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
key, _ := crypto.HexToECDSA(testPrivHex)
|
||||||
|
|
||||||
|
f_msg := "Hello World"
|
||||||
|
first_message := []byte(f_msg)
|
||||||
|
new_msg2 := crypto.Keccak256(first_message)
|
||||||
|
fmt.Println("HASH IS ::", hexutil.Encode(new_msg2))
|
||||||
|
|
||||||
|
//send_message := append(new_msg2, []byte{byte(10)}...)
|
||||||
|
new_sig , err := crypto.Sign(new_msg2, key)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("The crypto.Sign err is ", err)
|
||||||
|
}
|
||||||
|
hex_sig := hexutil.Encode(new_sig)
|
||||||
|
fmt.Println("HEX SIG ::", hex_sig)
|
||||||
|
|
||||||
|
conn.Write([]byte("Broadcasting Message"))
|
||||||
|
conn.Write([]byte("\n"))
|
||||||
|
conn.Write([]byte(f_msg))
|
||||||
|
conn.Write([]byte("\n"))
|
||||||
|
conn.Write(new_sig)
|
||||||
|
conn.Write([]byte("\n"))
|
||||||
|
}()
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue