blockchain

package
v0.0.0-...-2caa6c1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2021 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckError

func CheckError(err error)

CheckError will check if there is any error and then gracefuly shutdown the system

func DBexists

func DBexists(path string) bool

DBexists will check if a badger db already exists in the db path

func HandlePanic

func HandlePanic()

handle panic will recover from panics and then gracefully shutdown the system. This necesary because badger needs time to collect all the garbage in the system

func ToHex

func ToHex(num int64) []byte

Tohex will decode the given number into bytes, set it in the bytes buffer and return the bytes porcion of the buffer

Types

type Block

type Block struct {
	TimeStamp    int64          // reprensents the time when the block was created
	Hash         []byte         // represents the hash of the block
	Transactions []*Transaction // represents the transactions of the block
	PrevHash     []byte         // represents last block hash
	Nonce        int            // represents the diffyculty
	Heigth       int            // represents the heigth of the current block
}

func CreateBlock

func CreateBlock(tsx []*Transaction, prevHash []byte, heigth int) *Block

CreateBlock will generate a new Block instance with a pointer

func Deserialize

func Deserialize(data []byte) *Block

Deserialize will deserialize a chunk of data into a Block struct

func Genesis

func Genesis(coinbase *Transaction) *Block

Genesis will create the first block in the blockchain

func (*Block) HashTransactions

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

HashTransactions will allow to use a hashing mechanism to provide a unique reperesentation of all the transactions

func (*Block) Serialize

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

Serialize will serializer the block struct in to bytes

type BlockChain

type BlockChain struct {
	LastHash []byte     // represents the last hash of the current block
	Database *badger.DB // represents the db where the blocks will be store
}

func ContinueBlockChain

func ContinueBlockChain(nodeId string) *BlockChain

ContinueBlockchain will continue the blockchain with the last hashed block

func InitBLockChain

func InitBLockChain(address, nodeId string) *BlockChain

InitBLockChain will start the blockchain

func (*BlockChain) AddBlock

func (chain *BlockChain) AddBlock(block *Block)

add block will add the block to the db and check if the heigth is the grater in the blockchain

func (*BlockChain) FindTransaction

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

FindTransaction will check in the blockchain if the given transaction ID exists if exits its return else we return a error

func (*BlockChain) FindUnspentTransactions

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

FindUnspentTransactions will find all unspent transactions assing to one address. Unspent transactions are transactions that have outputs wich are not referenced by other inputs. This is important because if there is an output hassent been spent that means that those tokens still exists for a certain user.

func (*BlockChain) GetBestHeigth

func (chain *BlockChain) GetBestHeigth() int

Get BestHeigth will retrieve the larger block heigth

func (*BlockChain) GetBlock

func (chain *BlockChain) GetBlock(blockHash []byte) (Block, error)

Get block will retrieve the block from the db if exists

func (*BlockChain) GetBlockHashes

func (chain *BlockChain) GetBlockHashes() [][]byte

Get block hashes will retrieve a 2 dimensional array of all block hashes in the blockchain

func (*BlockChain) Iterator

func (chain *BlockChain) Iterator() *BlockChainIterator

Iterator will return a new block chain iterator instance whit the blockchain data

func (*BlockChain) MineBlock

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

MineBlock will add a block to the block chain

func (*BlockChain) SingTransaction

func (chain *BlockChain) SingTransaction(tx *Transaction, privKey ecdsa.PrivateKey)

SignTransaction will sign the transaction with the user private key

func (*BlockChain) VerifyTransaction

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

VerifyTransaction will check if the given transaction is valid

type BlockChainIterator

type BlockChainIterator struct {
	CurrentHash []byte // represents the current hash
	Database    *badger.DB
}

func (*BlockChainIterator) Next

func (iter *BlockChainIterator) Next() *Block

Next will return the next block on the list, until the genesis

type MerkleNode

type MerkleNode struct {
	Left  *MerkleNode // represents the left child of the node
	Rigth *MerkleNode // represents the rigth child of the node
	Data  []byte      // represents the data in the node
}

func NewMerkleNode

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

NewMerkleNode will generate a new merkle tree node instance

type MerkleTree

type MerkleTree struct {
	RootNode *MerkleNode // represents the Root of the merkle tree
}

