pow

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2021 License: Apache-2.0, BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const NonceBytes = 8

NonceBytes specifies the number of bytes required for the nonce.

Variables

View Source
var (
	ErrCancelled = errors.New("canceled")
	ErrDone      = errors.New("done")
)

errors returned by the PoW

Functions

This section is empty.

Types

type Hash

type Hash interface {
	// Size returns the length, in bytes, of a digest resulting from the given hash function.
	Size() int
	// New returns a new hash.Hash calculating the given hash function.
	New() hash.Hash
}

Hash identifies a cryptographic hash function that is implemented in another package.

type Worker

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

The Worker provides PoW functionality using an arbitrary hash function.

func New

func New(numWorkers ...int) *Worker

New creates a new PoW based on the provided hash. The optional numWorkers specifies how many go routines are used to mine.

func (*Worker) LeadingZeros

func (w *Worker) LeadingZeros(data []byte) (int, error)

LeadingZeros returns the number of leading zeros in the digest of the given data.

func (*Worker) LeadingZerosWithNonce

func (w *Worker) LeadingZerosWithNonce(msg []byte, nonce uint64) (int, error)

LeadingZerosWithNonce returns the number of leading zeros in the digest after the provided 8-byte nonce is appended to msg.

func (*Worker) Mine

func (w *Worker) Mine(ctx context.Context, msg []byte, target int) (uint64, error)

Mine performs the PoW. It appends the 8-byte nonce to the provided msg and tries to find a nonce until the target number of leading zeroes is reached. The computation can be be canceled using the provided ctx.

Jump to

Keyboard shortcuts

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