chain

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2019 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBlockNotFound is returned when looking for a block that is not in the chain.
	ErrBlockNotFound = errors.New("block not found in the chain")

	// ErrBlockNumberIncorrect is returned when adding a block with a bad number.
	ErrBlockNumberIncorrect = errors.New("block number does not correspond to hash")

	// ErrInvalidPreviousBlock is returned when adding a block with a bad number or previous hash.
	ErrInvalidPreviousBlock = errors.New("link to previous block is invalid")
)
View Source
var OptGenesisBlock = func(genesis *pb.Block) Opt {
	if genesis.BlockNumber() != 0 {
		panic("Genesis block should have number 0")
	}

	return func(c *chainDB) {
		if err := c.AddBlock(genesis); err != nil {
			panic(err)
		}

		if err := c.SetHead(genesis); err != nil {
			panic(err)
		}
	}
}

OptGenesisBlock sets the genesis block for the chain.

View Source
var OptPrefix = func(prefix []byte) Opt {
	return func(c *chainDB) {
		c.prefix = prefix
	}
}

OptPrefix sets a prefix for all the database keys.

Functions

func GetPath

func GetPath(c Reader, from *pb.Block, to *pb.Block) (rollbacks []*pb.Block, replays []*pb.Block, err error)

GetPath returns the path from current header to a given block (excluding that block).

Types

type Chain

type Chain interface {
	Reader
	Writer
}

Chain defines methods to interact with the local blockchain.

func NewChainDB

func NewChainDB(db db.DB, opts ...Opt) Chain

NewChainDB returns a new blockchain using a given DB instance.

type Config

type Config struct {
}

Config describes the blockchain's chain configuration.

type Opt

type Opt func(*chainDB)

Opt is an option for Chain

type Reader

type Reader interface {
	// Config retrieves the blockchain's chain configuration.
	Config() *Config

	// CurrentHeader retrieves the current header from the local chain.
	CurrentHeader() (*pb.Header, error)

	// GetHeadersByNumber retrieves block headers from the database by number.
	// In case of forks there might be multiple headers with the same number,
	GetHeadersByNumber(number uint64) ([]*pb.Header, error)

	// GetHeaderByNumber retrieves a header from the main branch by number.
	GetHeaderByNumber(number uint64) (*pb.Header, error)

	// GetHeaderByHash retrieves a block header from the database by its hash.
	GetHeaderByHash(hash []byte) (*pb.Header, error)

	// CurrentBlock retrieves the current block from the local chain.
	CurrentBlock() (*pb.Block, error)

	// GetBlock retrieves a block from the database by header hash and number.
	GetBlock(hash []byte, number uint64) (*pb.Block, error)

	// GetBlockByHash retrieves a block from the database by header hash.
	GetBlockByHash(hash []byte) (*pb.Block, error)

	// GetBlockByNumber retrieves a block from the main branch by number.
	GetBlockByNumber(number uint64) (*pb.Block, error)

	// GetParentBlock retrieves the header's parent block.
	GetParentBlock(header *pb.Header) (*pb.Block, error)
}

Reader defines a small collection of methods needed to access the local blockchain.

type Writer

type Writer interface {
	// AddBlock adds a block to the chain.
	// It assumes that the block has been validated.
	AddBlock(block *pb.Block) error

	// SetHead sets the head of the chain.
	SetHead(block *pb.Block) error
}

Writer defines methods needed to write to the local blockchain.

Directories

Path Synopsis
Package mockchain is a generated GoMock package.
Package mockchain is a generated GoMock package.

Jump to

Keyboard shortcuts

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