From 3749b9fcc19681d5038d3f234f1c7b73ca296e94 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Wed, 19 Jun 2024 12:56:18 +0700 Subject: [PATCH] Create validator.sol --- .../blockchain/contracts/PoS/validator.sol | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 network/blockchain/contracts/PoS/validator.sol diff --git a/network/blockchain/contracts/PoS/validator.sol b/network/blockchain/contracts/PoS/validator.sol new file mode 100644 index 0000000..d941dd0 --- /dev/null +++ b/network/blockchain/contracts/PoS/validator.sol @@ -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())