Skip to content

Commit

Permalink
Add ContractAddress to stored receipt fields for arb txs
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower committed Feb 28, 2023
1 parent 01cc043 commit 7270603
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
5 changes: 2 additions & 3 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
)

Expand Down Expand Up @@ -131,8 +130,8 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, author *com
receipt.GasUsed = result.UsedGas

// If the transaction created a contract, store the creation address in the receipt.
if msg.To() == nil {
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
if result.TopLevelDeployed != nil {
receipt.ContractAddress = *result.TopLevelDeployed
}

// Set the receipt logs and create the bloom filter.
Expand Down
16 changes: 10 additions & 6 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ type ExecutionResult struct {
ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode)

// Arbitrum: a tx may yield others that need to run afterward (see retryables)
ScheduledTxes types.Transactions
ScheduledTxes types.Transactions
TopLevelDeployed *common.Address
}

// Unwrap returns the internal evm error which allows us for further
Expand Down Expand Up @@ -360,12 +361,14 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
if rules.IsBerlin {
st.state.PrepareAccessList(msg.From(), msg.To(), vm.ActivePrecompiles(rules), msg.AccessList())
}
var deployedContract *common.Address
var (
ret []byte
vmerr error // vm errors do not effect consensus and are therefore not assigned to err
)
if contractCreation {
ret, _, st.gas, vmerr = st.evm.Create(sender, st.data, st.gas, st.value)
deployedContract = &common.Address{}
ret, *deployedContract, st.gas, vmerr = st.evm.Create(sender, st.data, st.gas, st.value)
} else {
// Increment the nonce for the next transaction
st.state.SetNonce(msg.From(), st.state.GetNonce(sender.Address())+1)
Expand Down Expand Up @@ -410,10 +413,11 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
}

return &ExecutionResult{
UsedGas: st.gasUsed(),
Err: vmerr,
ReturnData: ret,
ScheduledTxes: st.evm.ProcessingHook.ScheduledTxes(),
UsedGas: st.gasUsed(),
Err: vmerr,
ReturnData: ret,
ScheduledTxes: st.evm.ProcessingHook.ScheduledTxes(),
TopLevelDeployed: deployedContract,
}, nil
}

Expand Down
9 changes: 8 additions & 1 deletion core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type storedReceiptRLP struct {
CumulativeGasUsed uint64
L1GasUsed uint64
Logs []*LogForStorage
ContractAddress *common.Address `rlp:"optional"` // set on new versions if an Arbitrum tx type
}

type arbLegacyStoredReceiptRLP struct {
Expand Down Expand Up @@ -313,6 +314,9 @@ func (r *ReceiptForStorage) EncodeRLP(_w io.Writer) error {
}
}
w.ListEnd(logList)
if r.Type >= ArbitrumDepositTxType && r.Type != ArbitrumLegacyTxType && r.ContractAddress != (common.Address{}) {
w.WriteBytes(r.ContractAddress[:])
}
w.ListEnd(outerList)
return w.Flush()
}
Expand Down Expand Up @@ -379,6 +383,9 @@ func decodeStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
r.Logs[i] = (*Log)(log)
}
r.Bloom = CreateBloom(Receipts{(*Receipt)(r)})
if stored.ContractAddress != nil {
r.ContractAddress = *stored.ContractAddress
}

return nil
}
Expand Down Expand Up @@ -464,7 +471,7 @@ func (rs Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, nu

if rs[i].Type != ArbitrumLegacyTxType {
// The contract address can be derived from the transaction itself
if txs[i].To() == nil {
if txs[i].To() == nil && rs[i].ContractAddress == (common.Address{}) {
// Deriving the signer is expensive, only do if it's actually needed
from, _ := Sender(signer, txs[i])
rs[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce())
Expand Down

0 comments on commit 7270603

Please sign in to comment.