blc02

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: 8 Imported by: 0

Documentation

Index

Constants

View Source
const TargetBit = 16

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

Variables

This section is empty.

Functions

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 NewBlock

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

func (*Block) SetHash

func (block *Block) SetHash()

step3:设置区块的hash

type BlockChain

type BlockChain struct {
	Blocks []*Block //存储有序的区块
}

step5:创建区块链

func CreateBlockChainWithGenesisBlock

func CreateBlockChainWithGenesisBlock(data string) *BlockChain

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

func (*BlockChain) AddBlockToBlockChain

func (bc *BlockChain) AddBlockToBlockChain(data string, height int64, prevHash []byte)

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

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