Help us shape the future of Go! Take the 2026 Go Developer Survey

fastrlp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2022 License: Apache-2.0 Imports: 9 Imported by: 477

README

FastRlp

FastRlp is a high performant encoding/decoding library for the RLP Ethereum format. This library is based on fastjson.

Usage

FastRlp does not uses reflect to avoid bottlenecks. It provides a single value primitive that can be encoded or decoded into any specific type.

Encode:

a := &fastrlp.Arena{}

// Encode a uint
v := a.NewUint(300)
buf := v.MarshalTo(nil)

// Encode an array
v = a.NewArray()
v.Set(a.NewUint(300))
buf = v.MarshalTo(nil)

You can find more examples here.

Decode:

p := &fastrlp.Parser{}
v, err := p.Parse([]byte{0x01})
if err != nil {
    panic(err)
}

num, err := v.GetUint64()
if err != nil {
    panic(err)
}
fmt.Println(num)

Benchmark

$ go-rlp-test go test -v ./. -run=XX -bench=.     
goos: linux
goarch: amd64
pkg: github.com/ferranbt/go-rlp-test
BenchmarkDecode100HeadersGeth-8      	   10000	    196183 ns/op	   32638 B/op	    1002 allocs/op
BenchmarkEncode100HeadersGeth-8      	   10000	    179328 ns/op	   88471 B/op	    1003 allocs/op
BenchmarkDecode100HeadersFastRlp-8   	   30000	     57179 ns/op	      16 B/op	       0 allocs/op
BenchmarkEncode100HeadersFastRlp-8   	   30000	     43967 ns/op	      23 B/op	       0 allocs/op
PASS
ok  	github.com/ferranbt/go-rlp-test	7.890s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fuzz

func Fuzz(num int, base FuzzObject, opts ...FuzzOption) error

func MarshalRLP

func MarshalRLP(m Marshaler) ([]byte, error)

MarshalRLP marshals an RLP object

func UnmarshalRLP

func UnmarshalRLP(buf []byte, m Unmarshaler) error

UnmarshalRLP unmarshals an RLP object

Types

type Arena

type Arena struct {
	// contains filtered or unexported fields
}

Arena is a pool of RLP values.

func (*Arena) NewArray

func (a *Arena) NewArray() *Value

NewArray returns a new array value.

func (*Arena) NewBigInt

func (a *Arena) NewBigInt(b *big.Int) *Value

NewBigInt returns a new big.int value.

func (*Arena) NewBool

func (a *Arena) NewBool(b bool) *Value

NewBool returns a new bool value.

func (*Arena) NewBytes

func (a *Arena) NewBytes(b []byte) *Value

NewBytes returns a bytes value.

func (*Arena) NewCopyBytes

func (a *Arena) NewCopyBytes(b []byte) *Value

NewCopyBytes returns a bytes value that copies the input.

func (*Arena) NewFalse

func (a *Arena) NewFalse() *Value

NewFalse returns a false value.

func (*Arena) NewNull

func (a *Arena) NewNull() *Value

NewNull returns a new null value.

func (*Arena) NewNullArray

func (a *Arena) NewNullArray() *Value

NewNullArray returns a null array value.

func (*Arena) NewString

func (a *Arena) NewString(s string) *Value

NewString returns a new string value.

func (*Arena) NewTrue

func (a *Arena) NewTrue() *Value

NewTrue returns a true value.

func (*Arena) NewUint

func (a *Arena) NewUint(i uint64) *Value

NewUint returns a new uint value.

func (*Arena) Reset

func (a *Arena) Reset()

Reset resets the values allocated in the arena.

type ArenaPool

type ArenaPool struct {
	// contains filtered or unexported fields
}

ArenaPool may be used for pooling Arenas for similarly typed RLPs.

var DefaultArenaPool ArenaPool

DefaultArenaPool is a default ArenaPool

func (*ArenaPool) Get

func (ap *ArenaPool) Get() *Arena

Get acquires an Arena from the pool.

func (*ArenaPool) Put

func (ap *ArenaPool) Put(a *Arena)

Put releases an Arena to the pool.

type FuzzError

type FuzzError struct {
	Source, Target interface{}
}

func (*FuzzError) Error

func (f *FuzzError) Error() string

type FuzzObject

type FuzzObject interface {
	Marshaler
	Unmarshaler
}

type FuzzOption

type FuzzOption func(f *Fuzzer)

func WithDefaults

func WithDefaults(fnc func(FuzzObject)) FuzzOption

