Block

package
v0.0.0-...-84bfe0d Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IntToHex

func IntToHex(num int64) []byte

IntToHex converts an int64 to a byte array

Types

type BlockByte

type BlockByte struct {
	Key   []byte
	Value []byte
}

type BlockChain

type BlockChain struct {
	DB *bolt.DB //数据库句柄
	// contains filtered or unexported fields
}

func NewBlockchain

func NewBlockchain(to string) *BlockChain

获取第一条链

func (*BlockChain) AddBlock

func (bc *BlockChain) AddBlock(Transactions []*Transaction)

添加区块

func (*BlockChain) Balance

func (bc *BlockChain) Balance(address string) []TXOutput

查询当前余额

func (*BlockChain) FindIsSpendableOutputs

func (bc *BlockChain) FindIsSpendableOutputs(txid []byte, pos int) bool

查找交易中 UTXO

func (*BlockChain) FindSpendableOutputs

func (bc *BlockChain) FindSpendableOutputs(address string, amount int) (int, map[string][]int)

查找交易中 至少amount的 UTXO

func (*BlockChain) FindTransaction

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

func (*BlockChain) FindTransactionList

func (bc *BlockChain) FindTransactionList(txid []byte) ([]Transaction, error)

func (*BlockChain) FindTransactionNext

func (bc *BlockChain) FindTransactionNext(txid []byte) (*Transaction, error)

寻找交易是否被引用

func (*BlockChain) FindUTXO

func (bc *BlockChain) FindUTXO(address string) []TXOutput

查找所有相关交易输出

func (*BlockChain) FindUnspentTransactions

func (bc *BlockChain) FindUnspentTransactions(address string) []Transaction

找到相关的交易

func (*BlockChain) GetBlockAll

func (bc *BlockChain) GetBlockAll() []BlockByte

获取DB中所有的Key和value

func (*BlockChain) GetValue

func (bc *BlockChain) GetValue(key []byte) []byte

func (*BlockChain) Iterator

func (bc *BlockChain) Iterator() *BlockChainIterator

func (*BlockChain) MineBlock

func (bc *BlockChain) MineBlock(transactions []*Transaction)

func (*BlockChain) SetBlockAll

func (bc *BlockChain) SetBlockAll(bs []BlockByte)

func (*BlockChain) SetValue

func (bc *BlockChain) SetValue(key, value []byte) error

func (*BlockChain) Traceability

func (bc *BlockChain) Traceability(address string) []Transaction

交易溯源

func (*BlockChain) TransactionList

func (bc *BlockChain) TransactionList() []Transaction

交易列表

func (*BlockChain) Users

func (bc *BlockChain) Users() []string

返回当前用户

func (*BlockChain) Version

func (bc *BlockChain) Version() int

获取当前区块链长度 当做版本

type BlockChainIterator

type BlockChainIterator struct {
	// contains filtered or unexported fields
}

区块链读取数据 迭代器

func (*BlockChainIterator) Next

func (i *BlockChainIterator) Next() *BlockData

type BlockData

type BlockData struct {
	Timestamp     int64
	Transactions  []*Transaction
	PrevBlockHash []byte
	Hash          []byte
	Nonce         int
}

func Deserialize

func Deserialize(d []byte) *BlockData

Block反序列化

func NewBlock

func NewBlock(Transactions []*Transaction, prevBlockHash []byte) *BlockData

生成新的区块

func NewGenesisBlock

func NewGenesisBlock(coinbase *Transaction) *BlockData

生成创世区块

func (*BlockData) HashTransactions

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

func (*BlockData) Serialize

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

Block数据系列化

type ProofOfWork

type ProofOfWork struct {
	BlockData *BlockData
	// contains filtered or unexported fields
}

func NewProofOfWork

func NewProofOfWork(b *BlockData) *ProofOfWork

func (*ProofOfWork) PrepareData

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

生成

func (*ProofOfWork) Run

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

计算哈希值

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

type TXInput

type TXInput struct {
	Txid      []byte //交易ID, 一个交易输入引用了之前一笔交易的一个输出, ID 表明是之前哪笔交易
	Vout      int    //输出索引
	ScriptSig string
}

交易输入

func (*TXInput) CanUnlockOutputWith

func (in *TXInput) CanUnlockOutputWith(unlockingData string) bool

验证输入持有者

type TXOutput

type TXOutput struct {
	Value        int
	ScriptPubKey string
}

交易输出

func (*TXOutput) CanBeUnlockedWith

func (out *TXOutput) CanBeUnlockedWith(unlockingData string) bool

验证输出持有者

type Transaction

type Transaction struct {
	ID   []byte     //ID
	Vin  []TXInput  //输入
	Vout []TXOutput //输出
	Data string     //交易数据
}

交易体

func NewCoinbaseTX

func NewCoinbaseTX(to, data string, subsidy int) *Transaction

创建 coinbase 交易

func NewUTXOTransaction

func NewUTXOTransaction(from, to, data string, amount int, bc *BlockChain) (*Transaction, error)

货币交易

func NewUTxIdTransaction

func NewUTxIdTransaction(from, to, data string, txid []byte, bc *BlockChain) (*Transaction, error)

通过Txid指定交易

func (Transaction) IsCoinbase

func (tx Transaction) IsCoinbase() bool

检查交易是否为 coinbase

func (*Transaction) SetID

func (tx *Transaction) SetID()

把交易结构数据打包成hash填入ID

Jump to

Keyboard shortcuts

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