blockchain

package
v0.0.0-...-1f6f007 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IntToHex

func IntToHex(n int64) []byte

IntToHex converts an int into a hexadecimal.

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

func DeserializeBlock(d []byte) (*Block, error)

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

func (b *Block) HashTransactions() []byte

HashTransactions returns a hash of the transactions in the block

func (*Block) SerializeBlock

func (b *Block) SerializeBlock() ([]byte, error)

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

func (u *UTXOSet) CountTransactions() int

CountTransactions returns the number of transactions in the UTXO set

func (*UTXOSet) FindSpendableOutputs

func (u *UTXOSet) FindSpendableOutputs(pubkeyHash []byte, amount int) (int, map[string][]int)

FindSpendableOutputs finds and returns unspent outputs to reference in inputs

func (*UTXOSet) FindUTXO

func (u *UTXOSet) FindUTXO(pubKeyHash []byte) []transaction.TXOutput

FindUTXO finds UTXO for a public key hash

func (*UTXOSet) Reindex

func (u *UTXOSet) Reindex()

Reindex rebuilds the UTXO set

func (*UTXOSet) Update

func (u *UTXOSet) Update(block *Block)

Update updates the UTXO set with transactions from the Block The Block is considered to be the tip of a Chain

Jump to

Keyboard shortcuts

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