blockchain

package
v0.0.0-...-52a7d35 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2020 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = 12

Variables

This section is empty.

Functions

func DBexists

func DBexists(path string) bool

func Handle

func Handle(err error)

func ToHex

func ToHex(num int64) []byte

Types

type Block

type Block struct {
	Timestamp    int64
	Hash         []byte
	Transactions []*Transaction
	PrevHash     []byte
	Nonce        int
	Height       int
}

func CreateBlock

func CreateBlock(txs []*Transaction, prev []byte, height int) *Block

func Deserialize

func Deserialize(data []byte) *Block

func Genesis

func Genesis(coinbase *Transaction) *Block

func (*Block) HashTransactions

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

func (*Block) Serialize

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

type BlockChain

type BlockChain struct {
	// Blocks   []*Block
	LastHash []byte
	Database *badger.DB
}

func ContinueBlockChain

func ContinueBlockChain(nodeID string) *BlockChain

从之前的位置恢复区块来呢

func InitBlockChain

func InitBlockChain(address, nodeID string) *BlockChain

从创世区块生成区块链

func (*BlockChain) AddBlock

func (chain *BlockChain) AddBlock(block *Block)

func (*BlockChain) FindTransaction

func (bc *BlockChain) FindTransaction(ID []byte) (Transaction, error)

func (*BlockChain) FindUTXO

func (chain *BlockChain) FindUTXO() map[string]TxOutputs

查找所有的UTXO

func (*BlockChain) GetBestHeight

func (chain *BlockChain) GetBestHeight() int

func (*BlockChain) GetBlock

func (chain *BlockChain) GetBlock(blockHash []byte) (Block, error)

func (*BlockChain) GetBlockHashes

func (chain *BlockChain) GetBlockHashes() [][]byte

func (*BlockChain) Iterator

func (chain *BlockChain) Iterator() *BlockChainIterator

func (*BlockChain) MineBlock

func (chain *BlockChain) MineBlock(txs []*Transaction) *Block

func (*BlockChain) SignTransaction

func (bc *BlockChain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)

func (*BlockChain) VerifyTransaction

func (bc *BlockChain) VerifyTransaction(tx *Transaction) bool

type BlockChainIterator

type BlockChainIterator struct {
	CurrentHash []byte
	Database    *badger.DB
}

func (*BlockChainIterator) Next

func (iter *BlockChainIterator) Next() *Block

type MerkleNode

type MerkleNode struct {
	Left  *MerkleNode
	Right *MerkleNode
	Data  []byte
}

func NewMerkleNode

func NewMerkleNode(left, right *MerkleNode, data []byte) *MerkleNode

type MerkleTree

type MerkleTree struct {
	RootNode *MerkleNode
}

func NewMerkleTree

func NewMerkleTree(data [][]byte) *MerkleTree

type ProofOfWork

type ProofOfWork struct {
	Block  *Block
	Target *big.Int
}

func NewProof

func NewProof(b *Block) *ProofOfWork

func (*ProofOfWork) InitData

func (pow *ProofOfWork) InitData(nonce int) []byte

func (*ProofOfWork) Run

func (pow *ProofOfWork) Run() (int, []byte)

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

type Transaction

type Transaction struct {
	ID      []byte
	Inputs  []TxInput
	Outputs []TxOutput
}

func CoinbaseTx

func CoinbaseTx(to, data string) *Transaction

Coinbase交易

func DeserializeTransaction

func DeserializeTransaction(data []byte) Transaction

func NewTransaction

func NewTransaction(w *wallet.Wallet, to string, amount int, UTXO *UTXOSet) *Transaction

func (*Transaction) Hash

func (tx *Transaction) Hash() []byte

为什么要采用复制的方式?

func (*Transaction) IsCoinbase

func (tx *Transaction) IsCoinbase() bool

func (Transaction) Serialize

func (tx Transaction) Serialize() []byte

func (*Transaction) SetID

func (tx *Transaction) SetID()

设置ID

func (*Transaction) Sign

func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prevTXs map[string]Transaction)

map[hash]Transaction

func (Transaction) String

func (tx Transaction) String() string

func (*Transaction) TrimmedCopy

func (tx *Transaction) TrimmedCopy() Transaction

实现深拷贝,防止变量被修改

func (*Transaction) Verify

func (tx *Transaction) Verify(prevTxs map[string]Transaction) bool

type TxInput

type TxInput struct {
	ID        []byte //关键的交易
	Out       int    //关联输出的index
	Signature []byte
	Pubkey    []byte
}

func (*TxInput) UserKey

func (in *TxInput) UserKey(pubKeyHash []byte) bool

type TxOutput

type TxOutput struct {
	Value      int
	PubkeyHash []byte
}

func NewTxOutput

func NewTxOutput(value int, address string) *TxOutput

func (*TxOutput) Lock

func (out *TxOutput) Lock(address []byte)

type TxOutputs

type TxOutputs struct {
	Outputs []TxOutput
}

func DeserializeOutputs

func DeserializeOutputs(data []byte) TxOutputs

func (TxOutputs) Serialize

func (outs TxOutputs) Serialize() []byte

type UTXOSet

type UTXOSet struct {
	BC *BlockChain
}

func (UTXOSet) CountTransactions

func (u UTXOSet) CountTransactions() int

计算所有UTXO中的交易数目

func (*UTXOSet) DeleteByPrefix

func (u *UTXOSet) DeleteByPrefix(prefix []byte)

func (UTXOSet) FindSpendableOutputs

func (u UTXOSet) FindSpendableOutputs(pubKeyHash []byte, amount int) (int, map[string][]int)

func (UTXOSet) FindUnspentTransactions

func (u UTXOSet) FindUnspentTransactions(pubKeyHash []byte) []TxOutput

查找账户的UTXO

func (*UTXOSet) Reindex

func (u *UTXOSet) Reindex()

func (*UTXOSet) Update

func (u *UTXOSet) Update(block *Block)

加入最新的区块,更新UTXO 1.更新过去的UTXO 2.加入新的交易输出:包含coinbase和其他的输出

Jump to

Keyboard shortcuts

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