go-p2p/specs/node-levin.md
Virgil 82d425d01e docs(repo): populate package specs
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-27 20:28:53 +00:00

7.6 KiB

levin

Import: dappco.re/go/core/p2p/node/levin

Files: 4

Types

Connection

type Connection struct {
	MaxPayloadSize uint64
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
}

Wrapper around net.Conn that reads and writes framed Levin packets.

  • MaxPayloadSize: per-connection payload ceiling enforced by ReadPacket. NewConnection starts with the package MaxPayloadSize default.
  • ReadTimeout: deadline applied before each ReadPacket call. NewConnection sets this to DefaultReadTimeout.
  • WriteTimeout: deadline applied before each write. NewConnection sets this to DefaultWriteTimeout.

Header

type Header struct {
	Signature       uint64
	PayloadSize     uint64
	ExpectResponse  bool
	Command         uint32
	ReturnCode      int32
	Flags           uint32
	ProtocolVersion uint32
}

Packed 33-byte Levin frame header. EncodeHeader writes these fields little-endian, and DecodeHeader validates the Signature and package-level MaxPayloadSize.

Section

type Section map[string]Value

Portable-storage object used by the Levin encoder and decoder. EncodeStorage sorts keys alphabetically for deterministic output.

Value

type Value struct {
	Type uint8
}

Tagged portable-storage value. The exported Type field identifies which internal scalar or array slot is populated; constructors such as Uint64Val, StringVal, and ObjectArrayVal create correctly-typed instances.

Functions

Top-level framing and storage functions

Name Signature Description
NewConnection func NewConnection(conn net.Conn) *Connection Wraps conn with Levin defaults: 100 MB payload limit, 120 s read timeout, and 30 s write timeout.
EncodeHeader func EncodeHeader(h *Header) [HeaderSize]byte Serialises h into the fixed 33-byte Levin header format.
DecodeHeader func DecodeHeader(buf [HeaderSize]byte) (Header, error) Parses a 33-byte header, rejecting bad magic signatures and payload sizes above the package-level limit.
PackVarint func PackVarint(v uint64) []byte Encodes v using the epee portable-storage varint scheme where the low two bits of the first byte encode the width.
UnpackVarint func UnpackVarint(buf []byte) (value uint64, bytesConsumed int, err error) Decodes one portable-storage varint and returns the value, consumed width, and any truncation or overflow error.
EncodeStorage func EncodeStorage(s Section) ([]byte, error) Serialises a Section into portable-storage binary form, including the 9-byte storage header.
DecodeStorage func DecodeStorage(data []byte) (Section, error) Deserialises portable-storage binary data, validates the storage signatures and version, and reconstructs a Section.

Value constructors

Name Signature Description
Uint64Val func Uint64Val(v uint64) Value Creates a scalar Value with TypeUint64.
Uint32Val func Uint32Val(v uint32) Value Creates a scalar Value with TypeUint32.
Uint16Val func Uint16Val(v uint16) Value Creates a scalar Value with TypeUint16.
Uint8Val func Uint8Val(v uint8) Value Creates a scalar Value with TypeUint8.
Int64Val func Int64Val(v int64) Value Creates a scalar Value with TypeInt64.
Int32Val func Int32Val(v int32) Value Creates a scalar Value with TypeInt32.
Int16Val func Int16Val(v int16) Value Creates a scalar Value with TypeInt16.
Int8Val func Int8Val(v int8) Value Creates a scalar Value with TypeInt8.
BoolVal func BoolVal(v bool) Value Creates a scalar Value with TypeBool.
DoubleVal func DoubleVal(v float64) Value Creates a scalar Value with TypeDouble.
StringVal func StringVal(v []byte) Value Creates a scalar Value with TypeString. The byte slice is stored without copying.
ObjectVal func ObjectVal(s Section) Value Creates a scalar Value with TypeObject that wraps a nested Section.
Uint64ArrayVal func Uint64ArrayVal(vs []uint64) Value Creates an array Value tagged as `ArrayFlag
Uint32ArrayVal func Uint32ArrayVal(vs []uint32) Value Creates an array Value tagged as `ArrayFlag
StringArrayVal func StringArrayVal(vs [][]byte) Value Creates an array Value tagged as `ArrayFlag
ObjectArrayVal func ObjectArrayVal(vs []Section) Value Creates an array Value tagged as `ArrayFlag

*Connection methods

Name Signature Description
WritePacket func (c *Connection) WritePacket(cmd uint32, payload []byte, expectResponse bool) error Sends a Levin request or notification with FlagRequest, ReturnOK, and the current protocol version. Header and payload writes are serialised by an internal mutex.
WriteResponse func (c *Connection) WriteResponse(cmd uint32, payload []byte, returnCode int32) error Sends a Levin response with FlagResponse and the supplied return code.
ReadPacket func (c *Connection) ReadPacket() (Header, []byte, error) Applies the read deadline, reads exactly one header and payload, validates the frame, and enforces the connection-specific MaxPayloadSize. Empty payloads are returned as nil without allocation.
Close func (c *Connection) Close() error Closes the wrapped network connection.
RemoteAddr func (c *Connection) RemoteAddr() string Returns the wrapped connection's remote address string.

Value methods

Name Signature Description
AsUint64 func (v Value) AsUint64() (uint64, error) Returns the scalar uint64 value or ErrStorageTypeMismatch.
AsUint32 func (v Value) AsUint32() (uint32, error) Returns the scalar uint32 value or ErrStorageTypeMismatch.
AsUint16 func (v Value) AsUint16() (uint16, error) Returns the scalar uint16 value or ErrStorageTypeMismatch.
AsUint8 func (v Value) AsUint8() (uint8, error) Returns the scalar uint8 value or ErrStorageTypeMismatch.
AsInt64 func (v Value) AsInt64() (int64, error) Returns the scalar int64 value or ErrStorageTypeMismatch.
AsInt32 func (v Value) AsInt32() (int32, error) Returns the scalar int32 value or ErrStorageTypeMismatch.
AsInt16 func (v Value) AsInt16() (int16, error) Returns the scalar int16 value or ErrStorageTypeMismatch.
AsInt8 func (v Value) AsInt8() (int8, error) Returns the scalar int8 value or ErrStorageTypeMismatch.
AsBool func (v Value) AsBool() (bool, error) Returns the scalar bool value or ErrStorageTypeMismatch.
AsDouble func (v Value) AsDouble() (float64, error) Returns the scalar float64 value or ErrStorageTypeMismatch.
AsString func (v Value) AsString() ([]byte, error) Returns the scalar byte-string or ErrStorageTypeMismatch.
AsSection func (v Value) AsSection() (Section, error) Returns the nested Section or ErrStorageTypeMismatch.
AsUint64Array func (v Value) AsUint64Array() ([]uint64, error) Returns the []uint64 array or ErrStorageTypeMismatch.
AsUint32Array func (v Value) AsUint32Array() ([]uint32, error) Returns the []uint32 array or ErrStorageTypeMismatch.
AsStringArray func (v Value) AsStringArray() ([][]byte, error) Returns the [][]byte array or ErrStorageTypeMismatch.
AsSectionArray func (v Value) AsSectionArray() ([]Section, error) Returns the []Section array or ErrStorageTypeMismatch.