add custom tx method From()

This commit is contained in:
greg 2018-04-16 13:27:01 -04:00
parent 75e452e2d9
commit c697b3d097
2 changed files with 34 additions and 42 deletions

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

@ -32,16 +32,9 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error {
hash := block.Header().Hash().Bytes() hash := block.Header().Hash().Bytes()
if block.Transactions().Len() > 0 { 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() { for i, tx := range block.Transactions() {
fmt.Println(tx.Hash()) tx_strs[i] = WriteTransactions(db, tx, hash)
fmt.Println("TX HASH") //tx_bytes[i] = tx.Hash().Bytes()
fmt.Println(tx.To().Hex())
tx_strs[i] = tx.Hash().String()
//tx_bytes[i] = tx.Hash().Bytes()
} }
} }
@ -60,41 +53,24 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error {
return nil return nil
} }
func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash []byte) string {
for _, tx := range transactions { txData := txEntry{
var from = GenerateFromAddr() TxHash: tx.Hash(),
txData := txEntry{ To: tx.To(),
TxHash: tx.Hash(), From: tx.From(),
To: tx.To(), BlockHash: blockHash,
From: from, Amount: tx.Value(),
BlockHash: blockHash, GasPrice: tx.GasPrice(),
Amount: tx.Value(), Gas: tx.Gas(),
GasPrice: tx.GasPrice(), Nonce: tx.Nonce(),
Gas: tx.Gas(), Data: tx.Data(),
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)
} }
GetAllTransactions(db) fmt.Println(txData)
return nil 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)
// 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[:])
} }
return tx.Hash().String()
} }
// Meant for internal tests // Meant for internal tests