pow

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	MAX_TRIES = 1 << 32 // mining时的最大尝试次数
	BLOCK_BUF = 100
)

Variables

View Source
var (
	PoWBlockItemErr   = errors.New("invalid block structure, pls check item nonce & targetbits")
	OODMineErr        = errors.New("mining height is out of date")
	TryTooMuchMineErr = errors.New("mining max tries threshold")
	InternalErr       = errors.New("Consensus module found internal error")
	BlockSignErr      = errors.New("invalid block sign")
	MakeBlockErr      = errors.New("make blockid err")
)

Functions

func GetCompact

func GetCompact(number *big.Int) (uint32, bool)

GetCompact 将一个256bits的大数转换为一个target

func NewPoWConsensus

NewPoWConsensus 初始化实例

func SetCompact

func SetCompact(nCompact uint32) (*big.Int, bool, bool)

SetCompact 将一个uint32的target转换为一个difficulty

Types

type PoWConfig

type PoWConfig struct {
	DefaultTarget        uint32 `json:"defaultTarget"`
	AdjustHeightGap      int32  `json:"adjustHeightGap"`
	ExpectedPeriodMilSec int32  `json:"expectedPeriod"`
	MaxTarget            uint32 `json:"maxTarget"`
}

PoWConfig pow需要解析的创始块解析格式

 根据Bitcoin推荐
   AdjustHeightGap: 2016,
	  MaxTarget: 0x1d00FFFF,
   DefaultTarget: 0x207FFFFF

type PoWConsensus

type PoWConsensus struct {
	context.ConsensusCtx
	// contains filtered or unexported fields
}

PoWConsensus pow具体结构

func (*PoWConsensus) CalculateBlock

func (pow *PoWConsensus) CalculateBlock(block context.BlockInterface) error

CalculateBlock 挖矿过程

func (*PoWConsensus) CheckMinerMatch

func (pow *PoWConsensus) CheckMinerMatch(ctx xcontext.XContext, block context.BlockInterface) (bool, error)

CheckMinerMatch 验证区块,包括merkel根和hash ATTENTION: TODO: 上层需要先检查VerifyMerkle(block)

func (*PoWConsensus) CompeteMaster

func (pow *PoWConsensus) CompeteMaster(height int64) (bool, bool, error)

CompeteMaster PoW单一节点都为矿工,故返回为true

func (*PoWConsensus) GetConsensusStatus

func (pow *PoWConsensus) GetConsensusStatus() (consensus.ConsensusStatus, error)

GetConsensusStatus 获取pow实例状态

func (*PoWConsensus) IsProofed

func (pow *PoWConsensus) IsProofed(blockID []byte, targetBits uint32) bool

IsProofed check workload proof

func (*PoWConsensus) ParseConsensusStorage

func (pow *PoWConsensus) ParseConsensusStorage(block context.BlockInterface) (interface{}, error)

ParseConsensusStorage PoW parse专有存储的逻辑,即targetBits

func (*PoWConsensus) ProcessBeforeMiner

func (pow *PoWConsensus) ProcessBeforeMiner(height, timestamp int64) ([]byte, []byte, error)

ProcessBeforeMiner 更新下一次pow挖矿时的targetBits

func (*PoWConsensus) ProcessConfirmBlock

func (pow *PoWConsensus) ProcessConfirmBlock(block context.BlockInterface) error

ProcessConfirmBlock 此处更新最新的block高度

func (*PoWConsensus) Start

func (pow *PoWConsensus) Start() error

Start 重启实例

func (*PoWConsensus) Stop

func (pow *PoWConsensus) Stop() error

Stop 立即停止当前挖矿

type PoWStatus

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

PoWStatus 实现了ConsensusStatus接口

func (*PoWStatus) GetConsensusBeginInfo

func (s *PoWStatus) GetConsensusBeginInfo() int64

GetConsensusBeginInfo 返回该实例初始高度

func (*PoWStatus) GetConsensusName

func (s *PoWStatus) GetConsensusName() string

GetConsensusName 获取共识类型

func (*PoWStatus) GetCurrentTerm

func (s *PoWStatus) GetCurrentTerm() int64

GetCurrentTerm 获取当前状态机term

func (*PoWStatus) GetCurrentValidatorsInfo

func (s *PoWStatus) GetCurrentValidatorsInfo() []byte

GetCurrentValidatorsInfo 获取当前矿工信息

func (*PoWStatus) GetStepConsensusIndex

func (s *PoWStatus) GetStepConsensusIndex() int

GetStepConsensusIndex 获取共识item所在consensus slice中的index

func (*PoWStatus) GetVersion

func (s *PoWStatus) GetVersion() int64

GetVersion 返回pow所在共识version

type PoWStorage

type PoWStorage struct {
	// TargetBits 以一个uint32类型解析,16进制格式为0xFFFFFFFF
	// 真正difficulty按照Bitcoin标准转化,将TargetBits转换为一个uint256 bits的大数
	TargetBits uint32 `json:"targetBits,omitempty"`
}

目前未定义pb结构 PoWStorage pow占用block中consensusStorage json串的格式

type ValidatorsInfo

type ValidatorsInfo struct {
	Validators []string `json:"validators"`
}

Jump to

Keyboard shortcuts

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