Documentation ¶
Index ¶
- Constants
- Variables
- func ClearPendingPool()
- func CreateBlockchain(wif string, callback func(*Blockchain))
- func DeleteBlockchain() error
- func FindAllUnpackTransaction() map[string][]*Transaction
- func GetBalance(address string) int64
- func GetBlockHeight() int64
- func GetMaxUnpackNonce(transactions []*Transaction) int64
- func NewGenesisBlock(trans *Transaction, coinbase string, callback func(*Block, *MerkleTree))
- func PrintChain()
- func RegisterSCBTypes()
- func RevertTo(Height int64)
- func SavePendingBlock(block *Block)
- func SaveSinglePendingBlock(block *Block)
- type Account
- func DeserializeAccount(d []byte) *Account
- func FindAccount(accounts []*Account, address string) *Account
- func GetAccount(address string) *Account
- func GetAllAccount() []*Account
- func InitAccount(address string, idx int64) *Account
- func NewAccount(address string, balance, nonce, index int64) *Account
- type AccountIncreaseMent
- type Block
- func (block *Block) DeleteTransactions()
- func (b *Block) GetAccountTree(preprocess bool) *MerkleTree
- func (b *Block) HashTransactions() []byte
- func (block *Block) PreProcessAccountBalance(accounts []*Account) ([]*Account, []*Account)
- func (block *Block) SaveAccounts()
- func (block *Block) SaveTransactions()
- func (b *Block) Serialize() []byte
- func (b *Block) Sign(privKey *elliptic.PrivateKey) *Block
- func (block *Block) VerifyCoinbase() bool
- func (block *Block) VerifyHash() bool
- func (block *Block) VerifyMerkleHash() bool
- func (block *Block) VerifyPow(preprocess bool) bool
- func (block *Block) VerifyPowV2(prevStateTree *MerkleTree) bool
- func (block *Block) VerifyTransaction() bool
- type BlockChainPending
- type BlockChainPendingTail
- type BlockContent
- type BlockHeader
- type Blockchain
- func (bc *Blockchain) AcceptNewBlock(block *Block, st *MerkleTree)
- func (bc *Blockchain) AcceptNewPendingChain(chain *BlockChainPending)
- func (bc *Blockchain) CombineBlock(block *Block)
- func (bc *Blockchain) DeleteTransaction(trans *Transaction)
- func (bc *Blockchain) FindUnpackTransaction(address string) []*Transaction
- func (bc *Blockchain) FindUnpackTransactionById(id []byte) *Transaction
- func (bc *Blockchain) GetBlock(height int64) *Block
- func (bc *Blockchain) GetBlockByHash(hash []byte) *Block
- func (bc *Blockchain) GetBlockByHeight(height int64) *Block
- func (bc *Blockchain) HasBlock(hash []byte) *Block
- func (bc *Blockchain) Iterator() *BlockchainIterator
- func (bc *Blockchain) MineBlock(wif string, transactions []*Transaction, callback func(*Block, *MerkleTree)) *ProofOfWork
- func (bc *Blockchain) SaveTransaction(trans *Transaction)
- func (bc *Blockchain) VerifyBlockHash(b *Block) bool
- type BlockchainIterator
- type BlockchainPendingPool
- func (bcp *BlockchainPendingPool) AcceptBlock(block *Block) *BlockChainPending
- func (bcp *BlockchainPendingPool) ConnectSinglePendingPool(block *Block, rootBlock *Block, pendlingLength byte, blocktailHash []byte) (byte, []byte)
- func (bcp *BlockchainPendingPool) DerivationPendingTree(block *Block) *MerkleTree
- func (bcp *BlockchainPendingPool) FindRootBlock(block *Block) (byte, *Block)
- func (bcp *BlockchainPendingPool) GetBlockPendingChains(block *Block) *BlockChainPending
- func (bcp *BlockchainPendingPool) GetLongChain() *BlockChainPending
- func (bcp *BlockchainPendingPool) MineBlock(wif string, transactions []*Transaction, callback func(*Block, *MerkleTree)) *ProofOfWork
- func (bcp *BlockchainPendingPool) SavePendingBlockDetails(block *Block) (byte, []byte, *Block)
- type MerkleTree
- func (m *MerkleTree) BreadthFirstSerialize() []byte
- func (m *MerkleTree) Depth() int64
- func (m *MerkleTree) DeserializeAccount() []*Account
- func (m *MerkleTree) FindInsertPoint() *Node
- func (m *MerkleTree) GetContentPath(content BlockContent) ([][]byte, error)
- func (m *MerkleTree) GetNodePath(node *Node) ([][]byte, error)
- func (m *MerkleTree) InsertContent(content BlockContent) *MerkleTree
- func (m *MerkleTree) LeafCount() int64
- func (left *MerkleTree) MergeTree(right *MerkleTree) (*MerkleTree, error)
- func (m *MerkleTree) MerkleRoot() []byte
- func (m *MerkleTree) RebuildTree() error
- func (m *MerkleTree) RebuildTreeWith(cs []BlockContent) error
- func (m *MerkleTree) String() string
- func (m *MerkleTree) UpdateNode(node *Node, content BlockContent, paths [][]byte)
- func (m *MerkleTree) UpdateTree(changedAccounts []*Account, newAccounts []*Account) (*MerkleTree, error)
- func (m *MerkleTree) VerifyContent(content BlockContent) (bool, error)
- func (m *MerkleTree) VerifyTree() (bool, error)
- type Node
- type NodeShadow
- type ProofOfWork
- func Mine(wif string, callback func(*Block), processedSign func()) *ProofOfWork
- func NewBlock(transactions []*Transaction, prevBlockHash []byte, height int64, ...) *ProofOfWork
- func NewBlockV2(transactions []*Transaction, prevBlockHash []byte, height int64, ...) *ProofOfWork
- func NewProofOfWork(b *Block) *ProofOfWork
- type Transaction
Constants ¶
const Subsidy = 100
挖矿奖励金
Variables ¶
var (
CURRENT_USER, _ = osuser.Current()
)
Functions ¶
func ClearPendingPool ¶
func ClearPendingPool()
func CreateBlockchain ¶
func CreateBlockchain(wif string, callback func(*Blockchain))
new blockchain with genesis Block
func DeleteBlockchain ¶
func DeleteBlockchain() error
func FindAllUnpackTransaction ¶
func FindAllUnpackTransaction() map[string][]*Transaction
func GetBalance ¶
func GetBlockHeight ¶
func GetBlockHeight() int64
func GetMaxUnpackNonce ¶
func GetMaxUnpackNonce(transactions []*Transaction) int64
func NewGenesisBlock ¶
func NewGenesisBlock(trans *Transaction, coinbase string, callback func(*Block, *MerkleTree))
NewGenesisBlock creates and returns genesis Block
func PrintChain ¶
func PrintChain()
func RegisterSCBTypes ¶
func RegisterSCBTypes()
func SavePendingBlock ¶
func SavePendingBlock(block *Block)
func SaveSinglePendingBlock ¶
func SaveSinglePendingBlock(block *Block)
Types ¶
type Account ¶
func FindAccount ¶
func GetAccount ¶
func GetAllAccount ¶
func GetAllAccount() []*Account
func InitAccount ¶
func NewAccount ¶
type AccountIncreaseMent ¶
func DeserializeAccountIncreasement ¶
func DeserializeAccountIncreasement(d []byte) *AccountIncreaseMent
Deserializes account increasement
func (*AccountIncreaseMent) Serialize ¶
func (a *AccountIncreaseMent) Serialize() []byte
Serializes the account increasement
type Block ¶
type Block struct { Header BlockHeader Transactions []*Transaction Content []byte }
func GetLastBlock ¶
func GetLastBlock() *Block
func GetPendingBlock ¶
func GetSinglePendingBlock ¶
func (*Block) DeleteTransactions ¶
func (block *Block) DeleteTransactions()
func (*Block) GetAccountTree ¶
func (b *Block) GetAccountTree(preprocess bool) *MerkleTree
func (*Block) HashTransactions ¶
Hash transactions with merkle tree
func (*Block) PreProcessAccountBalance ¶
func (*Block) SaveAccounts ¶
func (block *Block) SaveAccounts()
func (*Block) SaveTransactions ¶
func (block *Block) SaveTransactions()
func (*Block) VerifyCoinbase ¶
func (*Block) VerifyHash ¶
func (*Block) VerifyMerkleHash ¶
func (*Block) VerifyPowV2 ¶
func (block *Block) VerifyPowV2(prevStateTree *MerkleTree) bool
func (*Block) VerifyTransaction ¶
type BlockChainPending ¶
type BlockChainPending struct { Head []byte Tail *BlockChainPendingTail }
func (*BlockChainPending) ConvertPendingBlockchain2Blocks ¶
func (bc *BlockChainPending) ConvertPendingBlockchain2Blocks() []*Block
func (*BlockChainPending) GetLastBlock ¶
func (bc *BlockChainPending) GetLastBlock() *Block
type BlockChainPendingTail ¶
type BlockChainPendingTail struct {
// contains filtered or unexported fields
}
type BlockContent ¶
implements the Content interface provided by merkletree and represents the content stored in the tree.
func (BlockContent) CalculateHash ¶
func (t BlockContent) CalculateHash() ([]byte, error)
CalculateHash hashes the values of a TestContent
func (BlockContent) Equals ¶
func (t BlockContent) Equals(other BlockContent) (bool, error)
Equals tests for equality of two Contents
func (BlockContent) IsDup ¶
func (t BlockContent) IsDup() (bool, error)
func (BlockContent) SetDup ¶
func (t BlockContent) SetDup(dup bool) BlockContent
type BlockHeader ¶
type BlockHeader struct { Timestamp int64 Difficulty int64 PrevBlockHash []byte Hash []byte Nonce int64 Height int64 Coinbase string MerkleRootHash []byte MerkleRootAccountHash []byte // MerkleRootAccountGasHash []byte Signature []byte }
func (BlockHeader) HashString ¶
func (h BlockHeader) HashString() string
type Blockchain ¶
type Blockchain struct {
// contains filtered or unexported fields
}
func CreateEmptyBlockchain ¶
func CreateEmptyBlockchain() *Blockchain
new empty blockchain, just the db initialized.
func (*Blockchain) AcceptNewBlock ¶
func (bc *Blockchain) AcceptNewBlock(block *Block, st *MerkleTree)
func (*Blockchain) AcceptNewPendingChain ¶
func (bc *Blockchain) AcceptNewPendingChain(chain *BlockChainPending)
func (*Blockchain) CombineBlock ¶
func (bc *Blockchain) CombineBlock(block *Block)
func (*Blockchain) DeleteTransaction ¶
func (bc *Blockchain) DeleteTransaction(trans *Transaction)
func (*Blockchain) FindUnpackTransaction ¶
func (bc *Blockchain) FindUnpackTransaction(address string) []*Transaction
func (*Blockchain) FindUnpackTransactionById ¶
func (bc *Blockchain) FindUnpackTransactionById(id []byte) *Transaction
func (*Blockchain) GetBlock ¶
func (bc *Blockchain) GetBlock(height int64) *Block
func (*Blockchain) GetBlockByHash ¶
func (bc *Blockchain) GetBlockByHash(hash []byte) *Block
func (*Blockchain) GetBlockByHeight ¶
func (bc *Blockchain) GetBlockByHeight(height int64) *Block
func (*Blockchain) HasBlock ¶
func (bc *Blockchain) HasBlock(hash []byte) *Block
func (*Blockchain) Iterator ¶
func (bc *Blockchain) Iterator() *BlockchainIterator
Iterator returns a BlockchainIterat
func (*Blockchain) MineBlock ¶
func (bc *Blockchain) MineBlock(wif string, transactions []*Transaction, callback func(*Block, *MerkleTree)) *ProofOfWork
MineBlock mines a new block with the provided transactions
func (*Blockchain) SaveTransaction ¶
func (bc *Blockchain) SaveTransaction(trans *Transaction)
func (*Blockchain) VerifyBlockHash ¶
func (bc *Blockchain) VerifyBlockHash(b *Block) bool
type BlockchainIterator ¶
type BlockchainIterator struct {
// contains filtered or unexported fields
}
BlockchainIterator is used to iterate over blockchain blocks
func (*BlockchainIterator) Next ¶
func (i *BlockchainIterator) Next() *Block
Next returns next block starting from the tip
type BlockchainPendingPool ¶
type BlockchainPendingPool struct { Root []byte RootHeight int64 RootStateTree *MerkleTree PendingChains []*BlockChainPending }
func LoadPendingPool ¶
func LoadPendingPool() *BlockchainPendingPool
func (*BlockchainPendingPool) AcceptBlock ¶
func (bcp *BlockchainPendingPool) AcceptBlock(block *Block) *BlockChainPending
func (*BlockchainPendingPool) ConnectSinglePendingPool ¶
func (*BlockchainPendingPool) DerivationPendingTree ¶
func (bcp *BlockchainPendingPool) DerivationPendingTree(block *Block) *MerkleTree
func (*BlockchainPendingPool) FindRootBlock ¶
func (bcp *BlockchainPendingPool) FindRootBlock(block *Block) (byte, *Block)
func (*BlockchainPendingPool) GetBlockPendingChains ¶
func (bcp *BlockchainPendingPool) GetBlockPendingChains(block *Block) *BlockChainPending
func (*BlockchainPendingPool) GetLongChain ¶
func (bcp *BlockchainPendingPool) GetLongChain() *BlockChainPending
func (*BlockchainPendingPool) MineBlock ¶
func (bcp *BlockchainPendingPool) MineBlock(wif string, transactions []*Transaction, callback func(*Block, *MerkleTree)) *ProofOfWork
func (*BlockchainPendingPool) SavePendingBlockDetails ¶
func (bcp *BlockchainPendingPool) SavePendingBlockDetails(block *Block) (byte, []byte, *Block)
type MerkleTree ¶
func GetLastMerkleTree ¶
func GetLastMerkleTree() *MerkleTree
func GetMerkleTreeByHeight ¶
func GetMerkleTreeByHeight(height int64) *MerkleTree
func InitGenesisStateTree ¶
func InitGenesisStateTree(coinbase string) *MerkleTree
func NewTree ¶
func NewTree(cs []BlockContent) (*MerkleTree, error)
func (*MerkleTree) BreadthFirstSerialize ¶
func (m *MerkleTree) BreadthFirstSerialize() []byte
广度优先序列化merkle树
func (*MerkleTree) DeserializeAccount ¶
func (m *MerkleTree) DeserializeAccount() []*Account
func (*MerkleTree) GetContentPath ¶
func (m *MerkleTree) GetContentPath(content BlockContent) ([][]byte, error)
通过回溯兄弟节点获取Content的证明路径
func (*MerkleTree) GetNodePath ¶
func (m *MerkleTree) GetNodePath(node *Node) ([][]byte, error)
通过回溯兄弟节点获取节点的证明路径
func (*MerkleTree) InsertContent ¶
func (m *MerkleTree) InsertContent(content BlockContent) *MerkleTree
merkle插入新的节点
- 若能找到插入节点, 更新此插入节点的content, 并更新回溯路径
- 若未能找到插入点,构建当前merkle 右子树 并与当前树合并
func (*MerkleTree) MergeTree ¶
func (left *MerkleTree) MergeTree(right *MerkleTree) (*MerkleTree, error)
merge 左右两颗结构一致的树为新树
func (*MerkleTree) MerkleRoot ¶
func (m *MerkleTree) MerkleRoot() []byte
func (*MerkleTree) RebuildTree ¶
func (m *MerkleTree) RebuildTree() error
func (*MerkleTree) RebuildTreeWith ¶
func (m *MerkleTree) RebuildTreeWith(cs []BlockContent) error
func (*MerkleTree) String ¶
func (m *MerkleTree) String() string
func (*MerkleTree) UpdateNode ¶
func (m *MerkleTree) UpdateNode(node *Node, content BlockContent, paths [][]byte)
根据证明路径更新节点
func (*MerkleTree) UpdateTree ¶
func (m *MerkleTree) UpdateTree(changedAccounts []*Account, newAccounts []*Account) (*MerkleTree, error)
func (*MerkleTree) VerifyContent ¶
func (m *MerkleTree) VerifyContent(content BlockContent) (bool, error)
func (*MerkleTree) VerifyTree ¶
func (m *MerkleTree) VerifyTree() (bool, error)
type Node ¶
type Node struct { Parent *Node Left *Node Right *Node Hash []byte C BlockContent // contains filtered or unexported fields }
type NodeShadow ¶
type NodeShadow struct { Leaf bool Dup bool Virtual bool Hash []byte C BlockContent }
func DeserializeNode ¶
func DeserializeNode(d []byte) *NodeShadow
func (*NodeShadow) Serialize ¶
func (n *NodeShadow) Serialize() []byte
type ProofOfWork ¶
type ProofOfWork struct { Cancelled chan struct{} Done chan []byte // contains filtered or unexported fields }
ProofOfWork represents a proof-of-work
func Mine ¶
func Mine(wif string, callback func(*Block), processedSign func()) *ProofOfWork
func NewBlock ¶
func NewBlock(transactions []*Transaction, prevBlockHash []byte, height int64, coinbase string, callback func(*Block, *MerkleTree)) *ProofOfWork
NewBlock creates and returns Block
func NewBlockV2 ¶
func NewBlockV2(transactions []*Transaction, prevBlockHash []byte, height int64, coinbase string, prevStateTree *MerkleTree, callback func(*Block, *MerkleTree)) *ProofOfWork
func NewProofOfWork ¶
func NewProofOfWork(b *Block) *ProofOfWork
NewProofOfWork builds and returns a ProofOfWork
func (*ProofOfWork) IsFinished ¶
func (pow *ProofOfWork) IsFinished() bool
func (*ProofOfWork) Run ¶
func (pow *ProofOfWork) Run(callback func(int64, []byte))
Run performs a proof-of-work
func (*ProofOfWork) Runv2 ¶
func (pow *ProofOfWork) Runv2(merkleRoot []byte, callback func(int64, []byte))
Run performs a proof-of-work
func (*ProofOfWork) Stop ¶
func (pow *ProofOfWork) Stop()
type Transaction ¶
type Transaction struct { ID []byte Nonce int64 From string To string Amount int64 Signature []byte }
Transaction represents a Bitcoin transaction
func DeserializeTransction ¶
func DeserializeTransction(d []byte) *Transaction
Deserializes Transaction
func NewTransaction ¶
func NewTransaction(nonce, amount int64, from, to string) *Transaction
func (*Transaction) IDString ¶
func (tx *Transaction) IDString() string
func (*Transaction) SetID ¶
func (tx *Transaction) SetID()
func (*Transaction) Sign ¶
func (tx *Transaction) Sign(privKey *elliptic.PrivateKey) *Transaction
func (*Transaction) Verify ¶
func (tx *Transaction) Verify() bool