Commit c7c84885 authored by mergify[bot]'s avatar mergify[bot] Committed by GitHub

Merge pull request #3769 from ethereum-optimism/jg/max_rlp_bytes_per_channel

specs,op-node: Clarify max bytes per channel comparison
parents 248908b4 3616c249
...@@ -84,7 +84,7 @@ func (co *ChannelOut) AddBlock(block *types.Block) error { ...@@ -84,7 +84,7 @@ func (co *ChannelOut) AddBlock(block *types.Block) error {
return err return err
} }
// We encode to a temporary buffer to determine the encoded length to // We encode to a temporary buffer to determine the encoded length to
// ensure that the total size of all RLP elements is less than MAX_RLP_BYTES_PER_CHANNEL // ensure that the total size of all RLP elements is less than or equal to MAX_RLP_BYTES_PER_CHANNEL
var buf bytes.Buffer var buf bytes.Buffer
if err := rlp.Encode(&buf, batch); err != nil { if err := rlp.Encode(&buf, batch); err != nil {
return err return err
......
package derive package derive
import ( import (
"bytes"
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
...@@ -25,3 +27,25 @@ func TestChannelOutAddBlock(t *testing.T) { ...@@ -25,3 +27,25 @@ func TestChannelOutAddBlock(t *testing.T) {
require.Equal(t, ErrNotDepositTx, err) require.Equal(t, ErrNotDepositTx, err)
}) })
} }
// TestRLPByteLimit ensures that stream encoder is properly limiting the length.
// It will decode the input if `len(input) <= inputLimit`.
func TestRLPByteLimit(t *testing.T) {
// Should succeed if `len(input) == inputLimit`
enc := []byte("\x8bhello world") // RLP encoding of the string "hello world"
in := bytes.NewBuffer(enc)
var out string
stream := rlp.NewStream(in, 12)
err := stream.Decode(&out)
require.Nil(t, err)
require.Equal(t, out, "hello world")
// Should fail if the `inputLimit = len(input) - 1`
enc = []byte("\x8bhello world") // RLP encoding of the string "hello world"
in = bytes.NewBuffer(enc)
var out2 string
stream = rlp.NewStream(in, 11)
err = stream.Decode(&out2)
require.Equal(t, err, rlp.ErrValueTooLarge)
require.Equal(t, out2, "")
}
...@@ -363,11 +363,10 @@ where: ...@@ -363,11 +363,10 @@ where:
When decompressing a channel, we limit the amount of decompressed data to `MAX_RLP_BYTES_PER_CHANNEL` (currently When decompressing a channel, we limit the amount of decompressed data to `MAX_RLP_BYTES_PER_CHANNEL` (currently
10,000,000 bytes), in order to avoid "zip-bomb" types of attack (where a small compressed input decompresses to a 10,000,000 bytes), in order to avoid "zip-bomb" types of attack (where a small compressed input decompresses to a
humongous amount of data). If the decompressed data exceeds the limit, things proceeds as thought the channel contained humongous amount of data). If the decompressed data exceeds the limit, things proceeds as though the channel contained
only the first `MAX_RLP_BYTES_PER_CHANNEL` decompressed bytes. only the first `MAX_RLP_BYTES_PER_CHANNEL` decompressed bytes. The limit is set on RLP decoding, so all batches that
can be decoded in `MAX_RLP_BYTES_PER_CHANNEL` will be accepted ven if the size of the channel is greater than
When decoding batches, all batches that can be completly decoded below `MAX_RLP_BYTES_PER_CHANNEL` will be accepted `MAX_RLP_BYTES_PER_CHANNEL`. The exact requirement is that `length(input) <= MAX_RLP_BYTES_PER_CHANNEL`.
even if the size of the channel is greater than `MAX_RLP_BYTES_PER_CHANNEL`.
While the above pseudocode implies that all batches are known in advance, it is possible to perform streaming While the above pseudocode implies that all batches are known in advance, it is possible to perform streaming
compression and decompression of RLP-encoded batches. This means it is possible to start including channel frames in a compression and decompression of RLP-encoded batches. This means it is possible to start including channel frames in a
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment