Documentation ¶
Index ¶
- Variables
- func DetermineDifficultyForChainIndex(chain []Block, index int) int64
- func GetOutboundIP() net.IP
- func IsTransactionAlreadyInMemPoolOrChain(t Transaction, memPool []Transaction, chain []Block) bool
- func IsTransactionInChain(t Transaction, chain []Block) bool
- func IsTransactionInMemPool(t Transaction, memPool []Transaction) bool
- func SHA256(o interface{}) string
- func ValidateProof(block Block) bool
- func ValidateSignature(transaction Transaction, validationServerURL string) bool
- func ValidateTransaction(transaction Transaction, utxo UTXO, validationServerURL string) bool
- func WaitForCtrlC()
- type Block
- type BlockHeader
- type LocalNode
- func (l *LocalNode) AddMinedBlockToChain(block Block, alternativePeerConsensusFunction ...func()) bool
- func (l *LocalNode) AddTransactionToMemPool(transaction Transaction, doNotBroadcast ...bool)
- func (l *LocalNode) BroadcastBlock(b Block)
- func (l *LocalNode) BroadcastTransaction(t Transaction)
- func (l *LocalNode) Consensus(chains ...[]Block) bool
- func (l *LocalNode) GetPeerConsensus()
- func (l LocalNode) MineBlock(shouldMine *bool) *Block
- func (l *LocalNode) SendPeerOurChain(address string)
- func (l *LocalNode) Start(seedNodes []string)
- type NodeMessage
- type Proof
- type Transaction
- type UTXO
- type ValidationResponse
Constants ¶
This section is empty.
Variables ¶
var GenesisBlock = Block{BlockHeader: BlockHeader{Timestamp: 1585852979, Transactions: []Transaction{Transaction{Sender: "0", Recipient: "04500bdac952ec32d5031d6f540e2be9d4ff0d0add0b380b56f452ce5d86e713b78ff4d04a6d4bec5b61759b1d0b588a5ea7b720fb4e245036bfcd00d792fd0094", Amount: 100000000000000, Timestamp: 1585852961, Signature: ""}}, PreviousHash: ""}, Proof: Proof{Nonce: 0, DifficultyThreshold: 0}}
The first block in our Blockchain
var PortP2P uint16 = 7000
Functions ¶
func DetermineDifficultyForChainIndex ¶
Calculate the required difficulty threshold for an index in a chain based on the past delays in timestamps. Will default to 5 if chain length is less than 10.
func IsTransactionAlreadyInMemPoolOrChain ¶
func IsTransactionAlreadyInMemPoolOrChain(t Transaction, memPool []Transaction, chain []Block) bool
IsTransactionAlreadyInMemPoolOrChain checks whether the transaction already exists in a given MemPool + Blockchain.
func IsTransactionInChain ¶
func IsTransactionInChain(t Transaction, chain []Block) bool
IsTransactionInChain checks whether the transaction already exists in a given Blockchain.
func IsTransactionInMemPool ¶
func IsTransactionInMemPool(t Transaction, memPool []Transaction) bool
IsTransactionInMemPool checks whether the transaction already exists in a given MemPool.
func ValidateProof ¶
Checks whether a block has a proof that validates it.
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.
func ValidateTransaction ¶ added in v1.0.1
func ValidateTransaction(transaction Transaction, utxo UTXO, validationServerURL string) bool
Checks if a transaction is a positive number, the sender has enough coins the make the transaction, and that the signature is valid.
func WaitForCtrlC ¶
func WaitForCtrlC()
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.
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 LocalNode ¶
type LocalNode 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 string // A public key that is used to identify the node when mining (so this node can receive mining rewards IsMining bool // Stores whether the node is mining or not. If the node is mining and this bool is set to false, the node will terminate its mining process. MinimumChainsForConsensus int // How many chains we need before we run consensus // contains filtered or unexported fields }
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 (*LocalNode) AddMinedBlockToChain ¶
func (l *LocalNode) AddMinedBlockToChain(block Block, alternativePeerConsensusFunction ...func()) bool
Adds a new block to the chain (by first verifying it and getting its UTXO). It has side effects:
- It stops all mining processes on this node
- It removes the transactions inside the block from the MemPool
- It updates the UTXO
func (*LocalNode) AddTransactionToMemPool ¶
func (l *LocalNode) AddTransactionToMemPool(transaction Transaction, doNotBroadcast ...bool)
Adds a transaction to the MemPool (but will do nothing to incorporate it into a block or verify it).
func (*LocalNode) BroadcastBlock ¶
BroadcastBlock sends a block to all of our peers.
func (*LocalNode) BroadcastTransaction ¶
func (l *LocalNode) BroadcastTransaction(t Transaction)
BroadcastTransaction sends a transaction to all of our peers.
func (*LocalNode) Consensus ¶
Takes a slice of chains and finds the longest, valid chain and sets our chain to that chain. It will terminate if no chains are valid or once it finds a chain smaller than our current chain. It has side effects:
- It removes the transactions inside the chain's blocks from the MemPool
- It updates the UTXO
func (*LocalNode) GetPeerConsensus ¶
func (l *LocalNode) GetPeerConsensus()
GetPeerConsensus sends a message to all peers requesting their chain, then we choose 5 of them and run consensus on them.
func (LocalNode) MineBlock ¶
Finds a valid proof for a block and validates transactions from the MemPool. It removes invalid transactions. It returns a pointer to a new block that will be nil if the mining process was canceled. It does not add this block to the chain itself.
func (*LocalNode) SendPeerOurChain ¶
SendPeerOurChain sends a specific peer our chain.
type NodeMessage ¶
type NodeMessage struct { MessageType int // Can be: newBlock, newTransaction, thisIsMyChain, or needChain Body interface{} // The actual payload (it can be many types) }
Stores a type of message and a body.
func (NodeMessage) Marshal ¶
func (m NodeMessage) Marshal() []byte
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 string // The public key of the sender (ECDSA SECP256k1) Recipient string // The public key of the recipient (ECDSA SECP256k1) Amount uint64 // 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} ({TIMESTAMP})) }
A transaction stores information about a transaction with a signature.
func RemoveConfirmedTransactions ¶
func RemoveConfirmedTransactions(memPool []Transaction, confirmedTransactions []Transaction) []Transaction
RemoveConfirmedTransactions takes a list of transactions and a list of transactions that have been confirmed, and removes the ones that have been confirmed.
type UTXO ¶
The amount of unspent coin each user has associated with their public key
func ValidateBlock ¶
func ValidateBlock(blockIndex int, blocks []Block, utxo UTXO, validationServerURL string, shouldUseAltGenesisBlock ...bool) (bool, UTXO)
ValidateBlock takes the index of a block, the full Blockchain, a UTXO of the Blockchain up to that point, and a validationServerURL. It returns whether that block is valid and an updated UTXO including that block's transactions. 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
- Check that difficulty threshold is valid
- Check that there are not duplicate transactions in the block that appear earlier in the chain
func ValidateChain ¶
Runs the ValidateBlock function on each block in the chain (except the genesis block), and checks that the genesis block has not changed. It returns whether the chain is valid and an updated UTXO (or nil if not valid).
type ValidationResponse ¶
type ValidationResponse struct {
ValidSignature bool `json:"valid_signature"`
}
A struct to represent a response form a signature validation server.