dbft

package
v0.0.0-...-f94ef0f Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBlockFromContext

func NewBlockFromContext(ctx *Context) block.Block

NewBlockFromContext returns new block filled with given contexet

Types

type Config

type Config struct {
	// Logger
	Logger *zap.Logger
	// Timer
	Timer timer.Timer
	// SecondsPerBlock is the number of seconds that
	// need to pass before another block will be accepted.
	SecondsPerBlock time.Duration
	// TimestampIncrement increment is the amount of units to add to timestamp
	// if current time is less than that of previous context.
	// By default use millisecond precision.
	TimestampIncrement uint64
	// GetKeyPair returns an index of the node in the list of validators
	// together with it's key pair.
	GetKeyPair func([]*keys.PublicKey) (int, *keys.PrivateKey, *keys.PublicKey)
	// NewBlockFromContext should allocate, fill from Context and return new block.Block.
	NewBlockFromContext func(ctx *Context) block.Block
	// RequestTx is a callback which is called when transaction contained
	// in current block can't be found in memory pool.
	RequestTx func(h ...common.Hash)
	// GetTx returns a transaction from memory pool.
	GetTx func(h common.Hash) block.Transaction
	// GetVerified returns a slice of verified transactions
	// to be proposed in a new block.
	GetVerified func() []block.Transaction
	// VerifyBlock verifies if block is valid.
	VerifyBlock func(b block.Block) bool
	// Broadcast should broadcast payload m to the consensus nodes.
	Broadcast func(m payload.ConsensusPayload)
	// ProcessBlock is called every time new block is accepted.
	ProcessBlock func(b block.Block)
	// GetBlock should return block with hash.
	GetBlock func(h common.Hash) block.Block
	// WatchOnly tells if a node should only watch.
	WatchOnly func() bool
	// CurrentHeight returns index of the last accepted block.
	CurrentHeight func() uint32
	// CurrentBlockHash returns hash of the last accepted block.
	CurrentBlockHash func() common.Hash
	// GetValidators returns list of the validators.
	// When called with a transaction list it must return
	// list of the validators of the next block.
	// If this function ever returns 0-length slice, dbft will panic.
	GetValidators func(index uint32) []*keys.PublicKey
	// GetConsensusAddress returns hash of the validator list.
	GetConsensusAddress func(...*keys.PublicKey) common.Address
	// NewConsensusPayload is a constructor for payload.ConsensusPayload.
	NewConsensusPayload func(*Context, payload.MessageType, interface{}) payload.ConsensusPayload
	// NewPrepareRequest is a constructor for payload.PrepareRequest.
	NewPrepareRequest func() payload.PrepareRequest
	// NewPrepareResponse is a constructor for payload.PrepareResponse.
	NewPrepareResponse func() payload.PrepareResponse
	// NewChangeView is a constructor for payload.ChangeView.
	NewChangeView func() payload.ChangeView
	// NewCommit is a constructor for payload.Commit.
	NewCommit func() payload.Commit
	// NewRecoveryRequest is a constructor for payload.RecoveryRequest.
	NewRecoveryRequest func() payload.RecoveryRequest
	// NewRecoveryMessage is a constructor for payload.RecoveryMessage.
	NewRecoveryMessage func() payload.RecoveryMessage
	// VerifyPrepareRequest can perform external payload verification and returns true iff it was successful.
	VerifyPrepareRequest func(p payload.ConsensusPayload) error
	// VerifyPrepareResponse performs external PrepareResponse verification and returns nil if it's successful.
	VerifyPrepareResponse func(p payload.ConsensusPayload) error
}

Config contains initialization and working parameters for dBFT.

type Context

type Context struct {
	// Config is dBFT's Config instance.
	Config *Config

	// Priv is node's private key.
	Priv *keys.PrivateKey
	// Pub is node's public key.
	Pub *keys.PublicKey

	// BlockIndex is current block index.
	BlockIndex uint32
	// ViewNumber is current view number.
	ViewNumber byte
	// Validators is a current validator list.
	Validators []*keys.PublicKey
	// MyIndex is an index of the current node in the Validators array.
	// It is equal to -1 if node is not a validator or is WatchOnly.
	MyIndex int
	// PrimaryIndex is an index of the primary node in the current epoch.
	PrimaryIndex uint
	Version      uint32

	// NextConsensus is a hash of the validators which will be accepting the next block.
	NextConsensus common.Address
	// PrevHash is a hash of the previous block.
	PrevHash common.Hash

	// Timestamp is a nanosecond-precision timestamp
	Timestamp uint64
	Nonce     uint64
	// TransactionHashes is a slice of hashes of proposed transactions in the current block.
	TransactionHashes []common.Hash
	// MissingTransactions is a slice of hashes containing missing transactions for the current block.
	MissingTransactions []common.Hash
	// Transactions is a map containing actual transactions for the current block.
	Transactions map[common.Hash]block.Transaction

	// PreparationPayloads stores consensus Prepare* payloads for the current epoch.
	PreparationPayloads []payload.ConsensusPayload
	// CommitPayloads stores consensus Commit payloads for the current epoch.
	CommitPayloads []payload.ConsensusPayload
	// ChangeViewPayloads stores consensus ChangeView payloads for the current epoch.
	ChangeViewPayloads []payload.ConsensusPayload
	// LastChangeViewPayloads stores consensus ChangeView payloads for the last epoch.
	LastChangeViewPayloads []payload.ConsensusPayload
	// LastSeenMessage array stores the height of the last seen message, for each validator.
	// if this node never heard from validator i, LastSeenMessage[i] will be -1.
	LastSeenMessage []*timer.HV
	// contains filtered or unexported fields
}

