blockstore

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: BSD-3-Clause Imports: 14 Imported by: 5

Documentation

Overview

Package blockstore defines the different storage the ordering service is using.

The block store defines the primitives to store a block and read one from the disk. It also provide an API to read a chain from the genesis block to the latest block. It is important to notice that a block is stored alongside the link that has been created during the consensus.

The tree cache stores the latest state of the tree, which is modified after each new block.

The genesis store allows to set a definitive genesis block and persist it so that it can be reloaded later on.

Documentation Last Review: 13.10.2020

Index

Constants

This section is empty.

Variables

View Source
var ErrNoBlock = errors.New("no block")

ErrNoBlock is the error message returned when the block is unknown.

Functions

This section is empty.

Types

type BlockStore

type BlockStore interface {
	// Len must return the length of the store.
	Len() uint64

	// Store must store the block link only if it matches the latest link,
	// otherwise it must return an error.
	Store(types.BlockLink) error

	// Get must return the block link associated to the digest, or an error.
	Get(id types.Digest) (types.BlockLink, error)

	// GetByIndex return the block link associated to the index, or an error.
	GetByIndex(index uint64) (types.BlockLink, error)

	// GetChain returns a chain of the blocks. It can be used to prove the
	// integrity of the last block from the genesis.
	GetChain() (types.Chain, error)

	// Last must return the latest block link in the store.
	Last() (types.BlockLink, error)

	// Watch returns a channel that is filled with new block links. The is
	// closed as soon as the context is done.
	Watch(context.Context) <-chan types.BlockLink

	// WithTx returns a block store that is using the transaction to perform
	// operations on the database.
	WithTx(store.Transaction) BlockStore
}

BlockStore is the interface to store and get blocks.

type GenesisStore

type GenesisStore interface {
	// Get must return the genesis block if it is set, otherwise an error.
	Get() (types.Genesis, error)

	// Set must set the genesis block.
	Set(types.Genesis) error

	// Exists returns true if the genesis is already set.
	Exists() bool
}

GenesisStore is the interface to store and get the genesis block. It is left to the implementation to persist it.

func NewGenesisStore

func NewGenesisStore() GenesisStore

NewGenesisStore returns a new empty genesis store.

type InDisk

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

InDisk is a persistent storage implementation for the blocks.

- implements blockstore.BlockStore

func NewDiskStore

func NewDiskStore(db kv.DB, fac types.LinkFactory) *InDisk

NewDiskStore creates a new persistent storage.

func (*InDisk) Get

func (s *InDisk) Get(id types.Digest) (types.BlockLink, error)

Get implements blockstore.BlockStore. It loads the block with the given identifier if it exists, otherwise it returns an error.

func (*InDisk) GetByIndex

func (s *InDisk) GetByIndex(index uint64) (link types.BlockLink, err error)

GetByIndex implements blockstore.BlockStore. It returns the block associated to the index if it exists, otherwise it returns an error.

func (*InDisk) GetChain

func (s *InDisk) GetChain() (types.Chain, error)

GetChain implements blockstore.Blockstore. It returns a chain to the latest block.

func (*InDisk) Last

func (s *InDisk) Last() (types.BlockLink, error)

Last implements blockstore.BlockStore. It returns the last block stored in the database.

func (*InDisk) Len

func (s *InDisk) Len() uint64

Len implements blockstore.BlockStore. It returns the number of blocks stored in the database.

func (*InDisk) Load

func (s *InDisk) Load() error

Load reads the database to rebuild the cache.

func (*InDisk) Store

func (s *InDisk) Store(link types.BlockLink) error

Store implements blockstore.BlockStore. It stores the link in the database if it matches the latest link.

func (*InDisk) Watch

func (s *InDisk) Watch(ctx context.Context) <-chan types.BlockLink

Watch implements blockstore.BlockStore. It returns a channel populated with new blocks stored.

func (*InDisk) WithTx

func (s *InDisk) WithTx(txn store.Transaction) BlockStore

