blockchain

package
v0.0.0-...-aaa359e Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CoinbaseTransactionAmount = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	Address ed25519.PublicKey `json:"address"`
	Nonce   uint              `json:"nonce"`
	Balance uint              `json:"balance"`
}

Account holds coins and an incrementing nonce

type Accounts

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

Accounts represents all the accounts within the blockchain

func AccountsFromBlockchain

func AccountsFromBlockchain(blocks []*Block) *Accounts

AccountsFromBlockchain generates the current account states from the blockchain

func NewAccounts

func NewAccounts() *Accounts

NewAccounts returns an empty Accounts struct

func (*Accounts) ApplyTransaction

func (a *Accounts) ApplyTransaction(transaction Transaction) error

ApplyTransaction applies the transaction if it's valid

func (*Accounts) ListAccounts

func (a *Accounts) ListAccounts() []Account

ListAccounts returns all known accounts

func (*Accounts) Read

func (a *Accounts) Read(address ed25519.PublicKey) (*Account, error)

Read returns the account matching the address or an error if the account is unknown

type Block

type Block struct {
	Number       int           `json:"number"`
	Time         time.Time     `json:"time"`
	Transactions []Transaction `json:"transactions"`
	Nonce        int           `json:"nonce"`
	PreviousHash []byte        `json:"previousHash"`
	Hash         []byte        `json:"hash"`
}

Block is an individual block in the blockchain

func GenesisBlock

func GenesisBlock() *Block

GenesisBlock returns the fixed first block in the blockchain

func NewBlock

func NewBlock(number int, previousHash []byte, transactions []Transaction, nonce int) *Block

NewBlock creates a new block

func (*Block) ComputeHash

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

ComputeHash computes the hash for the block

func (*Block) IsValid

func (b *Block) IsValid(previous *Block) bool

IsValid indicates if the block is valid

func (Block) String

func (b Block) String() string

String returns the string representation of a block

type Blockchain

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

Blockchain represents a full blockchain

func NewBlockchain

func NewBlockchain(keyPair *keys.KeyPair) *Blockchain

NewBlockchain returns a new blockchain with a genesis block

func (*Blockchain) AddTransaction

func (b *Blockchain) AddTransaction(transaction Transaction) error

AddTransaction adds transaction to the pool of available transactions to include in next block

func (*Blockchain) LastBlock

func (b *Blockchain) LastBlock() *Block

LastBlock returns the last block in the blockchain

func (*Blockchain) MineBlock

func (b *Blockchain) MineBlock() Block

MineBlock mines a new valid block with transactions from the mempool

func (*Blockchain) SubmitExternalBlock

func (b *Blockchain) SubmitExternalBlock(block *Block)

SubmitExternalBlock sends an externally received block to the blockchain

type ProofOfWorkRequest

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

ProofOfWorkRequest is a request to mine a new block

type Transaction

type Transaction struct {
	Sender    []byte    `json:"sender"`
	Receiver  []byte    `json:"receiver"`
	Amount    uint      `json:"amount"`
	Nonce     uint      `json:"nonce"`
	Time      time.Time `json:"time"`
	Signature []byte    `json:"signature"`
}

Transaction represents an individual transaction

func CoinbaseTransactionTo

func CoinbaseTransactionTo(receiver ed25519.PublicKey) Transaction

CoinbaseTransaction contructs a coinbase transaction

func NewTransaction

func NewTransaction(sender ed25519.PublicKey, receiver ed25519.PublicKey, amount uint, nonce uint) *Transaction

NewTransaction returns a new unsigned transaction

func (*Transaction) Bytes

func (t *Transaction) Bytes() ([]byte, error)

Bytes returns the transaction as bytes

func (*Transaction) IsCoinbase

func (t *Transaction) IsCoinbase() bool

IsCoinBase tells whether the transaction is a coinbase transaction

func (*Transaction) Sign

func (t *Transaction) Sign(privateKey ed25519.PrivateKey) ([]byte, error)

Sign signs the transaction using the given key and returns the signature

func (Transaction) String

func (t Transaction) String() string

String returns the string representation of a transaction

func (*Transaction) ValidSignature

func (t *Transaction) ValidSignature() bool

ValidSignature indicates whether the transaction signature is valid

Jump to

Keyboard shortcuts

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