Context is a main dBFT structure which contains all information needed for performing transitions.

func (*Context) BlockSent

func (c *Context) BlockSent() bool

BlockSent returns true iff block was already formed for the current epoch.

func (*Context) CommitSent

func (c *Context) CommitSent() bool

CommitSent returns true iff Commit message was sent for the current epoch.

func (*Context) CountCommitted

func (c *Context) CountCommitted() (count int)

CountCommitted returns number of received Commit messages for the current epoch.

func (*Context) CountFailed

func (c *Context) CountFailed() (count int)

CountFailed returns number of nodes with which no communication was performed for this block/view.

func (*Context) CreateBlock

func (c *Context) CreateBlock() block.Block

CreateBlock returns resulting block for the current epoch.

func (*Context) F

func (c *Context) F() int

F returns number of validators which can be faulty.

func (*Context) Fill

func (c *Context) Fill()

Fill initializes consensus when node is a speaker.

func (*Context) GetPrimaryIndex

func (c *Context) GetPrimaryIndex(viewNumber byte) uint

GetPrimaryIndex returns index of a primary node for the specified view.

func (*Context) IsBackup

func (c *Context) IsBackup() bool

IsBackup returns true iff node is backup for current height and view.

func (*Context) IsPrimary

func (c *Context) IsPrimary() bool

IsPrimary returns true iff node is primary for current height and view.

func (*Context) M

func (c *Context) M() int

M returns number of validators which must function correctly.

func (*Context) MakeHeader

func (c *Context) MakeHeader() block.Block

MakeHeader returns half-filled block for the current epoch. All hashable fields will be filled.

func (*Context) MoreThanFNodesCommittedOrLost

func (c *Context) MoreThanFNodesCommittedOrLost() bool

MoreThanFNodesCommittedOrLost returns true iff a number of nodes which either committed or are faulty is more than maximum amount of allowed faulty nodes. A possible attack can happen if the last node to commit is malicious and either sends change view after his commit to stall nodes in a higher view, or if he refuses to send recovery messages. In addition, if a node asking change views loses network or crashes and comes back when nodes are committed in more than one higher numbered view, it is possible for the node accepting recovery to commit in any of the higher views, thus potentially splitting nodes among views and stalling the network.

func (*Context) N

func (c *Context) N() int

N returns total number of validators.

func (*Context) NotAcceptingPayloadsDueToViewChanging

func (c *Context) NotAcceptingPayloadsDueToViewChanging() bool

NotAcceptingPayloadsDueToViewChanging returns true if node should not accept new payloads.

func (*Context) RequestSentOrReceived

func (c *Context) RequestSentOrReceived() bool

RequestSentOrReceived returns true iff PrepareRequest was sent or received for the current epoch.

func (*Context) ResponseSent

func (c *Context) ResponseSent() bool

ResponseSent returns true iff Prepare* message was sent for the current epoch.

func (*Context) ViewChanging

func (c *Context) ViewChanging() bool

ViewChanging returns true iff node is in a process of changing view.

func (*Context) WatchOnly

func (c *Context) WatchOnly() bool

WatchOnly returns true iff node takes no active part in consensus.

type DBFT

type DBFT struct {
	Context
	Config

	*sync.Mutex
	// contains filtered or unexported fields
}

DBFT is a wrapper over Context containing service configuration and some other parameters not directly related to dBFT's state machine.

func New

func New(options ...Option) *DBFT

New returns new DBFT instance with provided options and nil if some of the options are missing or invalid.

func (*DBFT) InitializeConsensus

func (d *DBFT) InitializeConsensus(view byte)

InitializeConsensus initializes dBFT instance.

func (*DBFT) OnReceive

func (d *DBFT) OnReceive(msg payload.ConsensusPayload)

OnReceive advances state machine in accordance with msg.

func (*DBFT) OnTimeout

func (d *DBFT) OnTimeout(hv timer.HV)

OnTimeout advances state machine as if timeout was fired.

func (*DBFT) OnTransaction

