Skip to content

Commit

Permalink
[action] Increase Execution Size Limit to 48K (#3995)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Nov 28, 2023
1 parent 11e8ca1 commit 10c227e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type (
AddContractStakingVotes bool
FixContractStakingWeightedVotes bool
SharedGasWithDapp bool
ExecutionSizeLimit32KB bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -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),
},
)
}
Expand Down
15 changes: 11 additions & 4 deletions action/protocol/execution/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 23 additions & 3 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 10c227e

Please sign in to comment.