blockchain

package
v0.0.0-...-9a5c4dc Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2021 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const MIDDLEWARE_URL = "http://localhost:8090/newTransaction"
View Source
const REWARD_AMOUNT = 5

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	Index     int
	Timestamp string
	Data      Data
	PrevHash  string
	Hash      string
	Nonce     int
}

Block is the Block object

type CandidateBlock

type CandidateBlock struct {
	Block Block       `json:"block"`
	Miner PeerAddress `json:"miner"`
}

CandidateBlock represents a peer's mined block that must be validated

func (CandidateBlock) GetData

func (c CandidateBlock) GetData() Data

GetData is the interface method that is required to retrieve Data object

func (CandidateBlock) ToString

func (c CandidateBlock) ToString() string

ToString is the interface method that is required to transform the Data object into a string for communication

type Chain

type Chain struct {
	ChainCopy []Block `json:"chainCopy"`
}

Chain contains a slice, or chain, of blocks, representing a blockchain

func (Chain) GetData

func (c Chain) GetData() Data

GetData is the interface method that is required to retrieve Data object

func (Chain) ToString

func (c Chain) ToString() string

ToString is the interface method that is required to transform the Data object into a string for communication

type Client

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

func (*Client) HandleCommand

func (c *Client) HandleCommand(msg Message, com CommunicationComponent) (err error)

HandleCommand is the interface method that handles the passed message

func (*Client) Initialize

func (c *Client) Initialize(com CommunicationComponent, p *Peer) error

Initialize is the interface method that calls this component's initialize method

func (Client) Sign

func (c Client) Sign(t Transaction) (Transaction, error)

func (Client) Terminate

func (c Client) Terminate()

Terminate is the interface method that calls this component's cleanup method

func (Client) Verify

func (c Client) Verify(t Transaction) bool

type ClientComponent

type ClientComponent interface {
	Initialize(com CommunicationComponent, p *Peer) error
	Terminate()
	Verify(t Transaction) bool
	Sign(t Transaction) (Transaction, error)
	HandleCommand(msg Message, com CommunicationComponent) (err error)
}

ClientComponent standardizes methods for any Peer client component

type CommunicationComponent

type CommunicationComponent interface {
	Initialize() error
	InitializeWithPort(port int) error
	GetPeerNodes() []PeerAddress
	GetMiddlewarePeer() PeerAddress
	GetSelfAddress() PeerAddress
	GetMessageChannel() chan Message
	RecieveFromNetwork(withTimeout bool) error
	GenerateMessage(cmd string, data Data) (Message, error)
	BroadcastMsgToNetwork(m Message) error
	SendMsgToPeer(m Message, p PeerAddress) error
	PingNetwork() error
	Terminate()
	PrunePeerNodes()
}

CommunicationComponent standardizes methods for any Peer communcation component

type Communicator

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

Communicator implements CommunicationsComponent and facilities Blockchain communication

func (Communicator) BroadcastMsgToNetwork

func (c Communicator) BroadcastMsgToNetwork(m Message) error

BroadcastMsgToNetwork is the interface method that uses helper methods to broadcast messages

func (*Communicator) GenerateMessage

func (c *Communicator) GenerateMessage(cmd string, data Data) (Message, error)

GenerateMessage uses the passed values to generate a new Message

func (Communicator) GetMessageChannel

func (c Communicator) GetMessageChannel() chan Message

GetMessageChannel is the interface retriever method that returns the channel that a message from a peer is put into upon read

func (Communicator) GetMiddlewarePeer

func (c Communicator) GetMiddlewarePeer() PeerAddress

GetMiddlewarePeer is the interface retriever method that returns the Middleware Peer's address

func (Communicator) GetPeerNodes

func (c Communicator) GetPeerNodes() []PeerAddress

GetPeerNodes is the interface retriever method that returns this node's list of peers

func (Communicator) GetSelfAddress

func (c Communicator) GetSelfAddress() PeerAddress

GetSelfAddress is the interface retriever method that returns this Peer's adress

func (*Communicator) Initialize

func (c *Communicator) Initialize() error

Initialize initializes a new communicator by initializing a socket and ZeroConf service and discovering other services

func (*Communicator) InitializeWithPort

func (c *Communicator) InitializeWithPort(port int) error

InitializeWithPort "overloads" thr Initialize method, and initializes the communicator with the passed well-defined port, instead of dynamically assgning a port

func (Communicator) PingNetwork

func (c Communicator) PingNetwork() error

PingNetwork is the interface method that sends a ping to all known peer nodes

func (*Communicator) PrunePeerNodes

func (c *Communicator) PrunePeerNodes()

PrunePeerNodes is the interface method that removes nodes from the peerNodes list which have not sent a message within the previous 75 seconds, as we assume that node to have gone down in this case

func (*Communicator) RecieveFromNetwork

func (c *Communicator) RecieveFromNetwork(withTimeout bool) error

RecieveFromNetwork is the interface method that returns a Message that it reads from this peer's UDP socket

func (Communicator) SendMsgToPeer

func (c Communicator) SendMsgToPeer(m Message, p PeerAddress) error

SendMsgToPeer is the interface method that uses helper methods to send a message to a peer

func (Communicator) Terminate

func (c Communicator) Terminate()

Terminate is the interface method that calls this component's cleanup method

type ConsensusComponent

type ConsensusComponent interface {
	ValidateBlock(b Block) bool
	HandleCommand(msg Message, p *Peer) error
	GetCandidateBlock() Block
	Initialize() error
	Terminate()
}

ConsensusComponent standardizes methods for any Peer consensus component

type Data

type Data interface {
	GetData() Data
	ToString() string
}