func (d *DBFT) OnTransaction(tx block.Transaction)

OnTransaction notifies service about receiving new transaction.

func (*DBFT) Start

func (d *DBFT) Start()

Start initializes dBFT instance and starts protocol if node is primary.

type Option

type Option = func(*Config)

Option is a generic options type. It can modify config in any way it wants.

func WithBroadcast

func WithBroadcast(f func(m payload.ConsensusPayload)) Option

WithBroadcast sets Broadcast.

func WithCurrentBlockHash

func WithCurrentBlockHash(f func() common.Hash) Option

WithCurrentBlockHash sets CurrentBlockHash.

func WithCurrentHeight

func WithCurrentHeight(f func() uint32) Option

WithCurrentHeight sets CurrentHeight.

func WithGetBlock

func WithGetBlock(f func(h common.Hash) block.Block) Option

WithGetBlock sets GetBlock.

func WithGetConsensusAddress

func WithGetConsensusAddress(f func(keys ...*keys.PublicKey) common.Address) Option

WithGetConsensusAddress sets GetConsensusAddress.

func WithGetKeyPair

func WithGetKeyPair(f func([]*keys.PublicKey) (int, *keys.PrivateKey, *keys.PublicKey)) Option

WithGetKeyPair sets GetKeyPair.

func WithGetTx

func WithGetTx(f func(h common.Hash) block.Transaction) Option

WithGetTx sets GetTx.

func WithGetValidators

func WithGetValidators(f func(uint32) []*keys.PublicKey) Option

WithGetValidators sets GetValidators.

func WithGetVerified

func WithGetVerified(f func() []block.Transaction) Option

WithGetVerified sets GetVerified.

func WithKeyPair

func WithKeyPair(priv *keys.PrivateKey, pub *keys.PublicKey) Option

WithKeyPair sets GetKeyPair to a function returning default key pair if it is present in a list of validators.

func WithLogger

func WithLogger(log *zap.Logger) Option

WithLogger sets Logger.

func WithNewBlockFromContext

func WithNewBlockFromContext(f func(ctx *Context) block.Block) Option

WithNewBlockFromContext sets NewBlockFromContext.

func WithNewChangeView

func WithNewChangeView(f func() payload.ChangeView) Option

WithNewChangeView sets NewChangeView.

func WithNewCommit

func WithNewCommit(f func() payload.Commit) Option

WithNewCommit sets NewCommit.

func WithNewConsensusPayload

func WithNewConsensusPayload(f func(*Context, payload.MessageType, interface{}) payload.ConsensusPayload) Option

WithNewConsensusPayload sets NewConsensusPayload.

func WithNewPrepareRequest

func WithNewPrepareRequest(f func() payload.PrepareRequest) Option

WithNewPrepareRequest sets NewPrepareRequest.

func WithNewPrepareResponse

func WithNewPrepareResponse(f func() payload.PrepareResponse) Option

WithNewPrepareResponse sets NewPrepareResponse.

func WithNewRecoveryMessage

func WithNewRecoveryMessage(f func() payload.RecoveryMessage) Option

WithNewRecoveryMessage sets NewRecoveryMessage.

func WithNewRecoveryRequest

func WithNewRecoveryRequest(f func() payload.RecoveryRequest) Option

WithNewRecoveryRequest sets NewRecoveryRequest.

func WithProcessBlock

func WithProcessBlock(f func(b block.Block)) Option

WithProcessBlock sets ProcessBlock.

func WithRequestTx

func WithRequestTx(f func(h ...common.Hash)) Option

WithRequestTx sets RequestTx.

func WithSecondsPerBlock

func WithSecondsPerBlock(d time.Duration) Option

WithSecondsPerBlock sets SecondsPerBlock.

func WithTimer

func WithTimer(t timer.Timer) Option

WithTimer sets Timer.

func WithTimestampIncrement

func WithTimestampIncrement(u uint64) Option

WithTimestampIncrement sets TimestampIncrement.

func WithVerifyBlock

func WithVerifyBlock(f func(b block.Block) bool) Option

WithVerifyBlock sets VerifyBlock.

func WithVerifyPrepareRequest

func WithVerifyPrepareRequest(f func(payload.ConsensusPayload) error) Option

WithVerifyPrepareRequest sets VerifyPrepareRequest.

func WithVerifyPrepareResponse

func WithVerifyPrepareResponse(f func(payload.ConsensusPayload) error) Option

WithVerifyPrepareResponse sets VerifyPrepareResponse.

func WithWatchOnly

func WithWatchOnly(f func() bool) Option

WithWatchOnly sets WatchOnly.

type Service

type Service interface {
	Start()
	OnTransaction(block.Transaction)
	OnReceive(payload.ConsensusPayload)
	OnTimeout(timer.HV)
}

Service is an interface for dBFT consensus.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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