From 4364410f705ee6a4f6b34af442d4be87b3eb8a3d Mon Sep 17 00:00:00 2001 From: Tei Im Date: Wed, 17 Apr 2024 14:06:44 -0600 Subject: [PATCH] Make CommonTx have TransactionMisc as pointer Add store & load methods for atomic.Values to prevent nil pointer error --- core/types/access_list_tx.go | 12 +++++----- core/types/blob_tx.go | 8 +++---- core/types/dynamic_fee_tx.go | 12 +++++----- core/types/legacy_tx.go | 44 ++++++++++++++++++++++++++++-------- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/core/types/access_list_tx.go b/core/types/access_list_tx.go index c0b659fec12..02f0dc2bfaa 100644 --- a/core/types/access_list_tx.go +++ b/core/types/access_list_tx.go @@ -47,7 +47,7 @@ func (tx *AccessListTx) copy() *AccessListTx { cpy := &AccessListTx{ LegacyTx: LegacyTx{ CommonTx: CommonTx{ - TransactionMisc: TransactionMisc{}, + TransactionMisc: &TransactionMisc{}, Nonce: tx.Nonce, To: tx.To, // TODO: copy pointed-to address Data: libcommon.CopyBytes(tx.Data), @@ -455,13 +455,13 @@ func (tx *AccessListTx) FakeSign(address libcommon.Address) (Transaction, error) cpy.R.Set(u256.Num1) cpy.S.Set(u256.Num1) cpy.V.Set(u256.Num4) - cpy.from.Store(address) + cpy.StoreFrom(address) return cpy, nil } // Hash computes the hash (but not for signatures!) func (tx *AccessListTx) Hash() libcommon.Hash { - if hash := tx.hash.Load(); hash != nil { + if hash := tx.LoadHash(); hash != nil { return *hash.(*libcommon.Hash) } hash := prefixedRlpHash(AccessListTxType, []interface{}{ @@ -475,7 +475,7 @@ func (tx *AccessListTx) Hash() libcommon.Hash { tx.AccessList, tx.V, tx.R, tx.S, }) - tx.hash.Store(&hash) + tx.StoreHash(&hash) return hash } @@ -505,13 +505,13 @@ func (tx *AccessListTx) GetChainID() *uint256.Int { } func (tx *AccessListTx) Sender(signer Signer) (libcommon.Address, error) { - if sc := tx.from.Load(); sc != nil { + if sc := tx.LoadFrom(); sc != nil { return sc.(libcommon.Address), nil } addr, err := signer.Sender(tx) if err != nil { return libcommon.Address{}, err } - tx.from.Store(addr) + tx.StoreFrom(addr) return addr, nil } diff --git a/core/types/blob_tx.go b/core/types/blob_tx.go index 0953d97d451..6b57b1f47cc 100644 --- a/core/types/blob_tx.go +++ b/core/types/blob_tx.go @@ -81,19 +81,19 @@ func (stx *BlobTx) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (Me } func (stx *BlobTx) Sender(signer Signer) (libcommon.Address, error) { - if sc := stx.from.Load(); sc != nil { + if sc := stx.LoadFrom(); sc != nil { return sc.(libcommon.Address), nil } addr, err := signer.Sender(stx) if err != nil { return libcommon.Address{}, err } - stx.from.Store(addr) + stx.StoreFrom(addr) return addr, nil } func (stx *BlobTx) Hash() libcommon.Hash { - if hash := stx.hash.Load(); hash != nil { + if hash := stx.LoadHash(); hash != nil { return *hash.(*libcommon.Hash) } hash := prefixedRlpHash(BlobTxType, []interface{}{ @@ -110,7 +110,7 @@ func (stx *BlobTx) Hash() libcommon.Hash { stx.BlobVersionedHashes, stx.V, stx.R, stx.S, }) - stx.hash.Store(&hash) + stx.StoreHash(&hash) return hash } diff --git a/core/types/dynamic_fee_tx.go b/core/types/dynamic_fee_tx.go index 11e4ec8b45b..005fe239588 100644 --- a/core/types/dynamic_fee_tx.go +++ b/core/types/dynamic_fee_tx.go @@ -69,7 +69,7 @@ func (tx *DynamicFeeTransaction) Unwrap() Transaction { func (tx *DynamicFeeTransaction) copy() *DynamicFeeTransaction { cpy := &DynamicFeeTransaction{ CommonTx: CommonTx{ - TransactionMisc: TransactionMisc{}, + TransactionMisc: &TransactionMisc{}, Nonce: tx.Nonce, To: tx.To, // TODO: copy pointed-to address Data: libcommon.CopyBytes(tx.Data), @@ -172,7 +172,7 @@ func (tx *DynamicFeeTransaction) FakeSign(address libcommon.Address) (Transactio cpy.R.Set(u256.Num1) cpy.S.Set(u256.Num1) cpy.V.Set(u256.Num4) - cpy.from.Store(address) + cpy.StoreFrom(address) return cpy, nil } @@ -380,7 +380,7 @@ func (tx *DynamicFeeTransaction) AsMessage(s Signer, baseFee *big.Int, rules *ch // Hash computes the hash (but not for signatures!) func (tx *DynamicFeeTransaction) Hash() libcommon.Hash { - if hash := tx.hash.Load(); hash != nil { + if hash := tx.LoadHash(); hash != nil { return *hash.(*libcommon.Hash) } hash := prefixedRlpHash(DynamicFeeTxType, []interface{}{ @@ -395,7 +395,7 @@ func (tx *DynamicFeeTransaction) Hash() libcommon.Hash { tx.AccessList, tx.V, tx.R, tx.S, }) - tx.hash.Store(&hash) + tx.StoreHash(&hash) return hash } @@ -427,14 +427,14 @@ func (tx *DynamicFeeTransaction) GetChainID() *uint256.Int { } func (tx *DynamicFeeTransaction) Sender(signer Signer) (libcommon.Address, error) { - if sc := tx.from.Load(); sc != nil { + if sc := tx.LoadFrom(); sc != nil { return sc.(libcommon.Address), nil } addr, err := signer.Sender(tx) if err != nil { return libcommon.Address{}, err } - tx.from.Store(addr) + tx.StoreFrom(addr) return addr, nil } diff --git a/core/types/legacy_tx.go b/core/types/legacy_tx.go index 21139d9aa22..87b123d02a5 100644 --- a/core/types/legacy_tx.go +++ b/core/types/legacy_tx.go @@ -33,7 +33,7 @@ import ( ) type CommonTx struct { - TransactionMisc + *TransactionMisc Nonce uint64 // nonce of sender account Gas uint64 // gas limit @@ -67,15 +67,41 @@ func (ct *CommonTx) GetData() []byte { return ct.Data } +func (ct *CommonTx) initializeMisc() { + if ct.TransactionMisc == nil { + ct.TransactionMisc = &TransactionMisc{} + } +} + +func (ct *CommonTx) LoadHash() any { + ct.initializeMisc() + return ct.hash.Load() +} + +func (ct *CommonTx) StoreHash(val any) { + ct.initializeMisc() + ct.hash.Store(val) +} + +func (ct *CommonTx) LoadFrom() any { + ct.initializeMisc() + return ct.from.Load() +} + +func (ct *CommonTx) StoreFrom(val any) { + ct.initializeMisc() + ct.from.Store(val) +} + func (ct *CommonTx) GetSender() (libcommon.Address, bool) { - if sc := ct.from.Load(); sc != nil { + if sc := ct.LoadFrom(); sc != nil { return sc.(libcommon.Address), true } return libcommon.Address{}, false } func (ct *CommonTx) SetSender(addr libcommon.Address) { - ct.from.Store(addr) + ct.StoreFrom(addr) } func (ct *CommonTx) Protected() bool { @@ -162,7 +188,7 @@ func NewContractCreation(nonce uint64, amount *uint256.Int, gasLimit uint64, gas func (tx *LegacyTx) copy() *LegacyTx { cpy := &LegacyTx{ CommonTx: CommonTx{ - TransactionMisc: TransactionMisc{}, + TransactionMisc: &TransactionMisc{}, Nonce: tx.Nonce, To: tx.To, // TODO: copy pointed-to address Data: libcommon.CopyBytes(tx.Data), @@ -376,13 +402,13 @@ func (tx *LegacyTx) FakeSign(address libcommon.Address) (Transaction, error) { cpy.R.Set(u256.Num1) cpy.S.Set(u256.Num1) cpy.V.Set(u256.Num4) - cpy.from.Store(address) + cpy.StoreFrom(address) return cpy, nil } // Hash computes the hash (but not for signatures!) func (tx *LegacyTx) Hash() libcommon.Hash { - if hash := tx.hash.Load(); hash != nil { + if hash := tx.LoadHash(); hash != nil { return *hash.(*libcommon.Hash) } hash := rlpHash([]interface{}{ @@ -394,7 +420,7 @@ func (tx *LegacyTx) Hash() libcommon.Hash { tx.Data, tx.V, tx.R, tx.S, }) - tx.hash.Store(&hash) + tx.StoreHash(&hash) return hash } @@ -431,13 +457,13 @@ func (tx *LegacyTx) GetChainID() *uint256.Int { } func (tx *LegacyTx) Sender(signer Signer) (libcommon.Address, error) { - if sc := tx.from.Load(); sc != nil { + if sc := tx.LoadFrom(); sc != nil { return sc.(libcommon.Address), nil } addr, err := signer.Sender(tx) if err != nil { return libcommon.Address{}, err } - tx.from.Store(addr) + tx.StoreFrom(addr) return addr, nil }