Documentation ¶
Index ¶
- Constants
- func DBexists() bool
- func Handle(err error)
- func ToHex(num int64) []byte
- type Block
- type Blockchain
- func (chain *Blockchain) AddBlock(transactions []*Transaction) *Block
- func (chain *Blockchain) FindTransaction(ID []byte) (Transaction, error)
- func (chain *Blockchain) FindUTXO() map[string]TxOutputs
- func (chain *Blockchain) Iterator() *BlockchainIterator
- func (chain *Blockchain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)
- func (chain *Blockchain) VerifyTransaction(tx *Transaction) bool
- type BlockchainIterator
- type MerkleNode
- type MerkleTree
- type PrivateKey
- type ProofOfWork
- type Transaction
- func (tx *Transaction) HashTransaction() []byte
- func (tx *Transaction) Is_Coinbase() bool
- func (tx *Transaction) Serialize() []byte
- func (tx *Transaction) Sign(private_key ecdsa.PrivateKey, prevTXs map[string]Transaction)
- func (tx *Transaction) To_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) FindUnspentTransactions(pubKeyHash []byte) []TxOutput
- func (UTXO *UTXOSet) NewTransaction(send_address string, rec_address string, amount int) *Transaction
- func (u UTXOSet) Reindex()
- func (u UTXOSet) Update(block *Block)
Constants ¶
const Difficulty = 18
const MaxNonce = math.MaxInt64
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Block ¶
type Block struct { Hash []byte Transactions []*Transaction PrevHash []byte Nonce int }
func CreateBlock ¶
func CreateBlock(txs []*Transaction, prevHash []byte) *Block
Method for creating the block(Retuns a block pointer), A block can contain multiple transactions(atleast one)
func Genesis ¶
func Genesis(coinbase *Transaction) *Block
Creates the genesis block -- the first block in the blockchain
func (*Block) HashTransactions ¶
type Blockchain ¶
type Blockchain struct { LastHash []byte //The hash of the previous block Database *badger.DB //pointer to the database. }
func ContinueBlockchain ¶
func ContinueBlockchain(address string) *Blockchain
func InitBlockChain ¶
func InitBlockChain(address string) *Blockchain
func (*Blockchain) AddBlock ¶
func (chain *Blockchain) AddBlock(transactions []*Transaction) *Block
func (*Blockchain) FindTransaction ¶
func (chain *Blockchain) FindTransaction(ID []byte) (Transaction, error)
func (*Blockchain) FindUTXO ¶
func (chain *Blockchain) FindUTXO() map[string]TxOutputs
func (*Blockchain) Iterator ¶
func (chain *Blockchain) Iterator() *BlockchainIterator
Iterating from the newest to the genesis block(reverse iteration)
func (*Blockchain) SignTransaction ¶
func (chain *Blockchain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)
func (*Blockchain) VerifyTransaction ¶
func (chain *Blockchain) VerifyTransaction(tx *Transaction) bool
type BlockchainIterator ¶
type BlockchainIterator struct { CurrentHash []byte //similar to the last hash field Database *badger.DB }
to iterate over the blockchain
func (*BlockchainIterator) Next ¶
func (iter *BlockchainIterator) Next() *Block
type MerkleNode ¶
type MerkleNode struct { Left *MerkleNode Right *MerkleNode Data []byte }
func NewMerkleNode ¶
func NewMerkleNode(left *MerkleNode, right *MerkleNode, data []byte) *MerkleNode
type MerkleTree ¶
type MerkleTree struct {
RootNode *MerkleNode
}
func NewMerkleTree ¶
func NewMerkleTree(data [][]byte) *MerkleTree
type PrivateKey ¶
func (PrivateKey) ToECDSA ¶
func (priv PrivateKey) ToECDSA() ecdsa.PrivateKey
type ProofOfWork ¶
func NewProof ¶
func NewProof(block *Block) *ProofOfWork
func (*ProofOfWork) InitData ¶
func (proof *ProofOfWork) InitData(nonce int) []byte
func (*ProofOfWork) Run ¶
func (proof *ProofOfWork) Run() (int, []byte)
func (*ProofOfWork) Validate ¶
func (proof *ProofOfWork) Validate() bool
Computational part of the algorithm is relatively expensive, validation part is pretty simple. Changing a particular block requires the "expensive" part of the algorithm to be run again, hence the blockchain is secure(tamper-proof).
type Transaction ¶
Outpoint is the index of the output in the transaction + the transaction id
func CoinbaseTx ¶
func CoinbaseTx(rec_address string, data string) *Transaction
Coinbase Transaction --> A transaction that creates a new coin, it is the first transaction in a block(rewarding transaction). it has no inputs(no reference to previous outputs and no outpoint) and only one output.
func (*Transaction) HashTransaction ¶
func (tx *Transaction) HashTransaction() []byte
func (*Transaction) Is_Coinbase ¶
func (tx *Transaction) Is_Coinbase() bool
func (*Transaction) Serialize ¶
func (tx *Transaction) Serialize() []byte
func (*Transaction) Sign ¶
func (tx *Transaction) Sign(private_key ecdsa.PrivateKey, prevTXs map[string]Transaction)
func (*Transaction) To_String ¶
func (tx *Transaction) To_String() string
func (*Transaction) TrimmedCopy ¶
func (tx *Transaction) TrimmedCopy() Transaction
func (*Transaction) Verify ¶
func (tx *Transaction) Verify(prevTxs map[string]Transaction) bool
type TxInput ¶
type TxOutputs ¶
type TxOutputs struct {
Outputs []TxOutput
}
func DeserializeOutputs ¶
func (TxOutputs) SerializeOutputs ¶
type UTXOSet ¶
type UTXOSet struct {
Blockchain *Blockchain
}
func (UTXOSet) CountTransactions ¶
func (UTXOSet) DeleteByPrefix ¶
func (UTXOSet) FindSpendableOutputs ¶
func (UTXOSet) FindUnspentTransactions ¶
func (*UTXOSet) NewTransaction ¶
func (UTXO *UTXOSet) NewTransaction(send_address string, rec_address string, amount int) *Transaction