Commit 835dd0ff authored by Andreas Bigger's avatar Andreas Bigger

add maxSize check to the channel out OutputFrame

parent 2351ed46
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
var ErrMaxFrameSizeTooSmall = errors.New("maxSize is too small to fit the fixed frame overhead")
var ErrNotDepositTx = errors.New("first transaction in block is not a deposit tx") var ErrNotDepositTx = errors.New("first transaction in block is not a deposit tx")
var ErrTooManyRLPBytes = errors.New("batch would cause RLP bytes to go over limit") var ErrTooManyRLPBytes = errors.New("batch would cause RLP bytes to go over limit")
...@@ -150,6 +151,12 @@ func (co *ChannelOut) OutputFrame(w *bytes.Buffer, maxSize uint64) (uint16, erro ...@@ -150,6 +151,12 @@ func (co *ChannelOut) OutputFrame(w *bytes.Buffer, maxSize uint64) (uint16, erro
FrameNumber: uint16(co.frame), FrameNumber: uint16(co.frame),
} }
// Error if the `maxSize` is too small to fit the fixed frame
// overhead as specified below.
if maxSize < 23 {
return 0, ErrMaxFrameSizeTooSmall
}
// Copy data from the local buffer into the frame data buffer // Copy data from the local buffer into the frame data buffer
// Don't go past the maxSize with the fixed frame overhead. // Don't go past the maxSize with the fixed frame overhead.
// Fixed overhead: 16 + 2 + 4 + 1 = 23 bytes. // Fixed overhead: 16 + 2 + 4 + 1 = 23 bytes.
......
...@@ -29,6 +29,26 @@ func TestChannelOutAddBlock(t *testing.T) { ...@@ -29,6 +29,26 @@ func TestChannelOutAddBlock(t *testing.T) {
}) })
} }
// TestOutputFrameSmallMaxSize tests that calling [OutputFrame] with a small
// max size that is below the fixed frame size overhead of 23, will return
// an error.
func TestOutputFrameSmallMaxSize(t *testing.T) {
cout, err := NewChannelOut()
require.NoError(t, err)
// Set the channel out frame details
cout.id = ChannelID{0x01}
cout.frame = 0
// Call OutputFrame with the range of small max size values that err
w := bytes.NewBuffer([]byte{})
for i := 0; i < 23; i++ {
fid, err := cout.OutputFrame(w, uint64(i))
require.ErrorIs(t, err, ErrMaxFrameSizeTooSmall)
require.Zero(t, fid)
}
}
// TestRLPByteLimit ensures that stream encoder is properly limiting the length. // TestRLPByteLimit ensures that stream encoder is properly limiting the length.
// It will decode the input if `len(input) <= inputLimit`. // It will decode the input if `len(input) <= inputLimit`.
func TestRLPByteLimit(t *testing.T) { func TestRLPByteLimit(t *testing.T) {
...@@ -64,42 +84,42 @@ func TestForceCloseTxData(t *testing.T) { ...@@ -64,42 +84,42 @@ func TestForceCloseTxData(t *testing.T) {
output: "", output: "",
}, },
{ {
frames: []Frame{Frame{FrameNumber: 0, IsLast: false}, Frame{ID: id, FrameNumber: 1, IsLast: true}}, frames: []Frame{{FrameNumber: 0, IsLast: false}, {ID: id, FrameNumber: 1, IsLast: true}},
errors: true, errors: true,
output: "", output: "",
}, },
{ {
frames: []Frame{Frame{ID: id, FrameNumber: 0, IsLast: false}}, frames: []Frame{{ID: id, FrameNumber: 0, IsLast: false}},
errors: false, errors: false,
output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000001", output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000001",
}, },
{ {
frames: []Frame{Frame{ID: id, FrameNumber: 0, IsLast: true}}, frames: []Frame{{ID: id, FrameNumber: 0, IsLast: true}},
errors: false, errors: false,
output: "00", output: "00",
}, },
{ {
frames: []Frame{Frame{ID: id, FrameNumber: 1, IsLast: false}}, frames: []Frame{{ID: id, FrameNumber: 1, IsLast: false}},
errors: false, errors: false,
output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000001", output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000001",
}, },
{ {
frames: []Frame{Frame{ID: id, FrameNumber: 1, IsLast: true}}, frames: []Frame{{ID: id, FrameNumber: 1, IsLast: true}},
errors: false, errors: false,
output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000000", output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000000",
}, },
{ {
frames: []Frame{Frame{ID: id, FrameNumber: 2, IsLast: true}}, frames: []Frame{{ID: id, FrameNumber: 2, IsLast: true}},
errors: false, errors: false,
output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000000deadbeefdeadbeefdeadbeefdeadbeef00010000000000", output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000000deadbeefdeadbeefdeadbeefdeadbeef00010000000000",
}, },
{ {
frames: []Frame{Frame{ID: id, FrameNumber: 1, IsLast: false}, Frame{ID: id, FrameNumber: 3, IsLast: true}}, frames: []Frame{{ID: id, FrameNumber: 1, IsLast: false}, {ID: id, FrameNumber: 3, IsLast: true}},
errors: false, errors: false,
output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000000deadbeefdeadbeefdeadbeefdeadbeef00020000000000", output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000000deadbeefdeadbeefdeadbeefdeadbeef00020000000000",
}, },
{ {
frames: []Frame{Frame{ID: id, FrameNumber: 1, IsLast: false}, Frame{ID: id, FrameNumber: 3, IsLast: true}, Frame{ID: id, FrameNumber: 5, IsLast: true}}, frames: []Frame{{ID: id, FrameNumber: 1, IsLast: false}, {ID: id, FrameNumber: 3, IsLast: true}, {ID: id, FrameNumber: 5, IsLast: true}},
errors: false, errors: false,
output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000000deadbeefdeadbeefdeadbeefdeadbeef00020000000000", output: "00deadbeefdeadbeefdeadbeefdeadbeef00000000000000deadbeefdeadbeefdeadbeefdeadbeef00020000000000",
}, },
......
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