Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

push #32

Merged
merged 2 commits into from
Oct 5, 2023
Merged

push #32

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/state/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ type StateDBI interface {
StopPrefetcher()
StartPrefetcher(namespace string)
IntermediateRoot(deleteEmptyObjects bool) common.Hash
GetPrecompileManager() any
calbera marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 4 additions & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
return sdb, nil
}

func (s *StateDB) GetPrecompileManager() any {
return nil
}

// StartPrefetcher initializes a new trie prefetcher to pull in nodes from the
// state trie concurrently while the state is mutated so that when we reach the
// commit phase, most of the needed data is already hot.
Expand Down
99 changes: 4 additions & 95 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb state.StateDBI, cfg
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
statedb.SetTxContext(tx.Hash(), i)
receipt, err := applyTransaction(vmenv, msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas)
receipt, err := applyTransaction(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv)
if err != nil {
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
Expand All @@ -110,10 +110,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb state.StateDBI, cfg
return receipts, allLogs, *usedGas, nil
}

func applyTransaction(
evm *vm.EVM, msg *Message, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI,
blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64,
) (*types.Receipt, error) {
func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) {
// Create a new context to be used in the EVM environment.
txContext := NewEVMTxContext(msg)
evm.Reset(txContext, statedb)
Expand Down Expand Up @@ -163,107 +160,19 @@ func applyTransaction(
return receipt, err
}

func applyTransactionWithResult(
evm *vm.EVM, msg *Message, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI,
blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64,
) (*types.Receipt, *ExecutionResult, error) {
// Create a new context to be used in the EVM environment.
txContext := NewEVMTxContext(msg)
evm.Reset(txContext, statedb)

// Apply the transaction to the current state (included in the env).
result, err := ApplyMessage(evm, msg, gp)
if err != nil {
return nil, nil, err
}

// Update the state with pending changes.
var root []byte
if config.IsByzantium(blockNumber) {
statedb.Finalise(true)
} else {
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
}
*usedGas += result.UsedGas

// Create a new receipt for the transaction, storing the intermediate root and gas used
// by the tx.
receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: *usedGas}
if result.Failed() {
receipt.Status = types.ReceiptStatusFailed
} else {
receipt.Status = types.ReceiptStatusSuccessful
}
receipt.TxHash = tx.Hash()
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())
}

// Set the receipt logs and create the bloom filter.
receipt.Logs = statedb.GetLogs(tx.Hash(), blockNumber.Uint64(), blockHash)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = blockHash
receipt.BlockNumber = blockNumber
receipt.TransactionIndex = uint(statedb.TxIndex())
return receipt, result, err
}

// ApplyTransaction attempts to apply a transaction to the given state database
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyTransaction(
config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool,
statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config,
) (*types.Receipt, error) {
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number, header.Time), header.BaseFee)
if err != nil {
return nil, err
}
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
vmenv := vm.NewEVM(blockContext, vm.TxContext{BlobHashes: tx.BlobHashes()}, statedb, config, cfg)
return applyTransaction(vmenv, msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas)
}

func ApplyTransactionWithResult(
config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool,
statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config,
) (*types.Receipt, *ExecutionResult, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number, header.Time), header.BaseFee)
if err != nil {
return nil, nil, err
}
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
vmenv := vm.NewEVM(blockContext, vm.TxContext{BlobHashes: tx.BlobHashes()}, statedb, config, cfg)
return applyTransactionWithResult(vmenv, msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas)
}

func ApplyTransactionWithEVM(
vmenv *vm.EVM, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI,
header *types.Header, tx *types.Transaction, usedGas *uint64,
) (*types.Receipt, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number, header.Time), header.BaseFee)
if err != nil {
return nil, err
}
return applyTransaction(vmenv, msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas)
}

func ApplyTransactionWithEVMWithResult(
vmenv *vm.EVM, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI,
baseFee *big.Int, blockNumber *big.Int, blockHash common.Hash, blockTime uint64,
tx *types.Transaction, usedGas *uint64,
) (*types.Receipt, *ExecutionResult, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, blockNumber, blockTime), baseFee)
if err != nil {
return nil, nil, err
}
return applyTransactionWithResult(vmenv, msg, config, gp, statedb, blockNumber, blockHash, tx, usedGas)
return applyTransaction(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv)
}

// ProcessBeaconBlockRoot applies the EIP-4788 system call to the beacon block root
Expand Down
10 changes: 2 additions & 8 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (st *StateTransition) buyGas() error {

st.initialGas = st.msg.GasLimit
st.state.SubBalance(st.msg.From, mgval)
return st.state.Error()
itsdevbear marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

func (st *StateTransition) preCheck() error {
Expand Down Expand Up @@ -402,7 +402,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
// Execute the preparatory steps for state transition which includes:
// - prepare accessList(post-berlin)
// - reset transient storage(eip 1153)
st.state.Prepare(rules, msg.From, st.evm.Context.Coinbase, msg.To, st.evm.PrecompileManager.GetActive(&rules), msg.AccessList)
st.state.Prepare(rules, msg.From, st.evm.Context.Coinbase, msg.To, vm.ActivePrecompiles(st.evm, rules), msg.AccessList)

var (
ret []byte
Expand All @@ -413,9 +413,6 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
} else {
// Increment the nonce for the next transaction
st.state.SetNonce(msg.From, st.state.GetNonce(sender.Address())+1)
if err := st.state.Error(); err != nil {
return nil, err
}
ret, st.gasRemaining, vmerr = st.evm.Call(sender, st.to(), msg.Data, st.gasRemaining, msg.Value)
}

Expand All @@ -439,9 +436,6 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
fee := new(big.Int).SetUint64(st.gasUsed())
fee.Mul(fee, effectiveTip)
st.state.AddBalance(st.evm.Context.Coinbase, fee)
if err := st.state.Error(); err != nil {
return nil, err
}
}

return &ExecutionResult{
Expand Down
18 changes: 12 additions & 6 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,25 @@ func init() {
}

// ActivePrecompiles returns the precompiles enabled with the current configuration.
func ActivePrecompiles(rules *params.Rules) []common.Address {
func ActivePrecompiles(evm *EVM, rules params.Rules) []common.Address {
var precompiles []common.Address
switch {
case rules.IsCancun:
return PrecompiledAddressesCancun
precompiles = append(precompiles, PrecompiledAddressesCancun...)
case rules.IsBerlin:
return PrecompiledAddressesBerlin
precompiles = append(precompiles, PrecompiledAddressesBerlin...)
case rules.IsIstanbul:
return PrecompiledAddressesIstanbul
precompiles = append(precompiles, PrecompiledAddressesIstanbul...)
case rules.IsByzantium:
return PrecompiledAddressesByzantium
precompiles = append(precompiles, PrecompiledAddressesByzantium...)
default:
return PrecompiledAddressesHomestead
precompiles = append(precompiles, PrecompiledAddressesHomestead...)
}

if evm != nil {
precompiles = append(precompiles, evm.PrecompileManager.GetActive(rules)...)
}
return precompiles
}

// ECRECOVER implemented as a native contract.
Expand Down
Loading