Skip to content

Commit

Permalink
Create validator.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jun 19, 2024
1 parent 6dd9714 commit 3749b9f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions network/blockchain/contracts/PoS/validator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import random
import time

class Validator:
def __init__(self, address, stake):
self.address = address
self.stake = stake
self.last_validated = 0

def is_eligible_to_validate(self):
now = int(time.time())
if now - self.last_validated > 60: # validate every minute
return True
return False

class PoSConsensus:
def __init__(self, validators):
self.validators = validators

def select_validator(self):
total_stake = sum([v.stake for v in self.validators])
stake_sum = 0
selected_validator = None
for v in self.validators:
stake_sum += v.stake / total_stake
if random.random() < stake_sum:
selected_validator = v
break
return selected_validator

def validate_transaction(self, transaction):
validator = self.select_validator()
if validator.is_eligible_to_validate():
# validate the transaction
# reward the validator
validator.last_validated = int(time.time())

0 comments on commit 3749b9f

Please sign in to comment.