From 10c227e3203c63fb84313931f571535d60979208 Mon Sep 17 00:00:00 2001 From: Chen Chen <34592639+envestcc@users.noreply.github.com> Date: Tue, 28 Nov 2023 15:05:23 +0800 Subject: [PATCH] [action] Increase Execution Size Limit to 48K (#3995) --- action/protocol/context.go | 2 ++ action/protocol/execution/protocol.go | 15 +++++++++---- action/protocol/execution/protocol_test.go | 26 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/action/protocol/context.go b/action/protocol/context.go index 4557303fa4..d9d36852f6 100644 --- a/action/protocol/context.go +++ b/action/protocol/context.go @@ -114,6 +114,7 @@ type ( AddContractStakingVotes bool FixContractStakingWeightedVotes bool SharedGasWithDapp bool + ExecutionSizeLimit32KB bool } // FeatureWithHeightCtx provides feature check functions. @@ -253,6 +254,7 @@ func WithFeatureCtx(ctx context.Context) context.Context { AddContractStakingVotes: g.IsQuebec(height), FixContractStakingWeightedVotes: g.IsRedsea(height), SharedGasWithDapp: g.IsToBeEnabled(height), + ExecutionSizeLimit32KB: !g.IsToBeEnabled(height), }, ) } diff --git a/action/protocol/execution/protocol.go b/action/protocol/execution/protocol.go index bf190dc67e..71f1133eb1 100644 --- a/action/protocol/execution/protocol.go +++ b/action/protocol/execution/protocol.go @@ -20,8 +20,9 @@ import ( ) const ( - // ExecutionSizeLimit is the maximum size of execution allowed - ExecutionSizeLimit = 32 * 1024 + // the maximum size of execution allowed + _executionSizeLimit48KB = uint32(48 * 1024) + _executionSizeLimit32KB = uint32(32 * 1024) // TODO: it works only for one instance per protocol definition now _protocolID = "smart_contract" ) @@ -83,13 +84,19 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St } // Validate validates an execution -func (p *Protocol) Validate(_ context.Context, act action.Action, _ protocol.StateReader) error { +func (p *Protocol) Validate(ctx context.Context, act action.Action, _ protocol.StateReader) error { exec, ok := act.(*action.Execution) if !ok { return nil } + sizeLimit := _executionSizeLimit48KB + fCtx := protocol.MustGetFeatureCtx(ctx) + if fCtx.ExecutionSizeLimit32KB { + sizeLimit = _executionSizeLimit32KB + } + // Reject oversize execution - if exec.TotalSize() > ExecutionSizeLimit { + if exec.TotalSize() > sizeLimit { return action.ErrOversizedData } return nil diff --git a/action/protocol/execution/protocol_test.go b/action/protocol/execution/protocol_test.go index 1134c34d28..571a9a2b6c 100644 --- a/action/protocol/execution/protocol_test.go +++ b/action/protocol/execution/protocol_test.go @@ -615,9 +615,29 @@ func TestProtocol_Validate(t *testing.T) { return time.Time{}, nil }) - ex, err := action.NewExecution("2", uint64(1), big.NewInt(0), uint64(0), big.NewInt(0), make([]byte, 32684)) - require.NoError(err) - require.Equal(action.ErrOversizedData, errors.Cause(p.Validate(context.Background(), ex, nil))) + cases := []struct { + name string + height uint64 + size uint64 + expectErr error + }{ + {"limit 32KB", 0, 32684, action.ErrOversizedData}, + {"limit 48KB I", genesis.Default.ToBeEnabledBlockHeight, 32684, nil}, + {"limit 48KB II", genesis.Default.ToBeEnabledBlockHeight, 49153, action.ErrOversizedData}, + } + + for i := range cases { + t.Run(cases[i].name, func(t *testing.T) { + ex, err := action.NewExecution("2", uint64(1), big.NewInt(0), uint64(0), big.NewInt(0), make([]byte, cases[i].size)) + require.NoError(err) + ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis) + ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{ + BlockHeight: cases[i].height, + }) + ctx = protocol.WithFeatureCtx(ctx) + require.Equal(cases[i].expectErr, errors.Cause(p.Validate(ctx, ex, nil))) + }) + } } func TestProtocol_Handle(t *testing.T) {