WithTx implements blockstore.BlockStore. It returns a store that will use the transaction for the operations on the database.

type InMemory

type InMemory struct {
	sync.Mutex
	// contains filtered or unexported fields
}

InMemory is a block store that only stores the block in-memory which means they won't persist.

- implements blockstore.BlockStore

func NewInMemory

func NewInMemory() *InMemory

NewInMemory returns a new empty in-memory block store.

func (*InMemory) Get

func (s *InMemory) Get(id types.Digest) (types.BlockLink, error)

Get implements blockstore.BlockStore. It returns the block link associated to the digest if it exists, otherwise it returns an error.

func (*InMemory) GetByIndex

func (s *InMemory) GetByIndex(index uint64) (types.BlockLink, error)

GetByIndex implements blockstore.BlockStore. It returns the block associated to the index if it exists.

func (*InMemory) GetChain

func (s *InMemory) GetChain() (types.Chain, error)

GetChain implements blockstore.BlockStore. It returns the chain to the latest block.

func (*InMemory) Last

func (s *InMemory) Last() (types.BlockLink, error)

Last implements blockstore.BlockStore. It returns the latest block of the store.

func (*InMemory) Len

func (s *InMemory) Len() uint64

Len implements blockstore.BlockStore. It returns the length of the store.

func (*InMemory) Store

func (s *InMemory) Store(link types.BlockLink) error

Store implements blockstore.BlockStore. It stores the block only if the link matches the latest block.

func (*InMemory) Watch

func (s *InMemory) Watch(ctx context.Context) <-chan types.BlockLink

Watch implements blockstore.BlockStore. It returns a channel populated with new blocks.

func (*InMemory) WithTx

func (s *InMemory) WithTx(txn store.Transaction) BlockStore

WithTx implements blockstore.BlockStore. It returns a new store that will apply the list of blocks at the end of the transaction.

type PersistentGenesisCache

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

PersistentGenesisCache is a store to set a genesis block on disk. It also provides a function to load the block from the disk that will then stay in memory.

- implements blockstore.GenesisStore

func NewGenesisDiskStore

func NewGenesisDiskStore(db kv.DB, fac serde.Factory) PersistentGenesisCache

NewGenesisDiskStore creates a new store that will load or set the genesis block using the given database.

func (PersistentGenesisCache) Exists

func (s PersistentGenesisCache) Exists() bool

Exists implements blockstore.GenesisStore. It returns true if the genesis block is set.

func (PersistentGenesisCache) Get

func (s PersistentGenesisCache) Get() (types.Genesis, error)

Get implements blockstore.GenesisStore. It returns the genesis block if it is set, otherwise it returns an error.

func (PersistentGenesisCache) Load

func (s PersistentGenesisCache) Load() error

Load tries to read the genesis block in the database, and set it to memory only if it exists. It returns an error if the database cannot be read, or the value is malformed.

func (PersistentGenesisCache) Set

func (s PersistentGenesisCache) Set(genesis types.Genesis) error

Set implements blockstore.GenesisStore. It writes the genesis block in disk, and then in memory for fast access.

type TreeCache

type TreeCache interface {
	// Get returns the current value of the cache.
	Get() hashtree.Tree

	// GetWithLock implements blockstore.TreeCache. It returns the current value
	// of the cache alongside a function to unlock the cache. It allows one to
	// delay a set while fetching associated data. The function returned must be
	// called.
	GetWithLock() (tree hashtree.Tree, unlock func())

	// Set sets a new tree in the cache.
	Set(hashtree.Tree)

	// SetWithLock implements blockstore.TreeCache. It sets the tree while
	// holding the lock and returns a function to unlock it. It allows one to
	// prevent an access until associated data is updated. The function returned
	// must be called.
	SetWithLock(hashtree.Tree) (unlock func())
}

TreeCache is a cache to store a tree that needs to be accessed in different places.

func NewTreeCache

func NewTreeCache(tree hashtree.Tree) TreeCache

NewTreeCache creates a new cache with the given tree as the first value.

Jump to

Keyboard shortcuts

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