go-p2p/specs/node-levin.md
Virgil 711a43aa3f refactor(node): remove AX compatibility aliases
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 13:26:00 +00:00

7.7 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 Uint64Value, StringValue, and ObjectArrayValue 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
Uint64Value func Uint64Value(v uint64) Value Creates a scalar Value with TypeUint64.
Uint32Value func Uint32Value(v uint32) Value Creates a scalar Value with TypeUint32.
Uint16Value func Uint16Value(v uint16) Value Creates a scalar Value with TypeUint16.
Uint8Value func Uint8Value(v uint8) Value Creates a scalar Value with TypeUint8.
Int64Value func Int64Value(v int64) Value Creates a scalar Value with TypeInt64.
Int32Value func Int32Value(v int32) Value Creates a scalar Value with TypeInt32.
Int16Value func Int16Value(v int16) Value Creates a scalar Value with TypeInt16.
Int8Value func Int8Value(v int8) Value Creates a scalar Value with TypeInt8.
BoolValue func BoolValue(v bool) Value Creates a scalar Value with TypeBool.
DoubleValue func DoubleValue(v float64) Value Creates a scalar Value with TypeDouble.
StringValue func StringValue(v []byte) Value Creates a scalar Value with TypeString. The byte slice is stored without copying.
ObjectValue func ObjectValue(s Section) Value Creates a scalar Value with TypeObject that wraps a nested Section.
Uint64ArrayValue func Uint64ArrayValue(vs []uint64) Value Creates an array Value tagged as `ArrayFlag
Uint32ArrayValue func Uint32ArrayValue(vs []uint32) Value Creates an array Value tagged as `ArrayFlag
StringArrayValue func StringArrayValue(vs [][]byte) Value Creates an array Value tagged as `ArrayFlag
ObjectArrayValue func ObjectArrayValue(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.