Skip to content

Commit

Permalink
feat: revert if precompile call is failed
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed Aug 26, 2024
1 parent 4edcdef commit ebdc9dd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ require (
replace (
github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/b-harvest/cosmos-sdk v0.0.0-20240826083719-aa4a1aa73255
github.com/ethereum/go-ethereum => github.com/b-harvest/go-ethereum v0.0.0-20240822072248-9e1916420877
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.33
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ github.com/aws/aws-sdk-go-v2/service/sso v1.1.1/go.mod h1:SuZJxklHxLAXgLTc1iFXbE
github.com/aws/aws-sdk-go-v2/service/sts v1.1.1/go.mod h1:Wi0EBZwiz/K44YliU0EKxqTCJGUfYTWXrrBwkq736bM=
github.com/aws/smithy-go v1.1.0/go.mod h1:EzMw8dbp/YJL4A5/sbhGddag+NPT7q084agLbB9LgIw=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/b-harvest/cosmos-sdk v0.0.0-20240826083719-aa4a1aa73255 h1:KaTVLSPGYI+oyw6/gKVto7GnDDfh11SoJqOwDP0+aVI=
github.com/b-harvest/cosmos-sdk v0.0.0-20240826083719-aa4a1aa73255/go.mod h1:bScuNwWAP0TZJpUf+SHXRU3xGoUPp+X9nAzfeIXts40=
github.com/b-harvest/go-ethereum v0.0.0-20240822072248-9e1916420877 h1:lU3LJLDn7OA4/O9yTFZ1y6+fVvDMfpE30njFoIy+gkk=
github.com/b-harvest/go-ethereum v0.0.0-20240822072248-9e1916420877/go.mod h1:IJBNMtzKcNHPtllYihy6BL2IgK1u+32JriaTbdt4v+w=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -236,8 +238,6 @@ github.com/cosmos/cosmos-db v0.0.0-20221226095112-f3c38ecb5e32 h1:zlCp9n3uwQieEL
github.com/cosmos/cosmos-db v0.0.0-20221226095112-f3c38ecb5e32/go.mod h1:kwMlEC4wWvB48zAShGKVqboJL6w4zCLesaNQ3YLU2BQ=
github.com/cosmos/cosmos-proto v1.0.0-beta.3 h1:VitvZ1lPORTVxkmF2fAp3IiA61xVwArQYKXTdEcpW6o=
github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I=
github.com/cosmos/cosmos-sdk v0.45.16 h1:5ba/Bh5/LE55IwHQuCU4fiG4eXeDKtSWzehXRpaKDcw=
github.com/cosmos/cosmos-sdk v0.45.16/go.mod h1:bScuNwWAP0TZJpUf+SHXRU3xGoUPp+X9nAzfeIXts40=
github.com/cosmos/cosmos-sdk/ics23/go v0.8.0 h1:iKclrn3YEOwk4jQHT2ulgzuXyxmzmPczUalMwW4XH9k=
github.com/cosmos/cosmos-sdk/ics23/go v0.8.0/go.mod h1:2a4dBq88TUoqoWAU5eu0lGvpFP3wWDPgdHPargtyw30=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
Expand Down
14 changes: 14 additions & 0 deletions x/evm/statedb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"sort"

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

sdk "github.com/cosmos/cosmos-sdk/types"
)

// journalEntry is a modification entry in the state change journal that can be
Expand Down Expand Up @@ -139,6 +141,10 @@ type (
address *common.Address
slot *common.Hash
}
precompileChange struct {
ms sdk.MultiStore
es sdk.Events
}
)

func (ch createObjectChange) revert(s *StateDB) {
Expand Down Expand Up @@ -241,3 +247,11 @@ func (ch accessListAddSlotChange) revert(s *StateDB) {
func (ch accessListAddSlotChange) dirtied() *common.Address {
return nil
}

func (ch precompileChange) revert(s *StateDB) {
s.RevertWithMultiStoreSnapshot(ch.ms)
}

func (ch precompileChange) dirtied() *common.Address {
return nil
}
31 changes: 29 additions & 2 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package statedb

import (
"errors"
"fmt"
"math/big"
"sort"
Expand Down Expand Up @@ -287,12 +288,38 @@ func (s *StateDB) setStateObject(object *stateObject) {
s.stateObjects[object.Address()] = object
}

func (s *StateDB) GetCacheContext() sdk.Context {
func (s *StateDB) GetCacheContext() (sdk.Context, error) {
if s.writeCache == nil {
if s.ctx.MultiStore() == nil {
return s.ctx, errors.New("ctx has no multi store")
}
s.cacheCtx, s.writeCache = s.ctx.CacheContext()
}

return s.cacheCtx
return s.cacheCtx, nil
}

func (s *StateDB) MultiStoreSnapshot() (sdk.CacheMultiStore, error) {
ctx, err := s.GetCacheContext()
if err != nil { // means s.ctx.MultiStore() == nil
return nil, err
}

cms := ctx.MultiStore().(sdk.CacheMultiStore)
snapshot := cms.Copy()
return snapshot, nil
}

func (s *StateDB) RevertWithMultiStoreSnapshot(snapshot sdk.MultiStore) {
s.cacheCtx = s.cacheCtx.
WithMultiStore(snapshot).
WithEventManager(sdk.NewEventManager())
}

// If revert is occured, the snapshot of journal is overwrited to MultiStore of ctx,

Check failure on line 319 in x/evm/statedb/statedb.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

`occured` is a misspelling of `occurred` (misspell)
// The events is just for debug.
func (s *StateDB) PostPrecompileProcessing(snapshot sdk.MultiStore, events sdk.Events) {
s.journal.append(precompileChange{snapshot, events})
}

/*
Expand Down

0 comments on commit ebdc9dd

Please sign in to comment.