blockchain

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const WorkDifficulty = 18

Variables

This section is empty.

Functions

func BlockSerialize

func BlockSerialize(block *Block) []byte

A function to serialize a Block into gob of bytes

func DBexists

func DBexists() bool

A function to check if the DB exists

func DBopen

func DBopen() *badger.DB

A function to open the BadgerDB

func Handle

func Handle(err error)

A function to handle errors.

func Hexify

func Hexify(number int64) []byte

A function that generates and returns the Hex/Bytes representation of an int64

func TXOListSerialize

func TXOListSerialize(txos *TXOList) []byte

A function to serialize a UTXO into gob of bytes

func TxnSerialize

func TxnSerialize(txn *Transaction) []byte

A function to serialize a Transaction into gob of bytes

Types

type Block

type Block struct {
	Hash         []byte         // Represents the hash of the Block
	PrevHash     []byte         // Represents the hash of the previous Block
	Transactions []*Transaction // Represents the transaction on the Block
	Nonce        int            // Represents the nonce number that signed the block
	Difficulty   int            // Represent the difficulty value to sign the block
}

A structure that represents a single Block on the Animus BlockChain

func BlockDeserialize

func BlockDeserialize(gobdata []byte) *Block

A function to deserialize a gob of bytes into a Block

func NewBlock

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

A constructor function that generates and returns a new block from th hash of the previous block and a block data. Adds the data to a block and signs it using the PoW algorithm.

func (*Block) Compose

func (block *Block) Compose(nonce int) []byte

A method of Block that composes and returns the block data as slice of bytes for a given nonce number.

Considers the block data, the hash of the previous block, the block work difficulty and the given nonce number.

func (*Block) GenerateProofTarget

func (block *Block) GenerateProofTarget() *big.Int

A method of Block that generates the max value of the hash to sign the block. Returns a big integer

func (*Block) GenerateTransactionsHash

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

A method of Block that generates the hash of all transaction on the block

func (*Block) Sign

func (block *Block) Sign() (int, []byte)

A method of Block that runs the Proof of Work algorithm to generate the hash of the block and to sign it. Returns the nonce number that signed the block and the hash of the block

func (*Block) Validate

func (block *Block) Validate() bool

A method of Block that validates the block signature (hash)

type BlockChain

type BlockChain struct {
	Database *badger.DB // Represents the reference to the chain database
	LastHash []byte     // Represents the hash of the last block on the chain
}

A structure that represents the Animus BlockChain

func AnimateBlockChain

func AnimateBlockChain() (*BlockChain, error)

A constructor function that animates an existing blockchain i.e brings it to life. Returns an error if no Animus Blockchain exists.

func SeedBlockChain

func SeedBlockChain(address string) (*BlockChain, error)

A constructor function that seeds a new blockchain i.e creates one. Returns an error if an Animus Blockchain already exists.

func (*BlockChain) AccumulateUTX0S

func (chain *BlockChain) AccumulateUTX0S() map[string]TXOList

A method of BlockChain that accumulates all unspent transactions on the chain and returns them as a map transaction ID to TXOList.

func (*BlockChain) AddBlock

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

A method of BlockChain that adds a new Block to the chain and returns it

func (*BlockChain) CollectSpendableTXOS

func (chain *BlockChain) CollectSpendableTXOS(publickeyhash []byte, amount int) (int, map[string][]int)

A method of BlockChain that collects the spendable transaction outputs given a public key hash and a target amount upto which to collect.

func (*BlockChain) CountUTXOS

func (chain *BlockChain) CountUTXOS() int

A method of BlockChain that counts the number of unspent transactions stored on the database

func (*BlockChain) DeleteKeyPrefix

func (chain *BlockChain) DeleteKeyPrefix(prefix []byte)

A method of BlockChain that deletes all entries with a given prefix from the Badger DB.

func (*BlockChain) FetchUTXOS

func (chain *BlockChain) FetchUTXOS(publickeyhash []byte) TXOList

A method of BlockChain that fetches the unspent transaction outputs for a given public key hash and returns it in a list of transaction outputs

func (*BlockChain) FindTransaction

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

A method of BlockChain that finds a transaction from the chain given a valid Transaction ID

func (*BlockChain) ReindexUTXOS

func (chain *BlockChain) ReindexUTXOS()

