Skip to content

Commit

Permalink
Merge pull request ethereum#206 from OffchainLabs/arb-receipt-contrac…
Browse files Browse the repository at this point in the history
…t-address

Add ContractAddress to stored receipt fields for arb txs
  • Loading branch information
PlasmaPower authored Mar 4, 2023
2 parents 41b2829 + c9b4514 commit 48b19f4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 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
15 changes: 10 additions & 5 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ type ExecutionResult struct {

// Arbitrum: a tx may yield others that need to run afterward (see retryables)
ScheduledTxes types.Transactions
// Arbitrum: the contract deployed from the top-level transaction, or nil if not a contract creation tx
TopLevelDeployed *common.Address
}

// Unwrap returns the internal evm error which allows us for further
Expand Down Expand Up @@ -360,12 +362,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 +414,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 48b19f4

Please sign in to comment.