blockchain

package
v0.0.0-...-07263e3 Latest Latest
Warning

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

Go to latest
Published: May 14, 2021 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = 18

For now, the difficulty is static but it will be computed through an algorithm soon.

Variables

This section is empty.

Functions

func DBexists

func DBexists() bool

Checks if the DB exists.

func Handle

func Handle(err error)

func ToHex

func ToHex(num int64) []byte

Tool function that convert an int64 to an array of bytes.

Types

type Block

type Block struct {
	Hash         []byte
	Transactions []*Transaction
	PrevHash     []byte
	Nonce        int
}

Block object that contains byte represntation of the data, the hash of the previous Block object and its current hash.

func CreateBlock

func CreateBlock(txs []*Transaction, prevHash []byte) *Block

Creates a new Block object and fill it with a data string and the previous hash.

func Deserialize

func Deserialize(data []byte) *Block

func Genesis

func Genesis(coinbase *Transaction) *Block

Creates the first BlockChain's Block

func (*Block) HashTransactions

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

func (*Block) Serialize

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

type BlockChain

type BlockChain struct {
	LastHash []byte
	Database *badger.DB
}

BlackChain object that contains all the Block objects.

func ContinueBlockChain

func ContinueBlockChain(address string) *BlockChain

func InitBlockChain

func InitBlockChain(address string) *BlockChain

Initialises the BlockChain struct. It tries to open a data base to load the blockchain.

func (*BlockChain) AddBlock

func (chain *BlockChain) AddBlock(transactions []*Transaction)

Adds a Block object to the a BlockChain object.

func (*BlockChain) FindSpendableOutputs

func (chain *BlockChain) FindSpendableOutputs(address string, amount int) (
	int, map[string][]int)

func (*BlockChain) FindUTXO

func (chain *BlockChain) FindUTXO(address string) []TxOutput

func (*BlockChain) FindUnspentTransactions

func (chain *BlockChain) FindUnspentTransactions(address string) []Transaction

Gets an array of unspent transactions for a given adress.

func (*BlockChain) Iterator

func (chain *BlockChain) Iterator() *BlockChainIterator

Convert the BlockChain struct to an BlockChainIterator struct.

type BlockChainIterator

type BlockChainIterator struct {
	CurrentHash []byte
	Database    *badger.DB
}

Iterator that allows us to go through the blockchain stored in the database.

func (*BlockChainIterator) Next

func (iter *BlockChainIterator) Next() *Block

Gets the Next block which is in reality the previous one since we iterate from the last hash to the beginning.

type ProofOfWork

type ProofOfWork struct {
	Block  *Block
	Target *big.Int
}

ProofOfWork object used to write a block in the blockchain.

func NewProof

func NewProof(b *Block) *ProofOfWork

Produces a pointer to a ProofOfWork object from a pointer to a BLock object.

func (*ProofOfWork) InitData

func (pow *ProofOfWork) InitData(nonce int) []byte

Takes the data from the block.

func (*ProofOfWork) Run

func (pow *ProofOfWork) Run() (int, []byte)

Creates a hash of the data plus the counter. Checks the hash to see if it meets a set of requirements. Creates a counter (nonce) which starts at 0. Requirements: The first few bytes must contain 0s.

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

type Transaction

type Transaction struct {
	ID      []byte
	Inputs  []TxInput
	Outputs []TxOutput
}

func CoinbaseTx

func CoinbaseTx(to, data string) *Transaction

func NewTransaction

func NewTransaction(from, to string, amount int, chain *BlockChain) *Transaction

func (*Transaction) IsCoinbase

func (tx *Transaction) IsCoinbase() bool

func (*Transaction) SetID

func (tx *Transaction) SetID()

type TxInput

type TxInput struct {
	ID  []byte
	Out int
	Sig string
}

func (*TxInput) CanUnlock

func (in *TxInput) CanUnlock(data string) bool

type TxOutput

type TxOutput struct {
	Value  int
	PubKey string
}

func (*TxOutput) CanBeUnlocked

func (out *TxOutput) CanBeUnlocked(data string) bool

Jump to

Keyboard shortcuts

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