def StateTest(rpc: RPC) -> None: file: IO[Any] = open("e2e/Vectors/Merit/State.json", "r") blocks: List[Dict[str, Any]] = json.loads(file.read()) file.close() blockchain: Blockchain = Blockchain.fromJSON(blocks) state: State = State() def checkState(block: int) -> None: state.add(blockchain, block) meritSum: int = 0 for miner in range(len(state.balances)): meritSum += state.balances[miner] if rpc.call("merit", "getMerit", [miner]) != { "status": "Unlocked", "malicious": False, "merit": state.balances[miner] }: raise TestError("Merit doesn't match.") if meritSum != min(block, state.lifetime): raise TestError("Merit sum is invalid.") Liver(rpc, blocks, everyBlock=checkState).live()
def fromJSON(json: List[Dict[str, Any]]) -> Any: result: Merit = Merit.__new__(Merit) result.blockchain = Blockchain.fromJSON(json) result.state = State() result.epochs = Epochs() result.mints = [] for b in range(1, len(result.blockchain.blocks)): mint: Optional[Mint] = result.epochs.shift(result.state, result.blockchain, b) if mint is not None: result.mints.append(mint) result.state.add(result.blockchain, b) return result
def StateTest(rpc: RPC) -> None: #Blocks. file: IO[Any] = open("e2e/Vectors/Merit/StateBlocks.json", "r") blocks: List[Dict[str, Any]] = json.loads(file.read()) file.close() #Blockchain. blockchain: Blockchain = Blockchain.fromJSON(blocks) #State. state: State = State() def checkState(block: int) -> None: state.add(blockchain, block) for miner in state.unlocked: if rpc.call("merit", "getMerit", [miner]) != { "unlocked": True, "malicious": False, "merit": state.unlocked[miner] }: raise TestError("Merit doesn't match.") Liver(rpc, blocks, everyBlock=checkState).live()
def __init__(self) -> None: self.blockchain: Blockchain = Blockchain() self.state: State = State() self.epochs = Epochs() self.mints: List[Mint] = []
from typing import IO, Any from random import seed, getrandbits import json from e2e.Classes.Merit.State import State from e2e.Vectors.Generation.PrototypeChain import PrototypeChain #Ensure consistent vectors. seed(1) chain: PrototypeChain = PrototypeChain(1) miners: int = 1 #State is used solely to get the lifetime. #This test only has significance when Dead Merit is also tested. for _ in range(State().lifetime * 3): #Why manually specify hundreds of miners when we can randomize it? nextMiner: int = getrandbits(8) % miners #If the nextMiner is 0, it's either trying to use the original miner or a new miner. #Create a 50% chance of creating a new miner. if (nextMiner == 0) and (getrandbits(1) == 1): nextMiner = miners chain.add(nextMiner) vectors: IO[Any] = open("e2e/Vectors/Merit/State.json", "w") vectors.write(json.dumps(chain.toJSON())) vectors.close()