consensus

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 9 Imported by: 0

README

Validator Core Functionality:

Validator initialization and key management Block proposal and validation Transaction verification Signature generation and verification

Reputation System:

Dynamic reputation scoring (0-100) Reputation decay over time Slashing conditions Jailing mechanism for misbehavior

Staking Management:

Stake delegation and undelegation Minimum/maximum stake enforcement Power calculation based on stake and reputation Slashing penalty implementation

Performance Monitoring:

Block validation statistics Missed block tracking Uptime monitoring Performance metrics

Key components include:

Status Management:

Multiple validator states (Active, Inactive, Jailing, Jailed, Slashed) State transition handling Automatic unjailing after timeout

Consensus Participation:

Block proposal generation Block validation Transaction verification Signature handling

Background Processes:

Validation monitoring Reputation decay Statistics updates Missed block checking

Documentation

Overview

Package consensus implements the reputation scoring system for ATLYS

Package consensus implements the validator functionality for ATLYS

Package consensus implements the voting mechanism for ATLYS

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EpochManager

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

EpochManager handles epoch-based score calculations

type ReputationConfig

type ReputationConfig struct {
	InitialScore      uint32
	MinScore          uint32
	MaxScore          uint32
	DecayRate         float64
	DecayInterval     time.Duration
	UpdateInterval    time.Duration
	BonusMultiplier   float64
	PenaltyMultiplier float64
	SlashingThreshold uint32
	RecoveryRate      float64
	HistorySize       uint32
	ConsensusWeight   float64
	StakeWeight       float64
	UptimeWeight      float64
}

ReputationConfig contains configuration parameters for the reputation system

type ReputationScorer

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

ReputationScorer manages validator reputation scores

func NewReputationScorer

func NewReputationScorer(config *ReputationConfig) *ReputationScorer

NewReputationScorer creates a new instance of ReputationScorer

func (*ReputationScorer) GetEpochStats

func (rs *ReputationScorer) GetEpochStats() map[string]interface{}

GetEpochStats returns reputation statistics for the current epoch

func (*ReputationScorer) GetHistory

func (rs *ReputationScorer) GetHistory(address types.Address) (*ScoreHistory, error)

GetHistory returns a validator's reputation history

func (*ReputationScorer) GetScore

func (rs *ReputationScorer) GetScore(address types.Address) (uint32, error)

GetScore returns a validator's current reputation score

func (*ReputationScorer) RecoverScore

func (rs *ReputationScorer) RecoverScore(address types.Address) error

RecoverScore allows gradual score recovery after penalties

func (*ReputationScorer) RegisterValidator

func (rs *ReputationScorer) RegisterValidator(address types.Address, stake uint64) error

RegisterValidator initializes reputation tracking for a new validator

func (*ReputationScorer) SlashValidator

func (rs *ReputationScorer) SlashValidator(address types.Address, reason string, blockHeight uint64) error

SlashValidator applies a slashing penalty to a validator

func (*ReputationScorer) StartNewEpoch

func (rs *ReputationScorer) StartNewEpoch()

func (*ReputationScorer) UpdateScore

func (rs *ReputationScorer) UpdateScore(address types.Address, success bool, metrics *ValidationMetrics) error

UpdateScore updates a validator's reputation score based on performance

type ScoreHistory

type ScoreHistory struct {
	Scores      []uint32    // Historical scores
	Updates     []time.Time // Update timestamps
	SlashEvents []SlashEvent
	MaxEntries  uint32
}

ScoreHistory tracks historical reputation data

type SlashEvent

type SlashEvent struct {
	Timestamp   time.Time
	Reason      string
	Amount      uint32
	BlockHeight uint64
}

SlashEvent records details of a slashing event

type TimeoutConfig

type TimeoutConfig struct {
	ProposeTimeout   time.Duration
	PrevoteTimeout   time.Duration
	PrecommitTimeout time.Duration
	CommitTimeout    time.Duration
}

TimeoutConfig contains timeout parameters

type ValidationMetrics

type ValidationMetrics struct {
	ConsensusParticipation float64 // 0.0 to 1.0
	Uptime                 float64 // 0.0 to 1.0
	ResponseTime           time.Duration
	ProposedBlocks         uint64
	ValidatedBlocks        uint64
}

ValidationMetrics contains metrics used for score calculation

type Validator

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

Validator represents a validator node in the ATLYS network

func NewValidator

func NewValidator(config *ValidatorConfig) (*Validator, error)

NewValidator creates a new validator instance

func (*Validator) DelegateStake

func (v *Validator) DelegateStake(amount uint64) error

DelegateStake adds stake to the validator

func (*Validator) GetAddress

func (v *Validator) GetAddress() types.Address

GetAddress returns the validator's address

func (*Validator) GetPublicKey

