Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into rf-call
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Nov 28, 2024
2 parents 7a9ec8c + 5b64c2b commit f79657c
Show file tree
Hide file tree
Showing 60 changed files with 997 additions and 590 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ endif
endif
GO_VERSION := $(shell go version)
BUILD_TIME=$(shell date +%F-%Z/%T)
VersionImportPath := github.com/iotexproject/iotex-core/pkg/version
VersionImportPath := github.com/iotexproject/iotex-core/v2/pkg/version
PackageFlags += -X '$(VersionImportPath).PackageVersion=$(PACKAGE_VERSION)'
PackageFlags += -X '$(VersionImportPath).PackageCommitID=$(PACKAGE_COMMIT_ID)'
PackageFlags += -X '$(VersionImportPath).GitStatus=$(GIT_STATUS)'
Expand Down
2 changes: 1 addition & 1 deletion action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type (
Nonce() uint64
Gas() uint64
GasPrice() *big.Int
EffectiveGasPrice(*big.Int) *big.Int
TxDynamicGas
AccessList() types.AccessList
TxBlob
Expand All @@ -71,6 +70,7 @@ type (
TxDynamicGas interface {
GasTipCap() *big.Int
GasFeeCap() *big.Int
EffectiveGasPrice(*big.Int) *big.Int
}

TxBlob interface {
Expand Down
4 changes: 4 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type (
IntrinsicGas uint64
// Nonce is the nonce of the action
Nonce uint64
// ReadOnly indicates two scenarios: eth_estimateGas and eth_call
ReadOnly bool
}

// CheckFunc is function type to check by height.
Expand Down Expand Up @@ -150,6 +152,7 @@ type (
VerifyNotContainerBeforeRun bool
ValidateActionWithState bool
CheckStakingDurationUpperLimit bool
FixRevertSnapshot bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -313,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
31 changes: 22 additions & 9 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 Expand Up @@ -613,6 +617,7 @@ func SimulateExecution(
sm protocol.StateManager,
caller address.Address,
ex action.TxDataForSimulation,
opts ...protocol.SimulateOption,
) ([]byte, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "evm.SimulateExecution")
defer span.End()
Expand All @@ -626,13 +631,23 @@ func SimulateExecution(
protocol.ActionCtx{
Caller: caller,
ActionHash: hash.Hash256b(byteutil.Must(proto.Marshal(ex.Proto()))),
ReadOnly: true,
},
)
zeroAddr, err := address.FromString(address.ZeroAddress)
if err != nil {
return nil, nil, err
}
ctx = protocol.WithBlockCtx(
cfg := &protocol.SimulateOptionConfig{}
for _, opt := range opts {
opt(cfg)
}
if cfg.PreOpt != nil {
if err := cfg.PreOpt(sm); err != nil {
return nil, nil, err
}
}
ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(
ctx,
protocol.BlockCtx{
BlockHeight: bcCtx.Tip.Height + 1,
Expand All @@ -642,8 +657,6 @@ func SimulateExecution(
BaseFee: protocol.CalcBaseFee(g.Blockchain, &bcCtx.Tip),
ExcessBlobGas: protocol.CalcExcessBlobGas(bcCtx.Tip.ExcessBlobGas, bcCtx.Tip.BlobGasUsed),
},
)

ctx = protocol.WithFeatureCtx(ctx)
))
return ExecuteContract(ctx, sm, ex)
}
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
6 changes: 5 additions & 1 deletion action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,11 @@ func readExecution(
GetBlockTime: getBlockTimeForTest,
DepositGasFunc: rewarding.DepositGas,
})
return sf.SimulateExecution(ctx, addr, elp)
ws, err := sf.WorkingSet(ctx)
if err != nil {
return nil, nil, err
}
return evm.SimulateExecution(ctx, ws, addr, elp)
}

func (sct *SmartContractTest) runExecutions(
Expand Down
9 changes: 5 additions & 4 deletions action/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ func HashStringToAddress(str string) address.Address {

func SplitGas(ctx context.Context, tx action.TxDynamicGas, usedGas uint64) (*big.Int, *big.Int, error) {
var (
baseFee = MustGetBlockCtx(ctx).BaseFee
gas = new(big.Int).SetUint64(usedGas)
baseFee = MustGetBlockCtx(ctx).BaseFee
gas = new(big.Int).SetUint64(usedGas)
readOnly = MustGetActionCtx(ctx).ReadOnly
)
if baseFee == nil {
if baseFee == nil || readOnly {
// treat as basefee if before enabling EIP-1559
return new(big.Int), new(big.Int).Mul(tx.GasFeeCap(), gas), nil
}
Expand All @@ -148,7 +149,7 @@ func SplitGas(ctx context.Context, tx action.TxDynamicGas, usedGas uint64) (*big
return priority.Mul(priority, gas), base.Mul(base, gas), nil
}

func EffectiveGasPrice(ctx context.Context, tx action.TxCommon) *big.Int {
func EffectiveGasPrice(ctx context.Context, tx action.TxDynamicGas) *big.Int {
if !MustGetFeatureCtx(ctx).EnableDynamicFeeTx {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions action/protocol/rewarding/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (p *Protocol) deleteStateV2(sm protocol.StateManager, key []byte) error {
func (p *Protocol) settleSystemAction(
ctx context.Context,
sm protocol.StateManager,
act action.TxCommon,
act action.TxDynamicGas,
status uint64,
si int,
logs []*action.Log,
Expand All @@ -399,7 +399,7 @@ func (p *Protocol) settleSystemAction(
func (p *Protocol) settleUserAction(
ctx context.Context,
sm protocol.StateManager,
act action.TxCommon,
act action.TxDynamicGas,
status uint64,
si int,
logs []*action.Log,
Expand All @@ -411,7 +411,7 @@ func (p *Protocol) settleUserAction(
func (p *Protocol) settleAction(
ctx context.Context,
sm protocol.StateManager,
act action.TxCommon,
act action.TxDynamicGas,
status uint64,
si int,
isSystemAction bool,
Expand Down
Loading

0 comments on commit f79657c

Please sign in to comment.