block

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2016 License: ISC Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const DigestSize = 32

number of bytes in the digest

View Source
const (
	ExpectedMinutes = 10 // number of minutes to mine a block

)

constants

View Source
const (
	GenesisBlockNumber = uint64(1)
)

the starting block number

View Source
const (
	OP_RETURN = byte(0x6a)
)

bitcoin operations

View Source
const (
	Version = 2 // simulating block headers compatible with this Bitcoin version
)

constant values

Variables

View Source
var LiveGenesisBlock = Packed([]byte{
	0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x88, 0x01, 0x89, 0x7b,
	0x89, 0x71, 0x5a, 0x19, 0xbf, 0xbf, 0xb3, 0xd0,
	0x71, 0x81, 0xd5, 0xc5, 0x7c, 0xc7, 0x61, 0xaf,
	0xfd, 0xb8, 0x41, 0x06, 0x78, 0xd6, 0xac, 0xc6,
	0xa0, 0xa9, 0x7e, 0x69, 0xb7, 0x9a, 0x80, 0x56,
	0xff, 0xff, 0x00, 0x1d, 0x02, 0x40, 0xf1, 0xfd,
	0x64, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
	0xff, 0xff, 0xff, 0x11, 0x02, 0x01, 0x00, 0x04,
	0xb7, 0x9a, 0x80, 0x56, 0x08, 0x42, 0x4d, 0x52,
	0x4b, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x17, 0x6a, 0x00, 0x14, 0x44, 0x4f,
	0x57, 0x4e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x52,
	0x41, 0x42, 0x42, 0x49, 0x54, 0x20, 0x68, 0x6f,
	0x6c, 0x65, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
	0x88, 0x01, 0x89, 0x7b, 0x89, 0x71, 0x5a, 0x19,
	0xbf, 0xbf, 0xb3, 0xd0, 0x71, 0x81, 0xd5, 0xc5,
	0x7c, 0xc7, 0x61, 0xaf, 0xfd, 0xb8, 0x41, 0x06,
	0x78, 0xd6, 0xac, 0xc6, 0xa0, 0xa9, 0x7e, 0x69,
})
View Source
var LiveGenesisDigest = Digest([...]byte{
	0x77, 0xa0, 0xf7, 0x2e, 0x22, 0xf6, 0xb9, 0x18,
	0x61, 0x3d, 0x96, 0x3e, 0xe0, 0x97, 0xdc, 0x13,
	0x43, 0x05, 0x6f, 0x70, 0x6a, 0x84, 0xb9, 0x9e,
	0x30, 0x5f, 0x6b, 0x7e, 0x00, 0x00, 0x00, 0x00,
})

digest of the live genesis header 000000007e6b5f309eb9846a706f054313dc97e03e963d6118b9f6222ef7a077

View Source
var TestGenesisBlock = Packed([]byte{
	0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0xaa, 0x5e, 0x56, 0xa6,
	0xd8, 0x3f, 0xd0, 0xfd, 0xc7, 0x58, 0x3f, 0x33,
	0xb1, 0x17, 0xb5, 0x4d, 0xfb, 0x30, 0x12, 0x1c,
	0xa9, 0x2d, 0xcc, 0xf3, 0x34, 0xc3, 0x5b, 0x67,
	0x8c, 0xdc, 0xe9, 0x3c, 0x4b, 0x42, 0x78, 0x54,
	0xff, 0xff, 0x00, 0x1d, 0x3b, 0xa8, 0xcc, 0xae,
	0x6d, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
	0xff, 0xff, 0xff, 0x11, 0x02, 0x01, 0x00, 0x04,
	0x4b, 0x42, 0x78, 0x54, 0x08, 0x42, 0x4d, 0x52,
	0x4b, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x20, 0x6a, 0x00, 0x1d, 0x42, 0x69,
	0x74, 0x6d, 0x61, 0x72, 0x6b, 0x20, 0x54, 0x65,
	0x73, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x47, 0x65,
	0x6e, 0x65, 0x73, 0x69, 0x73, 0x20, 0x42, 0x6c,
	0x6f, 0x63, 0x6b, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0xaa, 0x5e, 0x56, 0xa6, 0xd8, 0x3f, 0xd0,
	0xfd, 0xc7, 0x58, 0x3f, 0x33, 0xb1, 0x17, 0xb5,
	0x4d, 0xfb, 0x30, 0x12, 0x1c, 0xa9, 0x2d, 0xcc,
	0xf3, 0x34, 0xc3, 0x5b, 0x67, 0x8c, 0xdc, 0xe9,
	0x3c,
})
View Source
var TestGenesisDigest = Digest([...]byte{
	0x20, 0xa0, 0x5d, 0x03, 0x00, 0xee, 0x7c, 0xf9,
	0x5b, 0x57, 0xa3, 0x2e, 0x0b, 0xfd, 0x3e, 0x45,
	0x21, 0xae, 0x16, 0x82, 0x29, 0x46, 0x19, 0x93,
	0x5a, 0x72, 0x6b, 0x70, 0x00, 0x00, 0x00, 0x00,
})

