From a27160bf0e26fa21e66b15c38999ddeadc7314d5 Mon Sep 17 00:00:00 2001 From: dustinxie Date: Mon, 9 Sep 2024 12:44:27 -0700 Subject: [PATCH] fix test --- actpool/actioniterator/actioniterator_test.go | 58 ++++--------- actpool/actpool_test.go | 44 +++------- api/coreservice.go | 56 ++++--------- api/coreservice_test.go | 81 ++++--------------- api/grpcserver_integrity_test.go | 2 +- blockchain/block/block_test.go | 19 +---- blockchain/block/runnable_test.go | 6 +- blockchain/block/utils_test.go | 34 ++------ blockchain/integrity/integrity_test.go | 8 +- chainservice/builder.go | 8 +- e2etest/bigint_test.go | 22 ++--- e2etest/contract_staking_test.go | 22 ++--- e2etest/local_transfer_test.go | 21 +---- e2etest/native_staking_test.go | 3 +- e2etest/staking_test.go | 6 +- gasstation/gasstattion_test.go | 15 +--- ioctl/cmd/action/actiontransfer.go | 7 +- ioctl/cmd/action/stake2create.go | 2 +- ioctl/cmd/ws/wsutil.go | 32 +++----- ioctl/newcmd/action/action.go | 18 ++--- ioctl/newcmd/action/actionhash_test.go | 18 +---- ioctl/newcmd/action/actiontransfer.go | 5 +- state/factory/factory_test.go | 3 +- .../internal/client/client_test.go | 6 +- tools/bot/server/bot/execution.go | 15 +--- tools/executiontester/blockchain/contract.go | 19 +---- tools/util/injectorutil.go | 39 +++------ 27 files changed, 145 insertions(+), 424 deletions(-) diff --git a/actpool/actioniterator/actioniterator_test.go b/actpool/actioniterator/actioniterator_test.go index 2b2a112664..759aaae95c 100644 --- a/actpool/actioniterator/actioniterator_test.go +++ b/actpool/actioniterator/actioniterator_test.go @@ -28,60 +28,42 @@ func TestActionIterator(t *testing.T) { c := identityset.Address(30) priKeyC := identityset.PrivateKey(30) accMap := make(map[string][]*action.SealedEnvelope) - tsf1, err := action.NewTransfer(uint64(1), big.NewInt(100), b.String(), nil, uint64(0), big.NewInt(13)) - require.NoError(err) - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(1). - SetGasPrice(big.NewInt(13)). + tsf1 := action.NewTransfer(big.NewInt(100), b.String(), nil) + elp := (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(13)). SetAction(tsf1).Build() selp1, err := action.Sign(elp, priKeyA) require.NoError(err) - tsf2, err := action.NewTransfer(uint64(2), big.NewInt(100), "2", nil, uint64(0), big.NewInt(30)) - require.NoError(err) - bd = &action.EnvelopeBuilder{} - elp = bd.SetNonce(2). - SetGasPrice(big.NewInt(30)). + tsf2 := action.NewTransfer(big.NewInt(100), "2", nil) + elp = (&action.EnvelopeBuilder{}).SetNonce(2).SetGasPrice(big.NewInt(30)). SetAction(tsf2).Build() selp2, err := action.Sign(elp, priKeyA) require.NoError(err) accMap[a.String()] = []*action.SealedEnvelope{selp1, selp2} - tsf3, err := action.NewTransfer(uint64(1), big.NewInt(100), c.String(), nil, uint64(0), big.NewInt(15)) - require.NoError(err) - bd = &action.EnvelopeBuilder{} - elp = bd.SetNonce(1). - SetGasPrice(big.NewInt(15)). + tsf3 := action.NewTransfer(big.NewInt(100), c.String(), nil) + elp = (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(15)). SetAction(tsf3).Build() selp3, err := action.Sign(elp, priKeyB) require.NoError(err) - tsf4, err := action.NewTransfer(uint64(2), big.NewInt(100), "3", nil, uint64(0), big.NewInt(10)) - require.NoError(err) - bd = &action.EnvelopeBuilder{} - elp = bd.SetNonce(2). - SetGasPrice(big.NewInt(10)). + tsf4 := action.NewTransfer(big.NewInt(100), "3", nil) + elp = (&action.EnvelopeBuilder{}).SetNonce(2).SetGasPrice(big.NewInt(10)). SetAction(tsf4).Build() selp4, err := action.Sign(elp, priKeyB) require.NoError(err) - tsf5, err := action.NewTransfer(uint64(3), big.NewInt(100), a.String(), nil, uint64(0), big.NewInt(2)) - require.NoError(err) - bd = &action.EnvelopeBuilder{} - elp = bd.SetNonce(3). - SetGasPrice(big.NewInt(20)). + tsf5 := action.NewTransfer(big.NewInt(100), a.String(), nil) + elp = (&action.EnvelopeBuilder{}).SetNonce(3).SetGasPrice(big.NewInt(20)). SetAction(tsf5).Build() selp5, err := action.Sign(elp, priKeyB) require.NoError(err) accMap[b.String()] = []*action.SealedEnvelope{selp3, selp4, selp5} - tsf6, err := action.NewTransfer(uint64(1), big.NewInt(100), "1", nil, uint64(0), big.NewInt(5)) - require.NoError(err) - bd = &action.EnvelopeBuilder{} - elp = bd.SetNonce(1). - SetGasPrice(big.NewInt(5)). + tsf6 := action.NewTransfer(big.NewInt(100), "1", nil) + elp = (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(5)). SetAction(tsf6).Build() selp6, err := action.Sign(elp, priKeyC) require.NoError(err) @@ -106,11 +88,8 @@ func TestActionByPrice(t *testing.T) { s := &actionByPrice{} require.Equal(0, s.Len()) - tsf1, err := action.NewTransfer(uint64(1), big.NewInt(100), "100", nil, uint64(0), big.NewInt(13)) - require.NoError(err) - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(1). - SetGasPrice(big.NewInt(5)). + tsf1 := action.NewTransfer(big.NewInt(100), "100", nil) + elp := (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(5)). SetAction(tsf1).Build() selp, err := action.Sign(elp, identityset.PrivateKey(28)) require.NoError(err) @@ -130,12 +109,9 @@ func BenchmarkLooping(b *testing.B) { require.NoError(b, err) addr := priKey.PublicKey().Address() require.NotNil(b, addr) - tsf, err := action.NewTransfer(uint64(1), big.NewInt(100), "1", nil, uint64(100000), big.NewInt(int64(i))) - require.NoError(b, err) - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(1). - SetGasPrice(big.NewInt(5)). - SetAction(tsf).Build() + tsf := action.NewTransfer(big.NewInt(100), "1", nil) + elp := (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(int64(i))). + SetGasLimit(100000).SetAction(tsf).Build() selp, err := action.Sign(elp, priKey) require.NoError(b, err) accMap[addr.String()] = []*action.SealedEnvelope{selp} diff --git a/actpool/actpool_test.go b/actpool/actpool_test.go index 377ddebfd1..31ef45dfe4 100644 --- a/actpool/actpool_test.go +++ b/actpool/actpool_test.go @@ -239,13 +239,8 @@ func TestActPool_AddActs(t *testing.T) { require.NoError(err) err = ap.Add(ctx, replaceTsf) require.Equal(action.ErrReplaceUnderpriced, errors.Cause(err)) - replaceTransfer, err := action.NewTransfer(uint64(4), big.NewInt(1), _addr2, []byte{}, uint64(100000), big.NewInt(0)) - require.NoError(err) - - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(4). - SetAction(replaceTransfer). - SetGasLimit(100000).Build() + replaceTransfer := action.NewTransfer(big.NewInt(1), _addr2, []byte{}) + elp := (&action.EnvelopeBuilder{}).SetNonce(4).SetAction(replaceTransfer).SetGasLimit(100000).Build() selp, err := action.Sign(elp, _priKey1) require.NoError(err) @@ -264,20 +259,9 @@ func TestActPool_AddActs(t *testing.T) { require.Equal(action.ErrInsufficientFunds, errors.Cause(err)) // Case VII: insufficient gas tmpData := [1234]byte{} - creationExecution, err := action.NewExecution( - action.EmptyAddress, - uint64(5), - big.NewInt(int64(0)), - 10, - big.NewInt(10), - tmpData[:], - ) - require.NoError(err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetNonce(5). - SetGasPrice(big.NewInt(10)). - SetGasLimit(10). + creationExecution := action.NewExecution(action.EmptyAddress, big.NewInt(int64(0)), tmpData[:]) + bd := &action.EnvelopeBuilder{} + elp = bd.SetNonce(5).SetGasPrice(big.NewInt(10)).SetGasLimit(10). SetAction(creationExecution).Build() selp, err = action.Sign(elp, _priKey1) require.NoError(err) @@ -724,13 +708,8 @@ func TestActPool_Reset(t *testing.T) { require.NoError(err) tsf22, err := action.SignedTransfer(_addr5, _priKey4, uint64(2), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0)) require.NoError(err) - tsf23, err := action.NewTransfer(uint64(3), big.NewInt(1), "", []byte{}, uint64(100000), big.NewInt(0)) - require.NoError(err) - - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(3). - SetGasLimit(20000). - SetAction(tsf23).Build() + tsf23 := action.NewTransfer(big.NewInt(1), "", []byte{}) + elp := (&action.EnvelopeBuilder{}).SetNonce(3).SetGasLimit(20000).SetAction(tsf23).Build() selp23, err := action.Sign(elp, _priKey4) require.NoError(err) @@ -738,13 +717,8 @@ func TestActPool_Reset(t *testing.T) { require.NoError(err) tsf25, err := action.SignedTransfer(_addr4, _priKey5, uint64(2), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0)) require.NoError(err) - tsf26, err := action.NewTransfer(uint64(3), big.NewInt(1), _addr4, []byte{}, uint64(20000), big.NewInt(0)) - require.NoError(err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetNonce(3). - SetGasLimit(20000). - SetAction(tsf26).Build() + tsf26 := action.NewTransfer(big.NewInt(1), _addr4, []byte{}) + elp = (&action.EnvelopeBuilder{}).SetNonce(3).SetGasLimit(20000).SetAction(tsf26).Build() selp26, err := action.Sign(elp, _priKey5) require.NoError(err) diff --git a/api/coreservice.go b/api/coreservice.go index c1a5996e26..63058bbd61 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -543,14 +543,6 @@ func (core *coreService) ReadContract(ctx context.Context, callerAddr address.Ad return res.Data, res.Receipt, nil } } - ctx = genesis.WithGenesisContext(ctx, core.bc.Genesis()) - ctx, err := core.bc.Context(ctx) - if err != nil { - return "", nil, err - } - ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{ - BlockHeight: core.bc.TipHeight(), - })) var ( g = core.bc.Genesis() blockGasLimit = g.BlockGasLimitByHeight(core.bc.TipHeight()) @@ -558,8 +550,7 @@ func (core *coreService) ReadContract(ctx context.Context, callerAddr address.Ad if elp.GasLimit() == 0 || blockGasLimit < elp.GasLimit() { elp.SetGas(blockGasLimit) } - - retval, receipt, err := core.simulateExecution(ctx, callerAddr, elp, core.dao.GetBlockHash, core.getBlockTime) + retval, receipt, err := core.simulateExecution(ctx, callerAddr, elp) if err != nil { return "", nil, status.Error(codes.Internal, err.Error()) } @@ -1533,14 +1524,6 @@ func (core *coreService) EstimateMigrateStakeGasConsumption(ctx context.Context, // EstimateExecutionGasConsumption estimate gas consumption for execution action func (core *coreService) EstimateExecutionGasConsumption(ctx context.Context, elp action.Envelope, callerAddr address.Address, opts ...protocol.SimulateOption) (uint64, error) { - ctx = genesis.WithGenesisContext(ctx, core.bc.Genesis()) - ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{ - BlockHeight: core.bc.TipHeight(), - })) - ctx, err := core.bc.Context(ctx) - if err != nil { - return 0, status.Error(codes.Internal, err.Error()) - } var ( g = core.bc.Genesis() blockGasLimit = g.BlockGasLimitByHeight(core.bc.TipHeight()) @@ -1592,11 +1575,7 @@ func (core *coreService) isGasLimitEnough( ) (bool, *action.Receipt, error) { ctx, span := tracer.NewSpan(ctx, "Server.isGasLimitEnough") defer span.End() - ctx, err := core.bc.Context(ctx) - if err != nil { - return false, nil, err - } - _, receipt, err := core.simulateExecution(ctx, caller, elp, core.dao.GetBlockHash, core.getBlockTime, opts...) + _, receipt, err := core.simulateExecution(ctx, caller, elp, opts...) if err != nil { return false, nil, err } @@ -1735,21 +1714,12 @@ func (core *coreService) ReceiveBlock(blk *block.Block) error { } func (core *coreService) SimulateExecution(ctx context.Context, addr address.Address, elp action.Envelope) ([]byte, *action.Receipt, error) { - ctx = genesis.WithGenesisContext(ctx, core.bc.Genesis()) - ctx, err := core.bc.Context(ctx) - if err != nil { - return nil, nil, err - } - // TODO (liuhaai): Use original nonce and gas limit properly - ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{ - BlockHeight: core.bc.TipHeight(), - })) var ( g = core.bc.Genesis() blockGasLimit = g.BlockGasLimitByHeight(core.bc.TipHeight()) ) elp.SetGas(blockGasLimit) - return core.simulateExecution(ctx, addr, elp, core.dao.GetBlockHash, core.getBlockTime) + return core.simulateExecution(ctx, addr, elp) } // SyncingProgress returns the syncing status of node @@ -1773,7 +1743,7 @@ func (core *coreService) TraceTransaction(ctx context.Context, actHash string, c } addr, _ := address.FromString(address.ZeroAddress) return core.traceTx(ctx, new(tracers.Context), config, func(ctx context.Context) ([]byte, *action.Receipt, error) { - return core.simulateExecution(ctx, addr, act.Envelope, core.dao.GetBlockHash, core.getBlockTime) + return core.simulateExecution(ctx, addr, act.Envelope) }) } @@ -1801,7 +1771,7 @@ func (core *coreService) TraceCall(ctx context.Context, elp := (&action.EnvelopeBuilder{}).SetAction(action.NewExecution(contractAddress, amount, data)). SetGasLimit(gasLimit).Build() return core.traceTx(ctx, new(tracers.Context), config, func(ctx context.Context) ([]byte, *action.Receipt, error) { - return core.simulateExecution(ctx, callerAddr, elp, core.dao.GetBlockHash, core.getBlockTime) + return core.simulateExecution(ctx, callerAddr, elp) }) } @@ -1855,19 +1825,23 @@ func (core *coreService) traceTx(ctx context.Context, txctx *tracers.Context, co Tracer: tracer, NoBaseFee: true, }) - ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{}) - ctx = genesis.WithGenesisContext(ctx, core.bc.Genesis()) - ctx = protocol.WithBlockchainCtx(protocol.WithFeatureCtx(ctx), protocol.BlockchainCtx{}) retval, receipt, err := simulateFn(ctx) return retval, receipt, tracer, err } -func (core *coreService) simulateExecution(ctx context.Context, addr address.Address, elp action.Envelope, getBlockHash evm.GetBlockHash, getBlockTime evm.GetBlockTime, opts ...protocol.SimulateOption) ([]byte, *action.Receipt, error) { +func (core *coreService) simulateExecution(ctx context.Context, addr address.Address, elp action.Envelope, opts ...protocol.SimulateOption) ([]byte, *action.Receipt, error) { + ctx, err := core.bc.Context(ctx) + if err != nil { + return nil, nil, status.Error(codes.Internal, err.Error()) + } state, err := accountutil.AccountState(ctx, core.sf, addr) if err != nil { return nil, nil, status.Error(codes.InvalidArgument, err.Error()) } var pendingNonce uint64 + ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{ + BlockHeight: core.bc.TipHeight(), + })) if protocol.MustGetFeatureCtx(ctx).RefactorFreshAccountConversion { pendingNonce = state.PendingNonceConsideringFreshAccount() } else { @@ -1875,8 +1849,8 @@ func (core *coreService) simulateExecution(ctx context.Context, addr address.Add } elp.SetNonce(pendingNonce) ctx = evm.WithHelperCtx(ctx, evm.HelperContext{ - GetBlockHash: getBlockHash, - GetBlockTime: getBlockTime, + GetBlockHash: core.dao.GetBlockHash, + GetBlockTime: core.getBlockTime, DepositGasFunc: rewarding.DepositGas, }) return core.sf.SimulateExecution(ctx, addr, elp, opts...) diff --git a/api/coreservice_test.go b/api/coreservice_test.go index 7d16047322..5a857249a4 100644 --- a/api/coreservice_test.go +++ b/api/coreservice_test.go @@ -487,10 +487,11 @@ func TestEstimateExecutionGasConsumption(t *testing.T) { p := NewPatches() defer p.Reset() - p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) p = p.ApplyFuncReturn(accountutil.AccountState, nil, errors.New(t.Name())) bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(1) + bc.EXPECT().TipHeight().Return(uint64(1)).Times(1) + bc.EXPECT().Context(gomock.Any()).Return(ctx, nil).Times(1) elp := (&action.EnvelopeBuilder{}).SetAction(&action.Execution{}).Build() _, err := cs.EstimateExecutionGasConsumption(ctx, elp, &address.AddrV1{}) require.ErrorContains(err, t.Name()) @@ -500,12 +501,7 @@ func TestEstimateExecutionGasConsumption(t *testing.T) { p := NewPatches() defer p.Reset() - p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) p = p.ApplyFuncReturn(accountutil.AccountState, &state.Account{}, nil) - p = p.ApplyFuncReturn(protocol.WithFeatureCtx, ctx) - p = p.ApplyFuncReturn(protocol.WithBlockCtx, ctx) - p = p.ApplyFuncReturn(protocol.MustGetFeatureCtx, protocol.FeatureCtx{RefactorFreshAccountConversion: true}) - p = p.ApplyMethodReturn(&state.Account{}, "PendingNonceConsideringFreshAccount", uint64(0)) p = p.ApplyMethodReturn(&genesis.Blockchain{}, "BlockGasLimitByHeight", uint64(0)) p = p.ApplyPrivateMethod( cs, @@ -532,13 +528,7 @@ func TestEstimateExecutionGasConsumption(t *testing.T) { defer p.Reset() receipt := &action.Receipt{} - p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) p = p.ApplyFuncReturn(accountutil.AccountState, &state.Account{}, nil) - p = p.ApplyFuncReturn(protocol.WithFeatureCtx, ctx) - p = p.ApplyFuncReturn(protocol.WithBlockCtx, ctx) - p = p.ApplyFuncReturn(protocol.MustGetFeatureCtx, protocol.FeatureCtx{}) - // pending nonce from state - p = p.ApplyMethodReturn(&state.Account{}, "PendingNonce", uint64(0)) p = p.ApplyMethodReturn(&genesis.Blockchain{}, "BlockGasLimitByHeight", uint64(0)) p = p.ApplyPrivateMethod( cs, @@ -553,8 +543,8 @@ func TestEstimateExecutionGasConsumption(t *testing.T) { ) p = p.ApplyMethodReturn(receipt, "ExecutionRevertMsg", "") - bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(2) - bc.EXPECT().TipHeight().Return(uint64(0)).Times(2) + bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(1) + bc.EXPECT().TipHeight().Return(uint64(0)).Times(1) elp := (&action.EnvelopeBuilder{}).SetAction(&action.Execution{}).Build() _, err := cs.EstimateExecutionGasConsumption(ctx, elp, &address.AddrV1{}) require.ErrorContains(err, "execution simulation failed:") @@ -565,13 +555,7 @@ func TestEstimateExecutionGasConsumption(t *testing.T) { defer p.Reset() receipt := &action.Receipt{} - p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) p = p.ApplyFuncReturn(accountutil.AccountState, &state.Account{}, nil) - p = p.ApplyFuncReturn(protocol.WithFeatureCtx, ctx) - p = p.ApplyFuncReturn(protocol.WithBlockCtx, ctx) - p = p.ApplyFuncReturn(protocol.MustGetFeatureCtx, protocol.FeatureCtx{RefactorFreshAccountConversion: true}) - // pending nonce from fresh account - p = p.ApplyMethodReturn(&state.Account{}, "PendingNonceConsideringFreshAccount", uint64(0)) p = p.ApplyMethodReturn(&genesis.Blockchain{}, "BlockGasLimitByHeight", uint64(0)) p = p.ApplyPrivateMethod( cs, @@ -586,8 +570,8 @@ func TestEstimateExecutionGasConsumption(t *testing.T) { ) p = p.ApplyMethodReturn(receipt, "ExecutionRevertMsg", "TestRevertMsg") - bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(2) - bc.EXPECT().TipHeight().Return(uint64(0)).Times(2) + // bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(1) + // bc.EXPECT().TipHeight().Return(uint64(0)).Times(1) elp := (&action.EnvelopeBuilder{}).SetAction(&action.Execution{}).Build() _, err := cs.EstimateExecutionGasConsumption(ctx, elp, &address.AddrV1{}) require.ErrorContains(err, "execution simulation is reverted due to the reason:") @@ -607,11 +591,11 @@ func TestEstimateExecutionGasConsumption(t *testing.T) { require.NoError(err) require.Equal(uint64(10000), estimatedGas) - //gasprice no zero, should return error before fixed + //gasprice no zero, should return error elp = (&action.EnvelopeBuilder{}).SetGasPrice(big.NewInt(100)).SetAction(sc).Build() estimatedGas, err = svr.EstimateExecutionGasConsumption(context.Background(), elp, callAddr) - require.NoError(err) - require.Equal(uint64(10000), estimatedGas) + require.ErrorContains(err, "rpc error: code = Internal desc = insufficient funds for gas * price + value") + require.Zero(estimatedGas) }) } @@ -1045,9 +1029,11 @@ func TestSimulateExecution(t *testing.T) { p := NewPatches() defer p.Reset() - p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) p = p.ApplyFuncReturn(accountutil.AccountState, nil, errors.New(t.Name())) bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(1) + // TODO: understand why Times(2) can pass the test, it should be 1, same as Genesis() + bc.EXPECT().TipHeight().Return(uint64(1)).Times(2) + bc.EXPECT().Context(gomock.Any()).Return(ctx, nil).Times(1) elp := (&action.EnvelopeBuilder{}).SetAction(&action.Execution{}).Build() _, _, err := cs.SimulateExecution(ctx, &address.AddrV1{}, elp) require.ErrorContains(err, t.Name()) @@ -1057,7 +1043,6 @@ func TestSimulateExecution(t *testing.T) { p := NewPatches() defer p.Reset() - p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) p = p.ApplyFuncReturn(accountutil.AccountState, &state.Account{}, nil) bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(1) bc.EXPECT().Context(gomock.Any()).Return(nil, errors.New(t.Name())).Times(1) @@ -1066,44 +1051,11 @@ func TestSimulateExecution(t *testing.T) { require.ErrorContains(err, t.Name()) }) - t.Run("GetPendingNonceFromFreshAccount", func(t *testing.T) { - p := NewPatches() - defer p.Reset() - - p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) - p = p.ApplyFuncReturn(accountutil.AccountState, &state.Account{}, nil) - p = p.ApplyFuncReturn(protocol.WithFeatureCtx, ctx) - p = p.ApplyFuncReturn(protocol.WithBlockCtx, ctx) - p = p.ApplyFuncReturn(protocol.MustGetFeatureCtx, protocol.FeatureCtx{RefactorFreshAccountConversion: true}) - p = p.ApplyMethodReturn(&state.Account{}, "PendingNonceConsideringFreshAccount", uint64(0)) - p = p.ApplyMethodReturn(&genesis.Blockchain{}, "BlockGasLimitByHeight", uint64(0)) - p = p.ApplyPrivateMethod( - cs, - "simulateExecution", - func(ctx context.Context, addr address.Address, exec *action.Execution, getBlockHash evm.GetBlockHash, getBlockTime evm.GetBlockTime) ([]byte, *action.Receipt, error) { - return []byte("success"), nil, nil - }, - ) - - bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(2) - bc.EXPECT().Context(gomock.Any()).Return(ctx, nil).Times(1) - bc.EXPECT().TipHeight().Return(uint64(0)).Times(2) - elp := (&action.EnvelopeBuilder{}).SetAction(&action.Execution{}).Build() - bytes, _, err := cs.SimulateExecution(ctx, &address.AddrV1{}, elp) - require.NoError(err) - require.Equal([]byte("success"), bytes) - }) - - t.Run("GetPendingNonce", func(t *testing.T) { + t.Run("Success", func(t *testing.T) { p := NewPatches() defer p.Reset() - p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) p = p.ApplyFuncReturn(accountutil.AccountState, &state.Account{}, nil) - p = p.ApplyFuncReturn(protocol.WithFeatureCtx, ctx) - p = p.ApplyFuncReturn(protocol.WithBlockCtx, ctx) - p = p.ApplyFuncReturn(protocol.MustGetFeatureCtx, protocol.FeatureCtx{}) - p = p.ApplyMethodReturn(&state.Account{}, "PendingNonce", uint64(0)) p = p.ApplyMethodReturn(&genesis.Blockchain{}, "BlockGasLimitByHeight", uint64(0)) p = p.ApplyPrivateMethod( cs, @@ -1113,9 +1065,8 @@ func TestSimulateExecution(t *testing.T) { }, ) - bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(2) - bc.EXPECT().Context(gomock.Any()).Return(ctx, nil).Times(1) - bc.EXPECT().TipHeight().Return(uint64(0)).Times(2) + bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(1) + bc.EXPECT().TipHeight().Return(uint64(0)).Times(1) elp := (&action.EnvelopeBuilder{}).SetAction(&action.Execution{}).Build() bytes, _, err := cs.SimulateExecution(ctx, &address.AddrV1{}, elp) require.NoError(err) @@ -1174,7 +1125,6 @@ func TestTraceTx(t *testing.T) { p = p.ApplyFuncReturn(logger.NewStructLogger, nil) p = p.ApplyFuncReturn(protocol.WithVMConfigCtx, ctx) p = p.ApplyFuncReturn(protocol.WithBlockCtx, ctx) - bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(1) p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) p = p.ApplyFuncReturn(protocol.WithBlockchainCtx, ctx) p = p.ApplyFuncReturn(protocol.WithFeatureCtx, ctx) @@ -1222,7 +1172,6 @@ func TestTraceTx(t *testing.T) { p = p.ApplyFuncReturn(logger.NewStructLogger, nil) p = p.ApplyFuncReturn(protocol.WithVMConfigCtx, ctx) p = p.ApplyFuncReturn(protocol.WithBlockCtx, ctx) - bc.EXPECT().Genesis().Return(genesis.Genesis{}).Times(1) p = p.ApplyFuncReturn(genesis.WithGenesisContext, ctx) p = p.ApplyFuncReturn(protocol.WithBlockchainCtx, ctx) p = p.ApplyFuncReturn(protocol.WithFeatureCtx, ctx) diff --git a/api/grpcserver_integrity_test.go b/api/grpcserver_integrity_test.go index 8e17234580..64f9dcb532 100644 --- a/api/grpcserver_integrity_test.go +++ b/api/grpcserver_integrity_test.go @@ -447,7 +447,7 @@ var ( { hex.EncodeToString(_executionHash1[:]), "", - "08b0066e10b5607e47159c2cf7ba36e36d0c980f5108dfca0ec20547a7adace4", + "23895376e424873b43603a4f43d92054fe99fa9f51dab1eada24b2ce808c5997", "", 10100, }, diff --git a/blockchain/block/block_test.go b/blockchain/block/block_test.go index 56e16f757b..6fda6891df 100644 --- a/blockchain/block/block_test.go +++ b/blockchain/block/block_test.go @@ -179,23 +179,12 @@ func makeBlock(tb testing.TB, n int) *Block { sevlps := make([]*action.SealedEnvelope, 0) for j := 1; j <= n; j++ { i := rand.Int() - tsf, err := action.NewTransfer( - uint64(i), + tsf := action.NewTransfer( unit.ConvertIotxToRau(1000+int64(i)), identityset.Address(i%identityset.Size()).String(), - nil, - 20000+uint64(i), - unit.ConvertIotxToRau(1+int64(i)), - ) - require.NoError(tb, err) - eb := action.EnvelopeBuilder{} - evlp := eb. - SetAction(tsf). - SetGasLimit(tsf.GasLimit()). - SetGasPrice(tsf.GasPrice()). - SetNonce(tsf.Nonce()). - SetVersion(1). - Build() + nil) + evlp := (&action.EnvelopeBuilder{}).SetNonce(uint64(i)).SetGasPrice(unit.ConvertIotxToRau(1 + int64(i))). + SetGasLimit(20000 + uint64(i)).SetAction(tsf).Build() sevlp, err := action.Sign(evlp, identityset.PrivateKey((i+1)%identityset.Size())) require.NoError(tb, err) sevlps = append(sevlps, sevlp) diff --git a/blockchain/block/runnable_test.go b/blockchain/block/runnable_test.go index a342b45612..5767331a29 100644 --- a/blockchain/block/runnable_test.go +++ b/blockchain/block/runnable_test.go @@ -15,12 +15,10 @@ func TestRunnableActionsBuilder(t *testing.T) { require := require.New(t) data, err := hex.DecodeString("") require.NoError(err) - v, err := action.NewExecution("", 0, big.NewInt(10), uint64(10), big.NewInt(10), data) - require.NoError(err) + v := action.NewExecution("", big.NewInt(10), data) ra := NewRunnableActionsBuilder() bd := &action.EnvelopeBuilder{} - elp := bd.SetGasPrice(big.NewInt(10)). - SetGasLimit(uint64(100000)). + elp := bd.SetGasPrice(big.NewInt(10)).SetGasLimit(uint64(100000)). SetAction(v).Build() selp, err := action.Sign(elp, identityset.PrivateKey(28)) diff --git a/blockchain/block/utils_test.go b/blockchain/block/utils_test.go index 97951b330d..fb4bd9f695 100644 --- a/blockchain/block/utils_test.go +++ b/blockchain/block/utils_test.go @@ -17,22 +17,13 @@ func TestBody_CalculateTxRoot(t *testing.T) { var sevlps []*action.SealedEnvelope for i := 1; i <= 10; i++ { - tsf, _ := action.NewTransfer( - uint64(i), + tsf := action.NewTransfer( unit.ConvertIotxToRau(1000+int64(i)), identityset.Address(i%identityset.Size()).String(), - nil, - 20000+uint64(i), - unit.ConvertIotxToRau(1+int64(i)), - ) + nil) eb := action.EnvelopeBuilder{} - evlp := eb. - SetAction(tsf). - SetGasLimit(tsf.GasLimit()). - SetGasPrice(tsf.GasPrice()). - SetNonce(tsf.Nonce()). - SetVersion(1). - Build() + evlp := eb.SetNonce(uint64(i)).SetGasPrice(unit.ConvertIotxToRau(1 + int64(i))). + SetGasLimit(20000 + uint64(i)).SetAction(tsf).Build() sevlp, err := action.Sign(evlp, identityset.PrivateKey((i+1)%identityset.Size())) requireT.NoError(err) sevlps = append(sevlps, sevlp) @@ -53,22 +44,13 @@ func TestBody_CalculateTransferAmount(t *testing.T) { transferAmount := big.NewInt(0) for i := 1; i <= 10; i++ { - tsf, _ := action.NewTransfer( - uint64(i), + tsf := action.NewTransfer( unit.ConvertIotxToRau(1000+int64(i)), identityset.Address(i%identityset.Size()).String(), - nil, - 20000+uint64(i), - unit.ConvertIotxToRau(1+int64(i)), - ) + nil) eb := action.EnvelopeBuilder{} - evlp := eb. - SetAction(tsf). - SetGasLimit(tsf.GasLimit()). - SetGasPrice(tsf.GasPrice()). - SetNonce(tsf.Nonce()). - SetVersion(1). - Build() + evlp := eb.SetNonce(uint64(i)).SetGasPrice(unit.ConvertIotxToRau(1 + int64(i))). + SetGasLimit(20000 + uint64(i)).SetAction(tsf).Build() sevlp, err := action.Sign(evlp, identityset.PrivateKey((i+1)%identityset.Size())) requireT.NoError(err) transferAmount.Add(transferAmount, tsf.Amount()) diff --git a/blockchain/integrity/integrity_test.go b/blockchain/integrity/integrity_test.go index 7cf0cd9bb0..6dc5ba89b8 100644 --- a/blockchain/integrity/integrity_test.go +++ b/blockchain/integrity/integrity_test.go @@ -1087,7 +1087,7 @@ func TestBlockchainHardForkFeatures(t *testing.T) { tsf1, err = action.Sign(elp, identityset.PrivateKey(25)) require.NoError(err) require.NoError(ap.Add(ctx, tsf1)) - t2 := action.NewTransfer(big.NewInt(200), identityset.Address(27).String()) + t2 := action.NewTransfer(big.NewInt(200), identityset.Address(27).String(), nil) elp = (&action.EnvelopeBuilder{}).SetNonce(1). SetChainID(cfg.Chain.ID). SetGasPrice(minGas). @@ -1282,14 +1282,12 @@ func TestBlockchainHardForkFeatures(t *testing.T) { require.EqualValues(iotextypes.Encoding_TX_CONTAINER, selp.Encoding()) require.NoError(ap.Add(ctx, selp)) } - claim := (&action.ClaimFromRewardingFundBuilder{}). - SetAmount(big.NewInt(200000000000)). - SetAddress(producer).Build() + claim := action.NewClaimFromRewardingFund(big.NewInt(200000000000), producer, nil) elp = (&action.EnvelopeBuilder{}).SetNonce(6). SetChainID(cfg.Chain.ID). SetGasPrice(minGas). SetGasLimit(100000). - SetAction(&claim).Build() + SetAction(claim).Build() tsf2, err = action.Sign(elp, priKey0) require.NoError(err) require.NoError(ap.Add(ctx, tsf2)) diff --git a/chainservice/builder.go b/chainservice/builder.go index 16cb6ab656..fb988bd952 100644 --- a/chainservice/builder.go +++ b/chainservice/builder.go @@ -640,10 +640,8 @@ func (builder *Builder) registerRollDPoSProtocol() error { if correctGas { gasLimit *= 10 } - ex, err := action.NewExecution(contract, 1, big.NewInt(0), gasLimit, big.NewInt(0), params) - if err != nil { - return nil, err - } + elp := (&action.EnvelopeBuilder{}).SetNonce(1).SetGasLimit(gasLimit). + SetAction(action.NewExecution(contract, big.NewInt(0), params)).Build() addr, err := address.FromString(address.ZeroAddress) if err != nil { @@ -655,7 +653,7 @@ func (builder *Builder) registerRollDPoSProtocol() error { GetBlockTime: getBlockTime, DepositGasFunc: rewarding.DepositGas, }) - data, _, err := factory.SimulateExecution(ctx, addr, ex) + data, _, err := factory.SimulateExecution(ctx, addr, elp) return data, err }, candidatesutil.CandidatesFromDB, diff --git a/e2etest/bigint_test.go b/e2etest/bigint_test.go index c3642da3eb..a569ce2fb7 100644 --- a/e2etest/bigint_test.go +++ b/e2etest/bigint_test.go @@ -125,26 +125,16 @@ func prepareBlockchain(ctx context.Context, _executor string, r *require.Asserti } func prepareTransfer(bc blockchain.Blockchain, sf factory.Factory, ap actpool.ActPool, r *require.Assertions) (*block.Block, error) { - exec, err := action.NewTransfer(1, big.NewInt(-10000), _recipient, nil, uint64(1000000), big.NewInt(9000000000000)) - r.NoError(err) - builder := &action.EnvelopeBuilder{} - elp := builder.SetAction(exec). - SetNonce(exec.Nonce()). - SetGasLimit(exec.GasLimit()). - SetGasPrice(exec.GasPrice()). - Build() + exec := action.NewTransfer(big.NewInt(-10000), _recipient, nil) + elp := (&action.EnvelopeBuilder{}).SetAction(exec).SetNonce(1).SetGasLimit(1000000). + SetGasPrice(big.NewInt(9000000000000)).Build() return prepare(bc, sf, ap, elp, r) } func prepareAction(bc blockchain.Blockchain, sf factory.Factory, ap actpool.ActPool, r *require.Assertions) (*block.Block, error) { - exec, err := action.NewExecution(action.EmptyAddress, 1, big.NewInt(-100), uint64(1000000), big.NewInt(9000000000000), []byte{}) - r.NoError(err) - builder := &action.EnvelopeBuilder{} - elp := builder.SetAction(exec). - SetNonce(exec.Nonce()). - SetGasLimit(exec.GasLimit()). - SetGasPrice(exec.GasPrice()). - Build() + exec := action.NewExecution(action.EmptyAddress, big.NewInt(-100), []byte{}) + elp := (&action.EnvelopeBuilder{}).SetAction(exec).SetNonce(1).SetGasLimit(1000000). + SetGasPrice(big.NewInt(9000000000000)).Build() return prepare(bc, sf, ap, elp, r) } diff --git a/e2etest/contract_staking_test.go b/e2etest/contract_staking_test.go index 1a853da1d6..e1b0fb4e15 100644 --- a/e2etest/contract_staking_test.go +++ b/e2etest/contract_staking_test.go @@ -2017,14 +2017,9 @@ func deployContracts( amount := param.amount gasLimit := param.gasLimit gasPrice := param.gasPrice - exec, err := action.NewExecution(action.EmptyAddress, nonce, amount, gasLimit, gasPrice, bytecode) - r.NoError(err) - builder := &action.EnvelopeBuilder{} - elp := builder.SetAction(exec). - SetNonce(exec.Nonce()). - SetGasLimit(gasLimit). - SetGasPrice(gasPrice). - Build() + exec := action.NewExecution(action.EmptyAddress, amount, bytecode) + elp := (&action.EnvelopeBuilder{}).SetAction(exec).SetNonce(nonce). + SetGasLimit(gasLimit).SetGasPrice(gasPrice).Build() selp, err := action.Sign(elp, sk) r.NoError(err) err = ap.Add(context.Background(), selp) @@ -2082,14 +2077,9 @@ func writeContract(bc blockchain.Blockchain, gasPrice := param.gasPrice bytecode, err := hex.DecodeString(param.bytecode) r.NoError(err) - exec, err := action.NewExecution(param.contractAddr, nonce, amount, gasLimit, gasPrice, bytecode) - r.NoError(err) - builder := &action.EnvelopeBuilder{} - elp := builder.SetAction(exec). - SetNonce(exec.Nonce()). - SetGasLimit(gasLimit). - SetGasPrice(gasPrice). - Build() + exec := action.NewExecution(param.contractAddr, amount, bytecode) + elp := (&action.EnvelopeBuilder{}).SetAction(exec).SetNonce(nonce). + SetGasLimit(gasLimit).SetGasPrice(gasPrice).Build() selp, err := action.Sign(elp, sk) r.NoError(err) err = ap.Add(context.Background(), selp) diff --git a/e2etest/local_transfer_test.go b/e2etest/local_transfer_test.go index 33d4270c4a..b1eaa60e64 100644 --- a/e2etest/local_transfer_test.go +++ b/e2etest/local_transfer_test.go @@ -685,21 +685,9 @@ func TestEnforceChainID(t *testing.T) { require.NoError(bc.Stop(ctx)) }() for i, c := range testCase { - tsf, err := action.NewTransfer( - uint64(i)+1, - big.NewInt(100), - identityset.Address(27).String(), - []byte{}, uint64(100000), - big.NewInt(1).Mul(big.NewInt(int64(i)+10), big.NewInt(unit.Qev)), - ) - require.NoError(err) - - bd := &action.EnvelopeBuilder{} - elp1 := bd.SetAction(tsf). - SetChainID(c.chainID). - SetNonce(c.nonce). - SetGasLimit(100000). - SetGasPrice(big.NewInt(1).Mul(big.NewInt(int64(i)+10), big.NewInt(unit.Qev))).Build() + tsf := action.NewTransfer(big.NewInt(100), identityset.Address(27).String(), []byte{}) + elp1 := (&action.EnvelopeBuilder{}).SetGasPrice(big.NewInt(1).Mul(big.NewInt(int64(i)+10), big.NewInt(unit.Qev))). + SetChainID(c.chainID).SetNonce(c.nonce).SetGasLimit(100000).SetAction(tsf).Build() selp, err := action.Sign(elp1, identityset.PrivateKey(0)) require.NoError(err) @@ -718,10 +706,9 @@ func TestEnforceChainID(t *testing.T) { // verify action has valid chainID if c.success { act := blk.Actions[0] - tsf, ok := act.Action().(*action.Transfer) + _, ok := act.Action().(*action.Transfer) require.True(ok) require.Equal(c.chainID, act.ChainID()) - require.Equal(c.chainID, tsf.ChainID()) } } } diff --git a/e2etest/native_staking_test.go b/e2etest/native_staking_test.go index c7bc1d720c..f4376051c0 100644 --- a/e2etest/native_staking_test.go +++ b/e2etest/native_staking_test.go @@ -1033,8 +1033,7 @@ func TestCandidateTransferOwnership(t *testing.T) { resp1, err := test.api.GetAccount(context.Background(), &iotexapi.GetAccountRequest{Address: identityset.Address(poorID).String()}) require.NoError(err) require.Equal("0", resp1.GetAccountMeta().Balance) - ms, err := action.NewMigrateStake(0, 6, gasLimit, gasPrice) - require.NoError(err) + ms := action.NewMigrateStake(6) resp, err := test.api.EstimateActionGasConsumption(context.Background(), &iotexapi.EstimateActionGasConsumptionRequest{ Action: &iotexapi.EstimateActionGasConsumptionRequest_StakeMigrate{StakeMigrate: ms.Proto()}, CallerAddress: identityset.Address(poorID).String(), diff --git a/e2etest/staking_test.go b/e2etest/staking_test.go index 03e322d1d2..8df8d4f8d7 100644 --- a/e2etest/staking_test.go +++ b/e2etest/staking_test.go @@ -113,8 +113,8 @@ func TestStakingContract(t *testing.T) { if correctGas { gasLimit *= 10 } - ex := action.NewExecution(contract, big.NewInt(0), params) - + elp := (&action.EnvelopeBuilder{}).SetAction(action.NewExecution(contract, big.NewInt(0), params)). + SetGasLimit(gasLimit).Build() addr, err := address.FromString(address.ZeroAddress) if err != nil { return nil, err @@ -125,7 +125,7 @@ func TestStakingContract(t *testing.T) { GetBlockTime: fakeGetBlockTime, DepositGasFunc: rewarding.DepositGas, }) - data, _, err := sf.SimulateExecution(ctx, addr, ex) + data, _, err := sf.SimulateExecution(ctx, addr, elp) return data, err }) diff --git a/gasstation/gasstattion_test.go b/gasstation/gasstattion_test.go index 695afd38f9..6ca3e3759e 100644 --- a/gasstation/gasstattion_test.go +++ b/gasstation/gasstattion_test.go @@ -109,19 +109,8 @@ func TestSuggestGasPriceForUserAction(t *testing.T) { }() for i := 0; i < 30; i++ { - tsf, err := action.NewTransfer( - uint64(i)+1, - big.NewInt(100), - identityset.Address(27).String(), - []byte{}, uint64(100000), - big.NewInt(1).Mul(big.NewInt(int64(i)+10), big.NewInt(unit.Qev)), - ) - require.NoError(t, err) - - bd := &action.EnvelopeBuilder{} - elp1 := bd.SetAction(tsf). - SetNonce(uint64(i) + 1). - SetGasLimit(100000). + tsf := action.NewTransfer(big.NewInt(100), identityset.Address(27).String(), []byte{}) + elp1 := (&action.EnvelopeBuilder{}).SetAction(tsf).SetNonce(uint64(i) + 1).SetGasLimit(100000). SetGasPrice(big.NewInt(1).Mul(big.NewInt(int64(i)+10), big.NewInt(unit.Qev))).Build() selp1, err := action.Sign(elp1, identityset.PrivateKey(0)) require.NoError(t, err) diff --git a/ioctl/cmd/action/actiontransfer.go b/ioctl/cmd/action/actiontransfer.go index 6f4a1e43d4..d7ea7c238d 100644 --- a/ioctl/cmd/action/actiontransfer.go +++ b/ioctl/cmd/action/actiontransfer.go @@ -88,11 +88,7 @@ func transfer(args []string) error { if err != nil { return output.NewError(0, "failed to get nonce ", err) } - tx, err := action.NewTransfer(nonce, amount, - recipient, payload, gasLimit, gasPriceRau) - if err != nil { - return output.NewError(output.InstantiationError, "failed to make a Transfer instance", err) - } + tx := action.NewTransfer(amount, recipient, payload) return SendAction( (&action.EnvelopeBuilder{}). SetNonce(nonce). @@ -101,5 +97,4 @@ func transfer(args []string) error { SetAction(tx).Build(), sender, ) - } diff --git a/ioctl/cmd/action/stake2create.go b/ioctl/cmd/action/stake2create.go index 34896e4f65..903aa35285 100644 --- a/ioctl/cmd/action/stake2create.go +++ b/ioctl/cmd/action/stake2create.go @@ -94,7 +94,7 @@ func stake2Create(args []string) error { return output.NewError(0, "failed to get nonce ", err) } - s2c, err := action.NewCreateStake(nonce, candidateName, amountStringInRau, duration, _stake2AutoStake, data, gasLimit, gasPriceRau) + s2c, err := action.NewCreateStake(candidateName, amountStringInRau, duration, _stake2AutoStake, data) if err != nil { return output.NewError(output.InstantiationError, "failed to make a createStake instance", err) } diff --git a/ioctl/cmd/ws/wsutil.go b/ioctl/cmd/ws/wsutil.go index a578e55c32..c2874617e2 100644 --- a/ioctl/cmd/ws/wsutil.go +++ b/ioctl/cmd/ws/wsutil.go @@ -148,48 +148,34 @@ func (c *ContractCaller) envelop(method string, arguments ...any) (action.Envelo if err != nil { return nil, errors.Wrapf(err, "failed to pack method: %s", method) } - tx, err := action.NewExecution(c.contract.String(), 0, c.amount, 0, big.NewInt(0), bytecode) - if err != nil { - return nil, errors.Wrap(err, "failed to new execution") - } - + tx := action.NewExecution(c.contract.String(), c.amount, bytecode) gasPrice, err := c.gasPrice() if err != nil { return nil, err } - tx.SetGasPrice(gasPrice) - meta, err := c.accountMeta() if err != nil { return nil, errors.Wrap(err, "failed to get account meta") } - tx.SetNonce(meta.GetPendingNonce()) - gasLimit, err := c.gasLimit(tx.Proto()) if err != nil { return nil, err // gasLimit = 20000000 } - tx.SetGasLimit(gasLimit) - balance, ok := new(big.Int).SetString(meta.GetBalance(), 10) if !ok { return nil, errors.Errorf("failed to convert balance: %s", meta.GetBalance()) } - cost, _ := tx.Cost() + elp := (&action.EnvelopeBuilder{}).SetNonce(meta.GetPendingNonce()).SetGasPrice(gasPrice). + SetGasLimit(gasLimit).SetAction(tx).SetChainID(c.chainID).Build() + cost, _ := elp.Cost() + if err != nil { + return nil, errors.Wrap(err, "failed to get action's cost") + } if balance.Cmp(cost) < 0 { - return nil, errors.Errorf("balance is not enouth: %s", meta.GetAddress()) + return nil, errors.Errorf("balance is not enough: %s", meta.GetAddress()) } - - envelop := (&action.EnvelopeBuilder{}). - SetNonce(tx.Nonce()). - SetGasPrice(tx.GasPrice()). - SetGasLimit(tx.GasLimit()). - SetAction(tx). - SetChainID(c.chainID). - Build() - - return envelop, nil + return elp, nil } func (c *ContractCaller) gasPrice() (*big.Int, error) { diff --git a/ioctl/newcmd/action/action.go b/ioctl/newcmd/action/action.go index 90c99bf20e..c9dea14994 100644 --- a/ioctl/newcmd/action/action.go +++ b/ioctl/newcmd/action/action.go @@ -265,10 +265,10 @@ func gasPriceInRau(client ioctl.Client, gasPrice string) (*big.Int, error) { return new(big.Int).SetUint64(rsp.GasPrice), nil } -func fixGasLimit(client ioctl.Client, caller string, execution *action.Execution) (*action.Execution, error) { +func fixGasLimit(client ioctl.Client, caller string, execution *action.Execution) (uint64, error) { cli, err := client.APIServiceClient() if err != nil { - return nil, errors.Wrap(err, "failed to connect to endpoint") + return 0, errors.Wrap(err, "failed to connect to endpoint") } ctx := context.Background() @@ -284,9 +284,9 @@ func fixGasLimit(client ioctl.Client, caller string, execution *action.Execution CallerAddress: caller, }) if err != nil { - return nil, handleClientRequestError(err, "EstimateActionGasConsumption") + return 0, handleClientRequestError(err, "EstimateActionGasConsumption") } - return action.NewExecution(execution.Contract(), execution.Nonce(), execution.Amount(), res.Gas, execution.GasPrice(), execution.Data()) + return res.Gas, nil } // SendRaw sends raw action to blockchain @@ -412,16 +412,12 @@ func Execute(client ioctl.Client, if err != nil { return errors.Wrap(err, "failed to get nonce") } - tx, err := action.NewExecution(contract, nonce, amount, gasLimit, gasPriceRau, bytecode) - if err != nil || tx == nil { - return errors.Wrap(err, "failed to make a Execution instance") - } + tx := action.NewExecution(contract, amount, bytecode) if gasLimit == 0 { - tx, err = fixGasLimit(client, sender, tx) - if err != nil || tx == nil { + gasLimit, err = fixGasLimit(client, sender, tx) + if err != nil { return errors.Wrap(err, "failed to fix Execution gas limit") } - gasLimit = tx.GasLimit() } return SendAction( client, diff --git a/ioctl/newcmd/action/actionhash_test.go b/ioctl/newcmd/action/actionhash_test.go index 2bd3277055..ec8911eba2 100644 --- a/ioctl/newcmd/action/actionhash_test.go +++ b/ioctl/newcmd/action/actionhash_test.go @@ -105,20 +105,10 @@ func TestNewActionHashCmd(t *testing.T) { } func createEnvelope(chainID uint32) action.Envelope { - tsf, _ := action.NewTransfer( - uint64(10), + tsf := action.NewTransfer( unit.ConvertIotxToRau(1000+int64(10)), identityset.Address(10%identityset.Size()).String(), - nil, - 20000+uint64(10), - unit.ConvertIotxToRau(1+int64(10)), - ) - eb := action.EnvelopeBuilder{} - return eb. - SetAction(tsf). - SetGasLimit(tsf.GasLimit()). - SetGasPrice(tsf.GasPrice()). - SetNonce(tsf.Nonce()). - SetVersion(1). - SetChainID(chainID).Build() + nil) + return (&action.EnvelopeBuilder{}).SetGasPrice(unit.ConvertIotxToRau(1 + int64(10))). + SetNonce(10).SetGasLimit(20010).SetAction(tsf).SetChainID(chainID).Build() } diff --git a/ioctl/newcmd/action/actiontransfer.go b/ioctl/newcmd/action/actiontransfer.go index 1c76b29b2d..0a48586f92 100644 --- a/ioctl/newcmd/action/actiontransfer.go +++ b/ioctl/newcmd/action/actiontransfer.go @@ -85,10 +85,7 @@ func NewActionTransferCmd(client ioctl.Client) *cobra.Command { if err != nil { return errors.Wrap(err, "failed to get nonce") } - tx, err := action.NewTransfer(nonce, amount, recipient, payload, gasLimit, gasPriceRau) - if err != nil { - return errors.Wrap(err, "failed to make a Transfer instance") - } + tx := action.NewTransfer(amount, recipient, payload) return SendAction( client, cmd, diff --git a/state/factory/factory_test.go b/state/factory/factory_test.go index 5e6c41e956..2453dc555c 100644 --- a/state/factory/factory_test.go +++ b/state/factory/factory_test.go @@ -1207,6 +1207,7 @@ func testSimulateExecution(ctx context.Context, sf Factory, t *testing.T) { data, _ := hex.DecodeString("608060405234801561001057600080fd5b5060df8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a7230582002faabbefbbda99b20217cf33cb8ab8100caf1542bf1f48117d72e2c59139aea0029") ex := action.NewExecution(action.EmptyAddress, big.NewInt(0), data) + elp := (&action.EnvelopeBuilder{}).SetGasLimit(100000).SetAction(ex).Build() addr, err := address.FromString(address.ZeroAddress) require.NoError(err) @@ -1219,7 +1220,7 @@ func testSimulateExecution(ctx context.Context, sf Factory, t *testing.T) { }, DepositGasFunc: rewarding.DepositGas, }) - _, _, err = sf.SimulateExecution(ctx, addr, ex) + _, _, err = sf.SimulateExecution(ctx, addr, elp) require.NoError(err) } diff --git a/tools/actioninjector.v2/internal/client/client_test.go b/tools/actioninjector.v2/internal/client/client_test.go index 186ad37dac..0b2ffc3aa2 100644 --- a/tools/actioninjector.v2/internal/client/client_test.go +++ b/tools/actioninjector.v2/internal/client/client_test.go @@ -45,10 +45,8 @@ func TestClient(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() - tx, err := action.NewTransfer(uint64(1), big.NewInt(10), b, nil, uint64(0), big.NewInt(0)) - require.NoError(err) - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(1).SetAction(tx).Build() + tx := action.NewTransfer(big.NewInt(10), b, nil) + elp := (&action.EnvelopeBuilder{}).SetNonce(1).SetAction(tx).Build() selp, err := action.Sign(elp, priKeyA) require.NoError(err) diff --git a/tools/bot/server/bot/execution.go b/tools/bot/server/bot/execution.go index a6f26f88d6..b0894cb236 100644 --- a/tools/bot/server/bot/execution.go +++ b/tools/bot/server/bot/execution.go @@ -155,20 +155,13 @@ func (s *Execution) exec(pri crypto.PrivateKey) (txhash string, err error) { err = errors.New("amount convert error") return } - tx, err := action.NewExecution(s.cfg.Execution.Contract, nonce, amount, - s.cfg.GasLimit, gasprice, dataBytes) + tx := action.NewExecution(s.cfg.Execution.Contract, amount, dataBytes) + gas, err := grpcutil.FixGasLimit(s.cfg.API.URL, addr.String(), tx) if err != nil { return } - tx, err = grpcutil.FixGasLimit(s.cfg.API.URL, addr.String(), tx) - if err != nil { - return - } - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(nonce). - SetGasLimit(tx.GasLimit()). - SetGasPrice(gasprice). - SetAction(tx).Build() + elp := (&action.EnvelopeBuilder{}).SetNonce(nonce).SetGasLimit(gas). + SetGasPrice(gasprice).SetAction(tx).Build() selp, err := action.Sign(elp, pri) if err != nil { return diff --git a/tools/executiontester/blockchain/contract.go b/tools/executiontester/blockchain/contract.go index b08d349a1d..0229e886c2 100644 --- a/tools/executiontester/blockchain/contract.go +++ b/tools/executiontester/blockchain/contract.go @@ -198,17 +198,7 @@ func (c *contract) Transact(data []byte, readOnly bool) (string, error) { nonce := response.AccountMeta.PendingNonce gasPrice := big.NewInt(0) gasLimit := GasLimitPerByte*uint64(len(data)) + 5000000 - tx, err := action.NewExecution( - c.addr, - nonce, - big.NewInt(0), - gasLimit, - gasPrice, - data) - if err != nil { - return "", err - } - + tx := action.NewExecution(c.addr, big.NewInt(0), data) if readOnly { response, err := cli.ReadContract(ctx, &iotexapi.ReadContractRequest{ Execution: tx.Proto(), @@ -220,11 +210,8 @@ func (c *contract) Transact(data []byte, readOnly bool) (string, error) { return response.Data, nil } - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(nonce). - SetGasPrice(gasPrice). - SetGasLimit(gasLimit). - SetAction(tx).Build() + elp := (&action.EnvelopeBuilder{}).SetNonce(nonce).SetGasPrice(gasPrice). + SetGasLimit(gasLimit).SetAction(tx).Build() prvKey, err := crypto.HexStringToPrivateKey(c.prvkey) if err != nil { return "", crypto.ErrInvalidKey diff --git a/tools/util/injectorutil.go b/tools/util/injectorutil.go index 98b71fc548..a85fbe755c 100644 --- a/tools/util/injectorutil.go +++ b/tools/util/injectorutil.go @@ -563,16 +563,9 @@ func createSignedTransfer( if err != nil { return nil, nil, errors.Wrapf(err, "failed to decode payload %s", payload) } - transfer, err := action.NewTransfer( - nonce, amount, recipient.EncodedAddr, transferPayload, gasLimit, gasPrice) - if err != nil { - return nil, nil, errors.Wrap(err, "failed to create raw transfer") - } - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(nonce). - SetGasPrice(gasPrice). - SetGasLimit(gasLimit). - SetAction(transfer).Build() + transfer := action.NewTransfer(amount, recipient.EncodedAddr, transferPayload) + elp := (&action.EnvelopeBuilder{}).SetNonce(nonce).SetGasPrice(gasPrice). + SetGasLimit(gasLimit).SetAction(transfer).Build() selp, err := action.Sign(elp, sender.PriKey) if err != nil { return nil, nil, errors.Wrapf(err, "failed to sign transfer %v", elp) @@ -594,15 +587,9 @@ func createSignedExecution( if err != nil { return nil, nil, errors.Wrapf(err, "failed to decode data %s", data) } - execution, err := action.NewExecution(contract, nonce, amount, gasLimit, gasPrice, executionData) - if err != nil { - return nil, nil, errors.Wrap(err, "failed to create raw execution") - } - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(nonce). - SetGasPrice(gasPrice). - SetGasLimit(gasLimit). - SetAction(execution).Build() + execution := action.NewExecution(contract, amount, executionData) + elp := (&action.EnvelopeBuilder{}).SetNonce(nonce).SetGasPrice(gasPrice). + SetGasLimit(gasLimit).SetAction(execution).Build() selp, err := action.Sign(elp, executor.PriKey) if err != nil { return nil, nil, errors.Wrapf(err, "failed to sign execution %v", elp) @@ -621,16 +608,12 @@ func createSignedStake( gasLimit uint64, gasPrice *big.Int, ) (*action.SealedEnvelope, *action.CreateStake, error) { - createStake, err := action.NewCreateStake(nonce, candidateName, amount, duration, autoStake, payload, gasLimit, gasPrice) + createStake, err := action.NewCreateStake(candidateName, amount, duration, autoStake, payload) if err != nil { return nil, nil, err } - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(nonce). - SetGasPrice(gasPrice). - SetGasLimit(gasLimit). - SetAction(createStake). - Build() + elp := (&action.EnvelopeBuilder{}).SetNonce(nonce).SetGasPrice(gasPrice). + SetGasLimit(gasLimit).SetAction(createStake).Build() selp, err := action.Sign(elp, executor.PriKey) if err != nil { return nil, nil, err @@ -739,7 +722,9 @@ func CheckPendingActionList( retErr = err return nil } - cost, err := act.Cost() + elp := (&action.EnvelopeBuilder{}).SetNonce(pbAct.GetNonce()).SetGasLimit(gasLimit). + SetGasPrice(gasPrice).SetAction(act).Build() + cost, err := elp.Cost() if err != nil { retErr = err return nil