Documentation
¶
Index ¶
- Constants
- Variables
- func HashAndVerify(block *core.Block) []byte
- type MiningState
- type MintedState
- type Neblet
- type Pow
- func (p *Pow) CanMining() bool
- func (p *Pow) Event(e consensus.Event)
- func (p *Pow) ForkChoice()
- func (p *Pow) SetCanMining(canMining bool)
- func (p *Pow) Start()
- func (p *Pow) Stop()
- func (p *Pow) Transit(from, to consensus.State, data interface{})
- func (p *Pow) TransitByKey(from string, to string, data interface{})
- func (p *Pow) VerifyBlock(block *core.Block) error
- type PrepareState
- type StartState
- type StoppedState
Constants ¶
const ( TimeoutEvent = "timeout" StoppingEvent = "stopping" )
Event list of PoW State-Machine
const (
// Mining mining state key
Mining = "mining"
)
const (
// Minted minted state key
Minted = "minted"
)
const (
// Prepare prepare state key.
Prepare = "prepare"
)
const (
// Start tart state key.
Start = "Start"
)
const (
// Stopped stopped state key.
Stopped = "stopped"
)
Variables ¶
var ( ErrInvalidDataType = errors.New("invalid data type, should be *core.Block") ErrInvalidBlockNonce = errors.New("invalid block nonce") )
Errors in PoW Consensus
Functions ¶
func HashAndVerify ¶
HashAndVerify return hash result if all requirements are met
Types ¶
type MiningState ¶
type MiningState struct {
// contains filtered or unexported fields
}
MiningState mining state.
func NewMiningState ¶
func NewMiningState(p *Pow) *MiningState
NewMiningState create MiningState instance.
func (*MiningState) Enter ¶
func (state *MiningState) Enter(data interface{})
Enter called when transiting to this state.
func (*MiningState) Leave ¶
func (state *MiningState) Leave(data interface{})
Leave called when leaving this state.
type MintedState ¶
type MintedState struct {
// contains filtered or unexported fields
}
MintedState minted state, transit from @MiningState
func NewMintedState ¶
func NewMintedState(p *Pow) *MintedState
NewMintedState create MintedState instance.
func (*MintedState) Enter ¶
func (state *MintedState) Enter(data interface{})
Enter called when transiting to this state.
func (*MintedState) Leave ¶
func (state *MintedState) Leave(data interface{})
Leave called when leaving this state.
type Neblet ¶ added in v0.2.0
type Neblet interface { Config() nebletpb.Config BlockChain() *core.BlockChain NetService() *p2p.NetService }
Neblet interface breaks cycle import dependency and hides unused services.
type Pow ¶
type Pow struct {
// contains filtered or unexported fields
}
Pow implementation of Proof-of-Work consensus, designed to be a state machine. The following is the state diagram:
@startuml [*] --> Prepare Prepare --> Mining : start mining Mining --> Prepare : new block received Mining --> Minted : found the nonce/block Minted --> Prepare : broadcast the block, and start over Prepare --> [*] : stop Mining --> [*] : stop Minted --> [*] : stop @enduml
func (*Pow) Event ¶
Event handle events from Network or State. The whole event process should be as the following: 1. dispatch to currentState to process. 2. if currentState does not captured it, consensus process it by default.
func (*Pow) ForkChoice ¶
func (p *Pow) ForkChoice()
ForkChoice Rule of PoW Consensus Choose the longest chain
func (*Pow) SetCanMining ¶ added in v0.2.0
SetCanMining set if consensus can do mining now
func (*Pow) TransitByKey ¶
TransitByKey transit state by stateKey.
type PrepareState ¶
type PrepareState struct {
// contains filtered or unexported fields
}
PrepareState the initial state of @Pow state machine. TODO(@roy): can be interrupted
func NewPrepareState ¶
func NewPrepareState(p *Pow) *PrepareState
NewPrepareState create PrepareState instance.
func (*PrepareState) Enter ¶
func (state *PrepareState) Enter(data interface{})
Enter called when transiting to this state.
func (*PrepareState) Leave ¶
func (state *PrepareState) Leave(data interface{})
Leave called when leaving this state.
type StartState ¶ added in v0.2.0
type StartState struct {
// contains filtered or unexported fields
}
StartState the initial state of @Pow state machine. TODO(@roy): can be interrupted
func NewStartState ¶ added in v0.2.0
func NewStartState(p *Pow) *StartState
NewStartState create StartState instance.
func (*StartState) Enter ¶ added in v0.2.0
func (state *StartState) Enter(data interface{})
Enter called when transiting to this state.
func (*StartState) Leave ¶ added in v0.2.0
func (state *StartState) Leave(data interface{})
Leave called when leaving this state.
type StoppedState ¶
type StoppedState struct {
// contains filtered or unexported fields
}
StoppedState stopped state.
func NewStoppedState ¶
func NewStoppedState(p *Pow) *StoppedState
NewStoppedState create @StoppedState instance.
func (*StoppedState) Enter ¶
func (state *StoppedState) Enter(data interface{})
Enter called when transiting to this state.
func (*StoppedState) Leave ¶
func (state *StoppedState) Leave(data interface{})
Leave called when leaving this state.