Documentation ¶
Index ¶
- func NewBlockFromContext(ctx *Context) block.Block
- type Config
- type Context
- func (c *Context) BlockSent() bool
- func (c *Context) CommitSent() bool
- func (c *Context) CountCommitted() (count int)
- func (c *Context) CountFailed() (count int)
- func (c *Context) CreateBlock() block.Block
- func (c *Context) F() int
- func (c *Context) Fill()
- func (c *Context) GetPrimaryIndex(viewNumber byte) uint
- func (c *Context) IsBackup() bool
- func (c *Context) IsPrimary() bool
- func (c *Context) M() int
- func (c *Context) MakeHeader() block.Block
- func (c *Context) MoreThanFNodesCommittedOrLost() bool
- func (c *Context) N() int
- func (c *Context) NotAcceptingPayloadsDueToViewChanging() bool
- func (c *Context) RequestSentOrReceived() bool
- func (c *Context) ResponseSent() bool
- func (c *Context) ViewChanging() bool
- func (c *Context) WatchOnly() bool
- type DBFT
- type Option
- func WithBroadcast(f func(m payload.ConsensusPayload)) Option
- func WithCurrentBlockHash(f func() common.Hash) Option
- func WithCurrentHeight(f func() uint32) Option
- func WithGetBlock(f func(h common.Hash) block.Block) Option
- func WithGetConsensusAddress(f func(keys ...*keys.PublicKey) common.Address) Option
- func WithGetKeyPair(f func([]*keys.PublicKey) (int, *keys.PrivateKey, *keys.PublicKey)) Option
- func WithGetTx(f func(h common.Hash) block.Transaction) Option
- func WithGetValidators(f func(uint32) []*keys.PublicKey) Option
- func WithGetVerified(f func() []block.Transaction) Option
- func WithKeyPair(priv *keys.PrivateKey, pub *keys.PublicKey) Option
- func WithLogger(log *zap.Logger) Option
- func WithNewBlockFromContext(f func(ctx *Context) block.Block) Option
- func WithNewChangeView(f func() payload.ChangeView) Option
- func WithNewCommit(f func() payload.Commit) Option
- func WithNewConsensusPayload(f func(*Context, payload.MessageType, interface{}) payload.ConsensusPayload) Option
- func WithNewPrepareRequest(f func() payload.PrepareRequest) Option
- func WithNewPrepareResponse(f func() payload.PrepareResponse) Option
- func WithNewRecoveryMessage(f func() payload.RecoveryMessage) Option
- func WithNewRecoveryRequest(f func() payload.RecoveryRequest) Option
- func WithProcessBlock(f func(b block.Block)) Option
- func WithRequestTx(f func(h ...common.Hash)) Option
- func WithSecondsPerBlock(d time.Duration) Option
- func WithTimer(t timer.Timer) Option
- func WithTimestampIncrement(u uint64) Option
- func WithVerifyBlock(f func(b block.Block) bool) Option
- func WithVerifyPrepareRequest(f func(payload.ConsensusPayload) error) Option
- func WithVerifyPrepareResponse(f func(payload.ConsensusPayload) error) Option
- func WithWatchOnly(f func() bool) Option
- type Service
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewBlockFromContext ¶
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 ¶
BlockSent returns true iff block was already formed for the current epoch.
func (*Context) CommitSent ¶
CommitSent returns true iff Commit message was sent for the current epoch.
func (*Context) CountCommitted ¶
CountCommitted returns number of received Commit messages for the current epoch.
func (*Context) CountFailed ¶
CountFailed returns number of nodes with which no communication was performed for this block/view.
func (*Context) CreateBlock ¶
CreateBlock returns resulting block for the current epoch.
func (*Context) GetPrimaryIndex ¶
GetPrimaryIndex returns index of a primary node for the specified view.
func (*Context) MakeHeader ¶
MakeHeader returns half-filled block for the current epoch. All hashable fields will be filled.
func (*Context) MoreThanFNodesCommittedOrLost ¶
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) NotAcceptingPayloadsDueToViewChanging ¶
NotAcceptingPayloadsDueToViewChanging returns true if node should not accept new payloads.
func (*Context) RequestSentOrReceived ¶
RequestSentOrReceived returns true iff PrepareRequest was sent or received for the current epoch.
func (*Context) ResponseSent ¶
ResponseSent returns true iff Prepare* message was sent for the current epoch.
func (*Context) ViewChanging ¶
ViewChanging returns true iff node is in a process of changing view.
type DBFT ¶
DBFT is a wrapper over Context containing service configuration and some other parameters not directly related to dBFT's state machine.
func New ¶
New returns new DBFT instance with provided options and nil if some of the options are missing or invalid.
func (*DBFT) InitializeConsensus ¶
InitializeConsensus initializes dBFT instance.
func (*DBFT) OnReceive ¶
func (d *DBFT) OnReceive(msg payload.ConsensusPayload)
OnReceive advances state machine in accordance with msg.
func (*DBFT) OnTransaction ¶
func (d *DBFT) OnTransaction(tx block.Transaction)
OnTransaction notifies service about receiving new transaction.
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 ¶
WithCurrentBlockHash sets CurrentBlockHash.
func WithCurrentHeight ¶
WithCurrentHeight sets CurrentHeight.
func WithGetBlock ¶
WithGetBlock sets GetBlock.
func WithGetConsensusAddress ¶
WithGetConsensusAddress sets GetConsensusAddress.
func WithGetKeyPair ¶
WithGetKeyPair sets GetKeyPair.
func WithGetTx ¶
func WithGetTx(f func(h common.Hash) block.Transaction) Option
WithGetTx sets GetTx.
func WithGetValidators ¶
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 WithNewBlockFromContext ¶
WithNewBlockFromContext sets NewBlockFromContext.
func WithNewChangeView ¶
func WithNewChangeView(f func() payload.ChangeView) Option
WithNewChangeView sets NewChangeView.
func WithNewCommit ¶
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 ¶
WithProcessBlock sets ProcessBlock.
func WithRequestTx ¶
WithRequestTx sets RequestTx.
func WithSecondsPerBlock ¶
WithSecondsPerBlock sets SecondsPerBlock.
func WithTimestampIncrement ¶
WithTimestampIncrement sets TimestampIncrement.
func WithVerifyBlock ¶
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.
type Service ¶
type Service interface { Start() OnTransaction(block.Transaction) OnReceive(payload.ConsensusPayload) OnTimeout(timer.HV) }
Service is an interface for dBFT consensus.