blockchain

package
v0.0.0-...-557c1a3 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = 18

Variables

This section is empty.

Functions

func DBexists

func DBexists() bool

helper function to check if DB exists

func Handle

func Handle(err error)

func ToHex

func ToHex(num int64) []byte

utility function to convert nonce and difficulty to []bytes

Types

type Block

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

func CreateBlock

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

func DeSerialize

func DeSerialize(data []byte) *Block

func Genesis

func Genesis(coinbase *Transaction) *Block

func (*Block) HashTransactions

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

create hash of transactions inside a block

func (*Block) Serialize

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

type BlockChain

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

func ContinueBlockChain

func ContinueBlockChain(address string) *BlockChain

func InitBlockChain

func InitBlockChain(address string) *BlockChain

func (*BlockChain) AddBlock

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

func (*BlockChain) FindTransaction

func (bc *BlockChain) FindTransaction(ID []byte) (Transaction, error)

func (*BlockChain) FindUTXO

func (chain *BlockChain) FindUTXO() map[string]TxOutputs

func (*BlockChain) Iterator

func (chain *BlockChain) Iterator() *BlockChainIterator

func (*BlockChain) SignTransaction

func (bc *BlockChain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)

func (*BlockChain) VerifyTransaction

func (bc *BlockChain) VerifyTransaction(tx *Transaction) bool

type BlockChainIterator

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

func (*BlockChainIterator) Next

func (iter *BlockChainIterator) Next() *Block

type MerkleNode

type MerkleNode struct {
	Left  *MerkleNode
	Right *MerkleNode
	Data  []byte
}

func NewMerkleNode

func NewMerkleNode(left, right *MerkleNode, data []byte) *MerkleNode

type MerkleTree

type MerkleTree struct {
	RootNode *MerkleNode
}

func NewMerkleTree

func NewMerkleTree(data [][]byte) *MerkleTree

type ProofOfWork

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

func NewProof

func NewProof(b *Block) *ProofOfWork

func (*ProofOfWork) InitData

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

func (*ProofOfWork) Run

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

Main function to run Proof of Work algorithm

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

validate the block using computed nonce in run method

type Transaction

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

func CoinbaseTx

func CoinbaseTx(to, data string) *Transaction

first transaction

func NewTransaction

func NewTransaction(from, to string, amount int, UTXO *UTXOSet) *Transaction

func (*Transaction) Hash

func (tx *Transaction) Hash() []byte

func (*Transaction) IsCoinbase

func (tx *Transaction) IsCoinbase() bool

func (Transaction) Serialize

func (tx Transaction) Serialize() []byte

func (*Transaction) Sign

func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prevTXs map[string]Transaction)

func (Transaction) String

func (tx Transaction) String() string

func (*Transaction) TrimmedCopy

func (tx *Transaction) TrimmedCopy() Transaction

func (*Transaction) Verify

func (tx *Transaction) Verify(prevTXs map[string]Transaction) bool

type TxInput

type TxInput struct {
	ID        []byte // references older output
	Out       int    //index of output if there are many outputs
	Signature []byte
	PubKey    []byte
}

func (*TxInput) UsesKey

func (in *TxInput) UsesKey(pubKeyHash []byte) bool

to check if the account i.e data owns the info inside the output which is referenced by the input

type TxOutput

type TxOutput struct {
	Value      int
	PubKeyHash []byte
}

func NewTXOutput

func NewTXOutput(value int, address string) *TxOutput

func (*TxOutput) IsLockedWithKey

func (out *TxOutput) IsLockedWithKey(pubKeyHash []byte) bool

func (*TxOutput) Lock

func (out *TxOutput) Lock(address []byte)

to check if the account i.e data owns info inside output

type TxOutputs

type TxOutputs struct {
	Outputs []TxOutput
}

func DeserializeOutputs

func DeserializeOutputs(data []byte) TxOutputs

func (TxOutputs) Serialize

func (outs TxOutputs) Serialize() []byte

type UTXOSet

type UTXOSet struct {
	Blockchain *BlockChain
}

func (UTXOSet) CountTransactions

func (u UTXOSet) CountTransactions() int

func (*UTXOSet) DeleteByPrefix

func (u *UTXOSet) DeleteByPrefix(prefix []byte)

func (UTXOSet) FindSpendableOutputs

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

normal transaction not coinbase

func (UTXOSet) FindUTXO

func (u UTXOSet) FindUTXO(pubKeyHash []byte) []TxOutput

func (UTXOSet) Reindex

func (u UTXOSet) Reindex()

func (*UTXOSet) Update

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

Jump to

Keyboard shortcuts

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