digest of the test genesis header 00000000706b725a931946298216ae21453efd0b2ea3575bf97cee00035da020

Functions

func CurrentCoinbase

func CurrentCoinbase(timestamp time.Time, nonceSize int, addresses []MinerAddress) (cb1 []byte, cb2 []byte)

create a split coinbase transaction for the miner using the current block number

func DigestFromBytes

func DigestFromBytes(digest *Digest, buffer []byte) error

convert and validate little endian binary byte slice to a digest

func Finalise

func Finalise()

finalise - flush unsaved data

func Initialise

func Initialise()

initialise the block numbering system

func MinerAddressFromBytes

func MinerAddressFromBytes(m *MinerAddress, buffer []byte) error

convert and validate little endian binary byte slice to a MinerAddress

func MinerCheckIn

func MinerCheckIn(timestamp time.Time, ntime uint32, nonce uint32, extraNonce []byte, addresses []MinerAddress, ids []Digest) (Digest, Packed, bool)

create the combined block assuming current state

binary format:

  1. packed header
  2. coinbase a. length (little endian int16) b. packed coinbase
  3. transaction count + 1 (to account for coinbase digest) (little endian int16) a. coinbase digest b. list of txids (digests)
  4. full merkle tree computed from 3

validate submitted data, and save the block

func NewCoinbase

func NewCoinbase(blockNumber uint64, timestamp time.Time, nonceSize int, addresses []MinerAddress) (cb1 []byte, cb2 []byte)

create a split coinbase transaction for the miner

func Number

func Number() uint64

access to the block number being mined

func Pack

func Pack(blockNumber uint64, timestamp time.Time, difficulty *difficulty.Difficulty, ntime uint32, nonce uint32, extraNonce []byte, addresses []MinerAddress, ids []Digest) (Digest, Packed, bool)

create a packed block from various pieces

func PrintBlockTimes

func PrintBlockTimes(fh io.Writer, beginBlockNumber uint64, endBlockNumber uint64)

Types

type Block

type Block struct {
	Digest    Digest
	Number    uint64
	Timestamp time.Time // the 64 bit timestamp from coinbase, _NOT_ block header ntime
	Addresses []MinerAddress
	Header    Header
	Coinbase  []byte
	TxIds     []Digest
}

type to hold the unpacked block

type CoinbaseData

type CoinbaseData struct {
	BlockNumber uint64
	Timestamp   time.Time
	Addresses   []MinerAddress
}

type to hold some data unpacked from the coinbase

type Digest

type Digest [DigestSize]byte

the type for a digest stored as little endian byte array represented as big endian hex value for print represented as little endian hex text for JSON encoding

func FullMerkleTree

func FullMerkleTree(coinbase Digest, txIds []Digest) []Digest

compute minimum merkle root from coinbase + minimum tree

structure is:

  1. coinbase digest
  2. N * transaction digests
  3. level 1..m digests
  4. merkle root digest

