Documentation
¶
Index ¶
- Variables
- func BigToCompact(n *big.Int) uint32
- func CalculateNextDifficulty(prevBlock Block) uint32
- func CompactToBig(compact uint32) *big.Int
- func IntToBytes(num int64) ([]byte, error)
- type Block
- type Chain
- func (c *Chain) AddBlock(block Block) error
- func (c *Chain) BestHeight() (int32, error)
- func (c *Chain) Block(hash []byte) (Block, error)
- func (c *Chain) BlocksHashes() ([][]byte, error)
- func (c *Chain) FindTransaction(id []byte) (Block, tx.Tx, error)
- func (c *Chain) FindUTXOs() (map[string][]tx.Output, error)
- func (c *Chain) LastBlock() (Block, error)
- func (c *Chain) NewIterator() *ChainIterator
- func (c *Chain) SignTransaction(t *tx.Tx, privKey *ecdsa.PrivateKey) error
- func (c *Chain) VerifyTx(t tx.Tx) error
- type ChainIterator
- type Header
Constants ¶
This section is empty.
Variables ¶
var ( // ErrBlockchainNotFound is thrown when the blockchain database file is not found. ErrBlockchainNotFound = errors.New("blockchain not found") )
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 ¶
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 ¶
CalculateNextDifficulty adjusts the difficulty to find a block's hash every blocksRetargetPeriod blocks.
func CompactToBig ¶
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 ¶
IntToBytes converts an int64 into a byte array.
Types ¶
type Block ¶
Block represents a block in the blockchain.
func NewBlock ¶
NewBlock creates and returns a block without a header hash and nonce. It should be mined before being saved in the databse.
func NewGenesis ¶
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.
type Chain ¶
Chain allows to read/write the blockchain file.
func LoadChain ¶
LoadChain reads the blockchain file and loads the tip of the chain.
Call Close to release the Chain's associated resources when done.
func (*Chain) BestHeight ¶
BestHeight returns the height of the latest block.
func (*Chain) BlocksHashes ¶
BlocksHashes returns a list of hashes of all the blocks in the chain.
func (*Chain) FindTransaction ¶
FindTransaction looks for a transaction by its id.
It returns the block containing the transaction and the transaction itself.
func (*Chain) FindUTXOs ¶
FindUTXOs finds all unspent transaction outputs and returns transactions with spent outputs removed.
func (*Chain) NewIterator ¶
func (c *Chain) NewIterator() *ChainIterator
NewIterator returns a BlockchainIterat
func (*Chain) SignTransaction ¶
SignTransaction signs inputs of a Transaction.
func (*Chain) VerifyTx ¶
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.