blockchain

package
v0.0.0-...-d2625bb Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2019 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = 18

Difficulty is the mining difficulty.

Variables

This section is empty.

Functions

func ToBytes

func ToBytes(num int64) []byte

ToBytes decodes a int64 into bytes.

Types

type Block

type Block struct {
	Hash         []byte
	Transactions []*Transaction
	PrevHash     []byte
	Nonce        int
}

Block represents a block in the blockchain.

func CreateBlock

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

CreateBlock creates a new block with a hash and returns a referrence to the created block.

func Deserialize

func Deserialize(data []byte) *Block

Deserialize deserializes a byte slice into a new Block and returns a reference to the created Block.

func (*Block) HashTransactions

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

HashTransactions hashes transactions into a byte slice.

func (*Block) Serialize

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

Serialize serializes a block into a byte slice so it can be stored in the db.

type BlockChain

type BlockChain struct {
	PrevHash []byte
	DB       *badger.DB
}

BlockChain is the representation of our blockchain.

func InitBlockChain

func InitBlockChain(address string) *BlockChain

InitBlockChain initializes a new BlockChain with an initial Genesis block or if the blockchain already exists, loads the prevHash.

func (*BlockChain) AddBlock

func (bc *BlockChain) AddBlock(transactions []*Transaction)

AddBlock adds a block to the receiver BlockChain.

func (*BlockChain) FindSpendableOutputs

func (bc *BlockChain) FindSpendableOutputs(pubKeyHash []byte, amount int) (int, map[string][]int)

FindSpendableOutputs ensures enough tokens exists in unspent transaction outputs to cover the amount.

func (*BlockChain) FindTransaction

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

FindTransaction finds a transaction in the Blockchain by ID.

func (*BlockChain) FindUnspentTransactions

func (bc *BlockChain) FindUnspentTransactions(pubKeyHash []byte) []Transaction

FindUnspentTransactions determines how many tokens an address has by finding transactions that have outputs which are not referenced by other inputs.

func (*BlockChain) FindUnspentTxOutputs

func (bc *BlockChain) FindUnspentTxOutputs(pubKeyHash []byte) []TxOutput

FindUnspentTxOutputs finds all unspent transaction outputs that correspond to an address.

func (*BlockChain) NewIterator

func (bc *BlockChain) NewIterator() *Iterator

NewIterator initializes and returns a reference to a new blockchain Iterator from a BlockChain.

func (*BlockChain) NewTransaction

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

NewTransaction initiates a new blockchain transaction.

func (*BlockChain) SignTransaction

func (bc *BlockChain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)

SignTransaction signs a blockchain Transaction.

func (*BlockChain) VerifyTransaction

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

VerifyTransaction verifies a Transaction on the blockchain.

type Iterator

type Iterator struct {
	CurrentHash []byte
	DB          *badger.DB
}

Iterator is a structure used to iterate over blocks in the database.

func (*Iterator) Next

func (iter *Iterator) Next() *Block

Next returns the next Block in a blockchain Iterator (order is reversed).

type ProofOfWork

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

ProofOfWork represents a proof of work.

func NewProof

func NewProof(b *Block) *ProofOfWork

NewProof creates a new proof of work and returns a reference to the new proof of work.

func (*ProofOfWork) InitData

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

InitData initializes a proof of work with provided nonce.

func (*ProofOfWork) Run

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

Run executes a proof of work.

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

Validate verifies that a completed proof of work is valid.

type Transaction

type Transaction struct {
	ID      []byte
	Inputs  []TxInput
	Outputs []TxOutput
}

Transaction represents a blockchain transaction.

func CoinbaseTx

func CoinbaseTx(to, data string) *Transaction

CoinbaseTx is a transfer for rewarding an account for mining a block.

func (Transaction) GenerateHash

func (tx Transaction) GenerateHash() []byte

GenerateHash generates a sha256 hash from the bytes of a Transaction structure. It is important we do not use a pointer receiver here so that the original Transaction is not modified.

func (*Transaction) IsCoinbase

func (tx *Transaction) IsCoinbase() bool

IsCoinbase verifies if transaction is a Coinbase transaction.

func (*Transaction) Serialize

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

Serialize serializes a Transaction into bytes.

func (*Transaction) SetID

func (tx *Transaction) SetID()

SetID generates a hash id for a transaction.

func (*Transaction) Sign

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

Sign signs a Transaction.

func (*Transaction) String

func (tx *Transaction) String() string

ToString returns a string representation of a Transaction.

func (*Transaction) TrimmedCopy

func (tx *Transaction) TrimmedCopy() Transaction

TrimmedCopy makes a deep copy of a Transaction excluding the signature and public key for each TxInput.

func (*Transaction) Verify

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

Verify verifies a Transaction.

type TxInput

type TxInput struct {
	ID        []byte
	Out       int
	Signature []byte
	PubKey    []byte
}

TxInput represents an input transaction.

func (*TxInput) UsesKey

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

UsesKey verifies that a TxInput has a valid public key.

type TxOutput

type TxOutput struct {
	Value      int
	PubKeyHash []byte
}

TxOutput represents an output transaction.

func CreateTxOutput

func CreateTxOutput(value int, address string) *TxOutput

CreateTxOutput creates a new TxOutput.

func NewTXOutput

func NewTXOutput(value int, address string) *TxOutput

NewTXOutput creates a new output Transaction.

func (*TxOutput) IsLockedWithKey

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

IsLockedWithKey checks to see if output has public key hash equal to given public key hash.

func (*TxOutput) Lock

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

Lock locks TxOutput.

Jump to

Keyboard shortcuts

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