From 4989c3e101a916f42f84af603de14f56d2dd542b Mon Sep 17 00:00:00 2001 From: summerpro <974741468@qq.com> Date: Wed, 30 Dec 2020 16:06:22 +0800 Subject: [PATCH] add comment --- x/evm/handler.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/x/evm/handler.go b/x/evm/handler.go index 99d3a969a..9eda3e079 100644 --- a/x/evm/handler.go +++ b/x/evm/handler.go @@ -16,17 +16,31 @@ import ( func NewHandler(k Keeper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) (result *sdk.Result, err error) { snapshotStateDB := k.CommitStateDB.Copy() + + // The "recover" code here is used to solve the problem of dirty data + // in CommitStateDB due to insufficient gas. + + // The following is a detailed description: // If the gas is insufficient during the execution of the "handler", // panic will be thrown from the function "ConsumeGas" and finally // caught by the function "runTx" from Cosmos. The function "runTx" // will think that the execution of Msg has failed and the modified // data in the Store will not take effect. - // The fault is that the modified data in CommitStateDB has not been - // rolled back, resulting in bad data. + + // Stacktraceļ¼šrunTx->runMsgs->handler->...->gaskv.Store.Set->ConsumeGas + + // The problem is that when the modified data in the Store does not take + // effect, the data in the modified CommitStateDB is not rolled back, + // they take effect, and dirty data is generated. // Therefore, the code here specifically deals with this situation. // See https://github.com/cosmos/ethermint/issues/668 for more information. defer func() { if r := recover(); r != nil { + // We first used "k.CommitStateDB = snapshotStateDB" to roll back + // CommitStateDB, but this can only change the CommitStateDB in the + // current Keeper object, but the Keeper object will be destroyed + // soon, it is not a global variable, so the content pointed to by + // the CommitStateDB pointer can be modified to take effect. types.CopyCommitStateDB(snapshotStateDB, k.CommitStateDB) panic(r) }