From ca276d4484279d8f0ab5a7625a5640876c0d8292 Mon Sep 17 00:00:00 2001 From: envestcc Date: Tue, 2 Jul 2024 08:59:37 +0800 Subject: [PATCH] add blockctx --- api/coreservice.go | 15 +++++++++++++++ e2etest/native_staking_test.go | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/api/coreservice.go b/api/coreservice.go index 40df7be2ea..efcfae7103 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -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 diff --git a/e2etest/native_staking_test.go b/e2etest/native_staking_test.go index 439cc3b725..0c739ae901 100644 --- a/e2etest/native_staking_test.go +++ b/e2etest/native_staking_test.go @@ -971,6 +971,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) {