public static void Rescan(Nexus oldNexus, PhantasmaKeys owner, string[] args) { /*if (args.Length != 1) * { * throw new CommandException("Expected args: file_path"); * }*/ var genesisAddress = oldNexus.GetGenesisAddress(oldNexus.RootStorage); if (owner.Address != genesisAddress) { throw new CommandException("Invalid owner key"); } var oldGenesisBlock = oldNexus.GetGenesisBlock(); var newNexus = new Nexus(oldNexus.Name); newNexus.CreateGenesisBlock(owner, oldGenesisBlock.Timestamp, DomainSettings.LatestKnownProtocol); var oldRootChain = oldNexus.RootChain; var newRootChain = newNexus.RootChain; var height = oldRootChain.Height; BigInteger minFee = 0; Hash previousHash = Hash.Null; for (int i = 1; i <= height; i++) { logger.Message($"Processing block {i} out of {height}"); var oldBlockHash = oldRootChain.GetBlockHashAtHeight(i); var oldBlock = oldRootChain.GetBlockByHash(oldBlockHash); var transactions = oldBlock.TransactionHashes.Select(x => oldRootChain.GetTransactionByHash(x)); try { var newBlock = new Block(oldBlock.Height, oldBlock.ChainAddress, oldBlock.Timestamp, oldBlock.TransactionHashes, previousHash, oldBlock.Protocol, owner.Address, oldBlock.Payload); Transaction inflationTx = null; var changeSet = newRootChain.ProcessBlock(newBlock, transactions, minFee, out inflationTx, null); if (inflationTx != null) { transactions = transactions.Concat(new [] { inflationTx }); } newBlock.Sign(owner); newRootChain.AddBlock(newBlock, transactions, minFee, changeSet); } catch (Exception e) { throw new CommandException("Block validation failed: " + e.Message); } } }
public NexusSimulator(Nexus nexus, PhantasmaKeys ownerKey, int seed, Logger logger = null) { this.Logger = logger != null ? logger : new DummyLogger(); _owner = ownerKey; this.Nexus = nexus; CurrentTime = new DateTime(2018, 8, 26); if (!Nexus.HasGenesis) { if (!Nexus.CreateGenesisBlock("simnet", _owner, CurrentTime)) { throw new ChainException("Genesis block failure"); } } else { var genesisBlock = Nexus.GetGenesisBlock(); CurrentTime = new Timestamp(genesisBlock.Timestamp.Value + 1); } _rnd = new Random(seed); _keys.Add(_owner); var oneFuel = UnitConversion.ToBigInteger(1, DomainSettings.FuelTokenDecimals); var token = Nexus.GetTokenInfo(Nexus.RootStorage, DomainSettings.FuelTokenSymbol); var localBalance = Nexus.RootChain.GetTokenBalance(Nexus.RootStorage, token, _owner.Address); if (localBalance < oneFuel) { throw new Exception("Funds missing oops"); } var neoPlatform = Pay.Chains.NeoWallet.NeoPlatform; var neoKeys = InteropUtils.GenerateInteropKeys(_owner, Nexus.GetGenesisHash(Nexus.RootStorage), neoPlatform); var neoText = Phantasma.Neo.Core.NeoKeys.FromWIF(neoKeys.ToWIF()).Address; var neoAddress = Phantasma.Pay.Chains.NeoWallet.EncodeAddress(neoText); var ethPlatform = Pay.Chains.EthereumWallet.EthereumPlatform; var ethKeys = InteropUtils.GenerateInteropKeys(_owner, Nexus.GetGenesisHash(Nexus.RootStorage), ethPlatform); var ethText = Phantasma.Ethereum.EthereumKey.FromWIF(ethKeys.ToWIF()).Address; var ethAddress = Phantasma.Pay.Chains.EthereumWallet.EncodeAddress(ethText); BeginBlock(); GenerateCustomTransaction(_owner, 0, () => new ScriptBuilder().AllowGas(_owner.Address, Address.Null, MinimumFee, 9999). CallInterop("Nexus.CreatePlatform", _owner.Address, neoPlatform, neoText, neoAddress, "GAS"). CallInterop("Nexus.CreatePlatform", _owner.Address, ethPlatform, ethText, ethAddress, "ETH"). SpendGas(_owner.Address).EndScript()); var orgFunding = UnitConversion.ToBigInteger(1863626, DomainSettings.StakingTokenDecimals); var orgScript = new byte[0]; var orgID = DomainSettings.PhantomForceOrganizationName; var orgAddress = Address.FromHash(orgID); GenerateCustomTransaction(_owner, ProofOfWork.None, () => { return new ScriptBuilder().AllowGas(_owner.Address, Address.Null, 1, 99999). CallInterop("Nexus.CreateOrganization", _owner.Address, orgID, "Phantom Force", orgScript). CallInterop("Organization.AddMember", _owner.Address, orgID, _owner.Address). TransferTokens(DomainSettings.StakingTokenSymbol, _owner.Address, orgAddress, orgFunding). CallContract("swap", "SwapFee", orgAddress, DomainSettings.StakingTokenSymbol, 50000). CallContract("stake", "Stake", orgAddress, orgFunding - (5000)). SpendGas(_owner.Address). EndScript(); }); EndBlock(); BeginBlock(); var communitySupply = 100000; GenerateToken(_owner, "MKNI", "Mankini Token", UnitConversion.ToBigInteger(communitySupply, 0), 0, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Finite); MintTokens(_owner, _owner.Address, "MKNI", communitySupply); EndBlock(); /* var market = Nexus.FindChainByName("market"); var nftSales = new List<KeyValuePair<KeyPair, BigInteger>>(); BeginBlock(); for (int i = 1; i < 7; i++) { BigInteger ID = i + 100; TokenContent info; try { info = Nexus.GetNFT(nachoSymbol, ID); } catch { continue; } var chain = Nexus.FindChainByAddress(info.CurrentChain); if (chain == null) { continue; } var nftOwner = chain.GetTokenOwner(nachoSymbol, ID); if (nftOwner == Address.Null) { continue; } foreach (var key in _keys) { if (key.Address == nftOwner) { nftSales.Add(new KeyValuePair<KeyPair, BigInteger>(key, ID)); // send some gas to the sellers GenerateTransfer(_owner, key.Address, Nexus.RootChain, Nexus.FuelTokenSymbol, UnitConversion.ToBigInteger(0.01m, Nexus.FuelTokenDecimals)); } } } EndBlock(); BeginBlock(); foreach (var sale in nftSales) { // TODO this later should be the market chain instead of root GenerateNftSale(sale.Key, Nexus.RootChain, nachoSymbol, sale.Value, UnitConversion.ToBigInteger(100 + 5 * _rnd.Next() % 50, Nexus.FuelTokenDecimals)); } EndBlock(); */ }
public NexusSimulator(Nexus nexus, PhantasmaKeys ownerKey, int seed, Logger logger = null) { this.Logger = logger != null ? logger : new DummyLogger(); _owner = ownerKey; this.Nexus = nexus; CurrentTime = new DateTime(2018, 8, 26); if (!Nexus.HasGenesis) { if (!Nexus.CreateGenesisBlock("simnet", _owner, CurrentTime)) { throw new ChainException("Genesis block failure"); } } else { var genesisBlock = Nexus.GetGenesisBlock(); CurrentTime = new Timestamp(genesisBlock.Timestamp.Value + 1); } _rnd = new Random(seed); _keys.Add(_owner); var oneFuel = UnitConversion.ToBigInteger(1, DomainSettings.FuelTokenDecimals); var token = Nexus.GetTokenInfo(Nexus.RootStorage, DomainSettings.FuelTokenSymbol); var localBalance = Nexus.RootChain.GetTokenBalance(Nexus.RootStorage, token, _owner.Address); if (localBalance < oneFuel) { throw new Exception("Funds missing oops"); } var neoPlatform = Pay.Chains.NeoWallet.NeoPlatform; var neoKeys = InteropUtils.GenerateInteropKeys(_owner, Nexus.GetGenesisHash(Nexus.RootStorage), neoPlatform); var neoText = Phantasma.Neo.Core.NeoKeys.FromWIF(neoKeys.ToWIF()).Address; var neoAddress = Phantasma.Pay.Chains.NeoWallet.EncodeAddress(neoText); /* * var ethPlatform = Pay.Chains.EthereumWallet.EthereumPlatform; * var ethKeys = InteropUtils.GenerateInteropKeys(_owner, ethPlatform); * var ethText = Phantasma.Ethereum.EthereumKey.FromWIF(ethKeys.ToWIF()).address; * var ethAddress = Phantasma.Pay.Chains.EthereumWallet.EncodeAddress(ethText); */ BeginBlock(); GenerateCustomTransaction(_owner, 0, () => new ScriptBuilder().AllowGas(_owner.Address, Address.Null, MinimumFee, 9999). CallInterop("Nexus.CreatePlatform", _owner.Address, neoPlatform, neoText, neoAddress, "GAS"). //CallInterop("Nexus.CreatePlatform", _owner.Address, ethKeys.PublicKey, ethPlatform, "ETH"). SpendGas(_owner.Address).EndScript()); GenerateToken(_owner, "NEO", "NEO", neoPlatform, Hash.FromUnpaddedHex("c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b"), UnitConversion.ToBigInteger(100000000, 0), 0, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Finite | TokenFlags.External); GenerateToken(_owner, "GAS", "GAS", neoPlatform, Hash.FromUnpaddedHex("602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"), UnitConversion.ToBigInteger(100000000, 8), 8, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Divisible | TokenFlags.Finite | TokenFlags.External); //GenerateToken(_owner, "ETH", "Ethereum", ethPlatform, Hash.FromString("ETH"), UnitConversion.ToBigInteger(0, 18), 18, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Divisible | TokenFlags.External); //GenerateToken(_owner, "DAI", "Dai Stablecoin", ethPlatform, Hash.FromUnpaddedHex("89d24a6b4ccb1b6faa2625fe562bdd9a23260359"), UnitConversion.ToBigInteger(0, 18), 18, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Divisible | TokenFlags.External); //GenerateToken(_owner, "EOS", "EOS", "EOS", UnitConversion.ToBigInteger(1006245120, 18), 18, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Finite | TokenFlags.Divisible | TokenFlags.External); var orgFunding = UnitConversion.ToBigInteger(1863626, DomainSettings.StakingTokenDecimals); var orgScript = new byte[0]; var orgID = DomainSettings.PhantomForceOrganizationName; var orgAddress = Address.FromHash(orgID); GenerateCustomTransaction(_owner, ProofOfWork.None, () => { return(new ScriptBuilder().AllowGas(_owner.Address, Address.Null, 1, 99999). CallInterop("Nexus.CreateOrganization", _owner.Address, orgID, "Phantom Force", orgScript). CallInterop("Organization.AddMember", _owner.Address, orgID, _owner.Address). TransferTokens(DomainSettings.StakingTokenSymbol, _owner.Address, orgAddress, orgFunding). CallContract("swap", "SwapFee", orgAddress, DomainSettings.StakingTokenSymbol, 50000). CallContract("stake", "Stake", orgAddress, orgFunding - (5000)). SpendGas(_owner.Address). EndScript()); }); var communitySupply = 100000; GenerateToken(_owner, "MKNI", "Mankini Token", DomainSettings.PlatformName, Hash.FromString("MKNI"), UnitConversion.ToBigInteger(communitySupply, 0), 0, TokenFlags.Fungible | TokenFlags.Transferable | TokenFlags.Finite); MintTokens(_owner, _owner.Address, "MKNI", communitySupply); EndBlock(); /* * var market = Nexus.FindChainByName("market"); * var nftSales = new List<KeyValuePair<KeyPair, BigInteger>>(); * * BeginBlock(); * for (int i = 1; i < 7; i++) * { * BigInteger ID = i + 100; * TokenContent info; * try * { * info = Nexus.GetNFT(nachoSymbol, ID); * } * catch * { * continue; * } * * var chain = Nexus.FindChainByAddress(info.CurrentChain); * if (chain == null) * { * continue; * } * * var nftOwner = chain.GetTokenOwner(nachoSymbol, ID); * * if (nftOwner == Address.Null) * { * continue; * } * * foreach (var key in _keys) * { * if (key.Address == nftOwner) * { * nftSales.Add(new KeyValuePair<KeyPair, BigInteger>(key, ID)); * // send some gas to the sellers * GenerateTransfer(_owner, key.Address, Nexus.RootChain, Nexus.FuelTokenSymbol, UnitConversion.ToBigInteger(0.01m, Nexus.FuelTokenDecimals)); * } * } * } * * EndBlock(); * * BeginBlock(); * foreach (var sale in nftSales) * { * // TODO this later should be the market chain instead of root * GenerateNftSale(sale.Key, Nexus.RootChain, nachoSymbol, sale.Value, UnitConversion.ToBigInteger(100 + 5 * _rnd.Next() % 50, Nexus.FuelTokenDecimals)); * } * EndBlock(); */ }