Documentation ¶
Index ¶
- Constants
- func Base58Decode(input []byte) []byte
- func Base58Encode(input []byte) []byte
- func CheckSum(input []byte) []byte
- func GetEnvNodeId() string
- func IntToHex(data int64) []byte
- func IsValidArgs()
- func IsValidForAddress(addressBytes []byte) bool
- func JSONToSlice(jsonString string) []string
- func PrintUsage()
- func Reverse(data []byte)
- func Ripemd160Hash(pubKey []byte) []byte
- func StringToHash160(address string) []byte
- type Block
- type BlockChain
- func (bc *BlockChain) AddBlock(block *Block)
- func (blockchain *BlockChain) FindAllSpentOutputs() map[string][]*TxInput
- func (blockchain *BlockChain) FindSpendableUTXO(from string, amount int, txs []*Transaction) (int, map[string][]int)
- func (blockchain *BlockChain) FindTransaction(ID []byte) Transaction
- func (blockchain *BlockChain) FindUTXOMap() map[string]*TXOutputs
- func (bc *BlockChain) GetBlock(hash []byte) []byte
- func (bc *BlockChain) GetBlockHases() [][]byte
- func (bc *BlockChain) GetHeight() int64
- func (blc *BlockChain) Iterator() *BlockChainIterator
- func (blockchain *BlockChain) MineNewBlock(from, to, amount []string, nodeID string)
- func (bc *BlockChain) PrintChain()
- func (blockchain *BlockChain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)
- func (blockchain *BlockChain) SpentOutput(address string) map[string][]int
- func (blockchain *BlockChain) UnUTXOS(address string, txs []*Transaction) []*UTXO
- func (bc *BlockChain) VerifyTransaction(tx *Transaction) bool
- type BlockChainIterator
- type BlockData
- type CLI
- type GetBlocks
- type GetData
- type Inv
- type MerkleNode
- type MerkleTree
- type ProofOfWork
- type TXOutputs
- type Transaction
- func (tx *Transaction) Hash() []byte
- func (tx *Transaction) HashTransaction()
- func (tx *Transaction) IsCoinbaseTransaction() bool
- func (tx *Transaction) Serialize() []byte
- func (tx *Transaction) Sign(privateKey ecdsa.PrivateKey, prevTxs map[string]Transaction)
- func (tx *Transaction) TrimmedCopy() Transaction
- func (tx *Transaction) Verify(prevTxs map[string]Transaction) bool
- type TxInput
- type TxOutput
- type UTXO
- type UTXOSet
- type Version
- type Wallet
- type Wallets
Constants ¶
const ( //验证当前节点末端区块是否是最新区块 CMD_VERSION = "version" //从最长链上获取区块 CMD_GETBLOCKS = "getblocks" //向其他节点展示当前节点有哪些区块 CMD_INV = "inv" //请求指定区块 CMD_GETDATA = "getdata" //接收到新区块之后,进行处理 CMD_BLOCK = "block" )
命令分类
const CMMAND_LENGTH = 12
命令长度
const PROTOCOL = "tcp"
网络服务常量管理 协议
Variables ¶
This section is empty.
Functions ¶
func JSONToSlice ¶
标准JSON格式转切片 windows下需要添加引号 bc.exe send -from "[\"troytan\"]" -to "[\"Alice\"]" -amount "[\"5\"]" bc.exe send -from "[\"troytan\",\"Alice\"]" -to "[\"Alice\",\"troytan\"]" -amount "[\"5\",\"2\"]" troytan ->Alice 5 -->Alice 5 troytan 5 Alice ->troytan 2 -->Alice 3 troytan 7
Types ¶
type Block ¶
type Block struct { TimeStamp int64 //区块时间戳,代表区块时间 Hash []byte //当前区块哈希 PreBlockHash []byte //前区块哈希 Height int64 //区块高度 Txs []*Transaction //交易数据(交易列表) Nonce int64 //在运行pow时生成的哈希变化值,也代表pow运行时动态修改的数据 }
区块基本结构与功能管理文件
func (*Block) HashTransaction ¶
把指定区块中所有交易结构都序列化(类Merkle的哈希计算方法)
type BlockChain ¶
区块链的基本结构
func CreateBlockChainWithGenesisBlock ¶
func CreateBlockChainWithGenesisBlock(address string, nodeID string) *BlockChain
初始化区块链
func (*BlockChain) FindAllSpentOutputs ¶
func (blockchain *BlockChain) FindAllSpentOutputs() map[string][]*TxInput
查找整条区块链所有已花费输出
func (*BlockChain) FindSpendableUTXO ¶
func (blockchain *BlockChain) FindSpendableUTXO(from string, amount int, txs []*Transaction) (int, map[string][]int)
查找指定地址的可用UTXO,超过amount就中断查找 更新当前数据库中指定地址的UTXO数量 txs:缓存中的交易列表(用于多笔交易处理)
func (*BlockChain) FindTransaction ¶
func (blockchain *BlockChain) FindTransaction(ID []byte) Transaction
通过指定的交易哈希查找交易
func (*BlockChain) FindUTXOMap ¶
func (blockchain *BlockChain) FindUTXOMap() map[string]*TXOutputs
查找整条区块链中所有地址的UTXO
func (*BlockChain) MineNewBlock ¶
func (blockchain *BlockChain) MineNewBlock(from, to, amount []string, nodeID string)
实现挖矿功能 通过接收交易生成区块
func (*BlockChain) PrintChain ¶
func (bc *BlockChain) PrintChain()
添加区块到区块链中
func (bc *BlockChain) AddBlock(txs []*Transaction) { //更新区块数据(insert) err2:=bc.DB.Update(func(tx *bolt.Tx) error { //1.获取数据库桶 b:=tx.Bucket([]byte(blockTableName)) if nil!=b{ //2.获取最后插入的区块 blockBytes:=b.Get(bc.Tip) //3.区块数据反序列化 latest_block:=DeserializeBlock(blockBytes) //3.新建区块 newBlock:=NewBlock(latest_block.Height+1,latest_block.Hash,txs) //4.存入数据库 err:=b.Put(newBlock.Hash,newBlock.Serialize()) if err!=nil{ log.Panicf("insert the new block to db failed %v\n",err) } //更新最新区块的哈希(数据库) err1:=b.Put([]byte("1"),newBlock.Hash) if err1!=nil{ log.Panicf("updata the latest block hash to db failed %v\n",err1) } //更新区块连对象中的最新区块哈希 bc.Tip=newBlock.Hash } return nil }) if nil!=err2{ log.Panicf("insert block to db failed%v\n",err2) } }
遍历数据库,输出所有区块信息
func (*BlockChain) SignTransaction ¶
func (blockchain *BlockChain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)
交易签名
func (*BlockChain) SpentOutput ¶
func (blockchain *BlockChain) SpentOutput(address string) map[string][]int
获取指定地址所有已花费输出
func (*BlockChain) UnUTXOS ¶
func (blockchain *BlockChain) UnUTXOS(address string, txs []*Transaction) []*UTXO
func (*BlockChain) VerifyTransaction ¶
func (bc *BlockChain) VerifyTransaction(tx *Transaction) bool
验证签名
type BlockChainIterator ¶
迭代器基本结构
type MerkleNode ¶
type MerkleNode struct { //左子节点 Left *MerkleNode //右子节点 Right *MerkleNode //数据 Data []byte }
Merkle节点结构
func MakeMerkleNode ¶
func MakeMerkleNode(left, right *MerkleNode, data []byte) *MerkleNode
创建Merkle节点
type MerkleTree ¶
type MerkleTree struct { //根节点 RootNode *MerkleNode }
Merkle树实现管理
func NewMerkleTree ¶
func NewMerkleTree(txHashes [][]byte) *MerkleTree
创建Merkle树 txHashes:区块中的交易哈希列表 Merkle根节点之外的其它层次的节点数量必须是偶数个,如果是奇数个,则将最后一个节点复制一份
type ProofOfWork ¶
type ProofOfWork struct { //需要共识验证的模块 Block *Block // contains filtered or unexported fields }
工作量证明的结构
func (*ProofOfWork) Run ¶
func (proofOfWork *ProofOfWork) Run() ([]byte, int)
执行pow,比较哈希值 返回哈希值,以及碰撞次数
type Transaction ¶
type Transaction struct { //交易哈希(标识) TxHash []byte //输入列表 Vins []*TxInput //输出列表 Vouts []*TxOutput }
定义一个交易基本结构
func NewSimpleTransaction ¶
func NewSimpleTransaction(from string, to string, amount int, bc *BlockChain, txs []*Transaction, nodeID string) *Transaction
生成普通转账交易
func (*Transaction) HashTransaction ¶
func (tx *Transaction) HashTransaction()
生成交易哈希(交易序列化) 不同时间生成的交易哈希值不同
func (*Transaction) IsCoinbaseTransaction ¶
func (tx *Transaction) IsCoinbaseTransaction() bool
判断指定的交易是否是一个coinbase交易
func (*Transaction) Sign ¶
func (tx *Transaction) Sign(privateKey ecdsa.PrivateKey, prevTxs map[string]Transaction)
交易签名 prevTxs : 代表当前交易的输入所引用的所有OUTPUT松鼠的交易
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 { //交易哈希(不是指当前交易的哈希) TxHash []byte //引用的上一笔交易的输出索引号 Vout int //数字签名 Signature []byte //公钥 PublicKey []byte }
输入结构
func (*TxInput) UnLockRipemd160Hash ¶
传递哈希160进行判断
type UTXO ¶
type UTXO struct { //UTXO对应的交易哈希 TxHash []byte //UTXO在其所属交易的输出列表中的索引 Index int //Output本身 Output *TxOutput }
UTXO结构管理
type UTXOSet ¶
type UTXOSet struct {
Blockchain *BlockChain
}
func (*UTXOSet) FindUTXOWithAddress ¶
查找
type Wallet ¶
type Wallet struct { //1.私钥 PrivateKey ecdsa.PrivateKey //2.公钥 PublicKey []byte }
钱包基本结构
Source Files ¶
- BLCIter.go
- Block.go
- BlockChain.go
- CLI.go
- CLI_Createblockchain.go
- CLI_GetAccounts.go
- CLI_GetBalance.go
- CLI_Printchain.go
- CLI_Send.go
- CLI_SetNodeID.go
- CLI_StartNode.go
- CLI_TestFindUTXOMap.go
- CLI_createWallets.go
- Constant.go
- Merkle.go
- ProofOfWork.go
- Server.go
- Server_Block.go
- Server_GetBlocks.go
- Server_GetData.go
- Server_Handle.go
- Server_Inv.go
- Server_Send.go
- Server_Version.go
- Transaction.go
- Tx_Input.go
- Tx_Ouput.go
- Tx_Outputs.go
- UTXO.go
- UTXU_Set.go
- Wallet.go
- Wallets.go
- base58.go
- utils.go