blockchain

package module
v0.0.0-...-811092d Latest Latest
Warning

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

Go to latest
Published: May 23, 2018 License: MPL-2.0 Imports: 8 Imported by: 1

README

blockchain

Basic blockchain ledger. This library does not provide consensus and is meant to be used with an external implementation

Features

  • Input verification
  • Signature verification
  • Pluggable block verification
  • Pluggable storage interface
  • Pluggable hash function

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrPrevBlockMismatch is returned when a block references a previous
	// block that is not the actual previous block
	ErrPrevBlockMismatch = errors.New("previous block mismatch")
)

Functions

func NewGenesisBlock

func NewGenesisBlock(h hasher.Hasher) *bcpb.Block

NewGenesisBlock inits everything except the owner

Types

type BlockStorage

type BlockStorage interface {
	// Get a block by its digest id
	Get(bcpb.Digest) (*bcpb.Block, error)
	// Returns the genesis  block
	Genesis() (bcpb.Digest, *bcpb.Block)
	// Last block in the ledger
	Last() (bcpb.Digest, *bcpb.Block)
	// Get last executed
	LastExec() (bcpb.Digest, *bcpb.Block)
	// Sets the genesis block digest. It assumes the actual block is already in
	// in the store
	SetGenesis(bcpb.Digest) error
	// Set last block digest. It assumes the actual block is already in
	// in the store
	SetLast(bcpb.Digest) error
	// Sets the last executed block. It assumes the actual block is already in
	// in the store
	SetLastExec(bcpb.Digest) error
	// Returns true if the block by the given digest exists
	Exists(bcpb.Digest) bool
	// Adds a block to the ledger returning an error if it already exists
	Add(*bcpb.Block) (bcpb.Digest, error)
	// Iter iterates of each block in the ledger
	Iter(f stores.BlockIterator) error
}

BlockStorage implements a store for ledger blocks

type BlockValidator

type BlockValidator func(*bcpb.BlockHeader) error

BlockValidator is the validator function called to validate a block before signatures a verfified

type Blockchain

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

Blockchain is a blockchain instance that is able to perform all verification but does not include the consensus logic

func New

func New(conf *Config) *Blockchain

New instantiates a new blockchain. By default block validation is disabled

func (*Blockchain) Append

func (bc *Blockchain) Append(blk *bcpb.Block, txs []*bcpb.Tx) (bcpb.Digest, error)

Append appends the block and txs to the ledger. The supplied transactions must be part of the block. This does not update the last block reference or index any of the txos

func (*Blockchain) Commit

func (bc *Blockchain) Commit(id bcpb.Digest) error

Commit commits the block given by the id. It ensures it is the next in line i.e. the previous hash matches the current last block, sets the last block to the given id and indexes all transaction outputs in the block

func (*Blockchain) Genesis

func (bc *Blockchain) Genesis() *bcpb.Block

Genesis returns the genesis block or nil if the blockchain has not been initialized

func (*Blockchain) GetTXO

func (bc *Blockchain) GetTXO(txi *bcpb.TxInput) (*bcpb.TxOutput, error)

GetTXO returns the txo referenced by the TxInput. It returns an error if access is not authorized, any validation fails or is a base tx

func (*Blockchain) GetTXOByDataKey

func (bc *Blockchain) GetTXOByDataKey(key bcpb.DataKey) (*bcpb.TxOutput, error)

GetTXOByDataKey returns the TxOutput for the given key. It is the DataKey's last state

func (*Blockchain) Hasher

func (bc *Blockchain) Hasher() hasher.Hasher

Hasher returns the configured hash function used by the block chain.

func (*Blockchain) Last

func (bc *Blockchain) Last() *bcpb.Block

Last returns the last commited block in the chain

func (*Blockchain) NewTxInput

func (bc *Blockchain) NewTxInput(key bcpb.DataKey) (*bcpb.TxInput, error)

NewTxInput returns a new TxInput for the given key to use in a tx

func (*Blockchain) SetBlockValidator

func (bc *Blockchain) SetBlockValidator(bv BlockValidator)

SetBlockValidator sets the block validator function

func (*Blockchain) SetGenesis

func (bc *Blockchain) SetGenesis(genesis *bcpb.Block, txs []*bcpb.Tx) error

SetGenesis sets the genesis block and the associated transactions

func (*Blockchain) SetLastExec

func (bc *Blockchain) SetLastExec(digest bcpb.Digest) error

SetLastExec marks the given digest as the last executed block

type Config

type Config struct {
	// Hash function to use
	Hasher hasher.Hasher

	// Elliptic curve for verification
	Curve elliptic.Curve

	// These need to be specified by the user and are required
	BlockStorage BlockStorage
	TxStorage    TxStorage
	DataKeyIndex DataKeyIndex
}

Config holds the blockchain config

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a config with the default hasher and elliptic curve

type DataKeyIndex

type DataKeyIndex interface {
	Get(key bcpb.DataKey) (bcpb.Digest, int32, error)
	Set(key bcpb.DataKey, ref bcpb.Digest, idx int32) error
	Iter(prefix bcpb.DataKey, iter stores.DataKeyIterator) error
}

DataKeyIndex is an index of DataKey to the txref and output index of all unspent outputs.

type TxInputOutputValidator

type TxInputOutputValidator func(ref *bcpb.TxOutput, in *bcpb.TxInput) error

TxInputOutputValidator validates a single input. It takes the referenced output and the associated input as arguments. TxOutput <- TxInput

type TxStorage

type TxStorage interface {
	// Get a transaction
	Get(bcpb.Digest) (*bcpb.Tx, error)
	// Set a transaction
	Set(*bcpb.Tx) error
	// Set a batch of transactions
	SetBatch([]*bcpb.Tx) error
	// Iterate over all transactions
	Iter(func(bcpb.Tx) error)
}

TxStorage implements a transaction store

Directories

Path Synopsis
Package bcpb is a generated protocol buffer package.
Package bcpb is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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