lbc

package module
v0.0.0-...-05c07db Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2019 License: MIT Imports: 11 Imported by: 0

README

Let's Block Chain

Build Status Go Report Card codebeat badge

Let's Block Chain provides a simple block-chain implementation and simple, elegant command line tool write with Golang.

Prerequisites

  • Golang 1.12.1
  • GNU Make

Milestone

  • Basic Prototype
  • PoW with Hashcash
  • Block Chain Persistence
  • Command-Line user interface [with send/get balance/print]
  • Transaction
    • Transaction prototype, send and get balance.
  • Address
  • Network

Thanks

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	// previous block 32bit hash
	Prev []byte
	// 32bit hash for this block
	Hash []byte
	// created time for this block
	Timestamp int64
	// Transactions for this block
	Transactions []*Transaction
	// block version number
	Version int32
	// block nonce
	Nonce int
}

Block represents a block in block-chain

func NewBlock

func NewBlock(transactions []*Transaction, prev []byte) *Block

NewBlock will create a new block with spec data and prev block hash

type BlockChain

type BlockChain struct {
	// contains filtered or unexported fields
}

BlockChain represents a chain of ordered blocks

func NewBlockChain

func NewBlockChain(address string) *BlockChain

NewBlockChain will return a new block-chain, the first block will be Genesis Block

func (*BlockChain) AddBlock

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

AddBlock to this chain

func (*BlockChain) Close

func (bc *BlockChain) Close()

CLose block chain

func (*BlockChain) FindSpendableOutputs

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

FindSpendableOutputs will find the all spendable outputs in current chain, and ensure that they store enough value

func (*BlockChain) FindUTXO

func (bc *BlockChain) FindUTXO(addr string) []TXOutput

FindUTXO will find the specified address's unspent transaction outputs

func (*BlockChain) FindUnspentTransactions

func (bc *BlockChain) FindUnspentTransactions(addr string) []Transaction

FindUnspentTransactions will find the transactions include the unspent transaction output, unspent transaction output represent that these transaction output has not been referenced in any transaction include yet

func (*BlockChain) Iterator

func (bc *BlockChain) Iterator() *blockChainIterator

Iterator used for iteration block chain

type ProofOfWork

type ProofOfWork struct {
	// contains filtered or unexported fields
}

ProofOfWork represents had the auth to generate block, using `Hashcash` algorithm to limit the efficiency of block generation

func NewProofWork

func NewProofWork(b *Block) *ProofOfWork

NewProofWork will create pow for spec block

func (*ProofOfWork) Run

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

Run Hashcash algorithm for generating block, only hash reach the targetBits will return

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

Validate Proof of Work

type TXInput

type TXInput struct {
	TxID      []byte
	VOut      int
	ScriptSig string
}

TXInput represents a simple transaction input, contain output id of the last transaction and the index of this output in that transaction (because a transaction may have multiple outputs), ScriptSig is a script that provides data that unlocks the ScriptPubKey field in the TXOutput

func (*TXInput) CanUnlockOutputWith

func (in *TXInput) CanUnlockOutputWith(unlockingData string) bool

CanUnlockOutputWith specified data

type TXOutput

type TXOutput struct {
	Value        int
	ScriptPubKey string
}

TXOutput represents a simple transaction output, contain a virtual coins value and script to lock these virtual coins, only unlock script can be use these virtual coins

func (*TXOutput) CanBeUnlockedWith

func (out *TXOutput) CanBeUnlockedWith(unlockingData string) bool

CanBeUnlockedWith specified data

type Transaction

type Transaction struct {
	ID   []byte
	VIn  []TXInput
	VOut []TXOutput
}

Transaction use UTXO model, a transaction consists of a combination of inputs(TXInput) and outputs(TXOutput)

func NewCoinBaseTX

func NewCoinBaseTX(to, data string) *Transaction

NewCoinBaseTX will publish new virtual coins

func NewUTXOTransaction

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

NewUTXOTransaction will create new general transaction output, in this step, we need finds all unspent outputs to reference in inputs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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