blockchain

package
v0.0.0-...-65ff5bd Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	PrevHash     []byte         // 上一个区块的哈希值
	Timestamp    time.Time      // 区块创建时间
	Hash         []byte         // 区块哈希
	Nonce        int            // 当前计数器(挖矿使用)
	Height       int            // 当前区块高度
	Transactions []*Transaction // 区块中的交易记录
}

区块

func DeserializeBlock

func DeserializeBlock(d []byte) *Block

解码区块

func NewBlock

func NewBlock(prevHash []byte, height int) *Block

创建区块

func NewGenesisBlock

func NewGenesisBlock(address string) *Block

创建创世块

func (*Block) Mining

func (b *Block) Mining(txs []*Transaction)

func (*Block) Ore

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

提供给挖矿的数据

func (*Block) Serialize

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

编码区块

type Chain

type Chain struct {
	*leveldb.DB
	// contains filtered or unexported fields
}

func CreateChain

func CreateChain(address string) *Chain

创建区块链

func OpenChain

func OpenChain() *Chain

打开区块链

func (*Chain) AppendBlock

func (c *Chain) AppendBlock(b *Block) error

添加区块到链中

func (*Chain) GetBlock

func (c *Chain) GetBlock(hash []byte) *Block

通过区块的哈希值获取区块

func (*Chain) GetTransaction

func (c *Chain) GetTransaction(txId []byte) (Transaction, error)

通过交易TxID获取交易

func (*Chain) MineBlock

func (c *Chain) MineBlock(address string, txs []*Transaction)

挖矿

func (*Chain) SignTransaction

func (c *Chain) SignTransaction(tx *Transaction, privateKey ecdsa.PrivateKey)

交易签名

func (*Chain) VerifyTransaction

func (c *Chain) VerifyTransaction(tx *Transaction) bool

验证交易

type Merkle

type Merkle struct {
	RootNode *MerkleNode
}

默克林树形结构

func NewMerkle

func NewMerkle(data [][]byte) *Merkle

从字节数组中创建默克林树

type MerkleNode

type MerkleNode struct {
	Left  *MerkleNode
	Right *MerkleNode
	Hash  []byte //节点的哈希值
}

默克林树的节点

func NewMerkleNode

func NewMerkleNode(left, right *MerkleNode, data []byte) *MerkleNode

创建默克林节点

type TXInput

type TXInput struct {
	TxId      []byte // 交易的hash值
	TxOut     int    // 输出的索引
	Signature []byte // 签名
	PubKey    []byte // 公钥
}

type TXOutput

type TXOutput struct {
	V          int    // 输出多少币
	PubKeyHash []byte // 输出者的公钥
}

func NewTXOutput

func NewTXOutput(value int, address string) *TXOutput

创建一个新的输出

func (*TXOutput) IsLockedWithKey

func (out *TXOutput) IsLockedWithKey(pubKeyHash []byte) bool

检测输出是否是某签名签名过的

func (*TXOutput) Lock

func (out *TXOutput) Lock(address []byte)

输出签名(锁定)

type TXOutputs

type TXOutputs struct {
	Outputs []TXOutput
}

所有输出

func DeserializeOutputs

func DeserializeOutputs(data []byte) TXOutputs

解码输出

func (TXOutputs) Serialize

func (outs TXOutputs) Serialize() []byte

编码输出

type Transaction

type Transaction struct {
	Id        []byte    // 交易的hash值
	Inputs    []TXInput // 交易的所有输入(指明引用了哪个输出)
	TXOutputs           // 交易的所有输出
}

func DeserializeTransaction

func DeserializeTransaction(data []byte) Transaction

交易解码输出

func NewCoinbaseTx

func NewCoinbaseTx(to, data string) *Transaction

创建coinbase交易

func NewTransaction

func NewTransaction(w *wallet.Wallet, to string, amount int) *Transaction

创建交易

func (*Transaction) Hash

func (tx *Transaction) Hash() []byte

func (Transaction) IsCoinbase

func (tx Transaction) IsCoinbase() bool

检查是否是coinbase交易

func (Transaction) Serialize

func (tx Transaction) Serialize() []byte

编码交易

func (*Transaction) Sign

func (tx *Transaction) Sign(privateKey ecdsa.PrivateKey, prevTXs map[string]Transaction)

对交易进行私钥签名(要取输出的公钥一起加密)

func (Transaction) String

func (tx Transaction) String() string

可视化输出交易信息

func (*Transaction) TrimmedCopy

func (tx *Transaction) TrimmedCopy() Transaction

复制交易(避免修改到原交易信息)

func (*Transaction) Verify

func (tx *Transaction) Verify(prevTXs map[string]Transaction) bool

type UTXOSet

type UTXOSet struct {
	*leveldb.DB
}

未花掉的输出(相对接受者就是输入)

func NewUTXOSet

func NewUTXOSet() UTXOSet

创建UTXO集合

func (UTXOSet) AddTXOutputs

func (u UTXOSet) AddTXOutputs(tx *Transaction)

添加花费的输出(没有就创建)

func (UTXOSet) FindSpendableUTXO

func (u UTXOSet) FindSpendableUTXO(pubKeyHash []byte, amount int) (int, map[string][]int)

找到可用的UTXO

func (UTXOSet) FindUTXO

func (u UTXOSet) FindUTXO(address string) int

获取余额

func (UTXOSet) Update

func (u UTXOSet) Update(block *Block)

更新utxo集

Jump to

Keyboard shortcuts

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