blockchain4

package module
v0.0.0-...-bc0d8f0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2020 License: GPL-3.0 Imports: 14 Imported by: 0

README

blockchain4

增加交易功能 文档: https://www.kancloud.cn/daleboy/blockchain/1971570

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IntToHex

func IntToHex(num int64) []byte

IntToHex 将整型转为二进制数组

Types

type Block

type Block struct {
	Timestamp     int64
	Transactions  []*Transaction //存储交易数据,不再是字符串数据了
	PrevBlockHash []byte
	Nonce         int
	Hash          []byte
}

Block 区块结构新版,增加了计数器nonce,主要目的是为了校验区块是否合法 即挖出的区块是否满足工作量证明要求的条件

func DeserializeBlock

func DeserializeBlock(d []byte) *Block

DeserializeBlock 反序列化,注意返回的是Block的指针(引用)

func NewBlock

func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block

NewBlock 创建普通区块 一个block里面可以包含多个交易

func NewGenesisBlock

func NewGenesisBlock(coninbase *Transaction) *Block

NewGenesisBlock 创建创始区块,包含创始交易。注意,创建创始区块也需要挖矿。

func (*Block) HashTransactions

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

HashTransactions 计算交易组合的哈希值 获得每笔交易的哈希,将它们关联起来,然后获得一个连接后的组合哈希

func (*Block) Serialize

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

Serialize Block序列化 特别注意,block对象的任何不以大写字母开头命令的变量,其值都不会被序列化到[]byte中

type Blockchain

type Blockchain struct {
	Tip []byte   //区块链最后一块的哈希值
	Db  *bolt.DB //数据库
}

Blockchain 区块链结构 我们不在里面存储所有的区块了,而是仅存储区块链的 tip。 另外,我们存储了一个数据库连接。因为我们想要一旦打开它的话,就让它一直运行,直到程序运行结束。

func CreatBlockchain

func CreatBlockchain(address string) *Blockchain

CreatBlockchain 创建一个全新的区块链数据库 address用户发起创始交易,并挖矿,奖励也发给用户address 注意,创建后,数据库是open状态,需要使用者负责close数据库

func NewBlockchain

func NewBlockchain() *Blockchain

NewBlockchain 从数据库中取出最后一个区块的哈希,构建一个区块链实例

func (*Blockchain) FindSpendableOutput

func (bc *Blockchain) FindSpendableOutput(address string, amount int) (int, map[string][]int)

FindSpendableOutput 查找某个用户可以花费的输出,放到一个映射里面 从未花费交易里取出未花费的输出,直至取出输出的币总数大于或等于需要send的币数为止

func (*Blockchain) FindUTXO

func (bc *Blockchain) FindUTXO(address string) []TxOutput

FindUTXO 从未花费交易取得所有未花费输出

func (*Blockchain) FindUnspentTransaction

func (bc *Blockchain) FindUnspentTransaction(address string) []Transaction

FindUnspentTransaction 查找未花费的交易(即该交易的花费尚未花出,换句话说, 及该交易的输出尚未被其他交易作为输入包含进去)

func (*Blockchain) Iterator

func (bc *Blockchain) Iterator() *BlockchainIterator

Iterator 每当需要对链中的区块进行迭代时候,我们就通过Blockchain创建迭代器 注意,迭代器初始状态为链中的tip,因此迭代是从最新到最旧的进行获取

func (*Blockchain) MineBlock

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

MineBlock 挖出普通区块并将新区块加入到区块链中 此方法通过区块链的指针调用,将修改区块链bc的内容

type BlockchainIterator

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

BlockchainIterator 区块链迭代器,用于对区块链中的区块进行迭代

func (*BlockchainIterator) Next

func (i *BlockchainIterator) Next() *Block

Next 区块链迭代,返回当前区块,并更新迭代器的currentHash为当前区块的PrevBlockHash

type CLI

type CLI struct{}

CLI 响应处理命令行参数

func (*CLI) Run

func (cli *CLI) Run()

Run 读取命令行参数,执行相应的命令 使用标准库里面的 flag 包来解析命令行参数:

type ProofOfWork

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

ProofOfWork POW结构体 可以看出,每一个pow实例与具体的block相关 但在确定好挖矿难度系数后,所有区块的pow的target是相同的, 除非挖矿系数随着时间推移,挖矿难度系数不断增加

func NewProofOfWork

func NewProofOfWork(b *Block) *ProofOfWork

NewProofOfWork 初始化创建一个POW的函数,以block指针为参数(将修改该block) 主要目的是确定target

func (*ProofOfWork) Run

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

Run POW挖矿核心算法实现,注意,这是一个方法,不是函数, 因为挖矿的完整描述是:挖出包含某个实际交易信息(或数据)的区块 挖矿是为交易上链提供服务,矿工拿到交易信息后进行挖矿,挖出的有效区块将包含交易信息 有可能挖不出符合条件的区块,所以将区块上链之前,需要对挖出的区块进行验证(验证是否符合条件)

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

Validate 验证工作量证明POW

type Transaction

type Transaction struct {
	ID   []byte     //交易ID
	Vin  []TxInput  //交易输入,由上次交易输入(可能多个)
	Vout []TxOutput //交易输出,由本次交易产生(可能多个)
}

Transaction 交易结构,代表一个交易

func NewCoinbaseTX

func NewCoinbaseTX(to, data string) *Transaction

NewCoinbaseTX 创建一个区块链创始交易

func NewUTXOTransaction

func NewUTXOTransaction(from, to string, amount int, bc *Blockchain) *Transaction

NewUTXOTransaction 创建一个资金转移交易

func (Transaction) IsCoinbase

func (tx Transaction) IsCoinbase() bool

IsCoinbase 检查交易是否是创始区块交易 创始区块交易没有输入,详细见NewCoinbaseTX tx.Vin只有一个输入,数组长度为1 tx.Vin[0].Txid为[]byte{},因此长度为0 Vin[0].Vout设置为-1

func (*Transaction) SetID

func (tx *Transaction) SetID()

SetID 设置交易的ID:计算出交易实例的哈希,作为交易的ID 注意,ID是一个[32]byte数组

type TxInput

type TxInput struct {
	Txid []byte //前一笔交易的ID
	Vout int    //前一笔交易在该笔交易所有输出中的索引(一笔交易可能有多个输出,需要有信息指明具体是哪一个)

	//一个脚本,可作用于一个输出的scriptPubKey的数据,用于解锁输出
	//如果ScriptSig是正确的,那么引用的输出就会被解锁,然后被解锁的值就可以被用于产生新的输出
	//如果不正确,前一笔交易的输出就无法被引用在输入中,或者说,也就无法使用这个输出
	//这种机制,保证了用户无法花费其他人的币
	//这里仅仅存储用户的钱包地址
	ScriptSig string
}

TxInput 交易的输入 包含的是前一笔交易的一个输出

func (*TxInput) CanUnlockOutputWith

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

CanUnlockOutputWith 检查是否是地址unlockingData发起的交易 进行发起者unlockingData的身份检查 此方法的作用存疑

type TxOutput

type TxOutput struct {
	Value int //输出里面存储的“币”

	//解锁脚本(比特币里面是一个脚本,这里是用户的钱包地址),定义了
	//解锁该输出的逻辑。
	ScriptPubKey string
}

TxOutput 交易的输出

func (*TxOutput) CanBeUnlockedWith

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

CanBeUnlockedWith 检查输出是否可以被提供的数据unlockingData解锁 检查解锁数据的正确性,正确则返回true,否则返回false

Jump to

Keyboard shortcuts

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