serialization

package
v0.0.0-...-7ee7fde Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2020 License: Apache-2.0 Imports: 6 Imported by: 7

Documentation

Overview

Copyright (c) 2018-2020. The asimov developers Copyright (c) 2013-2017 The btcsuite developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2018-2020. The asimov developers Copyright (c) 2013-2017 The btcsuite developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Index

Constants

View Source
const (
	// MaxVarIntPayload is the maximum payload size for a variable length integer.
	MaxVarIntPayload = 9

	// MaxVarStringPayload is the maximum payload size for a variable string.
	MaxVarStringPayload = 1024
)

Variables

This section is empty.

Functions

func RandomUint64

func RandomUint64() (uint64, error)

RandomUint64 returns a cryptographically random uint64 value.

func ReadBool

func ReadBool(r io.Reader, e *bool) error

ReadVarInt reads a variable length integer from r and returns it as a bool.

func ReadNBytes

func ReadNBytes(r io.Reader, buf []byte, n int) error

ReadVarInt reads a variable length byte from r and returns it as a byte slice.

func ReadUint16

func ReadUint16(r io.Reader, e *uint16) error

ReadVarInt reads a variable length integer from r with littleEndian and returns it as a uint16.

func ReadUint32

func ReadUint32(r io.Reader, e *uint32) error

ReadVarInt reads a variable length integer from r with littleEndian and returns it as a uint32.

func ReadUint32B

func ReadUint32B(r io.Reader, e *uint32) error

ReadVarInt reads a variable length integer from r with bigEndian and returns it as a uint32.

func ReadUint64

func ReadUint64(r io.Reader, e *uint64) error

ReadVarInt reads a variable length integer from r with littleEndian and returns it as a uint64.

func ReadUint64B

func ReadUint64B(r io.Reader, e *uint64) error

ReadVarInt reads a variable length integer from r with bigEndian and returns it as a uint64.

func ReadUint8

func ReadUint8(r io.Reader, e *uint8) error

ReadVarInt reads a variable length integer from r and returns it as a uint8.

func ReadVarBytes

func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
	fieldName string) ([]byte, error)

serialization.ReadVarBytes reads a variable length byte array. A byte array is encoded as a varInt containing the length of the array followed by the bytes themselves. An error is returned if the length is greater than the passed maxAllowed parameter which helps protect against memory exhaustion attacks and forced panics through malformed messages. The fieldName parameter is only used for the error message so it provides more context in the error.

func ReadVarInt

func ReadVarInt(r io.Reader, pver uint32) (uint64, error)

ReadVarInt reads a variable length integer from r and returns it as a uint64.

func ReadVarString

func ReadVarString(r io.Reader, pver uint32) (string, error)

ReadVarString reads a variable length string from r and returns it as a Go string. A variable length string is encoded as a variable length integer containing the length of the string followed by the bytes that represent the string itself. An error is returned if the length is greater than the maximum block payload size since it helps protect against memory exhaustion attacks and forced panics through malformed messages.

func ReadVarUint

func ReadVarUint(r io.Reader) (uint64, error)

func VarIntSerializeSize

func VarIntSerializeSize(val uint64) int

VarIntSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.

func WriteBool

func WriteBool(w io.Writer, e bool) error

func WriteNBytes

func WriteNBytes(w io.Writer, bytes []byte) error

func WriteUint16

func WriteUint16(w io.Writer, e uint16) error

func WriteUint32

func WriteUint32(w io.Writer, e uint32) error

func WriteUint32B

func WriteUint32B(w io.Writer, e uint32) error

func WriteUint64

func WriteUint64(w io.Writer, e uint64) error

func WriteUint64B

func WriteUint64B(w io.Writer, e uint64) error

func WriteUint8

func WriteUint8(w io.Writer, e uint8) error

func WriteVarBytes

func WriteVarBytes(w io.Writer, pver uint32, bytes []byte) error

WriteVarBytes serializes a variable length byte array to w as a varInt containing the number of bytes, followed by the bytes themselves.

func WriteVarInt

func WriteVarInt(w io.Writer, pver uint32, val uint64) error

WriteVarInt serializes val to w using a variable number of bytes depending on its value.

func WriteVarString

func WriteVarString(w io.Writer, pver uint32, str string) error

WriteVarString serializes str to w as a variable length integer containing the length of the string followed by the bytes that represent the string itself.

func WriteVarUint

func WriteVarUint(w io.Writer, val uint64) error

Types

type MessageError

type MessageError struct {
	Func        string // Function name
	Type        string // Type
	Description string // Human readable description of the issue
}

MessageError describes an issue with a message. An example of some potential issues are messages from the wrong bitcoin network, invalid commands, mismatched checksums, and exceeding max payloads.

This provides a mechanism for the caller to type assert the error to differentiate between general io errors such as io.EOF and issues that resulted from malformed messages.

func (*MessageError) Error

func (e *MessageError) Error() string

Error satisfies the error interface and prints human-readable errors.

type SerializableData

type SerializableData interface {
	// Write data to writer
	Serialize(w io.Writer) error

	// read data to reader
	Deserialize(r io.Reader) error
}

SerializableData describe the data need be serialized.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL