pow

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2017 License: LGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TimeoutEvent  = "timeout"
	StoppingEvent = "stopping"
)

Event list of PoW State-Machine

View Source
const (
	// Mining mining state key
	Mining = "mining"
)
View Source
const (
	// Minted minted state key
	Minted = "minted"
)
View Source
const (
	// Prepare prepare state key.
	Prepare = "prepare"
)
View Source
const (
	// Start tart state key.
	Start = "Start"
)
View Source
const (
	// Stopped stopped state key.
	Stopped = "stopped"
)

Variables

View Source
var (
	ErrInvalidDataType   = errors.New("invalid data type, should be *core.Block")
	ErrInvalidBlockNonce = errors.New("invalid block nonce")
)

Errors in PoW Consensus

Functions

func HashAndVerify

func HashAndVerify(block *core.Block) []byte

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) Event

func (state *MiningState) Event(e consensus.Event) (bool, consensus.State)

Event handle event.

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) Event

func (state *MintedState) Event(e consensus.Event) (bool, consensus.State)

Event handle event.

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 NewPow

func NewPow(neblet Neblet) *Pow

NewPow create Pow instance.

func (*Pow) CanMining added in v0.2.0

func (p *Pow) CanMining() bool

CanMining return if consensus can do mining now

func (*Pow) Event

func (p *Pow) Event(e consensus.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

func (p *Pow) SetCanMining(canMining bool)

SetCanMining set if consensus can do mining now

func (*Pow) Start

func (p *Pow) Start()

Start start pow service.

func (*Pow) Stop

func (p *Pow) Stop()

Stop stop pow service.

func (*Pow) Transit

func (p *Pow) Transit(from, to consensus.State, data interface{})

Transit transit state.

func (*Pow) TransitByKey

func (p *Pow) TransitByKey(from string, to string, data interface{})

TransitByKey transit state by stateKey.

func (*Pow) VerifyBlock

func (p *Pow) VerifyBlock(block *core.Block) error

VerifyBlock return nil if nonce is right, otherwise return error.

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) Event

func (state *PrepareState) Event(e consensus.Event) (bool, consensus.State)

Event handle event.

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) Event added in v0.2.0

func (state *StartState) Event(e consensus.Event) (bool, consensus.State)

Event handle event.

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) Event

func (state *StoppedState) Event(e consensus.Event) (bool, consensus.State)

Event handle event.

func (*StoppedState) Leave

func (state *StoppedState) Leave(data interface{})

Leave called when leaving this state.

Jump to

Keyboard shortcuts

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