Documentation ¶
Index ¶
- func IntToHex(n int64) []byte
- type Block
- type Blockchain
- func (bc *Blockchain) AddBlock(block *Block)
- func (bc *Blockchain) AddGenesis(block *Block)
- func (bc *Blockchain) FindPreviousTransactions(tx *transaction.Transaction) (map[string]transaction.Transaction, error)
- func (bc *Blockchain) FindTransaction(ID []byte) (transaction.Transaction, error)
- func (bc *Blockchain) FindUTXO() map[string]transaction.TXOutputs
- func (bc *Blockchain) GetBestHeight() int
- func (bc *Blockchain) GetBlock(blockHash []byte) (Block, error)
- func (bc *Blockchain) GetBlockHashes() [][]byte
- func (bc *Blockchain) Iterator() *BlockchainIterator
- func (bc *Blockchain) MineBlock(transactions []*transaction.Transaction) *Block
- func (bc *Blockchain) VerifyTransaction(tx *transaction.Transaction) bool
- type BlockchainIterator
- type ChainManager
- type MerkleNode
- type MerkleTree
- type ProofOfWork
- type TransactionPool
- type UTXOSet
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Block ¶
type Block struct { Timestamp int64 PrevBlockHash []byte Transactions []*transaction.Transaction Hash []byte Nonce int Height int }
Block is the unit structure of a blockchain. It will store information which will be hashed, the hash of the previous block and the time of its creation
func DeserializeBlock ¶
DeserializeBlock is used to decode the Block before insertion in BoltDB
func NewBlock ¶
func NewBlock(prevBlockHash []byte, transactions []*transaction.Transaction, height int) *Block
NewBlock is the func to create a new block
func (*Block) HashTransactions ¶
HashTransactions returns a hash of the transactions in the block
func (*Block) SerializeBlock ¶
SerializeBlock is used to encode the Block before insertion in BoltDB
type Blockchain ¶
type Blockchain struct { Tip []byte // contains filtered or unexported fields }
Blockchain is an array of blocks. Arrays in Go are ordered by default, which helps with some minor issues
func CreateBlockchain ¶
func CreateBlockchain(address string) (*Blockchain, error)
CreateBlockchain creates a new blockchain
func NewBlockchain ¶
func NewBlockchain(path string) (*Blockchain, error)
NewBlockchain is used to open a db file, check if a Blockchain already existed, if so gets the current blockchain tip, else generates the genesis block and sets it as the tip
func (*Blockchain) AddBlock ¶
func (bc *Blockchain) AddBlock(block *Block)
AddBlock saves the block into the blockchain
func (*Blockchain) AddGenesis ¶
func (bc *Blockchain) AddGenesis(block *Block)
AddGenesis saves the block into the blockchain
func (*Blockchain) FindPreviousTransactions ¶
func (bc *Blockchain) FindPreviousTransactions(tx *transaction.Transaction) (map[string]transaction.Transaction, error)
FindPreviousTransactions is used to get the previous transactions associated with the passed transaction's Vins
func (*Blockchain) FindTransaction ¶
func (bc *Blockchain) FindTransaction(ID []byte) (transaction.Transaction, error)
FindTransaction is used to get a Transaction by the given transaction hash passed as the ID
func (*Blockchain) FindUTXO ¶
func (bc *Blockchain) FindUTXO() map[string]transaction.TXOutputs
FindUTXO finds and returns all unspent transaction outputs
func (*Blockchain) GetBestHeight ¶
func (bc *Blockchain) GetBestHeight() int
GetBestHeight returns the height of the latest block
func (*Blockchain) GetBlock ¶
func (bc *Blockchain) GetBlock(blockHash []byte) (Block, error)
GetBlock finds a block by its hash and returns it
func (*Blockchain) GetBlockHashes ¶
func (bc *Blockchain) GetBlockHashes() [][]byte
GetBlockHashes returns a list of hashes of all the blocks in the chain
func (*Blockchain) Iterator ¶
func (bc *Blockchain) Iterator() *BlockchainIterator
Iterator is the method used to create an iterator, it will be linked to the blockchain tip
func (*Blockchain) MineBlock ¶
func (bc *Blockchain) MineBlock(transactions []*transaction.Transaction) *Block
MineBlock is the method used to mine a block with the provided transactions. The parameter `transactions` passed as a pointer to a slice of transactions
func (*Blockchain) VerifyTransaction ¶
func (bc *Blockchain) VerifyTransaction(tx *transaction.Transaction) bool
VerifyTransaction is used to verify the given transaction
type BlockchainIterator ¶
type BlockchainIterator struct {
// contains filtered or unexported fields
}
BlockchainIterator is the struct defining the iterator used to iterate over all the keys in a boltdb bucket
func (*BlockchainIterator) Next ¶
func (i *BlockchainIterator) Next() *Block
Next is the method used to get the next block while iterating the blockchain
type ChainManager ¶
type ChainManager struct { Chain *Blockchain MemPool TransactionPool UTXOSet *UTXOSet }
func NewChainManager ¶
func NewChainManager(chain *Blockchain, set *UTXOSet) *ChainManager
type MerkleNode ¶
type MerkleNode struct { Left *MerkleNode Right *MerkleNode Data []byte }
MerkleNode represent a Merkle tree node
func NewMerkleNode ¶
func NewMerkleNode(left, right *MerkleNode, data []byte) *MerkleNode
NewMerkleNode creates a new Merkle tree node
type MerkleTree ¶
type MerkleTree struct {
RootNode *MerkleNode
}
MerkleTree represent a Merkle tree
func NewMerkleTree ¶
func NewMerkleTree(data [][]byte) *MerkleTree
NewMerkleTree creates a new Merkle tree from a sequence of data
type ProofOfWork ¶
type ProofOfWork struct {
// contains filtered or unexported fields
}
ProofOfWork is a mechanism used in blockchains. The main ideia is that some hard work has to be done to add a block to the blockchain. This helps maintain the stability of the blockchain database.
func NewProofOfWork ¶
func NewProofOfWork(b *Block) *ProofOfWork
NewProofOfWork is the func that creates a new ProofOfWork struct. We use a big int because later we'll convert a hash into a big int and compare if it's less than the target. The target is like the upper boundary of a range. If a hash is lower than the boundary it's valid. Lowering the boundary makes if more difficult to find a valid hash.
func (*ProofOfWork) Validate ¶
func (pow *ProofOfWork) Validate() bool
Validate is the func that decides whether the proof of work is valid of not
type TransactionPool ¶
type TransactionPool map[string]transaction.Transaction
type UTXOSet ¶
type UTXOSet struct { Chain *Blockchain Mutex *sync.RWMutex }
UTXOSet is the structure which implements the UTXOSet A Set comprised of all the unspent transaction outputs
func (*UTXOSet) CountTransactions ¶
CountTransactions returns the number of transactions in the UTXO set
func (*UTXOSet) FindSpendableOutputs ¶
FindSpendableOutputs finds and returns unspent outputs to reference in inputs