Documentation ¶
Index ¶
- type Block
- type BlockChain
- func (bc *BlockChain) AddBlock(transactions []*Transaction)
- func (bc *BlockChain) Close()
- func (bc *BlockChain) FindSpendableOutputs(addr string, amount int) (int, map[string][]int)
- func (bc *BlockChain) FindUTXO(addr string) []TXOutput
- func (bc *BlockChain) FindUnspentTransactions(addr string) []Transaction
- func (bc *BlockChain) Iterator() *blockChainIterator
- type ProofOfWork
- type TXInput
- type TXOutput
- type Transaction
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) FindSpendableOutputs ¶
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
type TXInput ¶
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 ¶
CanUnlockOutputWith specified data
type TXOutput ¶
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 ¶
CanBeUnlockedWith specified data
type Transaction ¶
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