Skip to content

Commit

Permalink
Revert "[iip-15] Sharing gas-fee for DApps (#3844)"
Browse files Browse the repository at this point in the history
This reverts commit c8139e1.
  • Loading branch information
dustinxie committed Aug 12, 2024
1 parent e1e2962 commit 59f58e6
Show file tree
Hide file tree
Showing 24 changed files with 38 additions and 219 deletions.
2 changes: 0 additions & 2 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ type (
FixContractStakingWeightedVotes bool
ExecutionSizeLimit32KB bool
UseZeroNonceForFreshAccount bool
SharedGasWithDapp bool
CandidateRegisterMustWithStake bool
DisableDelegateEndorsement bool
RefactorFreshAccountConversion bool
Expand Down Expand Up @@ -267,7 +266,6 @@ func WithFeatureCtx(ctx context.Context) context.Context {
FixContractStakingWeightedVotes: g.IsRedsea(height),
ExecutionSizeLimit32KB: !g.IsSumatra(height),
UseZeroNonceForFreshAccount: g.IsSumatra(height),
SharedGasWithDapp: g.IsToBeEnabled(height),
CandidateRegisterMustWithStake: !g.IsTsunami(height),
DisableDelegateEndorsement: !g.IsTsunami(height),
RefactorFreshAccountConversion: g.IsTsunami(height),
Expand Down
4 changes: 1 addition & 3 deletions action/protocol/execution/evm/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ type (
HelperContext struct {
GetBlockHash GetBlockHash
GetBlockTime GetBlockTime
DepositGasFunc DepositGasWithSGD
// TODO: sgd should be moved into depositGasFunc
Sgd SGDRegistry
DepositGasFunc DepositGas
}
)

Expand Down
49 changes: 4 additions & 45 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,8 @@ type (
// GetBlockTime gets block time by height
GetBlockTime func(uint64) (time.Time, error)

// DepositGasWithSGD deposits gas with Sharing of Gas-fee with DApps
DepositGasWithSGD func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error)

// SGDRegistry is the interface for handling Sharing of Gas-fee with DApps
SGDRegistry interface {
CheckContract(context.Context, string) (address.Address, uint64, bool, error)
}
// DepositGas deposits gas
DepositGas func(context.Context, protocol.StateManager, *big.Int) (*action.TransactionLog, error)
)

// CanTransfer checks whether the from account has enough balance
Expand Down Expand Up @@ -240,7 +235,6 @@ func ExecuteContract(
if err != nil {
return nil, nil, err
}
sgd := ps.helperCtx.Sgd
retval, depositGas, remainingGas, contractAddress, statusCode, err := executeInEVM(ps, stateDB)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -275,24 +269,8 @@ func ExecuteContract(
}
}
if consumedGas > 0 {
var (
receiver address.Address
sharedGas uint64
sharedGasFee, totalGasFee *big.Int
)
if ps.featureCtx.SharedGasWithDapp && sgd != nil {
// TODO: sgd is whether nil should be checked in processSGD
receiver, sharedGas, err = processSGD(ctx, sm, execution, consumedGas, sgd)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to process Sharing of Gas-fee with DApps")
}
}
if sharedGas > 0 {
sharedGasFee = big.NewInt(int64(sharedGas))
sharedGasFee.Mul(sharedGasFee, ps.txCtx.GasPrice)
}
totalGasFee = new(big.Int).Mul(new(big.Int).SetUint64(consumedGas), ps.txCtx.GasPrice)
depositLog, err = ps.helperCtx.DepositGasFunc(ctx, sm, receiver, totalGasFee, sharedGasFee)
gasValue := new(big.Int).Mul(new(big.Int).SetUint64(consumedGas), ps.txCtx.GasPrice)
depositLog, err = ps.helperCtx.DepositGasFunc(ctx, sm, gasValue)
if err != nil {
return nil, nil, err
}
Expand All @@ -319,25 +297,6 @@ func ExecuteContract(
return retval, receipt, nil
}

func processSGD(ctx context.Context, sm protocol.StateManager, execution *action.EvmTransaction, consumedGas uint64, sgd SGDRegistry,
) (address.Address, uint64, error) {
if execution.To() == nil {
return nil, 0, nil
}

contract, _ := address.FromBytes((*execution.To())[:])
receiver, percentage, ok, err := sgd.CheckContract(ctx, contract.String())
if err != nil || !ok {
return nil, 0, err
}

sharedGas := consumedGas * percentage / 100
if sharedGas > consumedGas {
sharedGas = consumedGas
}
return receiver, sharedGas, nil
}

// ReadContractStorage reads contract's storage
func ReadContractStorage(
ctx context.Context,
Expand Down
4 changes: 1 addition & 3 deletions action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"

"github.com/iotexproject/iotex-core/action"
Expand Down Expand Up @@ -67,10 +66,9 @@ func TestExecuteContractFailure(t *testing.T) {
GetBlockTime: func(uint64) (time.Time, error) {
return time.Time{}, nil
},
DepositGasFunc: func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
DepositGasFunc: func(context.Context, protocol.StateManager, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
Sgd: nil,
})
retval, receipt, err := ExecuteContract(ctx, sm, action.NewEvmTx(e))
require.Nil(t, retval)
Expand Down
8 changes: 3 additions & 5 deletions action/protocol/execution/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ const (
type Protocol struct {
getBlockHash evm.GetBlockHash
getBlockTime evm.GetBlockTime
depositGas evm.DepositGasWithSGD
depositGas evm.DepositGas
addr address.Address
sgdRegistry evm.SGDRegistry
}

// NewProtocol instantiates the protocol of exeuction
func NewProtocol(getBlockHash evm.GetBlockHash, depositGasWithSGD evm.DepositGasWithSGD, sgd evm.SGDRegistry, getBlockTime evm.GetBlockTime) *Protocol {
func NewProtocol(getBlockHash evm.GetBlockHash, depositGas evm.DepositGas, getBlockTime evm.GetBlockTime) *Protocol {
h := hash.Hash160b([]byte(_protocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
log.L().Panic("Error when constructing the address of vote protocol", zap.Error(err))
}
return &Protocol{getBlockHash: getBlockHash, depositGas: depositGasWithSGD, addr: addr, sgdRegistry: sgd, getBlockTime: getBlockTime}
return &Protocol{getBlockHash: getBlockHash, depositGas: depositGas, addr: addr, getBlockTime: getBlockTime}
}

// FindProtocol finds the registered protocol from registry
Expand Down Expand Up @@ -72,7 +71,6 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
GetBlockHash: p.getBlockHash,
GetBlockTime: p.getBlockTime,
DepositGasFunc: p.depositGas,
Sgd: p.sgdRegistry,
})
_, receipt, err := evm.ExecuteContract(ctx, sm, action.NewEvmTx(exec))

Expand Down
8 changes: 4 additions & 4 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func readExecution(
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: dao.GetBlockHash,
GetBlockTime: getBlockTimeForTest,
DepositGasFunc: rewarding.DepositGasWithSGD,
DepositGasFunc: rewarding.DepositGas,
})
return sf.SimulateExecution(ctx, addr, exec)
}
Expand Down Expand Up @@ -492,7 +492,7 @@ func (sct *SmartContractTest) prepareBlockchain(
r.NoError(reward.Register(registry))

r.NotNil(bc)
execution := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, getBlockTimeForTest)
execution := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, getBlockTimeForTest)
r.NoError(execution.Register(registry))
r.NoError(bc.Start(ctx))

