core/types: add Transaction.From()

This commit is contained in:
Jim McDonald 2018-01-22 16:26:00 +00:00
parent 5d4267911a
commit 26c839ce2d
2 changed files with 34 additions and 12 deletions

View file

@ -185,6 +185,20 @@ func (tx *Transaction) Value() *big.Int { return new(big.Int).Set(tx.data.Amo
func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce } func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce }
func (tx *Transaction) CheckNonce() bool { return true } func (tx *Transaction) CheckNonce() bool { return true }
// From returns the sender address of the transaction.
// It returns nil if the transaction is unsigned.
func (tx *Transaction) From() *common.Address {
if tx.data.V == nil {
return nil
}
signer := deriveSigner(tx.data.V)
if from, err := Sender(signer, tx); err != nil {
return nil
} else {
return &from
}
}
// To returns the recipient address of the transaction. // To returns the recipient address of the transaction.
// It returns nil if the transaction is a contract creation. // It returns nil if the transaction is a contract creation.
func (tx *Transaction) To() *common.Address { func (tx *Transaction) To() *common.Address {

View file

@ -93,12 +93,12 @@ func TestRecipientEmpty(t *testing.T) {
t.FailNow() t.FailNow()
} }
from, err := Sender(HomesteadSigner{}, tx) from := tx.From()
if err != nil { if from == nil {
t.Error(err) t.Error("derived address not present")
t.FailNow() t.FailNow()
} }
if addr != from { if addr != *from {
t.Error("derived address doesn't match") t.Error("derived address doesn't match")
} }
} }
@ -112,13 +112,13 @@ func TestRecipientNormal(t *testing.T) {
t.FailNow() t.FailNow()
} }
from, err := Sender(HomesteadSigner{}, tx) from := tx.From()
if err != nil { if from == nil {
t.Error(err) t.Error("derived address not present")
t.FailNow() t.FailNow()
} }
if addr != from { if addr != *from {
t.Error("derived address doesn't match") t.Error("derived address doesn't match")
} }
} }
@ -155,11 +155,19 @@ func TestTransactionPriceNonceSort(t *testing.T) {
t.Errorf("expected %d transactions, found %d", 25*25, len(txs)) t.Errorf("expected %d transactions, found %d", 25*25, len(txs))
} }
for i, txi := range txs { for i, txi := range txs {
fromi, _ := Sender(signer, txi) fromi := txi.From()
if fromi == nil {
t.Error("derived address not present")
t.FailNow()
}
// Make sure the nonce order is valid // Make sure the nonce order is valid
for j, txj := range txs[i+1:] { for j, txj := range txs[i+1:] {
fromj, _ := Sender(signer, txj) fromj := txj.From()
if fromj == nil {
t.Error("derived address not present")
t.FailNow()
}
if fromi == fromj && txi.Nonce() > txj.Nonce() { if fromi == fromj && txi.Nonce() > txj.Nonce() {
t.Errorf("invalid nonce ordering: tx #%d (A=%x N=%v) < tx #%d (A=%x N=%v)", i, fromi[:4], txi.Nonce(), i+j, fromj[:4], txj.Nonce()) t.Errorf("invalid nonce ordering: tx #%d (A=%x N=%v) < tx #%d (A=%x N=%v)", i, fromi[:4], txi.Nonce(), i+j, fromj[:4], txj.Nonce())
@ -168,13 +176,13 @@ func TestTransactionPriceNonceSort(t *testing.T) {
// Find the previous and next nonce of this account // Find the previous and next nonce of this account
prev, next := i-1, i+1 prev, next := i-1, i+1
for j := i - 1; j >= 0; j-- { for j := i - 1; j >= 0; j-- {
if fromj, _ := Sender(signer, txs[j]); fromi == fromj { if fromj, _ := Sender(signer, txs[j]); *fromi == fromj {
prev = j prev = j
break break
} }
} }
for j := i + 1; j < len(txs); j++ { for j := i + 1; j < len(txs); j++ {
if fromj, _ := Sender(signer, txs[j]); fromi == fromj { if fromj, _ := Sender(signer, txs[j]); *fromi == fromj {
next = j next = j
break break
} }