work

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2021 License: ISC Imports: 5 Imported by: 1

README

A Golang library for Bitcoin hash puzzles

Bitcoin hash puzzles arise from a combination of the original Bitcoin protocol and the Stratum protocol, which is the way that miners communicate with mining pools, aka nodes. This library provides data types for dealing with hash puzzles.

dependencies: libsv/go-bc

The goal of a hash puzzle is to create a bitcoin block header, which is an 80-byte data structure that includes a target such that, when hashed with Hash256 (aka double SHA256), the hash digest, when read as a little-endian number, should be less than the given target. This is done by providing a 32 bit nonce of arbitrary data. A timestamp must also be provided.

The block header establishes that a list of transactions belongs in the block through the Merkle root.

32 bits is not enough arbitrary data to ensure that a given hash puzzle has a solution. However, a wider space of solutions can be explored via other means. The timestamp increments every second and the Merkle root changes as more transactions are added to the block. Finally, the coinbase input script can have up to 100 bytes of arbitrary data, which is enough for any hash puzzle over a 32 byte hash digest space.

Stratum establishes additional properties of the hash puzzle. The mining pool creates the coinbase transaction other than 96 bits of data that will be in the input script. 32 of these is a user id that the pool assigns to the miner and the other 64 can be used by the miner.

ASIC Boost is a strategy for arriving faster at a given difficulty target by caching certain data. It has to do with the details of SHA256. Because miners were messing around with the timestamp in order to use ASIC Boost, which is bad for Bitcoin's functionality as a timestamp server, BIP 320 was designed to address the problem by assigning 16 bits of the version field of the block header to be used as additional nonce data that the miner provides to the mining pool. Thus, there 48 total bits of arbitrary data in the block header.

Documentation

Index

Constants

View Source
const Bip320GeneralPurposeBits uint32 = ^Bip320Mask
View Source
const Bip320Mask uint32 = 0xe0001fff

bip320 was designed for ASICBoost and it specifies some general purpose bits in the Version field that miners can use for whatever they want.

Variables

This section is empty.

Functions

func Version

func Version(u uint32, gpb uint32) uint32

Types

type Candidate

type Candidate struct {
	Version    uint32
	Digest     []byte
	Bits       []byte
	MerklePath []string
}

Candidate includes the parts of the blockheader that are known before the work has been done. It represents the parts of a work puzzle that are provided by getminingcandidate.

func MakeCandidate

func MakeCandidate(mc *bitcoin.MiningCandidate) (*Candidate, error)

type Job

type Job struct {
	Puzzle Puzzle

	// ExtraNonce1 is also the user id in Stratum.
	ExtraNonce1 uint32
}

A job is a work puzzle after a stratum id has been assigned to a given worker by the mining pool.

type Proof

type Proof struct {
	Puzzle   Puzzle
	Solution Solution
}

func CPUSolve

func CPUSolve(puzzle Puzzle, initial Solution) (*Proof, error)

func (*Proof) Blockheader

func (p *Proof) Blockheader() (*bc.BlockHeader, error)

func (*Proof) MerkleRoot

func (p *Proof) MerkleRoot() []byte

func (*Proof) Metadata

func (p *Proof) Metadata() []byte

the metadata corresponds to the coinbase transaction. In a general work puzzle, it can contain any information.

func (*Proof) Valid

func (p *Proof) Valid() bool

type Puzzle

type Puzzle struct {
	Candidate     Candidate
	CoinbaseBegin []byte
	CoinbaseEnd   []byte
	VersionMask   *uint32
}

Puzzle is what we get after the coinbase has been derived.

func MakePuzzle

func MakePuzzle(candidate Candidate, coinbaseBegin []byte, coinbaseEnd []byte) Puzzle

func MakePuzzleASICBoost

func MakePuzzleASICBoost(candidate Candidate, coinbaseBegin []byte, coinbaseEnd []byte, mask uint32) Puzzle

type Share

type Share struct {
	Time               uint32
	Nonce              uint32
	ExtraNonce2        []byte
	GeneralPurposeBits *uint32
}

A share is the data returned by the worker. Job + Share = Proof

func MakeShare

func MakeShare(time uint32, nonce uint32, extraNonce2 uint64) Share

func MakeShareASICBoost

func MakeShareASICBoost(time uint32, nonce uint32, extraNonce2 uint64, gpb uint32) Share

type Solution

type Solution struct {
	ExtraNonce1 uint32
	Share       Share
}

A Solution solves a Puzzle. Puzzle + Solution = Proof

Jump to

Keyboard shortcuts

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