block

package
v0.0.0-...-ae785a1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2019 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const Subsidy = 100

挖矿奖励金

Variables

View Source
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 GetBalance(address string) int64

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 RevertTo

func RevertTo(Height int64)

revert block chain to specific height

func SavePendingBlock

func SavePendingBlock(block *Block)

func SaveSinglePendingBlock

func SaveSinglePendingBlock(block *Block)

Types

type Account

type Account struct {
	Address string
	Balance int64
	Nonce   int64
	Index   int64
}

func DeserializeAccount

func DeserializeAccount(d []byte) *Account

Deserializes a Account

func FindAccount

func FindAccount(accounts []*Account, address string) *Account

func GetAccount

func GetAccount(address string) *Account

func GetAllAccount

func GetAllAccount() []*Account

func InitAccount

func InitAccount(address string, idx int64) *Account

func NewAccount

func NewAccount(address string, balance, nonce, index int64) *Account

func (*Account) Serialize

func (a *Account) Serialize() []byte

Serializes the account

type AccountIncreaseMent

type AccountIncreaseMent struct {
	ChangedAccount []*Account
	NewAccount     []*Account
}

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 DeserializeBlock

func DeserializeBlock(d []byte) *Block

Deserializes a block

func GetLastBlock

func GetLastBlock() *Block

func GetPendingBlock

func GetPendingBlock(blockHash []byte) *Block

func GetSinglePendingBlock

func GetSinglePendingBlock(blockHash []byte) *Block

func (*Block) DeleteTransactions

func (block *Block) DeleteTransactions()

func (*Block) GetAccountTree

func (b *Block) GetAccountTree(preprocess bool) *MerkleTree

func (*Block) HashTransactions

func (b *Block) HashTransactions() []byte

Hash transactions with merkle tree

func (*Block) PreProcessAccountBalance

func (block *Block) PreProcessAccountBalance(accounts []*Account) ([]*Account, []*Account)

func (*Block) SaveAccounts

func (block *Block) SaveAccounts()

func (*Block) SaveTransactions

func (block *Block) SaveTransactions()

func (*Block) Serialize

func (b *Block) Serialize() []byte

Serializes the block

func (*Block) Sign

func (b *Block) Sign(privKey *elliptic.PrivateKey) *Block

func (*Block) VerifyCoinbase

func (block *Block) VerifyCoinbase() bool

func (*Block) VerifyHash

func (block *Block) VerifyHash() bool

func (*Block) VerifyMerkleHash

func (block *Block) VerifyMerkleHash() bool

func (*Block) VerifyPow

func (block *Block) VerifyPow(preprocess bool) bool

func (*Block) VerifyPowV2

func (block *Block) VerifyPowV2(prevStateTree *MerkleTree) bool

func (*Block) VerifyTransaction

func (block *Block) VerifyTransaction() bool

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

type BlockContent struct {
	X   []byte
	Dup bool
}

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 LoadBlockchain

func LoadBlockchain() *Blockchain

load Blockchain from db

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 (bcp *BlockchainPendingPool) ConnectSinglePendingPool(block *Block, rootBlock *Block, pendlingLength byte, blocktailHash []byte) (byte, []byte)

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

type MerkleTree struct {
	Root *Node

	Leafs []*Node
	// contains filtered or unexported fields
}

func DeserializeNodeFromData

func DeserializeNodeFromData(d []byte) *MerkleTree

反序列化数据为merkle树

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) Depth

func (m *MerkleTree) Depth() int64

树深度

func (*MerkleTree) DeserializeAccount

func (m *MerkleTree) DeserializeAccount() []*Account

func (*MerkleTree) FindInsertPoint

func (m *MerkleTree) FindInsertPoint() *Node

寻找merkle的插入节点

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插入新的节点

  1. 若能找到插入节点, 更新此插入节点的content, 并更新回溯路径
  2. 若未能找到插入点,构建当前merkle 右子树 并与当前树合并

func (*MerkleTree) LeafCount

func (m *MerkleTree) LeafCount() int64

树叶子节点的数量

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
}

func GetNodeBrother

func GetNodeBrother(node *Node) *Node

获取节点的兄弟节点

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 SendTo

func SendTo(from, to string, amount int64, wif string) *Transaction

func (*Transaction) IDString

func (tx *Transaction) IDString() string

func (Transaction) Serialize

func (tx Transaction) Serialize() []byte

Serialized Transaction

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

Jump to

Keyboard shortcuts

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