func (v *Validator) GetPublicKey() []byte

GetPublicKey returns the validator's public key

func (*Validator) GetReputation

func (v *Validator) GetReputation() uint32

GetReputation returns the current reputation score

func (*Validator) GetStatus

func (v *Validator) GetStatus() map[string]interface{}

GetStatus returns the current validator status and statistics

func (*Validator) ProposeBlock

func (v *Validator) ProposeBlock(transactions []types.Transaction) (*core.Block, error)

ProposeBlock creates a new block proposal

func (*Validator) Start

func (v *Validator) Start(ctx context.Context) error

Start initializes the validator and begins participating in consensus

func (*Validator) Stop

func (v *Validator) Stop() error

Stop gracefully stops the validator

func (*Validator) UndelegateStake

func (v *Validator) UndelegateStake(amount uint64) error

UndelegateStake removes stake from the validator

func (*Validator) UpdateReputation

func (v *Validator) UpdateReputation(delta int32) error

UpdateReputation updates the validator's reputation score

func (*Validator) ValidateBlock

func (v *Validator) ValidateBlock(block *core.Block) error

ValidateBlock validates a proposed block

type ValidatorConfig

type ValidatorConfig struct {
	MinStake          uint64
	MaxStake          uint64
	UnbondingPeriod   time.Duration
	SlashingThreshold uint32
	ReputationDecay   float64
	JailDuration      time.Duration
	MaxMissedBlocks   uint32
	BlockTimeout      time.Duration
}

ValidatorConfig contains configuration parameters for the validator

type ValidatorScore

type ValidatorScore struct {
	CurrentScore    uint32    // Current reputation score (0-100)
	BaseScore       uint32    // Base score without penalties
	PenaltyScore    uint32    // Accumulated penalties
	LastUpdate      time.Time // Last score update timestamp
	ConsecutiveHits uint32    // Consecutive successful validations
	StakeWeight     float64   // Weight based on staked amount
}

ValidatorScore represents a validator's current reputation metrics

type ValidatorStats

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

ValidatorStats tracks validator performance metrics

type ValidatorStatus

type ValidatorStatus int

ValidatorStatus represents the current state of a validator

const (
	StatusInactive ValidatorStatus = iota
	StatusActive
	StatusJailing
	StatusJailed
	StatusSlashed
)

type Vote

type Vote struct {
	Type        VoteType
	Height      uint64
	Round       uint32
	BlockHash   types.Hash
	Timestamp   time.Time
	ValidatorID types.Address
	Signature   []byte
	Option      VoteOption
}

Vote represents a validator's vote

type VoteOption

type VoteOption int

VoteOption represents voting options

const (
	VoteNull VoteOption = iota
	VoteYes
	VoteNo
	VoteAbstain
)

type VoteSet

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

VoteSet tracks votes for a specific height and round

func (*VoteSet) AddVote

func (vs *VoteSet) AddVote(vote *Vote) error

func (*VoteSet) HasTwoThirdsMajority

func (vs *VoteSet) HasTwoThirdsMajority() bool

type VoteType

type VoteType int

VoteType represents different types of votes in the consensus

const (
	VoteTypePrevote VoteType = iota
	VoteTypePrecommit
	VoteTypeProposal
)

type VotingConfig

type VotingConfig struct {
	VotingPeriod     time.Duration
	ProposalTimeout  time.Duration
	PrevoteTimeout   time.Duration
	PrecommitTimeout time.Duration
	MinVotingPower   uint64
	MaxRounds        uint32
	RequiredMajority float64
}

VotingConfig contains configuration for voting

type VotingManager

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

VotingManager handles the voting process

func NewVotingManager

func NewVotingManager(validators *ValidatorSet, config *VotingConfig) *VotingManager

NewVotingManager creates a new instance of VotingManager

func (*VotingManager) AddVote

func (vm *VotingManager) AddVote(vote *Vote) error

AddVote adds a vote to the current vote set

func (*VotingManager) CreateVote

func (vm *VotingManager) CreateVote(
	voteType VoteType,
	height uint64,
	round uint32,
	blockHash types.Hash,
	option VoteOption,
	validator types.Address,
) (*Vote, error)

CreateVote creates a new signed vote

func (*VotingManager) GetVoteSet

func (vm *VotingManager) GetVoteSet(height uint64, round uint32, voteType VoteType) (*VoteSet, error)

GetVoteSet returns the vote set for a specific height and round

func (*VotingManager) GetVotingState

func (vm *VotingManager) GetVotingState() map[string]interface{}

GetVotingState returns the current voting state

func (*VotingManager) StartVoting

func (vm *VotingManager) StartVoting(ctx context.Context, height uint64) error

StartVoting begins the voting process for a new height

Jump to

Keyboard shortcuts

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