blockchain

package
v0.0.0-...-c5c7705 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2024 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VersionCmd   = "version"
	AddrCmd      = "addr"
	BlockCmd     = "block"
	GetDataCmd   = "get_data"
	InvCmd       = "inv"
	GetBlocksCmd = "get_blocks"
	TxCmd        = "tx"
)

Variables

View Source
var ErrNoBlock = errors.New("no more block")

Functions

func HashPubKey

func HashPubKey(pubKey []byte) []byte

HashPubKey hashes public key

func ValidateAddress

func ValidateAddress(address string) bool

ValidateAddress check if address is valid.

Types

type Account

type Account struct {
	PrivateKey ecdsa.PrivateKey
	PublicKey  []byte
}

Account stores private and public keys.

func NewAccount

func NewAccount() *Account

NewAccount creates and returns a Account

func (*Account) Address

func (a *Account) Address() []byte

Address returns account address.

func (*Account) String

func (a *Account) String() string

type Block

type Block struct {
	Timestamp     int64
	Transactions  []*Transaction
	PrevBlockHash []byte
	Hash          []byte
	Nonce         int
	Height        int
}

Block represents a block in the blockchain.

func DeserializeBlock

func DeserializeBlock(v []byte) *Block

DeserializeBlock deserializes a block.

func NewBlock

func NewBlock(transactions []*Transaction, prevBlockHash []byte, height int) *Block

NewBlock creates and returns Block.

func NewGenesisBlock

func NewGenesisBlock(coinbase *Transaction) *Block

NewGenesisBlock creates and returns genesis Block.

func (*Block) HashTransactions

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

HashTransactions returns a hash of the transactions in the block.

func (*Block) Serialize

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

Serialize serializes the block.

type Blockchain

type Blockchain struct {
	// contains filtered or unexported fields
}

Blockchain implements interactions with a DB.

func CreateBlockchain

func CreateBlockchain(node, address string) (*Blockchain, error)

CreateBlockchain creates a new blockchain DB.

func NewBlockchain

func NewBlockchain(node string) (*Blockchain, error)

NewBlockchain creates a new Blockchain with genesis Block.

func (*Blockchain) Foreach

func (bc *Blockchain) Foreach(fn func(*Block) error) error

func (*Blockchain) GetBestHeight

func (bc *Blockchain) GetBestHeight() int

GetBestHeight returns the height of the latest block

func (*Blockchain) GetBlockByHash

func (bc *Blockchain) GetBlockByHash(hash []byte) (Block, error)

GetBlockByHash finds a block by its hash and returns it

func (*Blockchain) GetBlockHashes

func (bc *Blockchain) GetBlockHashes() [][]byte

GetBlockHashes returns a list of hashes of all the blocks in the chain

func (*Blockchain) GetTransactionById

func (bc *Blockchain) GetTransactionById(id []byte) (Transaction, error)

GetTransactionById get a transaction by its ID.

func (*Blockchain) GetUTXO

func (bc *Blockchain) GetUTXO() map[string]TxOutputs

GetUTXO get all unspent transaction outputs and returns transactions with spent outputs removed.

func (*Blockchain) Mine

func (bc *Blockchain) Mine(txs []*Transaction) (*Block, error)

Mine mines a new block with the provided transactions.

func (*Blockchain) SignTx

func (bc *Blockchain) SignTx(tx *Transaction, privateKey ecdsa.PrivateKey) error

SignTx signs inputs of a Transaction

func (*Blockchain) Submit

func (bc *Blockchain) Submit(block *Block) error

Submit saves the block into the blockchain.

func (*Blockchain) T

func (bc *Blockchain) T()

func (*Blockchain) VerifyTx

func (bc *Blockchain) VerifyTx(tx *Transaction) bool

VerifyTx verifies transaction input signatures

type ProofOfWork

type ProofOfWork struct {
	// contains filtered or unexported fields
}

ProofOfWork represents a proof-of-work.

func NewProofOfWork

func NewProofOfWork(block *Block) *ProofOfWork

NewProofOfWork builds and returns a ProofOfWork.

func (*ProofOfWork) Run

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

Run performs a proof-of-work

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

Validate validates block's PoW.

type Server

type Server struct {
	Id           string
	MinerAddress string
	// contains filtered or unexported fields
}

func NewServer

func NewServer(id, miner string) *Server

func NewServerWithBlockchain

func NewServerWithBlockchain(bc *Blockchain, id, miner string) *Server

func (*Server) SendTx

func (n *Server) SendTx(tx *Transaction)

