Skip to content

Commit 4e599ee

Browse files
aaronbuchwaldfjl
andauthored
core/types: copy tx recipient address (ethereum#23376)
This resolves a long-standing TODO. The point of copying the address is to ensure that all data referenced by types.Transaction is independent of the data passed into the constructor. Co-authored-by: Felix Lange <[email protected]>
1 parent 57ff2de commit 4e599ee

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

core/types/access_list_tx.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type AccessListTx struct {
5959
func (tx *AccessListTx) copy() TxData {
6060
cpy := &AccessListTx{
6161
Nonce: tx.Nonce,
62-
To: tx.To, // TODO: copy pointed-to address
62+
To: copyAddressPtr(tx.To),
6363
Data: common.CopyBytes(tx.Data),
6464
Gas: tx.Gas,
6565
// These are copied below.
@@ -96,7 +96,6 @@ func (tx *AccessListTx) copy() TxData {
9696
// accessors for innerTx.
9797
func (tx *AccessListTx) txType() byte { return AccessListTxType }
9898
func (tx *AccessListTx) chainID() *big.Int { return tx.ChainID }
99-
func (tx *AccessListTx) protected() bool { return true }
10099
func (tx *AccessListTx) accessList() AccessList { return tx.AccessList }
101100
func (tx *AccessListTx) data() []byte { return tx.Data }
102101
func (tx *AccessListTx) gas() uint64 { return tx.Gas }

core/types/dynamic_fee_tx.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type DynamicFeeTx struct {
4343
func (tx *DynamicFeeTx) copy() TxData {
4444
cpy := &DynamicFeeTx{
4545
Nonce: tx.Nonce,
46-
To: tx.To, // TODO: copy pointed-to address
46+
To: copyAddressPtr(tx.To),
4747
Data: common.CopyBytes(tx.Data),
4848
Gas: tx.Gas,
4949
// These are copied below.
@@ -84,7 +84,6 @@ func (tx *DynamicFeeTx) copy() TxData {
8484
// accessors for innerTx.
8585
func (tx *DynamicFeeTx) txType() byte { return DynamicFeeTxType }
8686
func (tx *DynamicFeeTx) chainID() *big.Int { return tx.ChainID }
87-
func (tx *DynamicFeeTx) protected() bool { return true }
8887
func (tx *DynamicFeeTx) accessList() AccessList { return tx.AccessList }
8988
func (tx *DynamicFeeTx) data() []byte { return tx.Data }
9089
func (tx *DynamicFeeTx) gas() uint64 { return tx.Gas }

core/types/legacy_tx.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPric
6262
func (tx *LegacyTx) copy() TxData {
6363
cpy := &LegacyTx{
6464
Nonce: tx.Nonce,
65-
To: tx.To, // TODO: copy pointed-to address
65+
To: copyAddressPtr(tx.To),
6666
Data: common.CopyBytes(tx.Data),
6767
Gas: tx.Gas,
6868
// These are initialized below.

core/types/transaction.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,7 @@ func (tx *Transaction) Nonce() uint64 { return tx.inner.nonce() }
284284
// To returns the recipient address of the transaction.
285285
// For contract-creation transactions, To returns nil.
286286
func (tx *Transaction) To() *common.Address {
287-
// Copy the pointed-to address.
288-
ito := tx.inner.to()
289-
if ito == nil {
290-
return nil
291-
}
292-
cpy := *ito
293-
return &cpy
287+
return copyAddressPtr(tx.inner.to())
294288
}
295289

296290
// Cost returns gas * gasPrice + value.
@@ -632,3 +626,12 @@ func (m Message) Nonce() uint64 { return m.nonce }
632626
func (m Message) Data() []byte { return m.data }
633627
func (m Message) AccessList() AccessList { return m.accessList }
634628
func (m Message) IsFake() bool { return m.isFake }
629+
630+
// copyAddressPtr copies an address.
631+
func copyAddressPtr(a *common.Address) *common.Address {
632+
if a == nil {
633+
return nil
634+
}
635+
cpy := *a
636+
return &cpy
637+
}

0 commit comments

Comments
 (0)