blockchain

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 31, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = 18

Difficulty constant representing the diffuculty of finding the nonce

Variables

This section is empty.

Functions

func PanicHandle added in v0.0.2

func PanicHandle(err error)

PanicHandle to Panic throw the Error

func ReturnHandle added in v0.0.2

func ReturnHandle(err error) error

ReturnHandle to return throw errors

func ToHex

func ToHex(number int64) []byte

ToHex to convert an integer to Hex

Types

type Block

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

Block structure for the Block type in the blockchain

func CreateBlock

func CreateBlock(txns []*Transaction, previousHash []byte) *Block

CreateBlock to create a block in the blockchain

func Deserialize

func Deserialize(data []byte) *Block

Deserialize to deserialize the output from BadgerDB

func Genesis

func Genesis(coinbase *Transaction) *Block

Genesis to create the genesis block in the blockchain

func (*Block) HashTransactions

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

HashTransactions to hash the transactions in the block

func (*Block) Serialize

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

Serialize to serialize the input to BadgerDB

type BlockChain

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

BlockChain structure for the BlockChain type in the blockchain

func ContinueBlockChain

func ContinueBlockChain(address string) *BlockChain

ContinueBlockChain to continue blockchain validation

func InitBlockChain

func InitBlockChain(address string) *BlockChain

InitBlockChain to initialize the BlockChain

func (*BlockChain) AddBlock

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

AddBlock to add a block to the existing BlockChain

func (*BlockChain) FindSpendableOutputs

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

FindSpendableOutputs to find spendable outputs in the BlockChain

func (*BlockChain) FindTransaction added in v0.0.2

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

FindTransaction to find a transaction by ID in the list of transactions in the blocks

func (*BlockChain) FindUnspentTransactions

func (chain *BlockChain) FindUnspentTransactions(publicKeyHash []byte) []Transaction

FindUnspentTransactions to find unspent transactions in the blockchain

func (*BlockChain) FindUnspentTransactionsOutputs

func (chain *BlockChain) FindUnspentTransactionsOutputs(publicKeyHash []byte) []TxOutput

FindUnspentTransactionsOutputs to find unspent transaction outputs in the blockchain

func (*BlockChain) Iterator

func (chain *BlockChain) Iterator() *ChainIterator

Iterator to initialise a Iterator over badgerDB

func (*BlockChain) SignTransaction added in v0.0.2

func (chain *BlockChain) SignTransaction(tx *Transaction, privateKey ecdsa.PrivateKey)

SignTransaction to sign the transaction that is added to a block

func (*BlockChain) VerifyTransaction added in v0.0.2

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

VerifyTransaction to verify the transactions in a block

type ChainIterator

type ChainIterator struct {
	CurrentHash []byte
	DataBase    *badger.DB
}

ChainIterator structure to iterate the Blocks in badger.DB

func (*ChainIterator) Next

func (iterator *ChainIterator) Next() *Block

Next to navigate to the next Block in badgerDB

type ProofOfWork

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

ProofOfWork structure for the proof of work in mining

func NewProof

func NewProof(block *Block) *ProofOfWork

NewProof to create a new ProofOfWork to mine the Block

func (*ProofOfWork) InitData

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

InitData to initialize the data in the Block

func (*ProofOfWork) Run

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

Run to run the computation for the BlockChain

func (*ProofOfWork) Validate

func (proofOfWork *ProofOfWork) Validate() bool

Validate for validation of the ProofOfWork

type Transaction

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

Transaction structure for the Transaction type in the blockchain

func CoinBaseTx

func CoinBaseTx(to, data string) *Transaction

CoinBaseTx for the coin base transaction

func NewTransaction

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

NewTransaction for creating a new Transaction in the BlockChain

func (*Transaction) Hash added in v0.0.2

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

Hash to create a new hash for the given transaction

func (*Transaction) IsCoinBase

func (tx *Transaction) IsCoinBase() bool

IsCoinBase to check for CoinBase Transaction

func (*Transaction) Serialize added in v0.0.2

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

Serialize to serialize the transaction

func (*Transaction) SetID

func (tx *Transaction) SetID()

SetID to set the ID for the Transaction

func (*Transaction) Sign added in v0.0.2

func (tx *Transaction) Sign(privateKey ecdsa.PrivateKey, previousTXs map[string]Transaction)

Sign to sign the transation block to enable chaining

func (Transaction) String added in v0.0.2

func (tx Transaction) String() string

String to output transaction based output

func (*Transaction) TrimmedCopy added in v0.0.2

func (tx *Transaction) TrimmedCopy() Transaction

TrimmedCopy to create a trimmed copy of the entire transaction

func (*Transaction) Verify added in v0.0.2

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

Verify to verify the signature of the signed transactions

type TxInput

type TxInput struct {
	ID        []byte
	Out       int
	Signature []byte
	PublicKey []byte
}

TxInput structure for Input for the BlockChain

func (*TxInput) UsesKey added in v0.0.2

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

UsesKey to check for unlocking

type TxOutput

type TxOutput struct {
	Value         int
	PublicKeyHash []byte
}

TxOutput structure for Output for the BlockChainw

func NewTxOutput added in v0.0.2

func NewTxOutput(value int, address string) *TxOutput

NewTxOutput to create a new transaction output for the new transaction that is created by every spend

func (*TxOutput) IsLockedWithKey added in v0.0.2

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

IsLockedWithKey to verify that the transaction is locked with only the users public key

func (*TxOutput) Lock added in v0.0.2

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

Lock to lock the transaction from spending without authorisation

type TxOutputs added in v0.0.2

type TxOutputs struct {
	Outputs []TxOutput
}

TxOutputs structure for Transaction Outputs for the Transaction Listing

func DeserializeOutputs added in v0.0.2

func DeserializeOutputs(data []byte) TxOutputs

DeserializeOutputs to deserialize the TxOutputs from badger.DB

func (*TxOutputs) SerializeOutputs added in v0.0.2

func (outs *TxOutputs) SerializeOutputs() []byte

SerializeOutputs to serialize the TxOutputs for badger.DB

type UTXO added in v0.0.2

type UTXO struct {
	// contains filtered or unexported fields
}

UTXO struct for blockchain

func (*UTXO) DeleteByPrefix added in v0.0.2

func (utx *UTXO) DeleteByPrefix(prefix []byte)

DeleteByPrefix to delete persistence by given Prefix

Jump to

Keyboard shortcuts

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