forked from evmos/ethermint
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Initialize precompiles in UpdateParams #58
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,24 +16,34 @@ | |
package keeper | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"math/big" | ||
"strconv" | ||
|
||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
|
||
tmbytes "github.com/cometbft/cometbft/libs/bytes" | ||
tmtypes "github.com/cometbft/cometbft/types" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
"github.com/armon/go-metrics" | ||
tmbytes "github.com/cometbft/cometbft/libs/bytes" | ||
tmtypes "github.com/cometbft/cometbft/types" | ||
"github.com/cosmos/cosmos-sdk/telemetry" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
|
||
"github.com/evmos/ethermint/x/evm/statedb" | ||
"github.com/evmos/ethermint/x/evm/types" | ||
) | ||
|
||
const PrecompileNonce uint64 = 1 | ||
|
||
var ( | ||
PrecompileCode = []byte{0x1} | ||
PrecompileCodeHash = crypto.Keccak256Hash(PrecompileCode) | ||
) | ||
|
||
var _ types.MsgServer = &Keeper{} | ||
|
||
// EthereumTx implements the gRPC MsgServer interface. It receives a transaction which is then | ||
|
@@ -153,9 +163,64 @@ func (k *Keeper) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) | |
} | ||
|
||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
if err := k.updatePrecompiles(ctx, req.Params.EnabledPrecompiles); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := k.SetParams(ctx, req.Params); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &types.MsgUpdateParamsResponse{}, nil | ||
} | ||
|
||
func (k *Keeper) updatePrecompiles(ctx sdk.Context, newEnabledPrecompiles []string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we return an error if the precompile contract isn't registered in the geth precompile modules (RegisteredModules())? Also small optional thing: I think this could be moved to a separate file, just feels it could be separate from the msg_server |
||
oldParams := k.GetParams(ctx) | ||
oldEnabledPrecompiles := oldParams.GetEnabledPrecompiles() | ||
|
||
newEnabledPrecompilesMap := make(map[common.Address]struct{}, len(newEnabledPrecompiles)) | ||
|
||
// initialize newly enabled precompiles | ||
for _, hexAddr := range newEnabledPrecompiles { | ||
addr := common.HexToAddress(hexAddr) | ||
newEnabledPrecompilesMap[addr] = struct{}{} | ||
|
||
account := k.GetAccount(ctx, addr) | ||
if account != nil && account.Nonce == PrecompileNonce && bytes.Equal(account.CodeHash, PrecompileCodeHash[:]) { | ||
// skip initialization if account is already initialized | ||
continue | ||
} | ||
|
||
// Set the nonce of the precompile's address (as is done when a contract is created) to ensure | ||
// that it is marked as non-empty and will not be cleaned up when the statedb is finalized. | ||
err := k.SetAccount(ctx, addr, statedb.Account{ | ||
Nonce: PrecompileNonce, | ||
Balance: big.NewInt(0), | ||
CodeHash: PrecompileCodeHash[:], | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Set the code of the precompile's address to a non-zero length byte slice to ensure that the precompile | ||
// can be called from within Solidity contracts. Solidity adds a check before invoking a contract to ensure | ||
// that it does not attempt to invoke a non-existent contract. | ||
k.SetCode(ctx, PrecompileCodeHash[:], PrecompileCode) | ||
drklee3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// uninitialize disabled precompiles | ||
for _, hexAddr := range oldEnabledPrecompiles { | ||
addr := common.HexToAddress(hexAddr) | ||
|
||
if _, ok := newEnabledPrecompilesMap[addr]; ok { | ||
continue | ||
} | ||
|
||
// if precompile existed in oldEnabledPrecompiles, but doesn't exist in newEnabledPrecompiles - it means it's disabled and should be uninitialized | ||
if err := k.DeleteAccount(ctx, addr); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updatePrecompiles()
naming a bit vague in usage here since it's also set in state in SetParams() - could optionally rename to something more specific / different from just updating/setting like initializePreocmpiles