diff --git a/core/types/transaction.go b/core/types/transaction.go index 5660582baf..a2710b2cc7 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -195,6 +195,22 @@ func (tx *Transaction) To() *common.Address { 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. // It uniquely identifies the transaction. func (tx *Transaction) Hash() common.Hash { diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 4e6f5e0b32..bd338dfa5b 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -4,18 +4,34 @@ import ( "fmt" "bytes" "encoding/gob" + "math/big" + "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/util" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/common" ) + 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 { leng := block.Transactions().Len() var tx_strs = make([]string, leng) @@ -23,16 +39,9 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { hash := block.Header().Hash().Bytes() if block.Transactions().Len() > 0 { - // this is inefficient, there are 2 loops over - // block.Transactions - // TODO: Fix this so there is only one loop - WriteTransactions(db, block.Transactions(), hash) for i, tx := range block.Transactions() { - fmt.Println(tx.Hash()) - fmt.Println("TX HASH") - fmt.Println(tx.To().Hex()) - tx_strs[i] = tx.Hash().String() - //tx_bytes[i] = tx.Hash().Bytes() + tx_strs[i] = WriteTransactions(db, tx, block.Header().Hash()) + //tx_bytes[i] = tx.Hash().Bytes() } } @@ -51,16 +60,30 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { return nil } -func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { - for _, tx := range transactions { - key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) - if err := db.Put(key, []byte("Hello hello"), nil); err != nil { - log.Crit("Failed to store TX", "err", err) - } - GetTransaction(db, tx) +func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash common.Hash) string { + txData := ShyftTxEntry{ + TxHash: tx.Hash(), + To: tx.To(), + From: tx.From(), + BlockHash: blockHash, + 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) - return nil + return tx.Hash().String() } // Meant for internal tests @@ -100,7 +123,21 @@ func GetBlock(db *leveldb.DB, block *types.Block) []byte { func GetAllTransactions(db *leveldb.DB) { iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) 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() } @@ -112,6 +149,20 @@ func GetTransaction (db *leveldb.DB, tx *types.Transaction) { log.Crit("Could not retrieve TX", "err", err) } 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) } } \ No newline at end of file