Merge branch 'shyft/greg' of github.com:ShyftNetwork/shyft_go-ethereum into shyftBlockExp

This commit is contained in:
Dustin Brickwood 2018-04-16 14:54:05 -04:00
commit 6df5230f1b
3 changed files with 127 additions and 17 deletions

View file

@ -910,6 +910,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil {
return NonStatTy, err return NonStatTy, err
} }
//fmt.Println(shyftdb.GetAllBlocks(bc.blockExplorerDb))
//result := shyftdb.GetBlock(bc.blockExplorerDb, block) //result := shyftdb.GetBlock(bc.blockExplorerDb, block)
// this is WIP for decoding bytes rather than hex strings // this is WIP for decoding bytes rather than hex strings

View file

@ -195,6 +195,22 @@ func (tx *Transaction) To() *common.Address {
return &to return &to
} }
//@NOTE:SHYFT - Custom function to require FROM
func (tx *Transaction) From() *common.Address {
if tx.data.V != nil {
// make a best guess about the signer and use that to derive
// the sender.
signer := deriveSigner(tx.data.V)
if from, err := Sender(signer, tx); err != nil { // derive but don't cache
return nil
} else {
return &from
}
} else {
return nil
}
}
// Hash hashes the RLP encoding of tx. // Hash hashes the RLP encoding of tx.
// It uniquely identifies the transaction. // It uniquely identifies the transaction.
func (tx *Transaction) Hash() common.Hash { func (tx *Transaction) Hash() common.Hash {

View file

@ -4,60 +4,139 @@ import (
"fmt" "fmt"
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"math/big"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util" "github.com/syndtr/goleveldb/leveldb/util"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
type SBlock struct {
hash string
txes []string
}
type ShyftTxEntry struct {
TxHash common.Hash
To *common.Address
From *common.Address
BlockHash common.Hash
Amount *big.Int
GasPrice *big.Int
Gas uint64
Nonce uint64
Data []byte
}
func WriteBlock(db *leveldb.DB, block *types.Block) error { func WriteBlock(db *leveldb.DB, block *types.Block) error {
leng := block.Transactions().Len() leng := block.Transactions().Len()
var tx_strs = make([]string, leng) var tx_strs = make([]string, leng)
//var tx_bytes = make([]byte, leng) //var tx_bytes = make([]byte, leng)
hash := block.Header().Hash().Bytes()
if block.Transactions().Len() > 0 {
for i, tx := range block.Transactions() {
tx_strs[i] = WriteTransactions(db, tx, block.Header().Hash())
//tx_bytes[i] = tx.Hash().Bytes()
}
}
fmt.Println("The tx_strs is") fmt.Println("The tx_strs is")
fmt.Println(tx_strs) fmt.Println(tx_strs)
//strs := []string{"foo", "bar"} //strs := []string{"foo", "bar"}
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
gob.NewEncoder(buf).Encode(tx_strs) gob.NewEncoder(buf).Encode(tx_strs)
bs := buf.Bytes() bs := buf.Bytes()
hash := block.Header().Hash().Bytes()
if err := db.Put(hash, bs, nil); err != nil { key := append([]byte("bk-")[:], hash[:]...)
if err := db.Put(key, bs, nil); err != nil {
log.Crit("Failed to store block", "err", err) log.Crit("Failed to store block", "err", err)
return nil // Do we want to force an exit here? return nil // Do we want to force an exit here?
} }
if block.Transactions().Len() > 0 {
WriteTransactions(db, block.Transactions(), hash)
}
return nil return nil
} }
func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash common.Hash) string {
for _, tx := range transactions { txData := ShyftTxEntry{
key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) TxHash: tx.Hash(),
if err := db.Put(key, []byte("Hello hello"), nil); err != nil { To: tx.To(),
log.Crit("Failed to store TX", "err", err) From: tx.From(),
} BlockHash: blockHash,
GetTransaction(db, tx) Amount: tx.Value(),
GasPrice: tx.GasPrice(),
Gas: tx.Gas(),
Nonce: tx.Nonce(),
Data: tx.Data(),
} }
var encodedData bytes.Buffer
encoder := gob.NewEncoder(&encodedData)
if err := encoder.Encode(txData); err != nil {
log.Crit("Faild to encode TX data", "err", err)
}
key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...)
if err := db.Put(key, encodedData.Bytes(), nil); err != nil {
log.Crit("Failed to store TX", "err", err)
}
GetTransaction(db, tx)
GetAllTransactions(db) GetAllTransactions(db)
return nil return tx.Hash().String()
} }
// Meant for internal tests // Meant for internal tests
func GetAllBlocks(db *leveldb.DB) []SBlock{
var arr []SBlock
iter := db.NewIterator(util.BytesPrefix([]byte("bk-")), nil)
for iter.Next() {
result := iter.Value()
buf := bytes.NewBuffer(result)
strs2 := []string{}
gob.NewDecoder(buf).Decode(&strs2)
//fmt.Println("the key is")
hash := common.BytesToHash(iter.Key())
hex := hash.Hex()
//fmt.Println(hex)
sblock := SBlock{hex, strs2}
arr = append(arr, sblock)
func GetBlock(db *leveldb.DB, block *types.Block) { //fmt.Println("\n ALL BK BK VALUE" + string(result))
}
iter.Release()
return arr
}
func GetBlock(db *leveldb.DB, block *types.Block) []byte {
hash := block.Header().Hash().Bytes() hash := block.Header().Hash().Bytes()
data, err := db.Get(hash, nil) key := append([]byte("bk-")[:], hash[:]...)
data, err := db.Get(key, nil)
if err != nil { if err != nil {
log.Crit("Could not retrieve block", "err", err) log.Crit("Could not retrieve block", "err", err)
} }
fmt.Println("\nBLOCK Value: " + string(data)) fmt.Println("\nBLOCK Value: " + string(data))
return data
} }
func GetAllTransactions(db *leveldb.DB) { func GetAllTransactions(db *leveldb.DB) {
iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil)
for iter.Next() { for iter.Next() {
fmt.Println("\nALL TX VALUE: " + string(iter.Value())) var txData ShyftTxEntry
d := gob.NewDecoder(bytes.NewBuffer(iter.Value()))
if err := d.Decode(&txData); err != nil {
log.Crit("Failed to decode tx:", "err", err)
}
fmt.Println("DECODED TX")
fmt.Println("Tx Hash: ", txData.TxHash.Hex())
fmt.Println("From: ", txData.From.Hex())
fmt.Println("To: ", txData.To.Hex())
fmt.Println("BlockHash: ", txData.BlockHash.Hex())
fmt.Println("Amount: ", txData.Amount)
fmt.Println("Gas: ", txData.Gas)
fmt.Println("GasPrice: ", txData.GasPrice)
fmt.Println("Nonce: ", txData.Nonce)
fmt.Println("Data: ", txData.Data)
} }
iter.Release() iter.Release()
} }
@ -69,6 +148,20 @@ func GetTransaction (db *leveldb.DB, tx *types.Transaction) {
log.Crit("Could not retrieve TX", "err", err) log.Crit("Could not retrieve TX", "err", err)
} }
if len(data) > 0 { if len(data) > 0 {
fmt.Println("\nTX Value: " + string(data)) var txData ShyftTxEntry
d := gob.NewDecoder(bytes.NewBuffer(data))
if err := d.Decode(&txData); err != nil {
log.Crit("Failed to decode tx:", "err", err)
}
fmt.Println("DECODED TX")
fmt.Println("Tx Hash: ", txData.TxHash.Hex())
fmt.Println("From: ", txData.From.Hex())
fmt.Println("To: ", txData.To.Hex())
fmt.Println("BlockHash: ", txData.BlockHash.Hex())
fmt.Println("Amount: ", txData.Amount)
fmt.Println("Gas: ", txData.Gas)
fmt.Println("GasPrice: ", txData.GasPrice)
fmt.Println("Nonce: ", txData.Nonce)
fmt.Println("Data: ", txData.Data)
} }
} }