A method of BlockChain that reindexes all the utxo layer keys on the database.

func (*BlockChain) SignTransaction

func (chain *BlockChain) SignTransaction(txn *Transaction, privatekey ecdsa.PrivateKey)

A method of BlockChain that signs a transaction given a private key

func (*BlockChain) UpdateUTXOS

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

A method of BlockChain that updates the utxo layer keys from the transaction of a Block, given the block.

func (*BlockChain) VerifyTransaction

func (chain *BlockChain) VerifyTransaction(txn *Transaction, privatekey ecdsa.PrivateKey) bool

A method of BlockChain that verifies the signature of a transaction given a private key

type BlockChainIterator

type BlockChainIterator struct {
	CursorHash []byte     // Represents the hash of the block that the iterator is currently on
	Database   *badger.DB // Represents the reference to the chain database
}

A structure that represents an Iterator for the Animus BlockChain

func NewIterator

func NewIterator(chain *BlockChain) *BlockChainIterator

A constructor function that generates an iterator for the BlockChain

func (*BlockChainIterator) Next

func (iter *BlockChainIterator) Next() *Block

A method of BlockChainIterator that iterates over chain and returns the next block on the chain (backwards) from the chain DB and returns it

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(merkledata [][]byte) *MerkleTree

type TXOList

type TXOList []TxOutput

func TXOListDeserialize

func TXOListDeserialize(gobdata []byte) *TXOList

A function to deserialize a gob of bytes into a UTXO

type Transaction

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

A structure that represents a transaction on the Animus Blockchain

func NewCoinbaseTransaction

func NewCoinbaseTransaction(to string) *Transaction

A constructor function that generates and returns a coinbase Transaction. A Coinbase transaction refers to a first transaction on a block and does not refer to any previous output transactions and contains a token reward for the user who signs the block.

func NewTransaction

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

A constructor function that generates and returns a Transaction given the to and from addresses and the amount to transact.

func TxnDeserialize

func TxnDeserialize(gobdata []byte) *Transaction

A function to deserialize a gob of bytes into a Transaction

func (*Transaction) GenerateHash

func (txn *Transaction) GenerateHash() []byte

A method of Transaction that generates a hash of the Transaction

func (*Transaction) GenerateTrimCopy

func (txn *Transaction) GenerateTrimCopy() Transaction

A method of Transaction that generates a trimmed version of the Transaction that does not include the signature and public keys of the transaction inputs

func (*Transaction) IsCoinbaseTxn

func (txn *Transaction) IsCoinbaseTxn() bool

A method of Transaction that checks if it is a Coinbase Transaction

func (*Transaction) Sign

func (txn *Transaction) Sign(privatekey ecdsa.PrivateKey, prevtxns map[string]Transaction)

A method of Transaction that signs the transaction given the private key of the wallet and a map of previous Transaction IDs to their Transactions.

func (*Transaction) String

func (txn *Transaction) String() string

A method of Transaction that generates a string representation of the transaction and all its inputs and outputs.

func (*Transaction) Verify

func (txn *Transaction) Verify(prevtxns map[string]Transaction) bool

A method of Transaction that verifies if the transaction signature is valid for the given of map Transaction IDs to their Transactions.

type TxInput

type TxInput struct {
	ID        []byte // Represents the reference transaction of which the output is a part
	OutIndex  int    // Represents the index of output in the reference transaction
	Signature []byte // Represents the signature of the transaction
	PublicKey []byte // Represents the public key of the sending address
}

A structure that represents the inputs in a transaction which are really just references to previous outputs

func (*TxInput) CheckKey

func (txin *TxInput) CheckKey(publickeyhash []byte) bool

A method of TxInput that checks if the input public key is valid for a given public key hash

type TxOutput

type TxOutput struct {
	Value         int    // Represents the token value of a given transaction output
	PublicKeyHash []byte // Represents the hash of the public key of the recieving address
}

A structure that represents the outputs in a transaction

func NewTxOutput

func NewTxOutput(value int, address string) *TxOutput

A constructor function

func (*TxOutput) CheckLock

func (txout *TxOutput) CheckLock(lockhash []byte) bool

A method of TxOutput that checks if the ouput key hash is valid for a given locking hash

func (*TxOutput) Lock

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

A method of TxOutput that locks the output for a given address

Jump to

Keyboard shortcuts

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