Documentation
¶
Overview ¶
Package raft implements a basic version of the Raft consensus algorithm for a distributed blockchain network. Raft is a consensus algorithm designed for managing replicated logs in distributed systems, focusing on simplicity and understandability compared to other consensus algorithms like Paxos. It relies on leader election to ensure that only one node is actively managing updates, thereby simplifying the consensus process. This implementation simulates the core functions of Raft, including leader election, proposing new blocks, and achieving consensus before committing blocks to the blockchain.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Block ¶
type Block struct { Index int // Position of the block in the blockchain. Timestamp string // Time when the block was created. Data string // Data contained within the block (e.g., transactions). PrevHash string // Hash of the previous block to maintain immutability. Hash string // SHA-256 hash of the current block. }
Block represents an individual block in the blockchain. It contains information such as the index, timestamp, data, and cryptographic hashes.
func NewBlock ¶
NewBlock creates a new block given data, the previous block's hash, and the index. It calculates the block's hash to ensure integrity.
func (*Block) CalculateHash ¶
CalculateHash generates the SHA-256 hash of the block's contents. This ensures that any change to the block's data will produce a completely different hash.
type Blockchain ¶
type Blockchain struct { Blocks []Block // A slice of all blocks in the blockchain. Nodes []Node // A list of nodes participating in the Raft consensus network. Leader *Node // Pointer to the current leader node responsible for managing updates. }
Blockchain represents the distributed ledger that is managed by multiple nodes.
func NewBlockchain ¶
func NewBlockchain() *Blockchain
NewBlockchain initializes a new blockchain with a genesis block. The genesis block is the initial block that forms the foundation of the blockchain.
func NewRaftNetwork ¶
func NewRaftNetwork(size int) *Blockchain
NewRaftNetwork initializes a Raft network with the specified number of nodes. The nodes collaborate to reach consensus and elect a leader to manage block proposals.
func (*Blockchain) AddBlock ¶
func (bc *Blockchain) AddBlock(block Block)
AddBlock appends a new block to the blockchain. This function is called once a new block is validated and consensus is achieved.
func (*Blockchain) BroadcastBlock ¶
func (bc *Blockchain) BroadcastBlock(block Block) bool
BroadcastBlock sends a proposed block to all nodes for verification. A block is considered valid if more than half of the nodes approve it.
type Node ¶
type Node struct { ID int // Unique identifier for the node. IsLeader bool // Indicates if the node is the leader. Blockchain *Blockchain // Reference to the blockchain managed by the node. }
Node represents an individual node within the Raft network. Nodes can participate in leader elections, propose blocks, and verify or commit blocks.
func NewNode ¶
func NewNode(id int, blockchain *Blockchain) *Node
NewNode creates a new node with the given ID and associates it with a blockchain.
func (*Node) CommitBlock ¶
CommitBlock commits a verified block to the blockchain. This function is called by all nodes once consensus has been achieved.
func (*Node) Lead ¶
Lead allows the leader to propose and commit a new block to the blockchain. The leader proposes a block, broadcasts it for approval, and if approved, commits it.
func (*Node) ProposeBlock ¶
ProposeBlock allows the leader node to create a new block proposal based on the latest block.
func (*Node) RequestVote ¶
RequestVote allows a node to request votes from other nodes during the leader election process. If the node receives a majority of votes, it becomes the new leader.
func (*Node) VerifyBlock ¶
VerifyBlock allows a node to verify the validity of a proposed block. It checks if the previous hash matches the last block in the chain and if the block hash is correct.