Documentation ¶
Index ¶
- type Block
- func (block *Block) AddTransaction(tran *Transaction)
- func (block *Block) AddTransactions(trans []Transaction)
- func (block *Block) FinalizeBlockAt(naunce uint64, timeStampMs uint64)
- func (block *Block) GetBlockHash() [config.HashSize]byte
- func (block *Block) Print() string
- func (block *Block) VerifyBlockHash() bool
- type Blockchain
- func (chain *Blockchain) AcceptBroadcastedTransaction(tran *Transaction)
- func (chain *Blockchain) AddBlock(block *Block) error
- func (chain *Blockchain) BalanceOf(Address *rsa.PublicKey) uint64
- func (chain *Blockchain) GetDifficulty() Difficulty
- func (chain *Blockchain) GetLatestBlock() *Block
- func (chain *Blockchain) GetNLatestBlock(n int) *Block
- func (chain *Blockchain) Print() string
- func (chain *Blockchain) PrintAddressMap() string
- func (chain *Blockchain) PrintBlockList() string
- func (chain *Blockchain) PrintTransactionPool() string
- func (chain *Blockchain) PrintTxMap() string
- func (chain *Blockchain) PrintUTXOMap() string
- func (chain *Blockchain) ReachDifficulty(block *Block) bool
- func (chain *Blockchain) RegisterUser(user rsa.PublicKey, utxoMap map[UTXO]bool)
- func (chain *Blockchain) TransferCoin(from *rsa.PublicKey, to *rsa.PublicKey, amount uint64, fee uint64) (*Transaction, error)
- type Difficulty
- type MADifficulty
- type SimpleDifficulty
- type Transaction
- func (tran *Transaction) GetRawDataToHash() []byte
- func (tran *Transaction) GetRawDataToHashForTest() []byte
- func (tran Transaction) Print() string
- func (tran *Transaction) SignTransaction(signers []*rsa.PrivateKey) error
- func (tran *Transaction) VerifyTransaction(inputAddresses []*rsa.PublicKey) error
- type TransactionInput
- type TransactionOutput
- type UTXO
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Block ¶
type Block struct { Transactions []Transaction // contains filtered or unexported fields }
Block conains a group of valid transactions and the cryptographic hash of the prior block in the blockchain
func CreateFirstBlock ¶
CreateFirstBlock create first block of a chain.
func CreateNextBlock ¶
func CreateNextBlock(prevBlock *Block, timeStamp uint64, minerAddress *rsa.PublicKey, naunce uint64, transactions []Transaction) *Block
CreateNextBlock create next block of a chain.
func CreateNextEmptyBlock ¶
CreateNextEmptyBlock create next empty block of a chain.
func (*Block) AddTransaction ¶
func (block *Block) AddTransaction(tran *Transaction)
AddTransaction add a transaction to current block.
func (*Block) AddTransactions ¶
func (block *Block) AddTransactions(trans []Transaction)
AddTransactions add a series transactions to current block.
func (*Block) FinalizeBlockAt ¶
FinalizeBlockAt Finalize a block with specified timestamp
func (*Block) GetBlockHash ¶
GetBlockHash Get hash value of block
func (*Block) VerifyBlockHash ¶
VerifyBlockHash Verify block hash
type Blockchain ¶
type Blockchain struct { /* fields to support wallet */ AddressMap map[rsa.PublicKey]map[UTXO]bool /* map of all Addresses to their utxo list */ TransactionPool map[string]*Transaction /* all transaction broadcastd by user */ // contains filtered or unexported fields }
A Blockchain contains - a chain of blocks indexed by hash and block index - a set of unspent transaction output - a set of Transactions indexed by tx hash
func InitializeBlockchainWithDiff ¶
func InitializeBlockchainWithDiff(gensisAddress *rsa.PublicKey, diff Difficulty) Blockchain
InitializeBlockchainWithDiff creates a blockchain from scratch
func (*Blockchain) AcceptBroadcastedTransaction ¶
func (chain *Blockchain) AcceptBroadcastedTransaction(tran *Transaction)
AcceptBroadcastedTransaction Accept transaction which broadchated by others.
func (*Blockchain) AddBlock ¶
func (chain *Blockchain) AddBlock(block *Block) error
AddBlock Append the block to the end of the chain. TODO: Support appending the block to a block that is a few blocks ahead of the end
func (*Blockchain) BalanceOf ¶
func (chain *Blockchain) BalanceOf(Address *rsa.PublicKey) uint64
BalanceOf Check the balance of an Address
func (*Blockchain) GetDifficulty ¶
func (chain *Blockchain) GetDifficulty() Difficulty
GetDifficulty Get difficulty
func (*Blockchain) GetLatestBlock ¶
func (chain *Blockchain) GetLatestBlock() *Block
GetLatestBlock Get the latest block
func (*Blockchain) GetNLatestBlock ¶
func (chain *Blockchain) GetNLatestBlock(n int) *Block
GetNLatestBlock Get specified amount of latest blocks.
func (*Blockchain) PrintAddressMap ¶
func (chain *Blockchain) PrintAddressMap() string
PrintAddressMap Print information of 'AddressMap' in a chain.
func (*Blockchain) PrintBlockList ¶
func (chain *Blockchain) PrintBlockList() string
PrintBlockList Print information of 'blockList' in a chain.
func (*Blockchain) PrintTransactionPool ¶
func (chain *Blockchain) PrintTransactionPool() string
PrintTransactionPool Print details information of transactions in a chain.
func (*Blockchain) PrintTxMap ¶
func (chain *Blockchain) PrintTxMap() string
PrintTxMap Print information of 'TxMap' in a chain.
func (*Blockchain) PrintUTXOMap ¶
func (chain *Blockchain) PrintUTXOMap() string
PrintUTXOMap Print information of 'utxoMap' in a chain.
func (*Blockchain) ReachDifficulty ¶
func (chain *Blockchain) ReachDifficulty(block *Block) bool
ReachDifficulty Check whether the chain has reach difficulty
func (*Blockchain) RegisterUser ¶
func (chain *Blockchain) RegisterUser(user rsa.PublicKey, utxoMap map[UTXO]bool)
RegisterUser Register user
func (*Blockchain) TransferCoin ¶
func (chain *Blockchain) TransferCoin(from *rsa.PublicKey, to *rsa.PublicKey, amount uint64, fee uint64) (*Transaction, error)
TransferCoin Make a transaction to transfer coins from one account to target Address. Return nil if there is insufficient fund or amount is zero Note that the transaction is unsigned
type Difficulty ¶
type Difficulty interface { ReachDifficulty(hash [config.HashSize]byte) bool UpdateDifficulty(usedTimeMs uint64) error Print() string }
Difficulty used for encapsulate check/update/print functions relevant the calcuate difficulty.
func CreateMADifficulty ¶
func CreateMADifficulty(targetBlockIntervalMs uint64, prob float64, maSamples uint32) Difficulty
CreateMADifficulty Create a MADifficulty
func CreateSimpleDifficulty ¶
func CreateSimpleDifficulty(targetBlockIntervalMs uint64, prob float64) Difficulty
CreateSimpleDifficulty Create a 'SimpleDifficulty'
type MADifficulty ¶
type MADifficulty struct {
// contains filtered or unexported fields
}
MADifficulty Moving average difficulty algorithm Note that BCH's new difficulty algorithm is similar to the algorithm with - target interval is 10mins - maSamples is 144 That is moving average over the difficulty in a day
func (*MADifficulty) ReachDifficulty ¶
func (d *MADifficulty) ReachDifficulty(hash [config.HashSize]byte) bool
ReachDifficulty Check whether the block has reached the difficulty
func (*MADifficulty) UpdateDifficulty ¶
func (d *MADifficulty) UpdateDifficulty(usedTimeMs uint64) error
UpdateDifficulty Update the difficulty
type SimpleDifficulty ¶
type SimpleDifficulty struct {
// contains filtered or unexported fields
}
SimpleDifficulty A simple wrapper of difficulty.
func (*SimpleDifficulty) Print ¶
func (d *SimpleDifficulty) Print() string
Print details of a SimpleDifficulty
func (*SimpleDifficulty) ReachDifficulty ¶
func (d *SimpleDifficulty) ReachDifficulty(hash [config.HashSize]byte) bool
ReachDifficulty Check whether the block has reached the difficulty
func (*SimpleDifficulty) UpdateDifficulty ¶
func (d *SimpleDifficulty) UpdateDifficulty(usedTimeMs uint64) error
UpdateDifficulty Update the difficulty
type Transaction ¶
type Transaction struct { ID string Inputs []TransactionInput Outputs []TransactionOutput Sender rsa.PublicKey }
Transaction contains a list of Inputs and Outputs. To become a valid transation, it must contain the all signatures from all users
func CreateTransaction ¶
func CreateTransaction(ninput int, noutput int) Transaction
CreateTransaction create a transaction with specified count of inputs and outputs
func (*Transaction) GetRawDataToHash ¶
func (tran *Transaction) GetRawDataToHash() []byte
GetRawDataToHash Get the raw data to hash the whole transaction
func (*Transaction) GetRawDataToHashForTest ¶
func (tran *Transaction) GetRawDataToHashForTest() []byte
GetRawDataToHashForTest Get the raw data to hash the whole transaction
func (*Transaction) SignTransaction ¶
func (tran *Transaction) SignTransaction(signers []*rsa.PrivateKey) error
SignTransaction Sign a transaction in place (in practice, it should be called by each signer individually)
func (*Transaction) VerifyTransaction ¶
func (tran *Transaction) VerifyTransaction(inputAddresses []*rsa.PublicKey) error
VerifyTransaction Verify whether a transaction has valid signatures. Note that it doesn't verify whether the transaction is valid in the chain.
type TransactionInput ¶
type TransactionInput struct { PrevtxMap [config.HashSize]byte OutputIndex uint32 Signature []byte }
TransactionInput is a reference to an output of a previous transaction
func (TransactionInput) Print ¶
func (input TransactionInput) Print() string
Print details of transaction input
type TransactionOutput ¶
TransactionOutput contains instructions for sending bitcoins.
func (TransactionOutput) Print ¶
func (output TransactionOutput) Print() string
Print details of transaction output