Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] fix estimateGas panic for StakeMigrate #4320

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,21 @@ func (core *coreService) EstimateGasForNonExecution(actType action.Action) (uint

// EstimateMigrateStakeGasConsumption estimates gas consumption for migrate stake action
func (core *coreService) EstimateMigrateStakeGasConsumption(ctx context.Context, ms *action.MigrateStake, caller address.Address) (uint64, error) {
g := core.bc.Genesis()
header, err := core.bc.BlockHeaderByHeight(core.bc.TipHeight())
if err != nil {
return 0, err
}
zeroAddr, err := address.FromString(address.ZeroAddress)
if err != nil {
return 0, err
}
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: header.Height() + 1,
BlockTimeStamp: header.Timestamp().Add(g.BlockInterval),
GasLimit: g.BlockGasLimitByHeight(header.Height() + 1),
Producer: zeroAddr,
})
exec, err := staking.FindProtocol(core.registry).ConstructExecution(ctx, ms, core.sf)
if err != nil {
return 0, err
Expand Down
15 changes: 15 additions & 0 deletions e2etest/native_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,21 @@ func TestCandidateTransferOwnership(t *testing.T) {
&accountExpect{identityset.Address(stakerID), "99989899999999999996125955", test.nonceMgr[identityset.Address(stakerID).String()]},
},
},
{
name: "estimateGas",
act: &actionWithTime{mustNoErr(action.SignedCreateStake(test.nonceMgr.pop(identityset.Address(stakerID).String()), "cand1", stakeAmount.String(), stakeDurationDays, true, nil, gasLimit, gasPrice, identityset.PrivateKey(stakerID), action.WithChainID(chainID))), stakeTime},
expect: []actionExpect{&functionExpect{func(test *e2etest, act *action.SealedEnvelope, receipt *action.Receipt, err error) {
ms, err := action.NewMigrateStake(0, 3, gasLimit, gasPrice)
require.NoError(err)
resp, err := test.api.EstimateActionGasConsumption(context.Background(), &iotexapi.EstimateActionGasConsumptionRequest{
Action: &iotexapi.EstimateActionGasConsumptionRequest_StakeMigrate{StakeMigrate: ms.Proto()},
CallerAddress: identityset.Address(3).String(),
GasPrice: gasPrice.String(),
})
require.NoError(err)
require.Equal(uint64(194934), resp.Gas)
}}},
},
})
})
t.Run("new endorsement", func(t *testing.T) {
Expand Down
Loading