blc04

package
v0.0.0-...-b8c1206 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const BLOCKTABLENAME = "blocks" //表名
View Source
const DBNAME = "blockchain.db" //数据库名
View Source
const TargetBit = 16

0000 0000 0000 0000 1001 0001 0000 .... 0001 256位Hash里面前面至少有16个零

Variables

This section is empty.

Functions

func CreateBlockChainWithGenesisBlock

func CreateBlockChainWithGenesisBlock(data string)

step6:创建区块链,带有创世区块

func IntToHex

func IntToHex(num int64) []byte

* 将一个int64的整数:转为二进制后,每8bit一个byte。转为[]byte

Types

type Block

type Block struct {
	//高度Height:其实就是区块的编号,第一个区块叫创世区块,高度为0
	Height int64
	//上一个区块的哈希值ProvHash:
	PrevBlockHash []byte
	//交易数据 date 目前先设计为[]byte,后期是Transaction
	Data []byte
	//时间戳
	TimeStamp int64
	//哈希值hash 32字节 64个16进制数
	Hash []byte

	Nonce int64
}

step1:创建Block结构体

func CreateGenesisBlock

func CreateGenesisBlock(data string) *Block

step6:创建区块链,带有创世区块

func DeserializeBlock

func DeserializeBlock(blockBytes []byte) *Block

反序列化, 得到一个区块----设计为函数

func NewBlock

func NewBlock(data string, PrevBlockHash []byte, height int64) *Block

func (*Block) Serilalize

func (block *Block) Serilalize() []byte

区块序列化 得到一个字节数组---区块的行为,设计为方法

func (*Block) SetHash

func (block *Block) SetHash()

step3:设置区块的hash

type BlockChain

type BlockChain struct {
	//Blocks []*Block //存储有序的区块
	Tip []byte   //最后区块的hash值
	DB  *bolt.DB //数据库对象
}

step5:创建区块链

func GetBlockchainObject

func GetBlockchainObject() *BlockChain

新增方法, 用于获取区块链

func (*BlockChain) AddBlockToBlockChain

func (bc *BlockChain) AddBlockToBlockChain(data string)

step7:添加一个新的区块,到区块链中

func (*BlockChain) Iterator

func (bc *BlockChain) Iterator() *BlockChainIterator

2.获取一个迭代器的方法

func (*BlockChain) PrintChains

func (bc *BlockChain) PrintChains()

type BlockChainIterator

type BlockChainIterator struct {
	CurrentHash []byte //当前区块的hash
	Db          *bolt.DB
}

func (*BlockChainIterator) Next

func (blockChainIterator *BlockChainIterator) Next() *Block

获取区块

type CLi

type CLi struct {
}

func (*CLi) Run

func (cli *CLi) Run()

type ProofOfWork

type ProofOfWork struct {
	//需要验证的区块
	Block *Block

	//大整数存储 目标哈希
	Target *big.Int
}

step2:创建pow结构体

func NewProofOfwork

func NewProofOfwork(block *Block) *ProofOfWork

step3: 创建新的工作量证明对象

func (*ProofOfWork) IsValid

func (pow *ProofOfWork) IsValid() bool

验证

func (*ProofOfWork) Run

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

step4:返回有效的哈希和nonce值

Jump to

Keyboard shortcuts

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