Documentation ¶
Index ¶
- Constants
- func ToBytes(num int64) []byte
- type Block
- type BlockChain
- func (bc *BlockChain) AddBlock(transactions []*Transaction)
- func (bc *BlockChain) FindSpendableOutputs(pubKeyHash []byte, amount int) (int, map[string][]int)
- func (bc *BlockChain) FindTransaction(ID []byte) (Transaction, error)
- func (bc *BlockChain) FindUnspentTransactions(pubKeyHash []byte) []Transaction
- func (bc *BlockChain) FindUnspentTxOutputs(pubKeyHash []byte) []TxOutput
- func (bc *BlockChain) NewIterator() *Iterator
- func (bc *BlockChain) NewTransaction(from, to string, amount int) *Transaction
- func (bc *BlockChain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)
- func (bc *BlockChain) VerifyTransaction(tx *Transaction) bool
- type Iterator
- type ProofOfWork
- type Transaction
- func (tx Transaction) GenerateHash() []byte
- func (tx *Transaction) IsCoinbase() bool
- func (tx *Transaction) Serialize() []byte
- func (tx *Transaction) SetID()
- func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prevTXs map[string]Transaction)
- func (tx *Transaction) String() string
- func (tx *Transaction) TrimmedCopy() Transaction
- func (tx *Transaction) Verify(prevTXs map[string]Transaction) bool
- type TxInput
- type TxOutput
Constants ¶
const Difficulty = 18
Difficulty is the mining difficulty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Block ¶
type Block struct { Hash []byte Transactions []*Transaction PrevHash []byte Nonce int }
Block represents a block in the blockchain.
func CreateBlock ¶
func CreateBlock(txs []*Transaction, prevHash []byte) *Block
CreateBlock creates a new block with a hash and returns a referrence to the created block.
func Deserialize ¶
Deserialize deserializes a byte slice into a new Block and returns a reference to the created Block.
func (*Block) HashTransactions ¶
HashTransactions hashes transactions into a byte slice.
type BlockChain ¶
BlockChain is the representation of our blockchain.
func InitBlockChain ¶
func InitBlockChain(address string) *BlockChain
InitBlockChain initializes a new BlockChain with an initial Genesis block or if the blockchain already exists, loads the prevHash.
func (*BlockChain) AddBlock ¶
func (bc *BlockChain) AddBlock(transactions []*Transaction)
AddBlock adds a block to the receiver BlockChain.
func (*BlockChain) FindSpendableOutputs ¶
FindSpendableOutputs ensures enough tokens exists in unspent transaction outputs to cover the amount.
func (*BlockChain) FindTransaction ¶
func (bc *BlockChain) FindTransaction(ID []byte) (Transaction, error)
FindTransaction finds a transaction in the Blockchain by ID.
func (*BlockChain) FindUnspentTransactions ¶
func (bc *BlockChain) FindUnspentTransactions(pubKeyHash []byte) []Transaction
FindUnspentTransactions determines how many tokens an address has by finding transactions that have outputs which are not referenced by other inputs.
func (*BlockChain) FindUnspentTxOutputs ¶
func (bc *BlockChain) FindUnspentTxOutputs(pubKeyHash []byte) []TxOutput
FindUnspentTxOutputs finds all unspent transaction outputs that correspond to an address.
func (*BlockChain) NewIterator ¶
func (bc *BlockChain) NewIterator() *Iterator
NewIterator initializes and returns a reference to a new blockchain Iterator from a BlockChain.
func (*BlockChain) NewTransaction ¶
func (bc *BlockChain) NewTransaction(from, to string, amount int) *Transaction
NewTransaction initiates a new blockchain transaction.
func (*BlockChain) SignTransaction ¶
func (bc *BlockChain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)
SignTransaction signs a blockchain Transaction.
func (*BlockChain) VerifyTransaction ¶
func (bc *BlockChain) VerifyTransaction(tx *Transaction) bool
VerifyTransaction verifies a Transaction on the blockchain.
type ProofOfWork ¶
ProofOfWork represents a proof of work.
func NewProof ¶
func NewProof(b *Block) *ProofOfWork
NewProof creates a new proof of work and returns a reference to the new proof of work.
func (*ProofOfWork) InitData ¶
func (pow *ProofOfWork) InitData(nonce int) []byte
InitData initializes a proof of work with provided nonce.
func (*ProofOfWork) Validate ¶
func (pow *ProofOfWork) Validate() bool
Validate verifies that a completed proof of work is valid.
type Transaction ¶
Transaction represents a blockchain transaction.
func CoinbaseTx ¶
func CoinbaseTx(to, data string) *Transaction
CoinbaseTx is a transfer for rewarding an account for mining a block.
func (Transaction) GenerateHash ¶
func (tx Transaction) GenerateHash() []byte
GenerateHash generates a sha256 hash from the bytes of a Transaction structure. It is important we do not use a pointer receiver here so that the original Transaction is not modified.
func (*Transaction) IsCoinbase ¶
func (tx *Transaction) IsCoinbase() bool
IsCoinbase verifies if transaction is a Coinbase transaction.
func (*Transaction) Serialize ¶
func (tx *Transaction) Serialize() []byte
Serialize serializes a Transaction into bytes.
func (*Transaction) SetID ¶
func (tx *Transaction) SetID()
SetID generates a hash id for a transaction.
func (*Transaction) Sign ¶
func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prevTXs map[string]Transaction)
Sign signs a Transaction.
func (*Transaction) String ¶
func (tx *Transaction) String() string
ToString returns a string representation of a Transaction.
func (*Transaction) TrimmedCopy ¶
func (tx *Transaction) TrimmedCopy() Transaction
TrimmedCopy makes a deep copy of a Transaction excluding the signature and public key for each TxInput.
func (*Transaction) Verify ¶
func (tx *Transaction) Verify(prevTXs map[string]Transaction) bool
Verify verifies a Transaction.
type TxOutput ¶
TxOutput represents an output transaction.
func CreateTxOutput ¶
CreateTxOutput creates a new TxOutput.
func NewTXOutput ¶
NewTXOutput creates a new output Transaction.
func (*TxOutput) IsLockedWithKey ¶
IsLockedWithKey checks to see if output has public key hash equal to given public key hash.