Skip to content

Commit

Permalink
discard stale snapshot (#1427)
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy authored Mar 28, 2023
1 parent 98a1b05 commit 5f9b2fe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions smartcontract/storage/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ func (self *StateDB) Snapshot() int {
return len(self.snapshots) - 1
}

func (self *StateDB) DiscardSnapshot(idx int) {
if idx+1 > len(self.snapshots) {
panic("can not to revert snapshot")
}

self.snapshots = self.snapshots[:idx]
}

func (self *StateDB) RevertToSnapshot(idx int) {
if idx+1 > len(self.snapshots) {
panic("can not to revert snapshot")
Expand Down
11 changes: 10 additions & 1 deletion vm/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"

"github.com/ontio/ontology/core/types"
"github.com/ontio/ontology/smartcontract/service/native/utils"
"github.com/ontio/ontology/vm/evm/errors"
Expand Down Expand Up @@ -247,6 +246,8 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
if err != errors.ErrExecutionReverted {
gas = 0
}
} else {
evm.StateDB.DiscardSnapshot(snapshot)
}
return ret, gas, err
}
Expand Down Expand Up @@ -292,6 +293,8 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
if err != errors.ErrExecutionReverted {
gas = 0
}
} else {
evm.StateDB.DiscardSnapshot(snapshot)
}
return ret, gas, err
}
Expand Down Expand Up @@ -327,6 +330,8 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
if err != errors.ErrExecutionReverted {
gas = 0
}
} else {
evm.StateDB.DiscardSnapshot(snapshot)
}
return ret, gas, err
}
Expand Down Expand Up @@ -378,6 +383,8 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
if err != errors.ErrExecutionReverted {
gas = 0
}
} else {
evm.StateDB.DiscardSnapshot(snapshot)
}
return ret, gas, err
}
Expand Down Expand Up @@ -458,6 +465,8 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
if err != errors.ErrExecutionReverted {
contract.UseGas(contract.Gas)
}
} else {
evm.StateDB.DiscardSnapshot(snapshot)
}
// Assign err if contract code size exceeds the max while the err is still empty.
if maxCodeSizeExceeded && err == nil {
Expand Down
1 change: 1 addition & 0 deletions vm/evm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type StateDB interface {
Empty(common.Address) bool

RevertToSnapshot(int)
DiscardSnapshot(idx int)
Snapshot() int

AddLog(log *types.StorageLog)
Expand Down
9 changes: 9 additions & 0 deletions vm/evm/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,12 @@ func TestCreateOnDeletedAddress(t *testing.T) {
a.Nil(err, "fail")
a.True((big.NewInt(0).SetBytes(ret).Cmp(big.NewInt(0)) == 0), "should not get previous value 0x1234")
}

func TestVV(t *testing.T) {
//595b58323d5a58fa4656
input := []byte{0x59, 0x5b, 0x58, 0x32, 0x3d, 0x5a, 0x58, 0xfa, 0x46, 0x56}
Execute(input, input, &Config{
//GasLimit: 30000000,
GasLimit: 25000000,
})
}

0 comments on commit 5f9b2fe

Please sign in to comment.