Commit 40724cea authored by Joshua Gutow's avatar Joshua Gutow

op-node: Add frameSize function

This calculates the size of the frame for the purposes of the
channel bank pruning calculation.
parent e081a971
......@@ -32,8 +32,8 @@ type Channel struct {
// No other frame number must be larger than this.
endFrameNumber uint16
// Store a map of frame number -> frame data for constant time ordering
inputs map[uint64][]byte
// Store a map of frame number -> frame for constant time ordering
inputs map[uint64]Frame
highestL1InclusionBlock eth.L1BlockRef
}
......@@ -41,7 +41,7 @@ type Channel struct {
func NewChannel(id ChannelID, openBlock eth.L1BlockRef) *Channel {
return &Channel{
id: id,
inputs: make(map[uint64][]byte),
inputs: make(map[uint64]Frame),
openBlock: openBlock,
}
}
......@@ -77,7 +77,7 @@ func (ch *Channel) AddFrame(frame Frame, l1InclusionBlock eth.L1BlockRef) error
if id >= uint64(ch.endFrameNumber) {
idsToPrune = append(idsToPrune, id)
}
ch.size -= uint64(len(prunedFrame)) + frameOverhead
ch.size -= frameSize(prunedFrame)
}
for _, id := range idsToPrune {
delete(ch.inputs, id)
......@@ -92,8 +92,8 @@ func (ch *Channel) AddFrame(frame Frame, l1InclusionBlock eth.L1BlockRef) error
if ch.highestL1InclusionBlock.Number < l1InclusionBlock.Number {
ch.highestL1InclusionBlock = l1InclusionBlock
}
ch.inputs[uint64(frame.FrameNumber)] = frame.Data
ch.size += uint64(len(frame.Data)) + frameOverhead
ch.inputs[uint64(frame.FrameNumber)] = frame
ch.size += frameSize(frame)
return nil
}
......@@ -137,11 +137,11 @@ func (ch *Channel) IsReady() bool {
func (ch *Channel) Reader() io.Reader {
var readers []io.Reader
for i := uint64(0); i <= uint64(ch.endFrameNumber); i++ {
data, ok := ch.inputs[i]
frame, ok := ch.inputs[i]
if !ok {
panic("dev error in channel.Reader. Must be called after the channel is ready.")
}
readers = append(readers, bytes.NewBuffer(data))
readers = append(readers, bytes.NewBuffer(frame.Data))
}
return io.MultiReader(readers...)
}
......
......@@ -8,6 +8,14 @@ import (
// count the tagging info as 200 in terms of buffer size.
const frameOverhead = 200
// frameSize calculates the size of the frame + overhead for
// storing the frame. The sum of the frame size of each frame in
// a channel determines the channel's size. The sum of the channel
// sizes is used for pruning & compared against `MaxChannelBankSize`
func frameSize(frame Frame) uint64 {
return uint64(len(frame.Data)) + frameOverhead
}
const DerivationVersion0 = 0
// MaxChannelBankSize is the amount of memory space, in number of bytes,
......
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