postage

package
v1.4.1-rc5 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2021 License: BSD-3-Clause Imports: 12 Imported by: 21

Documentation

Index

Constants

View Source
const (
	StampSize   = 113
	IndexSize   = 8
	BucketDepth = 16
)

StampSize is the number of bytes in the serialisation of a stamp

Variables

View Source
var (
	// ErrNotFound is the error returned when issuer with given batch ID does not exist.
	ErrNotFound = errors.New("not found")
	// ErrNotUsable is the error returned when issuer with given batch ID is not usable.
	ErrNotUsable = errors.New("not usable")
)
View Source
var (
	// ErrOwnerMismatch is the error given for invalid signatures.
	ErrOwnerMismatch = errors.New("owner mismatch")
	// ErrInvalidIndex the error given for invalid stamp index.
	ErrInvalidIndex = errors.New("invalid index")
	// ErrStampInvalid is the error given if stamp cannot deserialise.
	ErrStampInvalid = errors.New("invalid stamp")
	// ErrBucketMismatch is the error given if stamp index bucket verification fails.
	ErrBucketMismatch = errors.New("bucket mismatch")
)
View Source
var (
	// ErrBucketFull is the error when a collision bucket is full.
	ErrBucketFull = errors.New("bucket full")
)

Functions

This section is empty.

Types

type Batch

type Batch struct {
	ID          []byte   // batch ID
	Value       *big.Int // normalised balance of the batch
	Start       uint64   // block number the batch was created
	Owner       []byte   // owner's ethereum address
	Depth       uint8    // batch depth, i.e., size = 2^{depth}
	BucketDepth uint8    // the depth of neighbourhoods t
	Immutable   bool     // if the batch allows adding new capacity (dilution)
	Radius      uint8    // reserve radius, non-serialised
}

Batch represents a postage batch, a payment on the blockchain.

func (*Batch) MarshalBinary

func (b *Batch) MarshalBinary() ([]byte, error)

MarshalBinary implements BinaryMarshaller. It will attempt to serialize the postage batch to a byte slice. serialised as ID(32)|big endian value(32)|start block(8)|owner addr(20)|BucketDepth(1)|depth(1)|immutable(1)

func (*Batch) UnmarshalBinary

func (b *Batch) UnmarshalBinary(buf []byte) error

UnmarshalBinary implements BinaryUnmarshaller. It will attempt deserialize the given byte slice into the batch.

type BatchEventListener added in v1.2.0

type BatchEventListener interface {
	HandleCreate(*Batch) error
	HandleTopUp(id []byte, newBalance *big.Int)
	HandleDepthIncrease(id []byte, newDepth uint8, normalisedBalance *big.Int)
}

type ChainState

type ChainState struct {
	Block        uint64   // The block number of the last postage event.
	TotalAmount  *big.Int // Cumulative amount paid per stamp.
	CurrentPrice *big.Int // Bzz/chunk/block normalised price.
}

ChainState contains data the batch service reads from the chain.

type EventUpdater

type EventUpdater interface {
	Create(id []byte, owner []byte, normalisedBalance *big.Int, depth, bucketDepth uint8, immutable bool, txHash []byte) error
	TopUp(id []byte, normalisedBalance *big.Int, txHash []byte) error
	UpdateDepth(id []byte, depth uint8, normalisedBalance *big.Int, txHash []byte) error
	UpdatePrice(price *big.Int, txHash []byte) error
	UpdateBlockNumber(blockNumber uint64) error
	Start(startBlock uint64) (<-chan struct{}, error)

	TransactionStart() error
	TransactionEnd() error
}

EventUpdater interface definitions reflect the updates triggered by events emitted by the postage contract on the blockchain.

type Listener

type Listener interface {
	io.Closer
	Listen(from uint64, updater EventUpdater) <-chan struct{}
}

Listener provides a blockchain event iterator.

type RadiusSetter

type RadiusSetter interface {
	SetRadius(uint8)
}

type ReserveState

type ReserveState struct {
	Radius        uint8
	StorageRadius uint8
	Available     int64
	Outer         *big.Int // lower value limit for outer layer = the further half of chunks
	Inner         *big.Int
}

type Service

type Service interface {
	Add(*StampIssuer) error
	StampIssuers() []*StampIssuer
	GetStampIssuer([]byte) (*StampIssuer, error)
	IssuerUsable(*StampIssuer) bool
	BatchEventListener
	io.Closer
}

Service is the postage service interface.

func NewService

func NewService(store storage.StateStorer, postageStore Storer, chainID int64) (Service, error)

NewService constructs a new Service.

type Stamp

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

Stamp represents a postage stamp as attached to a chunk.

func NewStamp

func NewStamp(batchID, index, timestamp, sig []byte) *Stamp

NewStamp constructs a new stamp from a given batch ID, index and signatures.

func (*Stamp) BatchID

func (s *Stamp) BatchID() []byte

BatchID returns the batch ID of the stamp.

func (*Stamp) Index added in v1.0.0

func (s *Stamp) Index() []byte

Index returns the within-batch index of the stamp.

func (*Stamp) MarshalBinary

func (s *Stamp) MarshalBinary() ([]byte, error)

