block

package
v0.0.0-...-7abacdd Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2022 License: Unlicense Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBlockchainNotFound is thrown when the blockchain database file is not found.
	ErrBlockchainNotFound = errors.New("blockchain not found")
)
View Source
var MaxTarget = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 255), big.NewInt(1))

MaxTarget is the highest proof of work value a block can have. It is the value 2^255 - 1. In the Bitcoin mainnet it's 2^224 - 1.

Functions

func BigToCompact

func BigToCompact(n *big.Int) uint32

BigToCompact converts a whole number N to a compact representation using an unsigned 32-bit number. The compact representation only provides 23 bits of precision, so values larger than (2^23 - 1) only encode the most significant digits of the number. See CompactToBig for details.

func CalculateNextDifficulty

func CalculateNextDifficulty(prevBlock Block) uint32

CalculateNextDifficulty adjusts the difficulty to find a block's hash every blocksRetargetPeriod blocks.

func CompactToBig

func CompactToBig(compact uint32) *big.Int

CompactToBig converts a compact representation of a whole number N to an unsigned 32-bit number. The representation is similar to IEEE754 floating point numbers.

Like IEEE754 floating point, there are three basic components: the sign, the exponent, and the mantissa. They are broken out as follows:

  • the most significant 8 bits represent the unsigned base 256 exponent

  • bit 23 (the 24th bit) represents the sign bit

  • the least significant 23 bits represent the mantissa

    ------------------------------------------------- | Exponent | Sign | Mantissa | ------------------------------------------------- | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] | -------------------------------------------------

The formula to calculate N is:

N = (-1^sign) * mantissa * 256^(exponent-3)

This compact form is only used in bitcoin to encode unsigned 256-bit numbers which represent difficulty targets, thus there really is not a need for a sign bit, but it is implemented here to stay consistent with bitcoind.

func IntToBytes

func IntToBytes(num int64) ([]byte, error)

IntToBytes converts an int64 into a byte array.

Types

type Block

type Block struct {
	*Header
	Hash         []byte
	Transactions []tx.Tx
	Height       int32
}

Block represents a block in the blockchain.

func NewBlock

func NewBlock(prevBlock *Block, txs []tx.Tx) (*Block, error)

NewBlock creates and returns a block without a header hash and nonce. It should be mined before being saved in the databse.

func NewGenesis

func NewGenesis() (*Block, error)

NewGenesis creates and returns the first block of the chain.

It's called the "genesis", it's pre-mined and statically embedded in the client so every node starts with one known block.

func (Block) IsGenesis

func (b Block) IsGenesis() bool

IsGenesis returns whether a block is the genesis one or not.

func (Block) IsValid

func (b Block) IsValid() bool

IsValid validates a block's proof-of-work.

func (Block) PowData

func (b Block) PowData() ([]byte, error)

PowData joins a block's fields so we can generate its hash.

It does not include the nonce, which should be appended to the end of the data.

type Chain

type Chain struct {
	*bolt.DB
	// contains filtered or unexported fields
}

Chain allows to read/write the blockchain file.

func LoadChain

func LoadChain() (*Chain, error)

LoadChain reads the blockchain file and loads the tip of the chain.

Call Close to release the Chain's associated resources when done.

func NewChain

func NewChain() (*Chain, error)

NewChain creates a new blockchain and adds the genesis block.

func (*Chain) AddBlock

func (c *Chain) AddBlock(block Block) error

AddBlock adds the block to the chain.

func (*Chain) BestHeight

func (c *Chain) BestHeight() (int32, error)

BestHeight returns the height of the latest block.

func (*Chain) Block

func (c *Chain) Block(hash []byte) (Block, error)

Block finds a block by its hash and returns it

func (*Chain) BlocksHashes

func (c *Chain) BlocksHashes() ([][]byte, error)

BlocksHashes returns a list of hashes of all the blocks in the chain.

func (*Chain) FindTransaction

func (c *Chain) FindTransaction(id []byte) (Block, tx.Tx, error)

FindTransaction looks for a transaction by its id.

It returns the block containing the transaction and the transaction itself.

func (*Chain) FindUTXOs

func (c *Chain) FindUTXOs() (map[string][]tx.Output, error)

FindUTXOs finds all unspent transaction outputs and returns transactions with spent outputs removed.

func (*Chain) LastBlock

func (c *Chain) LastBlock() (Block, error)

LastBlock returns the last block in the chain.

func (*Chain) NewIterator

func (c *Chain) NewIterator() *ChainIterator

NewIterator returns a BlockchainIterat

func (*Chain) SignTransaction

func (c *Chain) SignTransaction(t *tx.Tx, privKey *ecdsa.PrivateKey) error

SignTransaction signs inputs of a Transaction.

func (*Chain) VerifyTx

func (c *Chain) VerifyTx(t tx.Tx) error

VerifyTx returns an error if a transaction is not valid.

TODO:

  • utxo should not belong to the genesis block
  • the size of the serialized transaction should not exceed max block size
  • inputs referenced outputs should be unspent and in the chain/mempool
  • inputs referenced outputs and new outputs should have the same amount of coins

type ChainIterator

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

ChainIterator is used to iterate over blockchain blocks

func (*ChainIterator) BlocksCount

func (i *ChainIterator) BlocksCount() (int, error)

BlocksCount returns the number of blocks stored in the databse.

func (*ChainIterator) ForEach

func (i *ChainIterator) ForEach(f func(block Block) error) error

ForEach runs the f function on each iteration of the blockchain blocks.

func (*ChainIterator) Next

func (i *ChainIterator) Next(tx *bolt.Tx) (Block, error)

Next returns the next block starting from the tip.

type Header struct {
	PrevBlockHash  []byte
	MerkleRootHash []byte
	Timestamp      int64
	Nonce          uint32
	Version        int32
	Bits           uint32
}

Header represents a block header.

Jump to

Keyboard shortcuts

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