Skip to content

Commit

Permalink
remove actionPayload and EstimateGasForNonExecution
Browse files Browse the repository at this point in the history
  • Loading branch information
Liuhaai committed Mar 10, 2022
1 parent 5a56e04 commit 0b5dcd0
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 70 deletions.
5 changes: 0 additions & 5 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import (

// Action is the action can be Executed in protocols. The method is added to avoid mistakenly used empty interface as action.
type Action interface {
SetEnvelopeContext(SealedEnvelope)
SanityCheck() error
}

type actionPayload interface {
Cost() (*big.Int, error)
IntrinsicGas() (uint64, error)
SetEnvelopeContext(SealedEnvelope)
Expand Down
2 changes: 1 addition & 1 deletion action/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (b *EnvelopeBuilder) SetGasPriceByBytes(buf []byte) *EnvelopeBuilder {
}

// SetAction sets the action payload for the Envelope Builder is building.
func (b *EnvelopeBuilder) SetAction(action actionPayload) *EnvelopeBuilder {
func (b *EnvelopeBuilder) SetAction(action Action) *EnvelopeBuilder {
b.elp.payload = action
return b
}
Expand Down
2 changes: 1 addition & 1 deletion action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type (
nonce uint64
gasLimit uint64
gasPrice *big.Int
payload actionPayload
payload Action
}
)

Expand Down
2 changes: 1 addition & 1 deletion action/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestEnvelope_Actions(t *testing.T) {
cf := DepositToRewardingFundBuilder{}
depositToRewardingFund := cf.SetAmount(big.NewInt(1)).Build()

tests := []actionPayload{
tests := []Action{
putPollResult,
createStake,
depositToStake,
Expand Down
2 changes: 1 addition & 1 deletion action/sealedenvelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestSealedEnvelope_Actions(t *testing.T) {
candidateUpdate, err := NewCandidateUpdate(nonce, candidate1Name, cand1Addr, cand1Addr, gasLimit, gasPrice)
require.NoError(err)

tests := []actionPayload{
tests := []Action{
createStake,
depositToStake,
changeCandidate,
Expand Down
30 changes: 0 additions & 30 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ type (
ActPoolActions(actHashes []string) ([]*iotextypes.Action, error)
// UnconfirmedActionsByAddress returns all unconfirmed actions in actpool associated with an address
UnconfirmedActionsByAddress(address string, start uint64, count uint64) ([]*iotexapi.ActionInfo, error)
// EstimateGasForNonExecution estimates action gas except execution
EstimateGasForNonExecution(actType action.Action, payload []byte) (uint64, error)
// EstimateExecutionGasConsumption estimate gas consumption for execution action
EstimateExecutionGasConsumption(ctx context.Context, sc *action.Execution, callerAddr address.Address) (uint64, error)
// BlockMetas returns blockmetas response within the height range
Expand Down Expand Up @@ -1418,34 +1416,6 @@ func (core *coreService) correctLogsRange(start, end uint64) (uint64, uint64, er
return start, end, nil
}

// EstimateGasForNonExecution estimates action gas except execution
func (core *coreService) EstimateGasForNonExecution(actType action.Action, payload []byte) (uint64, error) {
switch act := actType.(type) {
case *action.Transfer:
return act.IntrinsicGas()
case *action.CreateStake:
return act.IntrinsicGas()
case *action.DepositToStake:
return act.IntrinsicGas()
case *action.ChangeCandidate:
return act.IntrinsicGas()
case *action.TransferStake:
return act.IntrinsicGas()
case *action.Unstake:
return act.IntrinsicGas()
case *action.WithdrawStake:
return act.IntrinsicGas()
case *action.Restake:
return act.IntrinsicGas()
case *action.CandidateRegister:
return act.IntrinsicGas()
case *action.CandidateUpdate:
return act.IntrinsicGas()
default:
return 0, errors.Errorf("invalid action type %T not supported", act)
}
}

// EstimateExecutionGasConsumption estimate gas consumption for execution action
func (core *coreService) EstimateExecutionGasConsumption(ctx context.Context, sc *action.Execution, callerAddr address.Address) (uint64, error) {
state, err := accountutil.AccountState(core.sf, callerAddr)
Expand Down
27 changes: 12 additions & 15 deletions api/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,75 +294,72 @@ func (svr *GRPCServer) EstimateActionGasConsumption(ctx context.Context, in *iot
}
return &iotexapi.EstimateActionGasConsumptionResponse{Gas: ret}, nil
}
var (
act action.Action
payload []byte
)
var act action.Action
switch {
case in.GetTransfer() != nil:
tmpAct := &action.Transfer{}
if err := tmpAct.LoadProto(in.GetTransfer()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, tmpAct.Payload()
act = tmpAct
case in.GetStakeCreate() != nil:
tmpAct := &action.CreateStake{}
if err := tmpAct.LoadProto(in.GetStakeCreate()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, tmpAct.Payload()
act = tmpAct
case in.GetStakeUnstake() != nil:
tmpAct := &action.Unstake{}
if err := tmpAct.LoadProto(in.GetStakeUnstake()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, tmpAct.Payload()
act = tmpAct
case in.GetStakeWithdraw() != nil:
tmpAct := &action.WithdrawStake{}
if err := tmpAct.LoadProto(in.GetStakeWithdraw()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, tmpAct.Payload()
act = tmpAct
case in.GetStakeAddDeposit() != nil:
tmpAct := &action.DepositToStake{}
if err := tmpAct.LoadProto(in.GetStakeAddDeposit()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, tmpAct.Payload()
act = tmpAct
case in.GetStakeRestake() != nil:
tmpAct := &action.Restake{}
if err := tmpAct.LoadProto(in.GetStakeRestake()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, tmpAct.Payload()
act = tmpAct
case in.GetStakeChangeCandidate() != nil:
tmpAct := &action.ChangeCandidate{}
if err := tmpAct.LoadProto(in.GetStakeChangeCandidate()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, tmpAct.Payload()
act = tmpAct
case in.GetStakeTransferOwnership() != nil:
tmpAct := &action.TransferStake{}
if err := tmpAct.LoadProto(in.GetStakeTransferOwnership()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, tmpAct.Payload()
act = tmpAct
case in.GetCandidateRegister() != nil:
tmpAct := &action.CandidateRegister{}
if err := tmpAct.LoadProto(in.GetCandidateRegister()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, tmpAct.Payload()
act = tmpAct
case in.GetCandidateUpdate() != nil:
tmpAct := &action.CandidateUpdate{}
if err := tmpAct.LoadProto(in.GetCandidateUpdate()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
act, payload = tmpAct, []byte{}
act = tmpAct
default:
return nil, status.Error(codes.InvalidArgument, "invalid argument")
}
estimatedGas, err := svr.coreService.EstimateGasForNonExecution(act, payload)
estimatedGas, err := act.IntrinsicGas()
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion api/web3server.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func (svr *Web3Server) estimateGas(in interface{}) (interface{}, error) {
if exec, ok := act.(*action.Execution); ok {
estimatedGas, err = svr.coreService.EstimateExecutionGasConsumption(context.Background(), exec, from)
} else {
estimatedGas, err = svr.coreService.EstimateGasForNonExecution(act, data)
estimatedGas, err = act.IntrinsicGas()
}
if err != nil {
return nil, err
Expand Down
15 changes: 0 additions & 15 deletions test/mock/mock_apicoreservice/mock_apicoreservice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b5dcd0

Please sign in to comment.