core

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: 21 Imported by: 0

Documentation

Overview

This module contains the definition and implementation of the Database structure and its methods

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DBExists

func DBExists() bool

A function to check if the DB exists

func NewBlock

func NewBlock(
	merkle *MerkleBuilder, priori primitives.Hash, height int,
	origin primitives.Address, weave []byte) *primitives.Block

A constructor function that generates and returns a new Block that has been minted for a given merkle builder, previous block hash, block height and a coinbase address.

func NewBlockHeader

func NewBlockHeader(priori primitives.Hash, root primitives.Hash, weave []byte) *primitives.BlockHeader

A constructor function that generates and returns a BlockHeader for a given priori hash, merkle root and weave net address.

func NewCoinbaseTransaction

func NewCoinbaseTransaction(to primitives.Address) *primitives.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 primitives.Address, amount int, chain *BlockChain) *primitives.Transaction

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

func NewTxOutput

func NewTxOutput(value int, address primitives.Address) *primitives.TXO

A constructor function that generates and returns a new transaction output given a token value and address

Types

type BlockChain

type BlockChain struct {
	// Represents the database where the chain is stored
	DB *DatabaseClient

	// Represents the hash of the latest block
	ChainHead primitives.Hash

	// Represents the number of block on the chain (last block height+1)
	ChainHeight int
}

A structure that represents the 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 primitives.Address) (*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]primitives.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 []*primitives.Transaction, addr primitives.Address) *primitives.Block

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

func (*BlockChain) CollectSpendableUTXOS

func (chain *BlockChain) CollectSpendableUTXOS(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) FetchUTXOS

func (chain *BlockChain) FetchUTXOS(publickeyhash []byte) primitives.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) (primitives.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 *primitives.Transaction, privatekey ecdsa.PrivateKey)

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

func (*BlockChain) UpdateUTXOS

func (chain *BlockChain) UpdateUTXOS(block *primitives.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 *primitives.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 {
	Cursor   []byte          // Represents the hash of the block that the iterator is currently on
	Database *DatabaseClient // Represents the reference to the chain database
}

A structure that represents an Iterator for the BlockChain

func NewIterator

func NewIterator(chain *BlockChain) *BlockChainIterator

A constructor function that generates and returns an iterator for the BlockChain

func (*BlockChainIterator) Next

func (iter *BlockChainIterator) Next() *primitives.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 DatabaseClient

type DatabaseClient struct {
	Client *badger.DB
	IsOpen bool
}

func NewDatabaseClient

func NewDatabaseClient() *DatabaseClient

A constructor function that generates and returns a new Database object that has been opened

func (*DatabaseClient) Close

func (db *DatabaseClient) Close()

A method of Database that closes the BadgerDB client

func (*DatabaseClient) CloseOnDeath

func (db *DatabaseClient) CloseOnDeath()

A method of Database that closes the connection of the BadgerDB client upon the runtime closing abruptly

func (*DatabaseClient) DeleteKeyPrefix

func (db *DatabaseClient) DeleteKeyPrefix(prefix []byte)

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

func (*DatabaseClient) Open

func (db *DatabaseClient) Open(opts badger.Options)

A method of Database that opens the BadgerDB client with the given badger DB options

type MerkleBuilder

type MerkleBuilder struct {
	// Represents the channel that accepts transactions to add to the Merkle Tree
	BuildQueue chan *primitives.Transaction

	// Represents the wait group for the tree builder tasks
	BuildGroup sync.WaitGroup

	// Represents the root hash of the Merkle Tree
	MerkleRoot primitives.Hash

	// Represents the Transaction inside the Merkle Tree
	Transactions []*primitives.Transaction

	// Represents the number of Transaction inside the Merkle Tree
	Count int
}

A structure that represents a Merkle Tree Builder

func NewMerkleBuilder

func NewMerkleBuilder() *MerkleBuilder

A constructor function that generates and returns a MerkleBuilder with its initialized fields

func (*MerkleBuilder) Build

func (mb *MerkleBuilder) Build()

A method of MerkleBuilder that begins the construction of the merkle tree based on the Transaction recieved on its build queue. The transaction are accumulated into the structure and the resulting merkle root is stored. Wait on the BuildGroup field to confirm the build completion

func (*MerkleBuilder) BuildWithTransactions

func (mb *MerkleBuilder) BuildWithTransactions(txns []*primitives.Transaction)

A method of MerkelBuilder that begins the merkle tree construction for a given a slice of Transactions. Internally handles trigger for the build runtime and closing the buildqueue.

type MerkleNode

type MerkleNode struct {
	// Represents the hash data of the left child
	Left primitives.Hash

	// Represents the hash data of the right child
	Right primitives.Hash

	// Represents the hash data of the merkle node
	Data primitives.Hash
}

A structure that represents a Node on the Merkle Tree

func NewMerkleNode

func NewMerkleNode(leftdata, rightdata []byte, isbase bool) *MerkleNode

A constructor function that generates and returns a MerkleNode for a given pair of bytes payloads and flag that indicates if the generated MerkleNode is base node (no children/leaves)

Jump to

Keyboard shortcuts

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