Skip to content

Commit

Permalink
Make CommonTx have TransactionMisc as pointer
Browse files Browse the repository at this point in the history
Add store & load methods for atomic.Values to prevent nil pointer error
  • Loading branch information
ImTei committed Apr 17, 2024
1 parent a77acf7 commit 4364410
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
12 changes: 6 additions & 6 deletions core/types/access_list_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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{}{
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions core/types/blob_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand All @@ -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
}

Expand Down
12 changes: 6 additions & 6 deletions core/types/dynamic_fee_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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{}{
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
44 changes: 35 additions & 9 deletions core/types/legacy_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

type CommonTx struct {
TransactionMisc
*TransactionMisc

Nonce uint64 // nonce of sender account
Gas uint64 // gas limit
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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{}{
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit 4364410

Please sign in to comment.