Documentation ¶
Index ¶
- func HandleErr(err error)
- func IntToHex(n int64) []byte
- func PathExists(path string) (bool, error)
- type Block
- type BlockChain
- func (bc *BlockChain) DbClose()
- func (bc *BlockChain) FindSpendableOutputs(address string, amount int) (int, map[string][]int)
- func (bc *BlockChain) FindUTXO(address string) []TXOutput
- func (bc *BlockChain) FindUnspentTransactions(address string) []Transaction
- func (bc *BlockChain) Iterator() *BlockchainIterator
- func (bc *BlockChain) MineBlock(transactions []*Transaction)
- type BlockchainIterator
- type CLI
- type ProofOfWork
- type TXInput
- type TXOutput
- type Transaction
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PathExists ¶
判断文件或者文件夹是否存在 使用os.Stat()函数返回的错误值进行判断: 如果返回的错误为nil,说明文件或文件夹存在 如果返回的错误类型使用os.IsNotExist()判断为true,说明文件或文件夹不存在 如果返回的错误为其它类型,则不确定是否在存在 Check if the file or folder exists Use the error value returned by the os.Stat() function to determine: If the returned error is nil, the file or folder exists If the returned error type is determined to be true by os.IsNotExist(), it means that the file or folder does not exist If the returned error is other type, it is not sure whether it exists
Types ¶
type Block ¶
type Block struct { Timestamp int64 // 区块创建时间戳 // time stamp for the Block creation Transactions []*Transaction PrevBlockHash []byte // 前一个区块的哈希值 // Hash value for the previous Block Hash []byte // 区块自身的哈希值,用于校验区块数据有效 // Hash value of the current block, used // to verify the valididy of the block data Nonce int // 工作量证明值,用来校验数据的 // Proof of work, used to verify data. }
区块结构体定义 Definition for Block struct
func DeserializeBlock ¶
把字节数组反序列化为一个Block Deserialize the byte array into a Block
func NewBlock ¶
func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block
NewBlock create and return Block
func NewGenesisBlock ¶
func NewGenesisBlock(coinbase *Transaction) *Block
创建创世区块 Create Genesis Block
func (*Block) HashTransaction ¶
把区块的所有交易ID做个hash处理 make a hash for all the transaction ID in the Block
type BlockChain ¶
type BlockChain struct {
// contains filtered or unexported fields
}
区块链结构 struct for the blockchain
func CreateBlockchain ¶
func CreateBlockchain(address string) *BlockChain
创建区块链,即是初始化区块链,添加创世区块 Creating a blockchain means initializing the blockchain and adding the genesis block
func NewBlockChain ¶
func NewBlockChain() *BlockChain
* 1. Open a database file 2. Check if a blockchain is already stored in the file 3. If a blockchain is already stored: Create a new Blockchain instance Set the Blockchain instance's tip to the hash of the last block stored in the database
- If there is no blockchain: Create Genesis Block store in database Save the genesis block hash as the hash of the last block Create a new Blockchain instance, initially the tip points to the genesis block (tip has a tail, meaning the tip, here the tip stores the hash of the last block)
func (*BlockChain) DbClose ¶
func (bc *BlockChain) DbClose()
关闭db数据库连接 close the connection to the db database
func (*BlockChain) FindSpendableOutputs ¶
可以用来spent的输出 Spendable outputs
func (*BlockChain) FindUTXO ¶
func (bc *BlockChain) FindUTXO(address string) []TXOutput
统计出address对应的所有没有花费出去的输出(也即是它的币) Count all unspent outputs corresponding to address (that is, its coins)
func (*BlockChain) FindUnspentTransactions ¶
func (bc *BlockChain) FindUnspentTransactions(address string) []Transaction
找到所有包含未花费输出的交易集合 Find the set of all transactions that contain unspent outputs
func (*BlockChain) Iterator ¶
func (bc *BlockChain) Iterator() *BlockchainIterator
迭代器的初始状态为链中的 tip,因此区块将从尾到头(创世块为头) The initial state of the iterator is the tip in the chain,
so the blocks will go from the end to the beginning (the genesis block is the head)
func (*BlockChain) MineBlock ¶
func (bc *BlockChain) MineBlock(transactions []*Transaction)
MineBlock mines a new block with the provided transactions 发送币意味着创建新的交易,并通过挖出新块的方式将交易打包到区块链中 Sending coins means creating a new transaction and packing the transaction into the blockchain by mining a new block
type BlockchainIterator ¶
type BlockchainIterator struct {
// contains filtered or unexported fields
}
区块链迭代器 block chain iterator
func (*BlockchainIterator) Next ¶
func (bci *BlockchainIterator) Next() *Block
只会做一件事情:返回链中的下一个块。 Will only do one thing: return the next block in the chain.
type ProofOfWork ¶
type ProofOfWork struct {
// contains filtered or unexported fields
}
工作量证明结构 proof of work struct
func NewProofOfWork ¶
func NewProofOfWork(b *Block) *ProofOfWork
func (*ProofOfWork) Run ¶
func (pow *ProofOfWork) Run() (int, []byte)
运行工作量证明得出新的区块哈希值以及Nonce Run the proof of work to get the new block hash value and Nonce
func (*ProofOfWork) Validate ¶
func (pow *ProofOfWork) Validate() bool
工作量生成的区块哈希值验证是否是有效的 Verify whether the block hash value generated by the workload is valid
type TXInput ¶
func (*TXInput) CanUnlockOutputWith ¶
type TXOutput ¶
type TXOutput struct { Value int // 一定量的比特币(Value) ScriptPubKey string // 一个锁定脚本(ScriptPubKey),要花这笔钱,必须要解锁该脚本。 }
交易输出结构体 transaction output struct
func (*TXOutput) CanBeUnlockedWith ¶
判断该输入是否可以被unlockingData解锁 Determine whether the input can be unlocked by unlockingData
type Transaction ¶
type Transaction struct { ID []byte // 交易id,使用输入输出等信息来哈希,确保信息不被篡改 // transaction id, Use information such as //input and output to hash to ensure that the information is not tampered with Vin []TXInput Vout []TXOutput }
交易结构体,用来存储一笔交易 transaction struct, used to store a transaction
func NewCoinbaseTransaction ¶
func NewCoinbaseTransaction(to, data string) *Transaction
创建挖矿奖励交易:交易只有一个输出,没有输入 Create a mining reward transaction: the transaction has only one output and no input
func NewUTXOTransaction ¶
func NewUTXOTransaction(from, to string, amount int, bc *BlockChain) *Transaction
创建转账交易记录 Create transfer transaction records
func (*Transaction) IsCoinbase ¶
func (tx *Transaction) IsCoinbase() bool
IsCoinbase check whether the transaction is coinbase
func (*Transaction) SetID ¶
func (tx *Transaction) SetID()
设置交易的ID编号,这里是做hash处理 Set the ID number of the transaction, which is processed with hash algorithm