chain

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2020 License: MIT Imports: 26 Imported by: 0

README

Chain

Responsibility

  • Given a block, verify whether the block is valid according to the consensus rules
  • Given a transaction, verify whether a transaction is valid

API

  • AcceptBlock(block) Verifies and saves a block
  • VerifyBlock(block) - Verifies a block is valid, including the transactions
  • VerifyTX(tx) - Verifies a tx is valid. For the mempool

Usage

  • AcceptBlock will be used by all nodes, when they recieve a new proposed block, that should be added to the chain

  • VerifyBlock will be used by consensus nodes, to verify that a block is valid without saving it.

  • VerifyTX will be used by the mempool to Verify a TX is valid and can be added to the mempool.

Consensus Rules
  • No double spending, check with utxo database
  • time difference between the last block should not be more than 60 minutes
  • Current blockheader, should reference the previous block header
  • Seed, public key, etc in block should be X Bytes
  • Zkproof should be valid in block
  • Certificates in the block of provisioners, should be valid.
  • Timestamp of previous block should be less than current block
Specification
  • Chain is the only process with a RW copy to the database

Documentation

Index

Constants

View Source
const (
	// SanityCheckHeight is the suggested amount of blocks to check when
	// calling Loader.PerformSanityCheck
	SanityCheckHeight uint64 = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Chain

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

Chain represents the nodes blockchain This struct will be aware of the current state of the node.

func New

func New(eventBus *eventbus.EventBus, rpcBus *rpcbus.RPCBus, counter *chainsync.Counter, loader Loader, verifier Verifier) (*Chain, error)

New returns a new chain object. It accepts the EventBus (for messages coming from (remote) consensus components, the RPCBus for dispatching synchronous data related to Certificates, Blocks, Rounds and progress. It also accepts a counter to manage the synchronization process and the hash of the genesis block TODO: the counter should be encapsulated in a specific component for synchronization

func (*Chain) AcceptBlock

func (c *Chain) AcceptBlock(blk block.Block) error

AcceptBlock will accept a block if 1. We have not seen it before 2. All stateless and statefull checks are true Returns nil, if checks passed and block was successfully saved

func (*Chain) Listen

func (c *Chain) Listen()

Listen to the collectors

type DBLoader added in v0.3.0

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

DBLoader performs database prefetching and sanityChecks at node startup

func NewDBLoader added in v0.3.0

func NewDBLoader(db database.DB, genesis *block.Block) *DBLoader

NewDBLoader returns a Loader which gets the Chain Tip from the DB

func (*DBLoader) Append added in v0.3.0

func (l *DBLoader) Append(blk *block.Block) error

Append stores a block in the DB

func (*DBLoader) BlockAt added in v0.3.0

func (l *DBLoader) BlockAt(searchingHeight uint64) (block.Block, error)

BlockAt returns the block stored at a given height

func (*DBLoader) CheckBlock added in v0.3.0

func (l *DBLoader) CheckBlock(prevBlock block.Block, blk block.Block) error

CheckBlock will verify whether a block is valid according to the rules of the consensus returns nil if a block is valid

func (*DBLoader) Clear added in v0.3.0

func (l *DBLoader) Clear() error

Clear the underlying DB

func (*DBLoader) Close added in v0.3.0

func (l *DBLoader) Close(driver string) error

Close the underlying DB usign the drivers

func (*DBLoader) Height added in v0.3.0

func (l *DBLoader) Height() (uint64, error)

Height returns the height of the blockchain stored in the DB

func (*DBLoader) LoadTip added in v0.3.0

func (l *DBLoader) LoadTip() (*block.Block, error)

LoadTip returns the tip of the chain

func (*DBLoader) PerformSanityCheck added in v0.3.0

func (l *DBLoader) PerformSanityCheck(startAt, firstBlocksAmount, lastBlocksAmount uint64) error

PerformSanityCheck checks the head and the tail of the blockchain to avoid inconsistencies and a faulty bootstrap

type Loader added in v0.3.0

type Loader interface {
	// LoadTip of the chain
	LoadTip() (*block.Block, error)
	// Clear removes everything from the DB
	Clear() error
	// Close the Loader and finalizes any pending connection
	Close(string) error
	// Height returns the current height as stored in the loader
	Height() (uint64, error)
	// BlockAt returns the block at a given height
	BlockAt(uint64) (block.Block, error)
	// Append a block on the storage
	Append(*block.Block) error
}

Loader is an interface which abstracts away the storage used by the Chain to store the blockchain

func NewMockLoader added in v0.3.0

func NewMockLoader() Loader

NewMockLoader creates a Mockup of the Loader interface

type MockLoader added in v0.3.0

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

MockLoader is the mock of the DB loader to help testing the chain

func (*MockLoader) Append added in v0.3.0

func (m *MockLoader) Append(blk *block.Block) error

Append the block to the internal blockchain representation

func (*MockLoader) BlockAt added in v0.3.0

func (m *MockLoader) BlockAt(index uint64) (block.Block, error)

BlockAt the block to the internal blockchain representation

func (*MockLoader) Clear added in v0.3.0

func (m *MockLoader) Clear() error

Clear the mock

func (*MockLoader) Close added in v0.3.0

func (m *MockLoader) Close(driver string) error

Close the mock

func (*MockLoader) Height added in v0.3.0

func (m *MockLoader) Height() (uint64, error)

Height returns the height currently known by the Loader

func (*MockLoader) LoadTip added in v0.3.0

func (m *MockLoader) LoadTip() (*block.Block, error)

LoadTip of the chain

func (*MockLoader) PerformSanityCheck added in v0.3.0

func (m *MockLoader) PerformSanityCheck(uint64, uint64, uint64) error

PerformSanityCheck on first N blocks and M last blocks

type MockVerifier added in v0.3.0

type MockVerifier struct {
}

MockVerifier is a mock for the chain.Verifier interface

func (*MockVerifier) CheckBlock added in v0.3.0

func (v *MockVerifier) CheckBlock(prevBlock block.Block, blk block.Block) error

CheckBlock will verify whether a block is valid according to the rules of the consensus

func (*MockVerifier) PerformSanityCheck added in v0.3.0

func (v *MockVerifier) PerformSanityCheck(uint64, uint64, uint64) error

PerformSanityCheck on first N blocks and M last blocks

type Verifier added in v0.3.0

type Verifier interface {
	// PerformSanityCheck on first N blocks and M last blocks
	PerformSanityCheck(uint64, uint64, uint64) error
	// CheckBlock will verify whether a block is valid according to the rules of the consensus
	CheckBlock(prevBlock block.Block, blk block.Block) error
}

Verifier performs checks on the blockchain and potentially new incoming block

Jump to

Keyboard shortcuts

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