public void UnstakeWithStoredFilesFailure() { var owner = KeyPair.Generate(); var simulator = new NexusSimulator(owner, 1234); var nexus = simulator.Nexus; var testUser = KeyPair.Generate(); var accountBalance = MinimumValidStake * 100; Transaction tx = null; simulator.BeginBlock(); simulator.GenerateTransfer(owner, testUser.Address, nexus.RootChain, Nexus.FuelTokenSymbol, 100000000); simulator.GenerateTransfer(owner, testUser.Address, nexus.RootChain, Nexus.StakingTokenSymbol, accountBalance); simulator.EndBlock(); //----------- //Perform a valid Stake call for minimum staking amount var stakedAmount = MinimumValidStake; var startingSoulBalance = simulator.Nexus.RootChain.GetTokenBalance(Nexus.StakingTokenSymbol, testUser.Address); simulator.BeginBlock(); tx = simulator.GenerateCustomTransaction(testUser, ProofOfWork.None, () => ScriptUtils.BeginScript().AllowGas(testUser.Address, Address.Null, 1, 9999) .CallContract(Nexus.StakeContractName, "Stake", testUser.Address, stakedAmount). SpendGas(testUser.Address).EndScript()); simulator.EndBlock(); //----------- //Upload a file var filename = "notAVirus.exe"; var headerSize = CalculateRequiredSize(filename, 0); var contentSize = (long)(stakedAmount / MinimumValidStake * KilobytesPerStake * 1024) - (long)headerSize; var content = new byte[contentSize]; var contentMerkle = new MerkleTree(content); simulator.BeginBlock(); tx = simulator.GenerateCustomTransaction(testUser, ProofOfWork.None, () => ScriptUtils.BeginScript().AllowGas(testUser.Address, Address.Null, 1, 9999) .CallContract("storage", "UploadFile", testUser.Address, filename, contentSize, contentMerkle, ArchiveFlags.None, new byte[0]). SpendGas(testUser.Address).EndScript()); simulator.EndBlock(); var usedSpace = simulator.Nexus.RootChain.InvokeContract("storage", "GetUsedSpace", testUser.Address).AsNumber(); Assert.IsTrue(usedSpace == contentSize + headerSize); var oldSpace = usedSpace; //----------- //Time skip 1 day simulator.TimeSkipDays(1); //----------- //Try to unstake everything: should fail due to files still existing for this user var initialStakedAmount = simulator.Nexus.RootChain.InvokeContract(Nexus.StakeContractName, "GetStake", testUser.Address).AsNumber(); var stakeReduction = initialStakedAmount - MinimumValidStake; startingSoulBalance = simulator.Nexus.RootChain.GetTokenBalance(Nexus.StakingTokenSymbol, testUser.Address); Assert.ThrowsException <ChainException>(() => { simulator.BeginBlock(); simulator.GenerateCustomTransaction(testUser, ProofOfWork.None, () => ScriptUtils.BeginScript().AllowGas(testUser.Address, Address.Null, 1, 9999) .CallContract(Nexus.StakeContractName, "Unstake", testUser.Address, stakeReduction). SpendGas(testUser.Address).EndScript()); simulator.EndBlock(); }); var finalStakedAmount = simulator.Nexus.RootChain.InvokeContract(Nexus.StakeContractName, "GetStake", testUser.Address).AsNumber(); Assert.IsTrue(initialStakedAmount == finalStakedAmount); usedSpace = simulator.Nexus.RootChain.InvokeContract("storage", "GetUsedSpace", testUser.Address).AsNumber(); Assert.IsTrue(usedSpace == oldSpace); }
private void AddNewValidator(PhantasmaKeys newValidator) { Timestamp startTime = simulator.CurrentTime; Timestamp endTime = simulator.CurrentTime.AddDays(1); var pollName = SystemPoll + ValidatorPollTag; var validators = (ValidatorEntry[])simulator.Nexus.RootChain.InvokeContract(simulator.Nexus.RootStorage, Nexus.ValidatorContractName, nameof(ValidatorContract.GetValidators)).ToObject(typeof(ValidatorEntry[])); var activeValidators = validators.Where(x => x.address != Address.Null); var activeValidatorCount = activeValidators.Count(); var choices = new PollChoice[activeValidatorCount + 1]; for (int i = 0; i < activeValidatorCount; i++) { choices[i] = new PollChoice() { value = validators[i].address.ToByteArray() }; } choices[activeValidatorCount] = new PollChoice() { value = newValidator.Address.ToByteArray() }; var serializedChoices = choices.Serialize(); //start vote for new validator simulator.BeginBlock(); simulator.GenerateCustomTransaction(owner, ProofOfWork.None, () => ScriptUtils.BeginScript(). AllowGas(owner.Address, Address.Null, 1, 9999). CallContract(ConsensusContractName, nameof(ConsensusContract.InitPoll), owner.Address, pollName, DomainSettings.ValidatorsOrganizationName, ConsensusMode.Majority, startTime, endTime, serializedChoices, 1). SpendGas(owner.Address). EndScript()); simulator.EndBlock(); for (int i = 0; i < activeValidatorCount; i++) { var validator = validatorKeyPairs[i]; //have each already existing validator vote on themselves to preserve the validator order simulator.BeginBlock(); simulator.GenerateCustomTransaction(validator, ProofOfWork.None, () => ScriptUtils.BeginScript(). AllowGas(validator.Address, Address.Null, 1, 9999). CallContract(ConsensusContractName, nameof(ConsensusContract.SingleVote), validator.Address, pollName, i). SpendGas(validator.Address). EndScript()); simulator.EndBlock(); } //skip until the voting is over simulator.TimeSkipDays(1.5); var votingRank = simulator.Nexus.RootChain.InvokeContract(simulator.Nexus.RootStorage, ConsensusContractName, nameof(ConsensusContract.GetRank), pollName, choices[activeValidatorCount].Serialize()).AsNumber(); //call SetValidator for each set validator address for (int i = 0; i <= activeValidatorCount; i++) { var validatorChoice = choices[i].value; ValidatorType validatorType = i < 2 ? Primary : Secondary; votingRank = simulator.Nexus.RootChain.InvokeContract(simulator.Nexus.RootStorage, ConsensusContractName, nameof(ConsensusContract.GetRank), pollName, validatorChoice).AsNumber(); simulator.BeginBlock(); var tx = simulator.GenerateCustomTransaction(owner, ProofOfWork.None, () => ScriptUtils.BeginScript(). AllowGas(owner.Address, Address.Null, 1, 9999). CallContract(ValidatorContractName, nameof(ValidatorContract.SetValidator), validatorChoice, votingRank, validatorType). SpendGas(owner.Address). EndScript()); simulator.EndBlock().First(); } }
public void ReduceAvailableSpace() { var owner = KeyPair.Generate(); var simulator = new NexusSimulator(owner, 1234); var nexus = simulator.Nexus; var testUser = KeyPair.Generate(); var accountBalance = MinimumValidStake * 100; Transaction tx = null; simulator.BeginBlock(); simulator.GenerateTransfer(owner, testUser.Address, nexus.RootChain, DomainSettings.FuelTokenSymbol, 100000000); simulator.GenerateTransfer(owner, testUser.Address, nexus.RootChain, DomainSettings.StakingTokenSymbol, accountBalance); simulator.EndBlock(); //----------- //Perform a valid Stake call for minimum staking amount var stakedAmount = MinimumValidStake * 5; var startingSoulBalance = simulator.Nexus.RootChain.GetTokenBalance(simulator.Nexus.RootStorage, DomainSettings.StakingTokenSymbol, testUser.Address); simulator.BeginBlock(); tx = simulator.GenerateCustomTransaction(testUser, ProofOfWork.None, () => ScriptUtils.BeginScript().AllowGas(testUser.Address, Address.Null, 1, 9999) .CallContract(Nexus.StakeContractName, "Stake", testUser.Address, stakedAmount). SpendGas(testUser.Address).EndScript()); simulator.EndBlock(); //----------- //Upload a file var filename = "notAVirus.exe"; var headerSize = CalculateRequiredSize(filename, 0); var contentSize = (long)(stakedAmount / MinimumValidStake * KilobytesPerStake * 1024 / 5) - (long)headerSize; var content = new byte[contentSize]; var contentMerkle = new MerkleTree(content); simulator.BeginBlock(); tx = simulator.GenerateCustomTransaction(testUser, ProofOfWork.None, () => ScriptUtils.BeginScript().AllowGas(testUser.Address, Address.Null, 1, 9999) .CallContract("storage", "UploadFile", testUser.Address, filename, contentSize, contentMerkle, ArchiveFlags.None, new byte[0]). SpendGas(testUser.Address).EndScript()); simulator.EndBlock(); var usedSpace = simulator.Nexus.RootChain.InvokeContract(simulator.Nexus.RootStorage, "storage", "GetUsedSpace", testUser.Address).AsNumber(); Assert.IsTrue(usedSpace == contentSize + headerSize); //----------- //Time skip 1 day simulator.TimeSkipDays(1); //----------- //Try partial unstake: should succeed var initialStakedAmount = simulator.Nexus.RootChain.InvokeContract(simulator.Nexus.RootStorage, Nexus.StakeContractName, "GetStake", testUser.Address).AsNumber(); var stakeReduction = stakedAmount / 5; simulator.BeginBlock(); simulator.GenerateCustomTransaction(testUser, ProofOfWork.None, () => ScriptUtils.BeginScript().AllowGas(testUser.Address, Address.Null, 1, 9999) .CallContract(Nexus.StakeContractName, "Unstake", testUser.Address, stakeReduction). SpendGas(testUser.Address).EndScript()); simulator.EndBlock(); var finalStakedAmount = simulator.Nexus.RootChain.InvokeContract(simulator.Nexus.RootStorage, Nexus.StakeContractName, "GetStake", testUser.Address).AsNumber(); Assert.IsTrue(finalStakedAmount == initialStakedAmount - stakeReduction); }