Data is an interface used to standardize methods for any type of Block data

type LotteryEntry

type LotteryEntry struct {
	Stake int         `json:"stake"`
	Peer  PeerAddress `json:"peer"`
}

LotteryEntry represents one entry in the proof of stake lottery

func (LotteryEntry) GetData

func (l LotteryEntry) GetData() Data

GetData is the interface method that is required to retrieve Data object

func (LotteryEntry) ToString

func (l LotteryEntry) ToString() string

ToString is the interface method that is required to transform the Data object into a string for communication

type Message

type Message struct {
	From    PeerAddress `json:"from"`
	Command string      `json:"command"`
	Data    Data        `json:"data,omitempty"`
}

Message is the struct that is marshalled/demarshalled between peers to communicate

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(bytes []byte) error

UnmarshalJSON is a custom JSON unmarshaller

type Middleware

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

Middleware is the Middleware object

func NewMiddleware

func NewMiddleware(com CommunicationComponent, udpPort int, serverPort int) (Middleware, error)

NewMiddleware creates and returns a new Middleware

func (*Middleware) Initialize

func (m *Middleware) Initialize(udpPort int, serverPort int) error

Initializes the middleware by starting the request handlers, initializing its components and serving itself on the network

func (*Middleware) Run

func (m *Middleware) Run()

Utilizes its component and the http package to receive requests from clients and send/recieve messages to blockchain peers on the p2p network

type Peer

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

Peer is the Peer object

func NewPeer

NewPeer creates and returns a new Peer, with the Genesis Block and Components initialized

func (*Peer) Run

func (p *Peer) Run()

Run utilizes the Peer components to run this Peer peer by sending/recieving requests and messages on the p2p network

type PeerAddress

type PeerAddress struct {
	Address         net.UDPAddr `json:"address"`
	LastMessageTime time.Time   `json:"lastMessageTime"`
}

PeerAddress represents a peer on the network and contains metadata about that peer

func (PeerAddress) String

func (p PeerAddress) String() string

type PeerChains

type PeerChains struct {
	List [][]Block `json:"list"`
}

PeerChains is a list of all the copies of the blockchain on the network

func (PeerChains) GetData

func (p PeerChains) GetData() Data

GetData is the interface method that is required to retrieve Data object

func (PeerChains) ToString

func (p PeerChains) ToString() string

ToString is the interface method that is required to transform the Data object into a string for communication

type ProofOfStake

type ProofOfStake struct {
	StakeAmount    int
	CandidateBlock Block
	// contains filtered or unexported fields
}

ProofOfStake algorithm used in mining blocks

func (ProofOfStake) CalculateHash

func (p ProofOfStake) CalculateHash(b Block) string

CalculateHash is the interface method that calculates a hash given some data

func (ProofOfStake) GetCandidateBlock

func (p ProofOfStake) GetCandidateBlock() Block

Terminate is the interface method that calls this component's cleanup method

func (*ProofOfStake) HandleCommand

func (p *ProofOfStake) HandleCommand(msg Message, peer *Peer) (err error)

HandleCommand is the interface method that handles the passed message

func (ProofOfStake) Initialize

func (p ProofOfStake) Initialize() error

Initialize is the interface method that calls this component's initialize method

func (ProofOfStake) Terminate

func (p ProofOfStake) Terminate()

Terminate is the interface method that calls this component's cleanup method

func (ProofOfStake) ValidateBlock

func (p ProofOfStake) ValidateBlock(b Block) bool

ValidateBlock is an interface method that verifies that the proof generated by this component's proof method is a valid proof for the block

type ProofOfWork

type ProofOfWork struct {
	ProofDifficulty int
	CandidateBlock  Block
	// contains filtered or unexported fields
}

ProofOfWork algorithm used in mining blocks

func (ProofOfWork) CalculateHash

func (p ProofOfWork) CalculateHash(b Block) string

CalculateHash is the interface method that calculates a hash given some data

func (ProofOfWork) GetCandidateBlock

func (p ProofOfWork) GetCandidateBlock() Block

Terminate is the interface method that calls this component's cleanup method

func (*ProofOfWork) HandleCommand

func (p *ProofOfWork) HandleCommand(msg Message, peer *Peer) (err error)

HandleCommand is the interface method that handles the passed message

func (ProofOfWork) Initialize

func (p ProofOfWork) Initialize() error

Initialize is the interface method that calls this component's initialize method

func (ProofOfWork) Terminate

func (p ProofOfWork) Terminate()

Terminate is the interface method that calls this component's cleanup method

func (ProofOfWork) ValidateBlock

func (p ProofOfWork) ValidateBlock(b Block) bool

ValidateBlock is an interface method that verifies that the proof generated by this component's proof method is a valid proof for the block

type PublicKey

type PublicKey struct {
	X string `json:"x"`
	Y string `json:"y"`
}

PublicKey represents a peer's mined block that must be validated

func (PublicKey) GetData

func (pk PublicKey) GetData() Data

GetData is the interface method that is required to retrieve Data object

func (PublicKey) ToString

func (pk PublicKey) ToString() string

ToString is the interface method that is required to transform the Data object into a string for communication

type Transaction

type Transaction struct {
	From      string `json:"from"`
	To        string `json:"to"`
	Amount    int    `json:"amount"`
	Signature string `json:"signature"`
}

Transaction is a type of Data

func (Transaction) GetData

func (t Transaction) GetData() Data

GetData is the interface method that is required to retrieve Data object

func (Transaction) ToString

func (t Transaction) ToString() string

ToString is the interface method that is required to transform the Data object into a string for communication

Jump to

Keyboard shortcuts

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