Skip to content

Commit 6321380

Browse files
authored
Remove per action gas limit cap (iotexproject#1289)
1 parent f64a6dd commit 6321380

27 files changed

+475
-432
lines changed

action/const.go

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ var (
1717
ErrInsufficientBalanceForGas = errors.New("Insufficient balance for gas")
1818
// ErrOutOfGas is the error when running out of gas
1919
ErrOutOfGas = errors.New("Out of gas")
20-
// ErrGasHigherThanLimit indicates the error of gas value
21-
ErrGasHigherThanLimit = errors.New("invalid gas for action")
2220
// ErrTransfer indicates the error of transfer
2321
ErrTransfer = errors.New("invalid transfer")
2422
// ErrNonce indicates the error of nonce

action/protocol/context.go

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ type RunActionsCtx struct {
3434
Caller address.Address
3535
// ActionHash is the hash of the action with the sealed envelope
3636
ActionHash hash.Hash256
37-
// ActionGasLimit is the action gas limit
38-
ActionGasLimit uint64
3937
// GasPrice is the action gas price
4038
GasPrice *big.Int
4139
// IntrinsicGas is the action intrinsic gas

action/protocol/context_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ func TestWithRunActionsCtx(t *testing.T) {
2222
require := require.New(t)
2323
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
2424
require.NoError(err)
25-
actionCtx := RunActionsCtx{1, time.Now(), 1, addr, addr, hash.ZeroHash256, 0, nil, 0, 0, nil}
25+
actionCtx := RunActionsCtx{1, time.Now(), 1, addr, addr, hash.ZeroHash256, nil, 0, 0, nil}
2626
require.NotNil(WithRunActionsCtx(context.Background(), actionCtx))
2727
}
2828
func TestGetRunActionsCtx(t *testing.T) {
2929
require := require.New(t)
3030
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
3131
require.NoError(err)
32-
actionCtx := RunActionsCtx{1111, time.Now(), 1, addr, addr, hash.ZeroHash256, 0, nil, 0, 0, nil}
32+
actionCtx := RunActionsCtx{1111, time.Now(), 1, addr, addr, hash.ZeroHash256, nil, 0, 0, nil}
3333
ctx := WithRunActionsCtx(context.Background(), actionCtx)
3434
require.NotNil(ctx)
3535
ret, ok := GetRunActionsCtx(ctx)
@@ -40,7 +40,7 @@ func TestMustGetRunActionsCtx(t *testing.T) {
4040
require := require.New(t)
4141
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
4242
require.NoError(err)
43-
actionCtx := RunActionsCtx{1111, time.Now(), 1, addr, addr, hash.ZeroHash256, 0, nil, 0, 0, nil}
43+
actionCtx := RunActionsCtx{1111, time.Now(), 1, addr, addr, hash.ZeroHash256, nil, 0, 0, nil}
4444
ctx := WithRunActionsCtx(context.Background(), actionCtx)
4545
require.NotNil(ctx)
4646
// Case I: Normal

action/protocol/execution/evm/evm.go

+20-18
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ import (
2121
"github.com/iotexproject/iotex-core/action"
2222
"github.com/iotexproject/iotex-core/action/protocol"
2323
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
24+
"github.com/iotexproject/iotex-core/blockchain/genesis"
2425
"github.com/iotexproject/iotex-core/pkg/log"
2526
)
2627

27-
// ErrInconsistentNonce is the error that the nonce is different from executor's nonce
28-
var ErrInconsistentNonce = errors.New("Nonce is not identical to executor nonce")
28+
var (
29+
// TODO: whenever ActionGasLimit is removed from genesis, we need to hard code it to 5M to make it compatible with
30+
// the mainnet.
31+
preAleutianActionGasLimit = genesis.Default.ActionGasLimit
32+
33+
// ErrInconsistentNonce is the error that the nonce is different from executor's nonce
34+
ErrInconsistentNonce = errors.New("Nonce is not identical to executor nonce")
35+
)
2936

3037
// CanTransfer checks whether the from account has enough balance
3138
func CanTransfer(db vm.StateDB, fromHash common.Address, balance *big.Int) bool {
@@ -65,16 +72,12 @@ func NewHeightChange(pacific, aleutian uint64) HeightChange {
6572
}
6673

6774
// NewParams creates a new context for use in the EVM.
68-
func NewParams(raCtx protocol.RunActionsCtx, execution *action.Execution, stateDB *StateDBAdapter) (*Params, error) {
69-
// If we don't have an explicit author (i.e. not mining), extract from the header
70-
/*
71-
var beneficiary common.Address
72-
if author == nil {
73-
beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation
74-
} else {
75-
beneficiary = *author
76-
}
77-
*/
75+
func NewParams(
76+
raCtx protocol.RunActionsCtx,
77+
execution *action.Execution,
78+
stateDB *StateDBAdapter,
79+
hc HeightChange,
80+
) (*Params, error) {
7881
executorAddr := common.BytesToAddress(raCtx.Caller.Bytes())
7982
var contractAddrPointer *common.Address
8083
if execution.Contract() != action.EmptyAddress {
@@ -85,20 +88,19 @@ func NewParams(raCtx protocol.RunActionsCtx, execution *action.Execution, stateD
8588
contractAddr := common.BytesToAddress(contract.Bytes())
8689
contractAddrPointer = &contractAddr
8790
}
88-
producer := common.BytesToAddress(raCtx.Producer.Bytes())
8991

9092
gasLimit := execution.GasLimit()
9193
// Reset gas limit to the system wide action gas limit cap if it's greater than it
92-
if gasLimit > raCtx.ActionGasLimit {
93-
gasLimit = raCtx.ActionGasLimit
94+
if raCtx.BlockHeight < hc.AleutianHeight && gasLimit > preAleutianActionGasLimit {
95+
gasLimit = preAleutianActionGasLimit
9496
}
9597

9698
context := vm.Context{
9799
CanTransfer: CanTransfer,
98100
Transfer: MakeTransfer,
99101
GetHash: GetHashFn(stateDB),
100102
Origin: executorAddr,
101-
Coinbase: producer,
103+
Coinbase: common.BytesToAddress(raCtx.Producer.Bytes()),
102104
BlockNumber: new(big.Int).SetUint64(raCtx.BlockHeight),
103105
Time: new(big.Int).SetInt64(raCtx.BlockTimeStamp.Unix()),
104106
Difficulty: new(big.Int).SetUint64(uint64(50)),
@@ -112,7 +114,7 @@ func NewParams(raCtx protocol.RunActionsCtx, execution *action.Execution, stateD
112114
raCtx.Caller.String(),
113115
execution.Amount(),
114116
contractAddrPointer,
115-
execution.GasLimit(),
117+
gasLimit,
116118
execution.Data(),
117119
}, nil
118120
}
@@ -157,7 +159,7 @@ func ExecuteContract(
157159
) ([]byte, *action.Receipt, error) {
158160
raCtx := protocol.MustGetRunActionsCtx(ctx)
159161
stateDB := NewStateDBAdapter(cm, sm, &hc, raCtx.BlockHeight, execution.Hash())
160-
ps, err := NewParams(raCtx, execution, stateDB)
162+
ps, err := NewParams(raCtx, execution, stateDB, hc)
161163
if err != nil {
162164
return nil, nil, err
163165
}

action/protocol/execution/protocol_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
3535
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
3636
"github.com/iotexproject/iotex-core/blockchain"
37-
"github.com/iotexproject/iotex-core/blockchain/genesis"
3837
"github.com/iotexproject/iotex-core/config"
3938
"github.com/iotexproject/iotex-core/pkg/log"
4039
"github.com/iotexproject/iotex-core/pkg/unit"
@@ -273,7 +272,7 @@ func (sct *SmartContractTest) prepareBlockchain(
273272
r.NoError(registry.Register(rewarding.ProtocolID, reward))
274273

275274
r.NotNil(bc)
276-
bc.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(bc, genesis.Default.ActionGasLimit))
275+
bc.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(bc))
277276
bc.Validator().AddActionValidators(account.NewProtocol(0), NewProtocol(bc, 0, 0), reward)
278277
sf := bc.GetFactory()
279278
r.NotNil(sf)
@@ -422,7 +421,7 @@ func TestProtocol_Handle(t *testing.T) {
422421
blockchain.BoltDBDaoOption(),
423422
blockchain.RegistryOption(&registry),
424423
)
425-
bc.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(bc, genesis.Default.ActionGasLimit))
424+
bc.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(bc))
426425
bc.Validator().AddActionValidators(account.NewProtocol(0), NewProtocol(bc, 0, 0))
427426
sf := bc.GetFactory()
428427
require.NotNil(sf)

action/protocol/generic_validator.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,20 @@ import (
1717

1818
// GenericValidator is the validator for generic action verification
1919
type GenericValidator struct {
20-
mu sync.RWMutex
21-
cm ChainManager
22-
actionGasLimit uint64
20+
mu sync.RWMutex
21+
cm ChainManager
2322
}
2423

2524
// NewGenericValidator constructs a new genericValidator
26-
func NewGenericValidator(cm ChainManager, actionGasLimit uint64) *GenericValidator {
25+
func NewGenericValidator(cm ChainManager) *GenericValidator {
2726
return &GenericValidator{
28-
cm: cm,
29-
actionGasLimit: actionGasLimit,
27+
cm: cm,
3028
}
3129
}
3230

3331
// Validate validates a generic action
3432
func (v *GenericValidator) Validate(ctx context.Context, act action.SealedEnvelope) error {
3533
vaCtx := MustGetValidateActionsCtx(ctx)
36-
// Reject over-gassed action
37-
if act.GasLimit() > v.actionGasLimit {
38-
return errors.Wrap(action.ErrGasHigherThanLimit, "gas is higher than gas limit")
39-
}
4034
// Reject action with insufficient gas limit
4135
intrinsicGas, err := act.IntrinsicGas()
4236
if intrinsicGas > act.GasLimit() || err != nil {

action/protocol/generic_validator_test.go

+4-20
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestActionProto(t *testing.T) {
3030
ctx := ValidateActionsCtx{1, "io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6", caller}
3131
c := WithValidateActionsCtx(context.Background(), ctx)
3232
cm := &MockChainManager{}
33-
valid := NewGenericValidator(cm, 100000)
33+
valid := NewGenericValidator(cm)
3434
data, err := hex.DecodeString("")
3535
require.NoError(err)
3636
// Case I: Normal
@@ -47,23 +47,7 @@ func TestActionProto(t *testing.T) {
4747
require.NoError(nselp.LoadProto(selp.Proto()))
4848
require.NoError(valid.Validate(c, nselp))
4949
}
50-
// Case II: GasLimit higher
51-
{
52-
v, err := action.NewExecution("", 0, big.NewInt(10), uint64(10), big.NewInt(10), data)
53-
require.NoError(err)
54-
bd := &action.EnvelopeBuilder{}
55-
elp := bd.SetGasPrice(big.NewInt(10)).
56-
SetGasLimit(uint64(1000000)).
57-
SetAction(v).Build()
58-
selp, err := action.Sign(elp, identityset.PrivateKey(28))
59-
require.NoError(err)
60-
nselp := action.SealedEnvelope{}
61-
require.NoError(nselp.LoadProto(selp.Proto()))
62-
err = valid.Validate(c, nselp)
63-
require.Error(err)
64-
require.True(strings.Contains(err.Error(), "gas is higher than gas limit"))
65-
}
66-
// Case III: GasLimit lower
50+
// Case II: GasLimit lower
6751
{
6852
v, err := action.NewExecution("", 0, big.NewInt(10), uint64(10), big.NewInt(10), data)
6953
require.NoError(err)
@@ -79,7 +63,7 @@ func TestActionProto(t *testing.T) {
7963
require.Error(err)
8064
require.True(strings.Contains(err.Error(), "insufficient gas"))
8165
}
82-
// Case IV: Call cm Nonce err
66+
// Case III: Call cm Nonce err
8367
{
8468
caller, err := address.FromString("io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6")
8569
require.NoError(err)
@@ -99,7 +83,7 @@ func TestActionProto(t *testing.T) {
9983
require.Error(err)
10084
require.True(strings.Contains(err.Error(), "invalid nonce value of account"))
10185
}
102-
// Case V: Call Nonce err
86+
// Case IV: Call Nonce err
10387
{
10488
v, err := action.NewExecution("", 1, big.NewInt(10), uint64(10), big.NewInt(10), data)
10589
require.NoError(err)

action/protocol/multichain/mainchain/protocol_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/iotexproject/iotex-core/action/protocol"
2020
"github.com/iotexproject/iotex-core/actpool"
2121
"github.com/iotexproject/iotex-core/blockchain"
22-
"github.com/iotexproject/iotex-core/blockchain/genesis"
2322
"github.com/iotexproject/iotex-core/config"
2423
"github.com/iotexproject/iotex-core/pkg/unit"
2524
"github.com/iotexproject/iotex-core/test/identityset"
@@ -47,7 +46,7 @@ func TestAddSubChainActions(t *testing.T) {
4746
require.NoError(t, err)
4847
p := NewProtocol(bc)
4948
ap.AddActionValidators(p)
50-
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(bc, genesis.Default.ActionGasLimit))
49+
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(bc))
5150
defer func() {
5251
require.NoError(t, bc.Stop(ctx))
5352
}()

0 commit comments

Comments
 (0)