Skip to content

Commit

Permalink
[evm] fix RevertSnapshot (#4492)
Browse files Browse the repository at this point in the history
Co-authored-by: envestcc <[email protected]>
  • Loading branch information
dustinxie and envestcc authored Nov 19, 2024
1 parent b2e6537 commit 65044c7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ type (
VerifyNotContainerBeforeRun bool
ValidateActionWithState bool
CheckStakingDurationUpperLimit bool
FixRevertSnapshot bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -315,6 +316,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
VerifyNotContainerBeforeRun: g.IsVanuatu(height),
ValidateActionWithState: g.IsVanuatu(height),
CheckStakingDurationUpperLimit: g.IsVanuatu(height),
FixRevertSnapshot: g.IsVanuatu(height),
},
)
}
Expand Down
14 changes: 9 additions & 5 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,12 @@ func ReadContractStorage(
}

func prepareStateDB(ctx context.Context, sm protocol.StateManager) (*StateDBAdapter, error) {
actionCtx := protocol.MustGetActionCtx(ctx)
blkCtx := protocol.MustGetBlockCtx(ctx)
featureCtx := protocol.MustGetFeatureCtx(ctx)
opts := []StateDBAdapterOption{}
var (
actionCtx = protocol.MustGetActionCtx(ctx)
blkCtx = protocol.MustGetBlockCtx(ctx)
featureCtx = protocol.MustGetFeatureCtx(ctx)
opts = []StateDBAdapterOption{}
)
if featureCtx.CreateLegacyNonceAccount {
opts = append(opts, LegacyNonceAccountOption())
}
Expand Down Expand Up @@ -376,7 +378,9 @@ func prepareStateDB(ctx context.Context, sm protocol.StateManager) (*StateDBAdap
if featureCtx.EnableCancunEVM {
opts = append(opts, EnableCancunEVMOption())
}

if featureCtx.FixRevertSnapshot || actionCtx.ReadOnly {
opts = append(opts, FixRevertSnapshotOption())
}
return NewStateDBAdapter(
sm,
blkCtx.BlockHeight,
Expand Down
31 changes: 22 additions & 9 deletions action/protocol/execution/evm/evmstatedbadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type (
zeroNonceForFreshAccount bool
panicUnrecoverableError bool
enableCancun bool
fixRevertSnapshot bool
}
)

Expand Down Expand Up @@ -187,6 +188,14 @@ func EnableCancunEVMOption() StateDBAdapterOption {
}
}

// FixRevertSnapshotOption set fixRevertSnapshot as true
func FixRevertSnapshotOption() StateDBAdapterOption {
return func(adapter *StateDBAdapter) error {
adapter.fixRevertSnapshot = true
return nil
}
}

// NewStateDBAdapter creates a new state db with iotex blockchain
func NewStateDBAdapter(
sm protocol.StateManager,
Expand Down Expand Up @@ -626,10 +635,14 @@ func (stateDB *StateDBAdapter) RevertToSnapshot(snapshot int) {
log.L().Error("Failed to revert to snapshot.", zap.Int("snapshot", snapshot))
return
}
deleteSnapshot := snapshot
if stateDB.fixRevertSnapshot {
deleteSnapshot++
}
// restore gas refund
if !stateDB.manualCorrectGasRefund {
stateDB.refund = stateDB.refundSnapshot[snapshot]
for i := snapshot; ; i++ {
for i := deleteSnapshot; ; i++ {
if _, ok := stateDB.refundSnapshot[i]; ok {
delete(stateDB.refundSnapshot, i)
} else {
Expand All @@ -640,7 +653,7 @@ func (stateDB *StateDBAdapter) RevertToSnapshot(snapshot int) {
// restore access list
stateDB.accessList = stateDB.accessListSnapshot[snapshot]
{
for i := snapshot; ; i++ {
for i := deleteSnapshot; ; i++ {
if _, ok := stateDB.accessListSnapshot[i]; ok {
delete(stateDB.accessListSnapshot, i)
} else {
Expand All @@ -652,7 +665,7 @@ func (stateDB *StateDBAdapter) RevertToSnapshot(snapshot int) {
//restore transientStorage
stateDB.transientStorage = stateDB.transientStorageSnapshot[snapshot]
{
for i := snapshot; ; i++ {
for i := deleteSnapshot; ; i++ {
if _, ok := stateDB.transientStorageSnapshot[i]; ok {
delete(stateDB.transientStorageSnapshot, i)
} else {
Expand All @@ -663,7 +676,7 @@ func (stateDB *StateDBAdapter) RevertToSnapshot(snapshot int) {
// restore created accounts
stateDB.createdAccount = stateDB.createdAccountSnapshot[snapshot]
{
for i := snapshot; ; i++ {
for i := deleteSnapshot; ; i++ {
if _, ok := stateDB.createdAccountSnapshot[i]; ok {
delete(stateDB.createdAccountSnapshot, i)
} else {
Expand All @@ -675,15 +688,15 @@ func (stateDB *StateDBAdapter) RevertToSnapshot(snapshot int) {
// restore logs and txLogs
if stateDB.revertLog {
stateDB.logs = stateDB.logs[:stateDB.logsSnapshot[snapshot]]
for i := snapshot; ; i++ {
for i := deleteSnapshot; ; i++ {
if _, ok := stateDB.logsSnapshot[i]; ok {
delete(stateDB.logsSnapshot, i)
} else {
break
}
}
stateDB.transactionLogs = stateDB.transactionLogs[:stateDB.txLogsSnapshot[snapshot]]
for i := snapshot; ; i++ {
for i := deleteSnapshot; ; i++ {
if _, ok := stateDB.txLogsSnapshot[i]; ok {
delete(stateDB.txLogsSnapshot, i)
} else {
Expand All @@ -694,7 +707,7 @@ func (stateDB *StateDBAdapter) RevertToSnapshot(snapshot int) {
// restore the SelfDestruct accounts
stateDB.selfDestructed = ds
if stateDB.fixSnapshotOrder {
for i := snapshot; ; i++ {
for i := deleteSnapshot; ; i++ {
if _, ok := stateDB.selfDestructedSnapshot[i]; ok {
delete(stateDB.selfDestructedSnapshot, i)
} else {
Expand All @@ -712,7 +725,7 @@ func (stateDB *StateDBAdapter) RevertToSnapshot(snapshot int) {
}
}
if stateDB.fixSnapshotOrder {
for i := snapshot; ; i++ {
for i := deleteSnapshot; ; i++ {
if _, ok := stateDB.contractSnapshot[i]; ok {
delete(stateDB.contractSnapshot, i)
} else {
Expand All @@ -723,7 +736,7 @@ func (stateDB *StateDBAdapter) RevertToSnapshot(snapshot int) {
// restore preimages
stateDB.preimages = stateDB.preimageSnapshot[snapshot]
if stateDB.fixSnapshotOrder {
for i := snapshot; ; i++ {
for i := deleteSnapshot; ; i++ {
if _, ok := stateDB.preimageSnapshot[i]; ok {
delete(stateDB.preimageSnapshot, i)
} else {
Expand Down

0 comments on commit 65044c7

Please sign in to comment.