Documentation
¶
Overview ¶
Package pbft implements a basic version of the Practical Byzantine Fault Tolerance (PBFT) consensus algorithm. PBFT is designed to allow a group of nodes to reach consensus in a distributed system, even in the presence of Byzantine faults—nodes that may behave arbitrarily or maliciously. This module simulates a blockchain network using PBFT to demonstrate how nodes work together to validate and commit new blocks, ensuring consistency and resilience against malicious actors. The algorithm requires agreement from at least 2/3 of the nodes for a block to be committed.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Block ¶
type Block struct {
Index int // The position of the block in the blockchain.
Timestamp string // The time when the block was created.
Data string // The data contained in the block (e.g., transactions).
PrevHash string // The hash of the previous block, establishing continuity of the chain.
Hash string // The cryptographic hash of the current block's contents.
}
Block represents an individual block in the blockchain. Each block contains essential information, including metadata, the cryptographic hash, and the data it stores.
func NewBlock ¶
NewBlock creates a new block given the data, index, and previous block hash. It calculates the hash for the new block to ensure data integrity.
func (*Block) CalculateHash ¶
CalculateHash generates the SHA-256 hash of the block's contents. This ensures that each block is uniquely represented and immutable.
type Blockchain ¶
type Blockchain struct {
Blocks []Block // A slice of all blocks in the blockchain.
Nodes []Node // A slice representing all nodes participating in PBFT consensus.
}
Blockchain represents the distributed ledger, which is maintained by nodes. It contains an ordered list of blocks, each of which is linked to its predecessor by cryptographic hash.
func NewBlockchain ¶
func NewBlockchain() *Blockchain
NewBlockchain initializes a new blockchain with a genesis block, which serves as the root of the chain.
func NewPBFTNetwork ¶
func NewPBFTNetwork(size int) *Blockchain
NewPBFTNetwork initializes a PBFT network with a specified number of nodes. The first node is assigned as the primary node, and all nodes are linked to the blockchain.
func (*Blockchain) AddBlock ¶
func (bc *Blockchain) AddBlock(block Block)
AddBlock appends a new block to the blockchain.
func (*Blockchain) BroadcastBlock ¶
func (bc *Blockchain) BroadcastBlock(block Block) bool
BroadcastBlock broadcasts a proposed block to all nodes in the network for verification. A block is considered valid if at least 2/3 of nodes approve it.
func (*Blockchain) RunPBFT ¶
func (bc *Blockchain) RunPBFT(data string)
RunPBFT initiates the Practical Byzantine Fault Tolerance consensus process. The primary node proposes a new block, and if it receives approval from 2/3 of nodes, all nodes commit the block.
type Node ¶
type Node struct {
ID int // A unique identifier for the node.
IsPrimary bool // A flag indicating if this node is the primary node (leader).
State string // The state of the node (optional, for future implementation).
Blockchain *Blockchain // Reference to the blockchain managed by the node.
}
Node represents an individual node participating in the PBFT protocol. Each node can propose, verify, and commit blocks, and maintains its own state and reference to the blockchain.
func NewNode ¶
func NewNode(id int, isPrimary bool, blockchain *Blockchain) *Node
NewNode creates a new node with the given ID, assigns it as primary or follower, and links it to the blockchain.
func (*Node) CommitBlock ¶
CommitBlock adds a block to the blockchain, once it has been verified and approved by the network.
func (*Node) ProposeBlock ¶
ProposeBlock allows the primary node to create a new block proposal. It retrieves the latest block and proposes a new block with the given data.
func (*Node) VerifyBlock ¶
VerifyBlock allows a node to verify the validity of a proposed block. The node checks if the block's previous hash matches the last block in the chain and if the block hash is valid.