-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnuvex.py
More file actions
95 lines (83 loc) · 4.1 KB
/
Copy pathnuvex.py
File metadata and controls
95 lines (83 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import time
import hashlib
import json
from typing import List, Dict
from powu import perform_pouw_task
from wasm_executor import execute_wasm_contract
from shard_manager import ShardManager
from tokens import TokenRegistry
class NuvexBlockchain:
def __init__(self, shard_count: int = 4):
self.shards = ShardManager(shard_count)
self.token_registry = TokenRegistry(["GRN", "PRN", "RRN"])
self.chain: List[Dict] = []
self.stake: Dict[str, float] = {}
self.processed_tx_hashes: set = set()
self.genesis_block()
def genesis_block(self):
for shard_id in range(self.shards.count):
block = {
"shard_id": shard_id,
"index": 0,
"timestamp": time.time(),
"previous_hash": "0",
"transactions": [],
"hash": self.calculate_hash(0, "0", [], shard_id)
}
self.shards.add_block(shard_id, block)
def calculate_hash(self, index: int, previous_hash: str, transactions: List, shard_id: int) -> str:
value = f"{shard_id}{index}{previous_hash}{json.dumps(transactions)}{time.time()}"
return hashlib.sha256(value.encode()).hexdigest()
def add_stake(self, address: str, amount: float):
self.stake[address] = self.stake.get(address, 0) + amount
def validate_block(self, block: Dict, shard_id: int) -> bool:
prev_block = self.shards.get_latest_block(shard_id)
if prev_block is None or block["index"] != prev_block["index"] + 1 or block["previous_hash"] != prev_block["hash"]:
return False
return True
def hash_transaction(self, tx: Dict) -> str:
tx_string = str(sorted(tx.items()))
return hashlib.sha256(tx_string.encode()).hexdigest()
def mine_block(self, shard_id: int, transactions: List[Dict], validator: str) -> Dict:
if validator not in self.stake or self.stake[validator] < 1.0:
raise ValueError("Insufficient stake")
for tx in transactions:
tx_hash = self.hash_transaction(tx)
if tx_hash in self.processed_tx_hashes:
raise ValueError("Double-spend detected! Transaction rejected.")
self.processed_tx_hashes.add(tx_hash)
sector = tx.get("sector", "unknown")
token = tx.get("token", "GRN")
pouw_result = perform_pouw_task(prev_block["index"] + 1, sector, tx, shard_id, token)
pouw_results.append(pouw_result)
block = {
"shard_id": shard_id,
"index": prev_block["index"] + 1,
"timestamp": time.time(),
"previous_hash": prev_block["hash"],
"transactions": transactions,
"pouw_results": pouw_results,
"hash": self.calculate_hash(prev_block["index"] + 1, prev_block["hash"], transactions, shard_id)
}
if self.validate_block(block, shard_id):
self.shards.add_block(shard_id, block)
self.reward_validator(validator, shard_id)
return block
raise ValueError("Block validation failed")
def reward_validator(self, validator: str, shard_id: int):
self.stake[validator] += 5.0
for tx in self.shards.get_latest_block(shard_id)["transactions"]:
token = tx.get("token", "GRN")
self.token_registry.add_balance(validator, token, 10.0)
def execute_transaction(self, tx: Dict) -> str:
shard_id = tx.get("shard_id", 0)
wasm_result = execute_wasm_contract(tx)
self.mine_block(shard_id, [tx], "validator1")
return wasm_result
if __name__ == "__main__":
nvx = NuvexBlockchain()
nvx.add_stake("validator1", 100.0)
tx = {"sector": "cannabis", "batch_id": "CA-123", "thc_level": 18.5, "shard_id": 0, "token": "GRN"}
print(nvx.execute_transaction(tx))