Documentation
¶
Overview ¶
Copyright (C) 2018 go-dappley authors
This file is part of the go-dappley library.
the go-dappley library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
the go-dappley library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU Gc eneral Public License along with the go-dappley library. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2018 go-dappley authors ¶
This file is part of the go-dappley library.
the go-dappley library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
the go-dappley library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with the go-dappley library. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2018 go-dappley authors ¶
This file is part of the go-dappley library.
the go-dappley library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
the go-dappley library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with the go-dappley library. If not, see <http://www.gnu.org/licenses/>.
Index ¶
- Constants
- Variables
- func CompareTransactionTips(a interface{}, b interface{}) int
- func DeserializeUTXO(d []byte) *utxoIndex
- func GetStoredUtxoMap(db storage.Storage, mapkey string) utxoIndex
- func HashAddress(address []byte) []byte
- func HashPubKey(pubKey []byte) ([]byte, error)
- func IsParentBlock(parentBlk, childBlk *Block) bool
- func IsParentBlockHash(parentBlk, childBlk *Block) bool
- func IsParentBlockHeight(parentBlk, childBlk *Block) bool
- type Address
- type Addresses
- type Block
- func Deserialize(d []byte) *Block
- func FakeNewBlockWithTimestamp(t int64, transactions []*Transaction, parent *Block) *Block
- func GenerateMockBlock() *Block
- func GenerateMockFork(size int, parent *Block) []*Block
- func GenerateMockForkWithInvalidTx(size int, parent *Block) []*Block
- func GenerateMockForkWithValidTx(size int, parent *Block) []*Block
- func NewBlock(transactions []*Transaction, parent *Block) *Block
- func NewGenesisBlock(address string) *Block
- func (blk Block) AddSpendableOutputsAfterNewBlock(mapkey string, db storage.Storage)
- func (b *Block) CalculateHash() Hash
- func (b *Block) CalculateHashWithNonce(nonce int64) Hash
- func (blk Block) ConsumeSpendableOutputsAfterNewBlock(mapkey string, db storage.Storage)
- func (b *Block) FindTransactionById(txid []byte) *Transaction
- func (b *Block) FromProto(pb proto.Message)
- func (b *Block) GetCoinbaseTransaction() *Transaction
- func (b *Block) GetHash() Hash
- func (b *Block) GetHeight() uint64
- func (b *Block) GetNonce() int64
- func (b *Block) GetPrevHash() Hash
- func (b *Block) GetTimestamp() int64
- func (b *Block) GetTransactions() []*Transaction
- func (b *Block) HashTransactions() []byte
- func (b *Block) Rollback(txPool *TransactionPool)
- func (b *Block) Serialize() []byte
- func (b *Block) SetHash(hash Hash)
- func (b *Block) SetNonce(nonce int64)
- func (b *Block) ToProto() proto.Message
- func (blk Block) UpdateUtxoIndexAfterNewBlock(mapkey string, db storage.Storage)
- func (b *Block) VerifyHash() bool
- func (b *Block) VerifyTransactions(utxo utxoIndex) bool
- type BlockHeader
- type BlockHeaderStream
- type BlockPool
- func (pool *BlockPool) BlockRequestCh() chan BlockRequestPars
- func (pool *BlockPool) GetBlockchain() *Blockchain
- func (pool *BlockPool) GetForkPoolHeadBlk() *Block
- func (pool *BlockPool) GetForkPoolTailBlk() *Block
- func (pool *BlockPool) IsHigherThanFork(block *Block) bool
- func (pool *BlockPool) IsParentOfFork(blk *Block) bool
- func (pool *BlockPool) IsTailOfFork(blk *Block) bool
- func (pool *BlockPool) Push(block *Block, pid peer.ID)
- func (pool *BlockPool) ReInitializeForkPool(blk *Block)
- func (pool *BlockPool) ResetForkPool()
- func (pool *BlockPool) SetBlockchain(bc *Blockchain)
- func (pool *BlockPool) VerifyTransactions(utxo utxoIndex) bool
- type BlockRequestPars
- type BlockStream
- type Blockchain
- func CreateBlockchain(address Address, db storage.Storage, consensus Consensus) *Blockchain
- func GenerateMockBlockchain(size int) *Blockchain
- func GenerateMockBlockchainWithCoinbaseTxOnly(size int) *Blockchain
- func GenerateMockBlockchainWithCoinbaseTxOnlyWithConsensus(size int, consensus Consensus) *Blockchain
- func GetBlockchain(db storage.Storage, consensus Consensus) (*Blockchain, error)
- func (bc *Blockchain) AddBlockToDb(block *Block)
- func (bc *Blockchain) AddBlockToTail(block *Block)
- func (bc *Blockchain) FindTransaction(ID []byte) (Transaction, error)
- func (bc *Blockchain) FindTransactionFromIndexBlock(txID []byte, blockId []byte) (Transaction, error)
- func (bc *Blockchain) FindUTXO(pubKeyHash []byte) ([]TXOutput, error)
- func (bc *Blockchain) FindUnspentTransactions(pubKeyHash []byte) ([]Transaction, error)
- func (bc *Blockchain) GetBlockByHash(hash Hash) (*Block, error)
- func (bc *Blockchain) GetBlockPool() *BlockPool
- func (bc *Blockchain) GetConsensus() Consensus
- func (bc *Blockchain) GetDb() storage.Storage
- func (bc *Blockchain) GetMaxHeight() uint64
- func (bc *Blockchain) GetTailBlock() (*Block, error)
- func (bc *Blockchain) GetTailBlockHash() Hash
- func (bc *Blockchain) GetTxPool() *TransactionPool
- func (bc Blockchain) GetUtxoStateAtBlockHash(db storage.Storage, hash []byte) (utxoIndex, error)
- func (bc *Blockchain) IsHigherThanBlockchain(block *Block) bool
- func (bc *Blockchain) IsInBlockchain(hash Hash) bool
- func (bc *Blockchain) Iterator() *Blockchain
- func (bc *Blockchain) MergeFork()
- func (bc *Blockchain) Next() (*Block, error)
- func (bc *Blockchain) NextFromIndex(indexHash []byte) (*Block, error)
- func (bc *Blockchain) Rollback(targetHash Hash) bool
- func (bc *Blockchain) SetConsensus(consensus Consensus)
- func (bc *Blockchain) SetTailBlockHash(tailBlockHash Hash)
- func (bc *Blockchain) String() string
- type Consensus
- type Hash
- type KeyPair
- type NetService
- type RcvedBlock
- type TXInput
- type TXOutput
- type Transaction
- func MockTransaction() *Transaction
- func NewCoinbaseTX(to, data string, blockHeight uint64) Transaction
- func NewUTXOTransaction(db storage.Storage, from, to Address, amount *common.Amount, ...) (Transaction, error)
- func NewUTXOTransactionforAddBalance(to Address, amount *common.Amount, keyPair KeyPair, bc *Blockchain) (Transaction, error)
- func (tx *Transaction) FindAllTxinsInUtxoPool(utxoPool utxoIndex) map[string]TXOutput
- func (tx *Transaction) FromProto(pb proto.Message)
- func (tx *Transaction) GetPrevTransactions(bc *Blockchain) map[string]Transaction
- func (tx *Transaction) Hash() []byte
- func (tx Transaction) IsCoinbase() bool
- func (tx Transaction) Serialize() []byte
- func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prevTXs map[string]Transaction)
- func (tx Transaction) String() string
- func (tx *Transaction) ToProto() proto.Message
- func (tx *Transaction) TrimmedCopy() Transaction
- func (tx *Transaction) Verify(utxo utxoIndex) bool
- func (tx *Transaction) VerifySignatures(prevTXs map[string]TXOutput) bool
- type TransactionPool
- func (txPool *TransactionPool) ConditionalAdd(tx Transaction)
- func (txPool *TransactionPool) FilterAllTransactions(utxoPool utxoIndex)
- func (txPool *TransactionPool) PopSortedTransactions() []*Transaction
- func (txPool *TransactionPool) PushTransaction(msg string)
- func (txPool *TransactionPool) RemoveMultipleTransactions(txs []*Transaction)
- func (txPool *TransactionPool) Start()
- func (txPool *TransactionPool) Stop()
- func (txPool *TransactionPool) StructDelete(tx interface{})
- func (txPool *TransactionPool) StructPush(val interface{})
- func (txPool *TransactionPool) Traverse(txHandler func(tx Transaction) bool)
- type TxIndex
- type UTXOutputStored
Constants ¶
const BlockPoolMaxSize = 100
const TransactionPoolLimit = 5
const UtxoForkMapKey = "utxoFork"
const UtxoMapKey = "utxo"
Variables ¶
var ( ErrBlockDoesNotExist = errors.New("ERROR: Block does not exist in blockchain") ErrTransactionNotFound = errors.New("ERROR: Transaction not found") )
var ( ErrInsufficientFund = errors.New("ERROR: The balance is insufficient") ErrInvalidAmount = errors.New("ERROR: Amount is invalid (must be > 0)") )
Functions ¶
func CompareTransactionTips ¶
func CompareTransactionTips(a interface{}, b interface{}) int
func DeserializeUTXO ¶
func DeserializeUTXO(d []byte) *utxoIndex
func GetStoredUtxoMap ¶
func HashAddress ¶
func HashPubKey ¶
func IsParentBlock ¶
func IsParentBlockHash ¶
func IsParentBlockHeight ¶
Types ¶
type Address ¶
type Address struct {
Address string
}
func NewAddress ¶
func (Address) ValidateAddress ¶
type Block ¶
type Block struct {
// contains filtered or unexported fields
}
func Deserialize ¶
func FakeNewBlockWithTimestamp ¶
func FakeNewBlockWithTimestamp(t int64, transactions []*Transaction, parent *Block) *Block
func GenerateMockBlock ¶
func GenerateMockBlock() *Block
func GenerateMockFork ¶
the first item is the tail of the fork
func GenerateMockForkWithInvalidTx ¶
the first item is the tail of the fork
func GenerateMockForkWithValidTx ¶
the first item is the tail of the fork
func NewBlock ¶
func NewBlock(transactions []*Transaction, parent *Block) *Block
func NewGenesisBlock ¶
func (Block) AddSpendableOutputsAfterNewBlock ¶
func (*Block) CalculateHash ¶
func (*Block) CalculateHashWithNonce ¶
func (Block) ConsumeSpendableOutputsAfterNewBlock ¶
func (*Block) FindTransactionById ¶
func (b *Block) FindTransactionById(txid []byte) *Transaction
func (*Block) GetCoinbaseTransaction ¶
func (b *Block) GetCoinbaseTransaction() *Transaction
func (*Block) GetPrevHash ¶
func (*Block) GetTimestamp ¶
func (*Block) GetTransactions ¶
func (b *Block) GetTransactions() []*Transaction
func (*Block) HashTransactions ¶
func (*Block) Rollback ¶
func (b *Block) Rollback(txPool *TransactionPool)
func (Block) UpdateUtxoIndexAfterNewBlock ¶
func (*Block) VerifyHash ¶
func (*Block) VerifyTransactions ¶
type BlockHeader ¶
type BlockHeader struct {
// contains filtered or unexported fields
}
func (*BlockHeader) FromProto ¶
func (bh *BlockHeader) FromProto(pb proto.Message)
func (*BlockHeader) ToProto ¶
func (bh *BlockHeader) ToProto() proto.Message
type BlockHeaderStream ¶
type BlockPool ¶
type BlockPool struct {
// contains filtered or unexported fields
}
func NewBlockPool ¶
func (*BlockPool) BlockRequestCh ¶
func (pool *BlockPool) BlockRequestCh() chan BlockRequestPars
func (*BlockPool) GetBlockchain ¶
func (pool *BlockPool) GetBlockchain() *Blockchain
func (*BlockPool) GetForkPoolHeadBlk ¶
func (*BlockPool) GetForkPoolTailBlk ¶
func (*BlockPool) IsHigherThanFork ¶
func (*BlockPool) IsParentOfFork ¶
func (*BlockPool) IsTailOfFork ¶
func (*BlockPool) ReInitializeForkPool ¶
func (*BlockPool) ResetForkPool ¶
func (pool *BlockPool) ResetForkPool()
func (*BlockPool) SetBlockchain ¶
func (pool *BlockPool) SetBlockchain(bc *Blockchain)
func (*BlockPool) VerifyTransactions ¶
Verify all transactions in a fork
type BlockRequestPars ¶
type BlockRequestPars struct {
BlockHash Hash
Pid peer.ID
}
type BlockStream ¶
type BlockStream struct {
Header *BlockHeaderStream
Transactions []*Transaction
Height uint64
}
type Blockchain ¶
type Blockchain struct {
// contains filtered or unexported fields
}
func CreateBlockchain ¶
func CreateBlockchain(address Address, db storage.Storage, consensus Consensus) *Blockchain
CreateBlockchain creates a new blockchain db
func GenerateMockBlockchain ¶
func GenerateMockBlockchain(size int) *Blockchain
func GenerateMockBlockchainWithCoinbaseTxOnly ¶
func GenerateMockBlockchainWithCoinbaseTxOnly(size int) *Blockchain
func GenerateMockBlockchainWithCoinbaseTxOnlyWithConsensus ¶
func GenerateMockBlockchainWithCoinbaseTxOnlyWithConsensus(size int, consensus Consensus) *Blockchain
func GetBlockchain ¶
func GetBlockchain(db storage.Storage, consensus Consensus) (*Blockchain, error)
func (*Blockchain) AddBlockToDb ¶ added in v0.1.1
func (bc *Blockchain) AddBlockToDb(block *Block)
record the new block in the database
func (*Blockchain) AddBlockToTail ¶ added in v0.1.1
func (bc *Blockchain) AddBlockToTail(block *Block)
func (*Blockchain) FindTransaction ¶
func (bc *Blockchain) FindTransaction(ID []byte) (Transaction, error)
TODO: optimize performance
func (*Blockchain) FindTransactionFromIndexBlock ¶
func (bc *Blockchain) FindTransactionFromIndexBlock(txID []byte, blockId []byte) (Transaction, error)
func (*Blockchain) FindUnspentTransactions ¶
func (bc *Blockchain) FindUnspentTransactions(pubKeyHash []byte) ([]Transaction, error)
TODO: optimize performance
func (*Blockchain) GetBlockByHash ¶
func (bc *Blockchain) GetBlockByHash(hash Hash) (*Block, error)
func (*Blockchain) GetBlockPool ¶ added in v0.1.1
func (bc *Blockchain) GetBlockPool() *BlockPool
func (*Blockchain) GetConsensus ¶ added in v0.1.1
func (bc *Blockchain) GetConsensus() Consensus
func (*Blockchain) GetDb ¶ added in v0.1.1
func (bc *Blockchain) GetDb() storage.Storage
func (*Blockchain) GetMaxHeight ¶
func (bc *Blockchain) GetMaxHeight() uint64
func (*Blockchain) GetTailBlock ¶
func (bc *Blockchain) GetTailBlock() (*Block, error)
func (*Blockchain) GetTailBlockHash ¶ added in v0.1.1
func (bc *Blockchain) GetTailBlockHash() Hash
func (*Blockchain) GetTxPool ¶ added in v0.1.1
func (bc *Blockchain) GetTxPool() *TransactionPool
func (Blockchain) GetUtxoStateAtBlockHash ¶
func (bc Blockchain) GetUtxoStateAtBlockHash(db storage.Storage, hash []byte) (utxoIndex, error)
input db and block hash, output utxoindex state @block hash block
func (*Blockchain) IsHigherThanBlockchain ¶ added in v0.1.1
func (bc *Blockchain) IsHigherThanBlockchain(block *Block) bool
func (*Blockchain) IsInBlockchain ¶
func (bc *Blockchain) IsInBlockchain(hash Hash) bool
func (*Blockchain) Iterator ¶
func (bc *Blockchain) Iterator() *Blockchain
func (*Blockchain) MergeFork ¶
func (bc *Blockchain) MergeFork()
func (*Blockchain) Next ¶
func (bc *Blockchain) Next() (*Block, error)
func (*Blockchain) NextFromIndex ¶
func (bc *Blockchain) NextFromIndex(indexHash []byte) (*Block, error)
func (*Blockchain) Rollback ¶ added in v0.1.1
func (bc *Blockchain) Rollback(targetHash Hash) bool
rollback the blockchain to a block with the targetHash
func (*Blockchain) SetConsensus ¶ added in v0.1.1
func (bc *Blockchain) SetConsensus(consensus Consensus)
func (*Blockchain) SetTailBlockHash ¶ added in v0.1.1
func (bc *Blockchain) SetTailBlockHash(tailBlockHash Hash)
func (*Blockchain) String ¶
func (bc *Blockchain) String() string
type Consensus ¶
type Consensus interface {
Validate(block *Block) bool
Start()
Stop()
StartNewBlockMinting()
Setup(NetService, string)
SetTargetBit(int)
}
type KeyPair ¶
type KeyPair struct {
PrivateKey ecdsa.PrivateKey
PublicKey []byte
}
func NewKeyPair ¶
func NewKeyPair() *KeyPair
func (KeyPair) GenerateAddress ¶
type NetService ¶
type NetService interface {
SendBlock(block *Block) error
GetPeerID() peer.ID
GetBlockchain() *Blockchain
}
type RcvedBlock ¶
type RcvedBlock struct {
Block *Block
Pid peer.ID
}
type TXInput ¶
func MockTxInputs ¶
func MockTxInputs() []TXInput
type TXOutput ¶
func MockTxOutputs ¶
func MockTxOutputs() []TXOutput
func (*TXOutput) IsLockedWithKey ¶
type Transaction ¶
func MockTransaction ¶
func MockTransaction() *Transaction
func NewCoinbaseTX ¶
func NewCoinbaseTX(to, data string, blockHeight uint64) Transaction
NewCoinbaseTX creates a new coinbase transaction
func NewUTXOTransaction ¶
func NewUTXOTransaction(db storage.Storage, from, to Address, amount *common.Amount, senderKeyPair KeyPair, bc *Blockchain, tip uint64) (Transaction, error)
NewUTXOTransaction creates a new transaction
func NewUTXOTransactionforAddBalance ¶
func NewUTXOTransactionforAddBalance(to Address, amount *common.Amount, keyPair KeyPair, bc *Blockchain) (Transaction, error)
for add balance
func (*Transaction) FindAllTxinsInUtxoPool ¶
func (tx *Transaction) FindAllTxinsInUtxoPool(utxoPool utxoIndex) map[string]TXOutput
Find the transaction in a utxo pool. Returns true only if all Vins are found in the utxo pool
func (*Transaction) FromProto ¶
func (tx *Transaction) FromProto(pb proto.Message)
func (*Transaction) GetPrevTransactions ¶ added in v0.1.1
func (tx *Transaction) GetPrevTransactions(bc *Blockchain) map[string]Transaction
func (*Transaction) Hash ¶
func (tx *Transaction) Hash() []byte
Hash returns the hash of the Transaction
func (Transaction) IsCoinbase ¶
func (tx Transaction) IsCoinbase() bool
func (Transaction) Serialize ¶
func (tx Transaction) Serialize() []byte
Serialize returns a serialized Transaction
func (*Transaction) Sign ¶
func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prevTXs map[string]Transaction)
Sign signs each input of a Transaction
func (Transaction) String ¶
func (tx Transaction) String() string
String returns a human-readable representation of a transaction
func (*Transaction) ToProto ¶
func (tx *Transaction) ToProto() proto.Message
func (*Transaction) TrimmedCopy ¶
func (tx *Transaction) TrimmedCopy() Transaction
TrimmedCopy creates a trimmed copy of Transaction to be used in signing
func (*Transaction) Verify ¶
func (tx *Transaction) Verify(utxo utxoIndex) bool
return true if the transaction is verified
func (*Transaction) VerifySignatures ¶
func (tx *Transaction) VerifySignatures(prevTXs map[string]TXOutput) bool
Verify verifies signatures of Transaction inputs
type TransactionPool ¶
func GenerateMockTransactionPool ¶
func GenerateMockTransactionPool(numOfTxs int) *TransactionPool
func NewTransactionPool ¶
func NewTransactionPool() *TransactionPool
func (*TransactionPool) ConditionalAdd ¶
func (txPool *TransactionPool) ConditionalAdd(tx Transaction)
func (*TransactionPool) FilterAllTransactions ¶
func (txPool *TransactionPool) FilterAllTransactions(utxoPool utxoIndex)
func (*TransactionPool) PopSortedTransactions ¶ added in v0.1.1
func (txPool *TransactionPool) PopSortedTransactions() []*Transaction
need to optimize
func (*TransactionPool) PushTransaction ¶
func (txPool *TransactionPool) PushTransaction(msg string)
todo: will change the input from string to transaction
func (*TransactionPool) RemoveMultipleTransactions ¶
func (txPool *TransactionPool) RemoveMultipleTransactions(txs []*Transaction)
func (*TransactionPool) Start ¶
func (txPool *TransactionPool) Start()
func (*TransactionPool) Stop ¶
func (txPool *TransactionPool) Stop()
func (*TransactionPool) StructDelete ¶
func (txPool *TransactionPool) StructDelete(tx interface{})
func (*TransactionPool) StructPush ¶
func (txPool *TransactionPool) StructPush(val interface{})
Push a new value into slice
func (*TransactionPool) Traverse ¶
func (txPool *TransactionPool) Traverse(txHandler func(tx Transaction) bool)
function f should return true if the transaction needs to be pushed back to the pool
type UTXOutputStored ¶
func GetAddressUTXOs ¶
func GetAddressUTXOs(mapkey string, pubkey []byte, db storage.Storage) []UTXOutputStored