func NewMerkletree

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

NewMerkleTree will generate a new merkle tree instance

type ProofOfWork

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

func NewProof

func NewProof(b *Block) *ProofOfWork

NewProof will create a new Proof of work instance

func (*ProofOfWork) InitData

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

IinitData will create a new byte slice from the block data and return it

func (*ProofOfWork) Run

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

Run will create a hash from the data + countter and then check if the hash meet a set of requirements

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

Validate will check one more time after run that the hash is valid

type Transaction

type Transaction struct {
	ID      []byte     // represents the id of the transaction
	Inputs  []TxInput  // represents the inputs of the transaction
	Outputs []TxOutput // represents the outputs of the transaction
}

func CoinbaseTx

func CoinbaseTx(to, data string) *Transaction

CoinbasTx will generate the coinbase transaction wich is the first transaction in the chain

func DeserializeTransaction

func DeserializeTransaction(data []byte) Transaction

Deseraialze transaction will take the chunk of bytes and decoded them into a transaction struct

func NewTransaction

func NewTransaction(w *wallet.Wallet, to string, amount int, utxo *UTXOSet) *Transaction

NewTransaction will create a new transacion and validate if the user has enough coins to make the transaction

func (*Transaction) Hash

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

Hash will create a new hash with the transaction data

func (*Transaction) IsCoinBase

func (tx *Transaction) IsCoinBase() bool

IsCoinbase will determine if the current transaction is a coinbase based on the data created by default in the coinbase function

func (*Transaction) Serialize

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

Serialze will serialize the transaction struct into bytes

func (*Transaction) Sign

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

Sign will allow to sign and verify the transactions

func (Transaction) String

func (tx Transaction) String() string

String will return a string representation of the transaction

func (*Transaction) TrimmedCopy

func (tx *Transaction) TrimmedCopy() Transaction

trimmed copy will return a copy of the given transaction

func (*Transaction) Verify

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

Verify will check that the current transaction is valid

type TxInput

type TxInput struct {
	ID        []byte // represents the transaction that the output is
	Out       int    // represents the index where the output appears
	Signature []byte // represents the data wich is use in the output pubkey
	PubKey    []byte // represents the public used in the transaction
}

func (*TxInput) UsesKey

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

UsesKey will check if the given publickey in equal to the transaction input pubkey hashed

type TxOutput

type TxOutput struct {
	Value      int    // represents the value in tokens
	PubKeyHash []byte // represents the public key
}

func NewTXOutput

func NewTXOutput(value int, address string) *TxOutput

NewTXOuput will generate a new output instance

func (*TxOutput) IsLockedWithKey

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

IsLocked with key will check is the given hash has been locked

func (*TxOutput) Lock

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

Lock will set a hashed pubkey with base58 algorithm to the output

type TxOutputs

type TxOutputs struct {
	Outputs []TxOutput // represents the outputs in the list of outputs
}

func DeserializeOutputs

func DeserializeOutputs(data []byte) TxOutputs

Deserialize will deserialize a chunk of bytes into a TxOutputs struct

func (TxOutputs) Serialize

func (outs TxOutputs) Serialize() []byte

serialize will serialize the outputs struct into bytes

type UTXOSet

type UTXOSet struct {
	BlockChain *BlockChain // represents the blockchain
}

unspent transaction set

func (UTXOSet) CountTransactions

func (u UTXOSet) CountTransactions() int

count transactins will count all the transactions of unspent transactions outputs in the blockchain

func (*UTXOSet) DeleteByPrefix

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

DeleteByPrefix will delete all the data in badger db with the given prefix

func (UTXOSet) FindSpendableOutputs

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

Find Spendable outputs will enable create normal transactions wich are not coinbase transactions this function will ensure that the user have the coins to make the transaction. something like the amount of coins that the user have

func (UTXOSet) FindUTXO

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

Find unspent transaction outputs will return all the unspent outputs of the given public key

func (UTXOSet) Reindex

func (u UTXOSet) Reindex()

Reindex will delete all the data with the utxoprefix and the rebuild the set inside of the db

func (*UTXOSet) Update

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

update will update the unspent transactions set in the badger db

Jump to

Keyboard shortcuts

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