core

package
v0.0.0-...-675d3c6 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2020 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SHA256

func SHA256(o interface{}) string

Hashes any type with SHA256 and converts to hex.

func ValidateProof

func ValidateProof(block Block) bool

Checks whether a block has a proof that validates its BlockHeader.

func ValidateSignature

func ValidateSignature(transaction Transaction, validationServerURL string) bool

A function that validates the signature on a transaction by requesting its validity from a validationServerURL.

Types

type Block

type Block struct {
	BlockHeader
	Proof Proof // The nonce and difficulty threshold that validates this block
}

A Block is a block header with a proof that when put into the format {Proof}-{BlockHeader}, can be hashed into a hex string with x leading 0s.

func LastBlock

func LastBlock(chain []Block) Block

Gets the most recent link in a chain of blocks.

func MineBlock

func MineBlock(utxo UTXO, transactions []Transaction, operatorKey UserPublicKey, previousBlock Block, validationServerURL string) Block

Finds a valid proof for a block and validates transactions from the MemPool. It returns a new, valid block. It does not add this block to the chain itself.

type BlockHeader

type BlockHeader struct {
	Timestamp    int64         // The time when this block header was generated
	Transactions []Transaction // The transactions the enclosing block validates
	PreviousHash string        // The hash of the previous block
}

A BlockHeader stores a timestamp, a list of transactions and the hash of the previous block.

type Blockchain

type Blockchain struct {
	Chain   []Block       // The actual chain of transactions that makes up this "Blockchain"
	MemPool []Transaction // The waiting room of transactions that are yet to be incorporated in a block. These get cleared out every 24 hours.
	UTXO    UTXO          // The amount of unspent transactions each user has associated with their public key

	ValidationServerURL string        // A link to a server that can be used to validate signatures
	OperatorPublicKey   UserPublicKey // A public key that is used to identify the node when mining (so this node can receive mining rewards
}

A Blockchain is a struct that stores a Chain of Blocks, as well as MemPool and manages its own UTXO map. It also stores a ValidationServerURL and an Operator Public key which is used to identify that node when mining

func (*Blockchain) AddMinedBlockToChain

func (b *Blockchain) AddMinedBlockToChain(block Block) bool

Adds a new block to the chain (by first verifying it and getting its UTXO). It has side effects:

  • It removes the transactions inside the block from the MemPool
  • It updates the UTXO

func (*Blockchain) AddTransactionToMemPool

func (b *Blockchain) AddTransactionToMemPool(transaction Transaction)

Adds a transaction to the MemPool (but will do nothing to incorporate it into a block or verify it).

type Proof

type Proof struct {
	Nonce               int64 // The random factor that changes the hash
	DifficultyThreshold int64 // The number of leading 0s required in the hash
}

The nonce and difficulty threshold achieved by the nonce and BlockHeader to generate proof of work.

type Transaction

type Transaction struct {
	Sender    UserPublicKey // The public key of the sender
	Recipient UserPublicKey // The public key of the recipient
	Amount    int           // The amount of coin transferred
	Timestamp int64         // The time at which this transaction was made. This value does not need to be accurate, it is only for the purpose of ordering transactions in a BlockHeader.
	Signature string        // A hex string that is an ECDSA signed representation of this transaction ({SENDER} -{AMOUNT}-> {RECIPIENT})
}

A transaction stores information about a transaction with a signature.

func RemoveConfirmedTransactions

func RemoveConfirmedTransactions(memPool []Transaction, confirmedTransactions []Transaction) []Transaction

Takes a list of transactions and a list of transactions that have been confirmed, and removes the ones that have been confirmed.

func RemoveFromTransactions

func RemoveFromTransactions(slice []Transaction, s int) []Transaction

Removes a transaction from a slice at x index.

type UTXO

type UTXO map[UserPublicKey]int

The amount of unspent coin each user has associated with their public key

func ValidateChain

func ValidateChain(blocks []Block, validationServerURL string) (bool, UTXO)

Does these checks to ensure the chain is valid:

  • Check that previous hashes are valid
  • Check that users have enough UTXO to afford transactions
  • Check that proofs are valid
  • Check that there are not more than one coinbase transaction in each block
  • Check that signatures are valid

It returns whether the chain is valid and an updated UTXO.

type UserPublicKey

type UserPublicKey string

A valid hexadecimal string that represents a user's public key. It must be on the ECDSA SECP256k1 curve.

type ValidationResponse

type ValidationResponse struct {
	ValidSignature bool `json:"valid_signature"`
}

A struct to represent a response form a signature validation server.

Jump to

Keyboard shortcuts

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