blockchain

package
v0.0.0-...-d8fc749 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const Difference = 12

Variables

This section is empty.

Functions

func DBexists

func DBexists() bool

func Handle

func Handle(err error)

func ToHex

func ToHex(num int64) []byte

Types

type Block

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

func CreateBlock

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

CreateBlock will create a new block, but not add it to the chain

func Deserialize

func Deserialize(data []byte) *Block

func Genesis

func Genesis(coinbase *Transaction) *Block

Genesis needs to be the first block in a chain, as the first block doesn't have an address to point back to

func (*Block) HashTransactions

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

func (*Block) Serialize

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

type BlockChain

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

BlockChain is an array of block pointers

func ContinueBlockChain

func ContinueBlockChain(address string) *BlockChain

ContinueBlockChain will be called to append to an existing blockchain

func InitBlockChain

func InitBlockChain(address string) *BlockChain

InitBlockChain will be what starts a new blockChain

func (*BlockChain) AddBlock

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

AddBlock Will add a Block type unit to a blockchain

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

func (*BlockChain) Iterator

func (chain *BlockChain) Iterator() *BlockChainIterator

Iterator takes our BlockChain struct and returns it as a BlockCHainIterator struct

type BlockChainIterator

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

func (*BlockChainIterator) Next

func (iterator *BlockChainIterator) Next() *Block

Next will iterate through the BlockChainIterator

type ProofOfWork

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

func NewProofOfWork

func NewProofOfWork(b *Block) *ProofOfWork

func (*ProofOfWork) InitData

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

InitData takes your block and adds a nonce (counter/incrementer) to it.

func (*ProofOfWork) Run

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

Run will hash our data, turn that hash into a big int, and then compare that big int to our Target which is inside the Proof Of Work Struct

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

Validate will check our Run() function performed as expected

type Transaction

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

func CoinbaseTx

func CoinbaseTx(toAddress, data string) *Transaction

CoinbaseTx is the function that will run when someone on a node succesfully "mines" a block. The reward inside as it were.

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
	//ID will find the Transaction that a specific output is inside of
	Out int
	//Out will be the index of the specific output we found within a transaction.
	//For example if a transaction has 4 outputs, we can use this "out" field to specify which output we are looking for
	Sig string
}

TxInput is represntative of a reference to a previous TxOutput

func (*TxInput) CanUnlock

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

type TxOutput

type TxOutput struct {
	Value int
	//Value would be representative of the amount of coins in a transaction
	PubKey string
}

func (*TxOutput) CanBeUnlocked

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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