Skip to content

Commit

Permalink
Merge pull request #2120 from CortexFoundation/dev
Browse files Browse the repository at this point in the history
vm errors of contract simplified
  • Loading branch information
ucwong committed Aug 20, 2024
2 parents 20e62d5 + 76f0d77 commit 72cfd9f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
57 changes: 33 additions & 24 deletions core/vm/cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,6 @@ func (cvm *CVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
contract := NewContract(caller, AccountRef(address), value, gas)
contract.SetCodeOptionalHash(&address, codeAndHash)

//if cvm.vmConfig.NoRecursion && cvm.depth > 0 {
// return nil, address, gas, nil, nil
//}

if cvm.Config().Tracer != nil {
if cvm.depth == 0 {
cvm.vmConfig.Tracer.CaptureStart(cvm, caller.Address(), address, true, codeAndHash.code, gas, value)
Expand All @@ -474,15 +470,44 @@ func (cvm *CVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
}
}

ret, err := cvm.interpreter.Run(contract, nil, false)
ret, err := cvm.initNewContract(contract, address, value)

// When an error was returned by the CVM or when setting the creation code
// above we revert to the snapshot and consume any gas remaining. Additionally
// when we're in homestead this also counts for code storage gas errors.
if err != nil && (cvm.chainRules.IsHomestead || err != ErrCodeStoreOutOfGas) {
cvm.StateDB.RevertToSnapshot(snapshot)
if err != ErrExecutionReverted {
contract.UseGas(contract.Gas)
}
}

if cvm.vmConfig.RPC_GetInternalTransaction {
ret = append(ret, []byte(caller.Address().String()+"-"+address.String()+"-"+value.String()+",")...)
}

if cvm.Config().Tracer != nil {
if cvm.depth == 0 {
cvm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, err)
} else {
cvm.Config().Tracer.CaptureExit(ret, gas-contract.Gas, err)
}
}

return ret, address, contract.Gas, contract.ModelGas, err
}

// initNewContract runs a new contract's creation code, performs checks on the
// resulting code that is to be deployed, and consumes necessary gas.
func (cvm *CVM) initNewContract(contract *Contract, address common.Address, value *big.Int) ([]byte, error) {
ret, err := cvm.interpreter.Run(contract, nil, false)
if err != nil {
return ret, err
}

// check whether the max code size has been exceeded
if err == nil && cvm.chainRules.IsEIP158 && len(ret) > params.MaxCodeSize {
err = errMaxCodeSizeExceeded
return ret, ErrMaxCodeSizeExceeded
}
// if the contract creation ran successfully and no errors were returned
// calculate the gas required to store the code. If the code could not
Expand All @@ -493,27 +518,11 @@ func (cvm *CVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
if contract.UseGas(createDataGas) {
cvm.StateDB.SetCode(address, ret)
} else {
err = ErrCodeStoreOutOfGas
return ret, ErrCodeStoreOutOfGas
}
}

// When an error was returned by the CVM or when setting the creation code
// above we revert to the snapshot and consume any gas remaining. Additionally
// when we're in homestead this also counts for code storage gas errors.
if err != nil && (cvm.chainRules.IsHomestead || err != ErrCodeStoreOutOfGas) {
cvm.StateDB.RevertToSnapshot(snapshot)
if err != ErrExecutionReverted {
contract.UseGas(contract.Gas)
}
}
if cvm.Config().Tracer != nil {
if cvm.depth == 0 {
cvm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, err)
} else {
cvm.Config().Tracer.CaptureExit(ret, gas-contract.Gas, err)
}
}
return ret, address, contract.Gas, contract.ModelGas, err
return ret, err
}

// Create creates a new contract using code as deployment code.
Expand Down
1 change: 1 addition & 0 deletions core/vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
ErrNoCompatibleInterpreter = errors.New("no compatible interpreter")
ErrInvalidMetaAuthor = errors.New("invalid meta author")

ErrMaxCodeSizeExceeded = errors.New("max code size exceeded")
ErrMaxInitCodeSizeExceeded = errors.New("max initcode size exceeded")
ErrGasUintOverflow = errors.New("gas uint64 overflow")
ErrInvalidJump = errors.New("invalid jump destination")
Expand Down

0 comments on commit 72cfd9f

Please sign in to comment.