MarshalBinary gives the byte slice serialisation of a stamp: batchID[32]|index[8]|timestamp[8]|Signature[65].

func (*Stamp) Sig

func (s *Stamp) Sig() []byte

Sig returns the signature of the stamp by the user

func (*Stamp) Timestamp added in v1.0.0

func (s *Stamp) Timestamp() []byte

Timestamp returns the timestamp of the stamp

func (*Stamp) UnmarshalBinary

func (s *Stamp) UnmarshalBinary(buf []byte) error

UnmarshalBinary parses a serialised stamp into id and signature.

func (*Stamp) Valid

func (s *Stamp) Valid(chunkAddr swarm.Address, ownerAddr []byte, depth, bucketDepth uint8, immutable bool) error

Valid checks the validity of the postage stamp; in particular: - authenticity - check batch is valid on the blockchain - authorisation - the batch owner is the stamp signer the validity check is only meaningful in its association of a chunk this chunk address needs to be given as argument

type StampIssuer

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

StampIssuer is a local extension of a batch issuing stamps for uploads. A StampIssuer instance extends a batch with bucket collision tracking embedded in multiple Stampers, can be used concurrently.

func NewStampIssuer

func NewStampIssuer(label, keyID string, batchID []byte, batchAmount *big.Int, batchDepth, bucketDepth uint8, blockNumber uint64, immutableFlag bool) *StampIssuer

NewStampIssuer constructs a StampIssuer as an extension of a batch for local upload.

BucketDepth must always be smaller than batchDepth otherwise inc() panics.

func (*StampIssuer) Amount added in v1.0.0

func (si *StampIssuer) Amount() *big.Int

Amount represent issued batch amount paid.

func (*StampIssuer) BlockNumber added in v1.0.0

func (si *StampIssuer) BlockNumber() uint64

BlockNumber when this batch was created.

func (*StampIssuer) BucketDepth added in v1.0.0

func (si *StampIssuer) BucketDepth() uint8

BucketDepth the depth of collision Buckets uniformity.

func (*StampIssuer) BucketUpperBound added in v1.1.0

func (si *StampIssuer) BucketUpperBound() uint32

BucketUpperBound returns the maximum number of collisions possible in a bucket given the batch's depth and bucket depth.

func (*StampIssuer) Buckets added in v1.1.0

func (si *StampIssuer) Buckets() []uint32

func (*StampIssuer) Depth added in v1.0.0

func (si *StampIssuer) Depth() uint8

Depth represent issued batch depth.

func (*StampIssuer) ID

func (si *StampIssuer) ID() []byte

ID returns the BatchID for this batch.

func (*StampIssuer) ImmutableFlag added in v1.0.0

func (si *StampIssuer) ImmutableFlag() bool

ImmutableFlag immutability of the created batch.

func (*StampIssuer) Label

func (si *StampIssuer) Label() string

Label returns the label of the issuer.

func (*StampIssuer) MarshalBinary

func (si *StampIssuer) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (*StampIssuer) UnmarshalBinary

func (si *StampIssuer) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*StampIssuer) Utilization

func (si *StampIssuer) Utilization() uint32

Utilization returns the batch utilization in the form of an integer between 0 and 4294967295. Batch fullness can be calculated with: max_bucket_value / 2 ^ (batch_depth - bucket_depth)

type Stamper

type Stamper interface {
	Stamp(swarm.Address) (*Stamp, error)
}

Stamper can issue stamps from the given address.

func NewStamper

func NewStamper(st *StampIssuer, signer crypto.Signer) Stamper

NewStamper constructs a Stamper.

type Storer

type Storer interface {
	Get(id []byte) (*Batch, error)
	Put(*Batch, *big.Int, uint8) error
	GetChainState() *ChainState
	PutChainState(*ChainState) error
	GetReserveState() *ReserveState
	SetRadiusSetter(RadiusSetter)
	Unreserve(UnreserveIteratorFn) error
	Exists(id []byte) (bool, error)

	Reset() error
}

Storer represents the persistence layer for batches on the current (highest available) block.

type UnreserveIteratorFn added in v1.0.0

type UnreserveIteratorFn func(id []byte, radius uint8) (bool, error)

type ValidStampFn added in v1.1.0

type ValidStampFn func(chunk swarm.Chunk, stampBytes []byte) (swarm.Chunk, error)

func ValidStamp

func ValidStamp(batchStore Storer) ValidStampFn

ValidStamp returns a stampvalidator function passed to protocols with chunk entrypoints.

Directories

Path Synopsis
Package batchstore implements the reserve the reserve serves to maintain chunks in the area of responsibility it has two components - the batchstore reserve which maintains information about batches, their values, priorities and synchronises with the blockchain - the localstore which stores chunks and manages garbage collection when a new chunk arrives in the localstore, the batchstore reserve is asked to check the batch used in the postage stamp attached to the chunk.
Package batchstore implements the reserve the reserve serves to maintain chunks in the area of responsibility it has two components - the batchstore reserve which maintains information about batches, their values, priorities and synchronises with the blockchain - the localstore which stores chunks and manages garbage collection when a new chunk arrives in the localstore, the batchstore reserve is asked to check the batch used in the postage stamp attached to the chunk.

Jump to

Keyboard shortcuts

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