Documentation ¶
Index ¶
- func CheckError(err error)
- func DBexists(path string) bool
- func HandlePanic()
- func ToHex(num int64) []byte
- type Block
- type BlockChain
- func (chain *BlockChain) AddBlock(block *Block)
- func (chain *BlockChain) FindTransaction(ID []byte) (Transaction, error)
- func (chain *BlockChain) FindUnspentTransactions() map[string]TxOutputs
- func (chain *BlockChain) GetBestHeigth() int
- func (chain *BlockChain) GetBlock(blockHash []byte) (Block, error)
- func (chain *BlockChain) GetBlockHashes() [][]byte
- func (chain *BlockChain) Iterator() *BlockChainIterator
- func (chain *BlockChain) MineBlock(transactions []*Transaction) *Block
- func (chain *BlockChain) SingTransaction(tx *Transaction, privKey ecdsa.PrivateKey)
- func (chain *BlockChain) VerifyTransaction(tx *Transaction) bool
- type BlockChainIterator
- type MerkleNode
- type MerkleTree
- type ProofOfWork
- type Transaction
- func (tx *Transaction) Hash() []byte
- func (tx *Transaction) IsCoinBase() bool
- func (tx *Transaction) Serialize() []byte
- func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prexTxs map[string]Transaction)
- func (tx Transaction) String() string
- func (tx *Transaction) TrimmedCopy() Transaction
- func (tx *Transaction) Verify(prevTxs map[string]Transaction) bool
- type TxInput
- type TxOutput
- type TxOutputs
- type UTXOSet
- func (u UTXOSet) CountTransactions() int
- func (u *UTXOSet) DeleteByPrefix(prefix []byte)
- func (u UTXOSet) FindSpendableOutputs(pubKeyHash []byte, amount int) (int, map[string][]int)
- func (u UTXOSet) FindUTXO(pubKeyHash []byte) []TxOutput
- func (u UTXOSet) Reindex()
- func (u *UTXOSet) Update(block *Block)
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 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
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 ¶
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 ¶
HashTransactions will allow to use a hashing mechanism to provide a unique reperesentation of all the transactions
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 ¶
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 ¶
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 TxOutput ¶
type TxOutput struct { Value int // represents the value in tokens PubKeyHash []byte // represents the public key }
func NewTXOutput ¶
NewTXOuput will generate a new output instance
func (*TxOutput) IsLockedWithKey ¶
IsLocked with key will check is the given hash has been locked
type TxOutputs ¶
type TxOutputs struct {
Outputs []TxOutput // represents the outputs in the list of outputs
}
func DeserializeOutputs ¶
Deserialize will deserialize a chunk of bytes into a TxOutputs struct
type UTXOSet ¶
type UTXOSet struct {
BlockChain *BlockChain // represents the blockchain
}
unspent transaction set
func (UTXOSet) CountTransactions ¶
count transactins will count all the transactions of unspent transactions outputs in the blockchain
func (*UTXOSet) DeleteByPrefix ¶
DeleteByPrefix will delete all the data in badger db with the given prefix
func (UTXOSet) FindSpendableOutputs ¶
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 ¶
Find unspent transaction outputs will return all the unspent outputs of the given public key