From c697b3d097ac92ad143977eca9527696adc584bc Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 13:27:01 -0400 Subject: [PATCH] add custom tx method From() --- core/types/transaction.go | 16 +++++++++ shyftdb/shyft_database_util.go | 60 ++++++++++------------------------ 2 files changed, 34 insertions(+), 42 deletions(-) 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 5d4c94725a..a19628b4a4 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -32,16 +32,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, hash) + //tx_bytes[i] = tx.Hash().Bytes() } } @@ -60,41 +53,24 @@ 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 { - var from = GenerateFromAddr() - txData := txEntry{ - TxHash: tx.Hash(), - To: tx.To(), - From: from, - BlockHash: blockHash, - Amount: tx.Value(), - GasPrice: tx.GasPrice(), - Gas: tx.Gas(), - Nonce: tx.Nonce(), - Data: tx.Data(), - } - fmt.Println(txData) - 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 []byte) string { + txData := txEntry{ + 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(), } - GetAllTransactions(db) - return nil -} - -// Helper functions - -func GenerateFromAddr(tx *types.Transaction) *common.Address { - var from *common.Address - signer := deriveSigner(tx.data.V) - if f, err := Sender(signer, tx); err != nil { // derive but don't cache - from = "[invalid sender: invalid sig]" - } else { - from = fmt.Sprintf("%x", f[:]) + fmt.Println(txData) + 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) } + return tx.Hash().String() } // Meant for internal tests