blockchain

package
v0.0.0-...-7c9a5d0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = 12

Variables

This section is empty.

Functions

func DbExists

func DbExists() bool

func HandleFatalErrors

func HandleFatalErrors(err error)

func SplitBinary

func SplitBinary(data []byte) (a big.Int, b big.Int)

func ToHex

func ToHex(num int64) []byte

Types

type Block

type Block struct {
	Timestamp    uint
	PrevHash     []byte
	Transactions []*Transaction
	Hash         []byte
	Nonce        int
}

func Deserialize

func Deserialize(data []byte) *Block

func Genesis

func Genesis(coinbase *Transaction) *Block

func NewBlock

func NewBlock(txs []*Transaction, prevHash []byte) *Block

func (*Block) HashTransaction

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

func (*Block) Serialize

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

type BlockChain

type BlockChain struct {
	LastHash []byte
	Database *badger.DB
}

func ContinueBlockChain

func ContinueBlockChain(enableLog bool, address string) *BlockChain

func InitBlockChain

func InitBlockChain(enableLog bool, address string) *BlockChain

func (*BlockChain) AddBlock

func (chain *BlockChain) AddBlock(txs []*Transaction)

func (*BlockChain) Close

func (chain *BlockChain) Close() error

func (*BlockChain) FindSpendableOutputs

func (chain *BlockChain) FindSpendableOutputs(publicKeyHash []byte, amount int) (int, map[string][]int)

func (*BlockChain) FindTransaction

func (chain *BlockChain) FindTransaction(id []byte) (Transaction, error)

func (*BlockChain) FindUTXO

func (chain *BlockChain) FindUTXO(publicKeyHash []byte) []TxOutput

func (*BlockChain) FindUnspentTransactions

func (chain *BlockChain) FindUnspentTransactions(publicKeyHash []byte) []Transaction

func (*BlockChain) Iterator

func (chain *BlockChain) Iterator() *BlockChainIterator

func (*BlockChain) SignTransaction

func (bc *BlockChain) SignTransaction(tx *Transaction, privateKey ecdsa.PrivateKey)

func (*BlockChain) VerifyTransaction

func (bc *BlockChain) VerifyTransaction(tx *Transaction) bool

type BlockChainIterator

type BlockChainIterator struct {
	CurrentHash []byte
	Database    *badger.DB
}

func (*BlockChainIterator) Next

func (iter *BlockChainIterator) Next() *Block

type ProofOfWork

type ProofOfWork struct {
	Block  *Block
	Target *big.Int
}

func NewProof

func NewProof(b *Block) *ProofOfWork

func (*ProofOfWork) InitData

func (pow *ProofOfWork) InitData(nonce int) []byte

func (*ProofOfWork) Run

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

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

type Transaction

type Transaction struct {
	Id      []byte
	Inputs  []TxInput
	Outputs []TxOutput
}

func CoinbaseTx

func CoinbaseTx(to, data string) *Transaction

func NewTransaction

func NewTransaction(from, to string, amount int, chain *BlockChain) *Transaction

func (*Transaction) Hash

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

func (*Transaction) IsCoinbase

func (tx *Transaction) IsCoinbase() bool

func (Transaction) Serialize

func (tx Transaction) Serialize() []byte

func (*Transaction) SetID

func (tx *Transaction) SetID()

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 TxInput

type TxInput struct {
	Id        []byte
	Out       int
	Signature []byte
	PubKey    []byte
}

func (*TxInput) UsesKey

func (in *TxInput) UsesKey(pubKeyHash []byte) bool

type TxOutput

type TxOutput struct {
	Value      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)

Jump to

Keyboard shortcuts

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