Blockchain

package
v0.0.0-...-64a9ec9 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = 18
View Source
const MaxNonce = math.MaxInt64

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

Method for creating the block(Retuns a block pointer), A block can contain multiple transactions(atleast one)

func Deserialize

func Deserialize(data []byte) *Block

decodes the byte slice into a block pointer

func Genesis

func Genesis(coinbase *Transaction) *Block

Creates the genesis block -- the first block in the blockchain

func (*Block) HashTransactions

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

func (*Block) Serialize

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

encodes the block into a byte slice

type Blockchain

type Blockchain struct {
	LastHash []byte     //The hash of the previous block
	Database *badger.DB //pointer to the database.
}

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 (chain *Blockchain) FindTransaction(ID []byte) (Transaction, error)

func (*Blockchain) FindUTXO

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

func (*Blockchain) Iterator

func (chain *Blockchain) Iterator() *BlockchainIterator

Iterating from the newest to the genesis block(reverse iteration)

func (*Blockchain) SignTransaction

func (chain *Blockchain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)

func (*Blockchain) VerifyTransaction

func (chain *Blockchain) VerifyTransaction(tx *Transaction) bool

type BlockchainIterator

type BlockchainIterator struct {
	CurrentHash []byte //similar to the last hash field
	Database    *badger.DB
}

to iterate over the blockchain

func (*BlockchainIterator) Next

func (iter *BlockchainIterator) Next() *Block

type MerkleNode

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

func NewMerkleNode

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

type MerkleTree

type MerkleTree struct {
	RootNode *MerkleNode
}

func NewMerkleTree

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

type PrivateKey

type PrivateKey struct {
	D *big.Int
	X *big.Int
	Y *big.Int
}

func (PrivateKey) ToECDSA

func (priv PrivateKey) ToECDSA() ecdsa.PrivateKey

type ProofOfWork

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

func NewProof

func NewProof(block *Block) *ProofOfWork

func (*ProofOfWork) InitData

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

func (*ProofOfWork) Run

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

func (*ProofOfWork) Validate

func (proof *ProofOfWork) Validate() bool

Computational part of the algorithm is relatively expensive, validation part is pretty simple. Changing a particular block requires the "expensive" part of the algorithm to be run again, hence the blockchain is secure(tamper-proof).

type Transaction

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

Outpoint is the index of the output in the transaction + the transaction id

func CoinbaseTx

func CoinbaseTx(rec_address string, data string) *Transaction

Coinbase Transaction --> A transaction that creates a new coin, it is the first transaction in a block(rewarding transaction). it has no inputs(no reference to previous outputs and no outpoint) and only one output.

func (*Transaction) HashTransaction

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

func (*Transaction) Is_Coinbase

func (tx *Transaction) Is_Coinbase() bool

func (*Transaction) Serialize

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

func (*Transaction) Sign

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

func (*Transaction) To_String

func (tx *Transaction) To_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 to the previous output that led to the input
	OutputIdx int    //index of the referenced output which is spent in the transaction
	Signature []byte //signature or the sender's public key
	PubKey    []byte //unhashed
}

func (*TxInput) UsesKey

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

type TxOutput

type TxOutput struct {
	Value      int
	PubKeyHash []byte
}

func NewTxOutput

func NewTxOutput(value int, address string) *TxOutput

func (*TxOutput) IsLockedWithKey

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

func (*TxOutput) Lock

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

type TxOutputs

type TxOutputs struct {
	Outputs []TxOutput
}

func DeserializeOutputs

func DeserializeOutputs(data []byte) TxOutputs

func (TxOutputs) SerializeOutputs

func (outputs TxOutputs) SerializeOutputs() []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)

func (UTXOSet) FindUnspentTransactions

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

func (*UTXOSet) NewTransaction

func (UTXO *UTXOSet) NewTransaction(send_address string, rec_address string, amount int) *Transaction

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