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

fix: use snapshot to support revert #90

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions x/evm/keeper/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ func (k Keeper) CreateEVM(ctx context.Context, caller common.Address, tracer *tr
return ctx, nil, err
}

// prepare SDK context for EVM execution
ctx, err = prepareSDKContext(sdk.UnwrapSDKContext(ctx))
if err != nil {
return ctx, nil, err
}

evm := &vm.EVM{}
blockContext, err := k.buildBlockContext(ctx, evm, fee)
if err != nil {
Expand All @@ -181,19 +187,13 @@ func (k Keeper) CreateEVM(ctx context.Context, caller common.Address, tracer *tr
NumRetainBlockHashes: &params.NumRetainBlockHashes,
}

// prepare SDK context for EVM execution
ctx, err = prepareSDKContext(sdk.UnwrapSDKContext(ctx))
if err != nil {
return ctx, nil, err
}

*evm = *vm.NewEVMWithPrecompiles(
blockContext,
txContext,
stateDB,
types.DefaultChainConfig(ctx),
vmConfig,
k.precompiles.toMap(ctx),
k.precompiles.toMap(stateDB),
)

if tracer != nil {
Expand Down
6 changes: 2 additions & 4 deletions x/evm/keeper/precompiles.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper

import (
"context"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"

Expand Down Expand Up @@ -55,10 +53,10 @@ func (k *Keeper) loadPrecompiles() error {
type precompiles []precompile

// toMap converts the precompiles to a map.
func (ps precompiles) toMap(ctx context.Context) map[common.Address]vm.PrecompiledContract {
func (ps precompiles) toMap(stateDB types.StateDB) map[common.Address]vm.PrecompiledContract {
m := make(map[common.Address]vm.PrecompiledContract)
for _, p := range ps {
m[p.addr] = p.contract.(types.WithContext).WithContext(ctx)
m[p.addr] = p.contract.(types.WithStateDB).WithStateDB(stateDB)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add type assertion safety check.

The unchecked type assertion p.contract.(types.WithStateDB) could cause a panic if a precompile doesn't implement the WithStateDB interface.

Consider adding a safety check:

-		m[p.addr] = p.contract.(types.WithStateDB).WithStateDB(stateDB)
+		if withState, ok := p.contract.(types.WithStateDB); ok {
+			m[p.addr] = withState.WithStateDB(stateDB)
+		} else {
+			panic(fmt.Sprintf("precompile at %s does not implement WithStateDB interface", p.addr))
+		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
m[p.addr] = p.contract.(types.WithStateDB).WithStateDB(stateDB)
if withState, ok := p.contract.(types.WithStateDB); ok {
m[p.addr] = withState.WithStateDB(stateDB)
} else {
panic(fmt.Sprintf("precompile at %s does not implement WithStateDB interface", p.addr))
}

}

return m
Expand Down
Loading
Loading