func (*Server) Start

func (n *Server) Start() error

Start starts a node.

type Transaction

type Transaction struct {
	ID   []byte
	Vin  []TxInput
	Vout []TxOutput
}

Transaction represents a Bitcoin transaction.

func DeserializeTx

func DeserializeTx(v []byte) Transaction

DeserializeTx deserializes a transaction.

func NewCoinbaseTx

func NewCoinbaseTx(to, data string) *Transaction

NewCoinbaseTx creates a new coinbase transaction

func NewUTXOTransaction

func NewUTXOTransaction(account *Account, to string, amount int, UTXOSet *UTXOSet) (*Transaction, error)

NewUTXOTransaction creates a new transaction.

func (*Transaction) Hash

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

Hash returns the hash of the Transaction.

func (*Transaction) IsCoinbase

func (tx *Transaction) IsCoinbase() bool

IsCoinbase checks whether the transaction is coinbase.

func (*Transaction) Serialize

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

Serialize returns a serialized Transaction.

func (*Transaction) Sign

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

Sign signs each input of a Transaction.

func (*Transaction) String

func (tx *Transaction) String() string

String returns a human-readable representation of a transaction.

func (*Transaction) TrimmedCopy

func (tx *Transaction) TrimmedCopy() Transaction

TrimmedCopy creates a trimmed copy of Transaction to be used in signing.

func (*Transaction) Verify

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

Verify verifies signatures of Transaction inputs.

type TxInput

type TxInput struct {
	TxId      []byte
	Vout      int
	Signature []byte
	PubKey    []byte
}

TxInput represents a transaction input.

func (*TxInput) UsesKey

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

UsesKey checks whether the address initiated the transaction.

type TxOutput

type TxOutput struct {
	Value      int
	PubKeyHash []byte
}

TxOutput represents a transaction output.

func NewTxOutput

func NewTxOutput(v int, address string) *TxOutput

NewTxOutput create a new TXOutput.

func (*TxOutput) IsLockedWithKey

func (to *TxOutput) IsLockedWithKey(pubKeyHash []byte) bool

IsLockedWithKey checks if the output can be used by the owner of the pubkey

func (*TxOutput) Lock

func (to *TxOutput) Lock(address []byte)

Lock signs the output.

type TxOutputs

type TxOutputs struct {
	Values []TxOutput
}

TxOutputs collects TXOutput

func DeserializeTxOutputs

func DeserializeTxOutputs(data []byte) TxOutputs

DeserializeTxOutputs deserializes TXOutputs

func (*TxOutputs) Serialize

func (to *TxOutputs) Serialize() []byte

Serialize serializes TXOutputs

type UTXOSet

type UTXOSet struct {
	// contains filtered or unexported fields
}

UTXOSet represents UTXO set.

func NewUTXOSet

func NewUTXOSet(bc *Blockchain) *UTXOSet

NewUTXOSet returns a UTXOSet.

func (*UTXOSet) GetSpendableOutputs

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

GetSpendableOutputs finds and returns unspent outputs to reference in inputs.

func (*UTXOSet) GetUTXO

func (u *UTXOSet) GetUTXO(pubKeyHash []byte) []TxOutput

GetUTXO finds UTXO for a public key hash.

func (*UTXOSet) Rebuild

func (u *UTXOSet) Rebuild() error

Rebuild rebuilds the UTXO set.

func (*UTXOSet) TxCount

func (u *UTXOSet) TxCount() int

TxCount returns the number of transactions in the UTXO set.

func (*UTXOSet) Update

func (u *UTXOSet) Update(block *Block) error

Update updates the UTXO set with transactions from the Block is considered to be the tip of a blockchain

type Wallet

type Wallet struct {
	Accounts map[string]*Account
}

Wallet stores a collection of accounts.

func NewWallet

func NewWallet(node string) (*Wallet, error)

NewWallet creates Wallet and fills it from a file if it exists.

func (*Wallet) GetAccount

func (w *Wallet) GetAccount(address string) Account

GetAccount returns an Account by its address

func (*Wallet) GetAddresses

func (w *Wallet) GetAddresses() []string

GetAddresses returns an array of addresses stored in the wallet file

func (*Wallet) Load

func (w *Wallet) Load(node string) error

Load loads accounts from the file

func (*Wallet) NewAccount

func (w *Wallet) NewAccount() string

NewAccount adds an Account to Wallet.

func (*Wallet) Save

func (w *Wallet) Save(node string) error

Save saves accounts to a file

Jump to

Keyboard shortcuts

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