Expand Down Expand Up @@ -642,7 +642,7 @@ func TestProtocol_Validate(t *testing.T) {
require := require.New(t)
p := execution.NewProtocol(func(uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
}, rewarding.DepositGasWithSGD, nil, getBlockTimeForTest)
}, rewarding.DepositGas, getBlockTimeForTest)

cases := []struct {
name string
Expand Down Expand Up @@ -735,7 +735,7 @@ func TestProtocol_Handle(t *testing.T) {
protocol.NewGenericValidator(sf, accountutil.AccountState),
)),
)
exeProtocol := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, getBlockTimeForTest)
exeProtocol := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, getBlockTimeForTest)
require.NoError(exeProtocol.Register(registry))
require.NoError(bc.Start(ctx))
require.NotNil(bc)
Expand Down
3 changes: 1 addition & 2 deletions action/protocol/poll/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,9 @@ func (cc *consortiumCommittee) CreateGenesisStates(ctx context.Context, sm proto
return hash.ZeroHash256, nil
},
GetBlockTime: getBlockTime,
DepositGasFunc: func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
DepositGasFunc: func(context.Context, protocol.StateManager, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
Sgd: nil,
})

// deploy consortiumCommittee contract
Expand Down
3 changes: 1 addition & 2 deletions action/protocol/poll/staking_committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ func (sc *stakingCommittee) CreateGenesisStates(ctx context.Context, sm protocol
// make sure the returned timestamp is after the current block time so that evm upgrades based on timestamp (Shanghai and onwards) are disabled
return blkCtx.BlockTimeStamp.Add(5 * time.Second), nil
},
DepositGasFunc: func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
DepositGasFunc: func(context.Context, protocol.StateManager, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
Sgd: nil,
})
// deploy native staking contract
_, receipt, err := evm.ExecuteContract(
Expand Down
57 changes: 2 additions & 55 deletions action/protocol/rewarding/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,6 @@ func (p *Protocol) Deposit(
sm protocol.StateManager,
amount *big.Int,
transactionLogType iotextypes.TransactionLogType,
) (*action.TransactionLog, error) {
// fallback to regular case by setting sgdAmount = nil
return p.deposit(ctx, sm, nil, amount, nil, transactionLogType)
}

func (p *Protocol) deposit(
ctx context.Context,
sm protocol.StateManager,
receiver address.Address,
amount, sgdAmount *big.Int,
transactionLogType iotextypes.TransactionLogType,
) (*action.TransactionLog, error) {
actionCtx := protocol.MustGetActionCtx(ctx)
accountCreationOpts := []state.AccountCreationOption{}
Expand All @@ -96,18 +85,8 @@ func (p *Protocol) deposit(
if _, err := p.state(ctx, sm, _fundKey, &f); err != nil {
return nil, err
}
f.totalBalance.Add(f.totalBalance, amount)
f.unclaimedBalance.Add(f.unclaimedBalance, amount)
if !isZero(sgdAmount) {
f.unclaimedBalance.Sub(f.unclaimedBalance, sgdAmount)
if f.unclaimedBalance.Sign() == -1 {
return nil, errors.New("no enough available balance")
}
// grant sgd amount to receiver
if err := p.grantToAccount(ctx, sm, receiver, sgdAmount); err != nil {
return nil, err
}
}
f.totalBalance = big.NewInt(0).Add(f.totalBalance, amount)
f.unclaimedBalance = big.NewInt(0).Add(f.unclaimedBalance, amount)
if err := p.putState(ctx, sm, _fundKey, &f); err != nil {
return nil, err
}
Expand Down Expand Up @@ -167,35 +146,3 @@ func DepositGas(ctx context.Context, sm protocol.StateManager, amount *big.Int)
}
return rp.Deposit(ctx, sm, amount, iotextypes.TransactionLogType_GAS_FEE)
}

// DepositGasWithSGD deposits gas into the rewarding fund with Sharing of Gas-fee with DApps
func DepositGasWithSGD(ctx context.Context, sm protocol.StateManager, sgdReceiver address.Address, totalAmount, sgdAmount *big.Int,
) (*action.TransactionLog, error) {
if isZero(sgdAmount) {
// fallback to regular case if SGD amount is zero
return DepositGas(ctx, sm, totalAmount)
}
if sgdReceiver == nil {
// a valid SGD amount but no valid receiver address
return nil, errors.New("no valid receiver address to receive the Sharing of Gas-fee with DApps")
}
// TODO: we bypass the gas deposit for the actions in genesis block. Later we should remove this after we remove
// genesis actions
blkCtx := protocol.MustGetBlockCtx(ctx)
if blkCtx.BlockHeight == 0 {
return nil, nil
}
reg, ok := protocol.GetRegistry(ctx)
if !ok {
return nil, nil
}
rp := FindProtocol(reg)
if rp == nil {
return nil, nil
}
return rp.deposit(ctx, sm, sgdReceiver, totalAmount, sgdAmount, iotextypes.TransactionLogType_GAS_FEE)
}

func isZero(a *big.Int) bool {
return a == nil || len(a.Bytes()) == 0
}
3 changes: 1 addition & 2 deletions action/protocol/staking/handler_stake_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ func TestHandleStakeMigrate(t *testing.T) {
r.Equal(uint64(iotextypes.ReceiptStatus_Success), receipts[1].Status)
excPrtl := execution.NewProtocol(
func(u uint64) (hash.Hash256, error) { return hash.ZeroHash256, nil },
func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) {
func(context.Context, protocol.StateManager, *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
nil,
func(uint64) (time.Time, error) { return time.Now(), nil },
)
reg := protocol.NewRegistry()
Expand Down
2 changes: 1 addition & 1 deletion api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@ func (core *coreService) simulateExecution(ctx context.Context, addr address.Add
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: getBlockHash,
GetBlockTime: getBlockTime,
DepositGasFunc: rewarding.DepositGasWithSGD,
DepositGasFunc: rewarding.DepositGas,
})
return core.sf.SimulateExecution(ctx, addr, exec)
}
Expand Down
2 changes: 1 addition & 1 deletion api/serverV2_integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func setupChain(cfg testConfig) (blockchain.Blockchain, blockdao.BlockDAO, block
}()

acc := account.NewProtocol(rewarding.DepositGas)
evm := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, func(u uint64) (time.Time, error) { return time.Time{}, nil })
evm := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, func(u uint64) (time.Time, error) { return time.Time{}, nil })
p := poll.NewLifeLongDelegatesProtocol(cfg.genesis.Delegates)
rolldposProtocol := rolldpos.NewProtocol(
genesis.Default.NumCandidateDelegates,
Expand Down
2 changes: 1 addition & 1 deletion blockchain/integrity/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func newChainInDB() (blockchain.Blockchain, actpool.ActPool, error) {
if bc == nil {
return nil, nil, errors.New("pointer is nil")
}
ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime)
ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, fakeGetBlockTime)
if err = ep.Register(registry); err != nil {
return nil, nil, err
}
Expand Down
Loading

0 comments on commit 59f58e6

Please sign in to comment.