params.go 2.06 KB
Newer Older
protolambda's avatar
protolambda committed
1 2 3
package derive

import (
4
	"encoding/hex"
protolambda's avatar
protolambda committed
5 6 7 8 9 10 11
	"errors"
	"fmt"
)

// count the tagging info as 200 in terms of buffer size.
const frameOverhead = 200

12 13 14 15 16 17 18 19
// 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
}

protolambda's avatar
protolambda committed
20 21
const DerivationVersion0 = 0

22 23 24 25 26
// MaxSpanBatchFieldSize is the maximum amount of bytes that will be read from
// a span batch to decode span batch field. This value cannot be larger than
// MaxRLPBytesPerChannel because single batch cannot be larger than channel size.
const MaxSpanBatchFieldSize = 10_000_000

protolambda's avatar
protolambda committed
27 28 29 30 31
// MaxChannelBankSize is the amount of memory space, in number of bytes,
// till the bank is pruned by removing channels,
// starting with the oldest channel.
const MaxChannelBankSize = 100_000_000

32 33 34 35
// MaxRLPBytesPerChannel is the maximum amount of bytes that will be read from
// a channel. This limit is set when decoding the RLP.
const MaxRLPBytesPerChannel = 10_000_000

protolambda's avatar
protolambda committed
36 37 38
// DuplicateErr is returned when a newly read frame is already known
var DuplicateErr = errors.New("duplicate frame")

39 40
// ChannelIDLength defines the length of the channel IDs
const ChannelIDLength = 16
protolambda's avatar
protolambda committed
41

42 43
// ChannelID is an opaque identifier for a channel. It is 128 bits to be globally unique.
type ChannelID [ChannelIDLength]byte
protolambda's avatar
protolambda committed
44 45

func (id ChannelID) String() string {
46
	return fmt.Sprintf("%x", id[:])
protolambda's avatar
protolambda committed
47 48
}

49 50
// TerminalString implements log.TerminalStringer, formatting a string for console output during logging.
func (id ChannelID) TerminalString() string {
51
	return fmt.Sprintf("%x..%x", id[:3], id[13:])
protolambda's avatar
protolambda committed
52
}
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67
func (id ChannelID) MarshalText() ([]byte, error) {
	return []byte(id.String()), nil
}

func (id *ChannelID) UnmarshalText(text []byte) error {
	h, err := hex.DecodeString(string(text))
	if err != nil {
		return err
	}
	if len(h) != ChannelIDLength {
		return errors.New("invalid length")
	}
	copy(id[:], h)
	return nil
68
}