consensus

package
v0.0.0-...-222c718 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TotalSupply int64 = 10000000 // Example value

TotalSupply represents the total supply of tokens in the network

Functions

func CalculateRewards

func CalculateRewards(validator Validator) int64

CalculateRewards calculates rewards for a validator

func DistributeRewards

func DistributeRewards(validators []Validator)

DistributeRewards distributes rewards across all validators

func GenerateKeyPair

func GenerateKeyPair() (ed25519.PrivateKey, ed25519.PublicKey)

GenerateKeyPair generates a new ed25519 key pair

func SlashValidator

func SlashValidator(validator Validator, slashingPercentage int64)

SlashValidator slashes a validator for misbehavior

Types

type Block

type Block struct {
	Index     int
	Timestamp string
	Data      string
	PrevHash  string
	Hash      string
	Signature []byte
	Validator string
}

Block represents a block in the blockchain

func NewBlock

func NewBlock(index int, data string, prevHash string, privateKey ed25519.PrivateKey, validator string) *Block

NewBlock creates a new block

func (*Block) CalculateHash

func (b *Block) CalculateHash() string

CalculateHash computes the hash of the block using SHA-256

func (*Block) VerifyBlock

func (b *Block) VerifyBlock(publicKey ed25519.PublicKey) bool

VerifyBlock verifies the block's signature

type Blockchain

type Blockchain struct {
	Blocks     []*Block
	PublicKey  ed25519.PublicKey // Store the public key for validation
	Validators []string          // List of validators
}

Blockchain represents the blockchain

func NewBlockchain

func NewBlockchain(publicKey ed25519.PublicKey) *Blockchain

NewBlockchain creates a new blockchain with a genesis block

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(data string, privateKey ed25519.PrivateKey)

AddBlock adds a new block to the blockchain

func (*Blockchain) SelectValidator

func (bc *Blockchain) SelectValidator() string

SelectValidator selects a validator from the list

func (*Blockchain) ValidateBlockchain

func (bc *Blockchain) ValidateBlockchain() bool

ValidateBlockchain validates the integrity of the blockchain

type Consensus

type Consensus struct {
	Node *p2p.Node
}

Consensus implements the Proof of Stake logic

func NewConsensus

func NewConsensus(node *p2p.Node) *Consensus

NewConsensus creates a new Consensus instance

func (*Consensus) BroadcastBlock

func (c *Consensus) BroadcastBlock(block *Block)

BroadcastBlock broadcasts a block to all peers

type Validator

type Validator struct {
	Address string
	Stake   int64
}

Validator represents a node that participates in consensus

Jump to

Keyboard shortcuts

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