Note for an empty txIds array the result is just one element

i.e. merkle root == coinbase (Digest)
only the genesis block is like this

func MinimumMerkleTree

func MinimumMerkleTree(ids []Digest) []Digest

merkle hashing

build a minimised tree lacking a coinbase i.e. ids[0] is 2nd transaction (the one after the coinbase)

func NewDigest

func NewDigest(record []byte) Digest

create a digest from a byte slice

BTC compatible double SHA2-256 Hash

func PreviousLink() Digest

access to previous link

func (Digest) BtcHex

func (digest Digest) BtcHex() string

convert a binary digest to BTC little endian word swapped hex string for use by miners

func (Digest) Cmp

func (digest Digest) Cmp(difficulty *big.Int) int

convert the hash to its equivalent big.Int

func (Digest) GoString

func (digest Digest) GoString() string

convert a binary digest to big endian hex string for use by the fmt package (for %#v)

func (Digest) MarshalJSON

func (digest Digest) MarshalJSON() ([]byte, error)

convert a binary digest to little endian hex for JSON

func (Digest) MarshalText

func (digest Digest) MarshalText() ([]byte, error)

convert digest to little endian hex text

func (Digest) MinerHex

func (digest Digest) MinerHex() string

convert a binary digest to hex string for use by the stratum miner

the stored version and the output string are both little endian

func (*Digest) Scan

func (digest *Digest) Scan(state fmt.ScanState, verb rune) error

convert a big endian hex representation to a digest for use by the format package scan routines

func (Digest) String

func (digest Digest) String() string

convert a binary digest to hex string for use by the fmt package (for %s)

the stored version is in little endian, but the output string is big endian

func (*Digest) UnmarshalJSON

func (digest *Digest) UnmarshalJSON(s []byte) error

convert a little endian hex string to a digest for conversion from JSON

func (*Digest) UnmarshalText

func (digest *Digest) UnmarshalText(s []byte) error

convert little endian hex text into a digest

type Header struct {
	Version       uint32
	PreviousBlock Digest
	MerkleRoot    Digest
	Time          uint32
	Bits          difficulty.Difficulty
	Nonce         uint32
}

the unpacked header structure the types here must match Bitcoin header types

func (*Header) Pack

func (header *Header) Pack() PackedHeader

turn a record into an array of bytes

the byte ordering matches a Bitcoin header

type Mined

type Mined []byte // tags an already mined block so background can ignore it

type MinerAddress

type MinerAddress struct {
	Currency string `json:"currency"`
	Address  string `json:"address"`
}

to hold miner address and its corresponding currency code

func (*MinerAddress) String

func (m *MinerAddress) String() string

convert a miner address to a string for use as a map index

type Packed

type Packed []byte // for general distribution

for packed block

func GenesisBlock

func GenesisBlock() Packed

return the Genesis Block

func Get

func Get(number uint64) (Packed, bool)

fetch a stored block

func (Packed) Save

func (blk Packed) Save(number uint64, digest *Digest, timestamp time.Time)

store a block requires number to save an unpack step which was probably already done or the number was known from the pack step

func (Packed) Unpack

func (pack Packed) Unpack(blk *Block) error

type PackedCoinbase

type PackedCoinbase []byte

the packed coinbase data

func NewFullCoinbase

func NewFullCoinbase(blockNumber uint64, timestamp time.Time, nonce []byte, addresses []MinerAddress) (cb PackedCoinbase)

create a full coinbase transaction for the block

func (PackedCoinbase) Unpack

func (coinbase PackedCoinbase) Unpack(coinbaseData *CoinbaseData) error

unpack some data from the coinbase

type PackedHeader

type PackedHeader []byte

packed records are just a byte slice

func (PackedHeader) Digest

func (record PackedHeader) Digest() Digest

digest for a packed

func (PackedHeader) Unpack

func (record PackedHeader) Unpack(header *Header) error

turn a byte slice into a record

Jump to

Keyboard shortcuts

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