func WithPostHook

func WithPostHook(fnc func(FuzzObject) error) FuzzOption

type Fuzzer

type Fuzzer struct {
	*fuzz.Fuzzer
	// contains filtered or unexported fields
}

type Keccak

type Keccak struct {
	// contains filtered or unexported fields
}

Keccak is the sha256 keccak hash

func NewKeccak256

func NewKeccak256() *Keccak

func (*Keccak) Read

func (k *Keccak) Read() []byte

Read hashes the content and returns the intermediate buffer.

func (*Keccak) Reset

func (k *Keccak) Reset()

Reset implements the hash interface

func (*Keccak) Sum

func (k *Keccak) Sum(dst []byte) []byte

Sum implements the hash interface

func (*Keccak) Write

func (k *Keccak) Write(b []byte) (int, error)

Write implements the hash interface

type Marshaler

type Marshaler interface {
	MarshalRLPTo(dst []byte) ([]byte, error)
	MarshalRLPWith(a *Arena) (*Value, error)
}

Marshaler is the interface implemented by types that can marshal themselves into valid RLP messages.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is a RLP parser

func (*Parser) Hash

func (p *Parser) Hash(dst []byte, v *Value) []byte

Hash performs a keccak hash of the rlp value

func (*Parser) Parse

func (p *Parser) Parse(b []byte) (*Value, error)

Parse parses a complete rlp encoding

func (*Parser) Raw

func (p *Parser) Raw(v *Value) []byte

Raw returns the raw bytes of the value

type ParserPool

type ParserPool struct {
	// contains filtered or unexported fields
}

ParserPool may be used for pooling Parsers for similarly typed RLPs.

var DefaultParserPool ParserPool

DefaultParserPool is a default ParserPool

func (*ParserPool) Get

func (pp *ParserPool) Get() *Parser

Get acquires a Parser from the pool.

func (*ParserPool) Put

func (pp *ParserPool) Put(p *Parser)

Put releases the parser to the pool.

type Type

type Type int

Type represents an RLP type.

const (
	// TypeArray is an RLP array value.
	TypeArray Type = iota

	// TypeBytes is an RLP bytes value.
	TypeBytes

	// TypeNull is an RLP bytes null (0x80)
	TypeNull

	// TypeArrayNull is an RLP array null (0xC0)
	TypeArrayNull
)

func (Type) String

func (t Type) String() string

String returns the string representation of the type.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalRLP(buf []byte) error
	UnmarshalRLPWith(v *Value) error
}

Unmarshaler is the interface implemented by types that can unmarshal a RLP description of themselves

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is an RLP value

func (*Value) Bytes

func (v *Value) Bytes() ([]byte, error)

Bytes returns the raw bytes.

func (*Value) Elems

func (v *Value) Elems() int

Elems returns the number of elements if its an array

func (*Value) Get

func (v *Value) Get(i int) *Value

Get returns the item at index i in the array

func (*Value) GetAddr

func (v *Value) GetAddr(buf []byte) error

GetAddr returns bytes of size 20.

func (*Value) GetBigInt

func (v *Value) GetBigInt(b *big.Int) error

GetBigInt returns big.int value.

func (*Value) GetBool

func (v *Value) GetBool() (bool, error)

GetBool returns bool value.

func (*Value) GetByte

func (v *Value) GetByte() (byte, error)

GetByte returns a byte

func (*Value) GetBytes

func (v *Value) GetBytes(dst []byte, bits ...int) ([]byte, error)

GetBytes returns bytes to dst.

func (*Value) GetElems

func (v *Value) GetElems() ([]*Value, error)

GetElems returns the elements of an array.

func (*Value) GetHash

func (v *Value) GetHash(buf []byte) error

GetHash returns bytes of size 32.

func (*Value) GetString

func (v *Value) GetString() (string, error)

GetString returns string value.

func (*Value) GetUint64

func (v *Value) GetUint64() (uint64, error)

GetUint64 returns uint64.

func (*Value) Len

func (v *Value) Len() uint64

Len returns the raw size of the value

func (*Value) MarshalTo

func (v *Value) MarshalTo(dst []byte) []byte

MarshalTo appends marshaled v to dst and returns the result.

func (*Value) Raw

func (v *Value) Raw() []byte

Raw returns the raw bytes

func (*Value) Set

func (v *Value) Set(vv *Value)

Set sets a value in the array

func (*Value) Type

func (v *Value) Type() Type

Type returns the type of the value

Jump to

Keyboard shortcuts

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