Exemplo n.º 1
0
        public PowMiningTestFixture()
        {
            this.Network       = Network.StratisTest;
            this.Chain         = new ConcurrentChain(this.Network);
            this.Key           = new Key();
            this.ReserveScript = new ReserveScript(this.Key.ScriptPubKey);

            var isProofOfStake = this.Network.NetworkOptions.IsProofOfStake;
            var blockSignature = Block.BlockSignature;
            var timestamp      = Transaction.TimeStamp;

            try
            {
                this.Network.NetworkOptions.IsProofOfStake = false;

                Block.BlockSignature  = false;
                Transaction.TimeStamp = false;

                this.Block1        = PrepareValidBlock(this.Chain.Tip, 1, this.Key.ScriptPubKey);
                this.ChainedBlock1 = new ChainedBlock(this.Block1.Header, this.Block1.GetHash(), this.Chain.Tip);
                this.Block2        = PrepareValidBlock(this.ChainedBlock1, 2, this.Key.ScriptPubKey);
                this.ChainedBlock2 = new ChainedBlock(this.Block2.Header, this.Block2.GetHash(), this.ChainedBlock1);
            }
            finally
            {
                this.Network.NetworkOptions.IsProofOfStake = isProofOfStake;

                Block.BlockSignature  = blockSignature;
                Transaction.TimeStamp = timestamp;
            }
        }
        public static (HdAddress AddressUsed, List <uint256> BlockHashes) MineBlocks(CoreNode node, int numberOfBlocks, string walletName = "mywallet", string walletPassword = "******", string accountName = "account 0")
        {
            Guard.NotNull(node, nameof(node));

            if (numberOfBlocks == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(numberOfBlocks), "Number of blocks must be greater than zero.");
            }

            if (node.MinerSecret == null)
            {
                HdAddress unusedAddress = node.FullNode.WalletManager().GetUnusedAddress(new WalletAccountReference(walletName, accountName));
                node.MinerHDAddress = unusedAddress;

                Wallet wallet             = node.FullNode.WalletManager().GetWalletByName(walletName);
                Key    extendedPrivateKey = wallet.GetExtendedPrivateKeyForAddress(walletPassword, unusedAddress).PrivateKey;
                node.SetDummyMinerSecret(new BitcoinSecret(extendedPrivateKey, node.FullNode.Network));
            }

            var script = new ReserveScript {
                ReserveFullNodeScript = node.MinerSecret.ScriptPubKey
            };
            var blockHashes = node.FullNode.Services.ServiceProvider.GetService <IPowMining>().GenerateBlocks(script, (ulong)numberOfBlocks, uint.MaxValue);

            TestHelper.WaitLoop(() => TestHelper.IsNodeSynced(node));

            return(node.MinerHDAddress, blockHashes);
        }
Exemplo n.º 3
0
 public MineBlockContext(ulong amountOfBlocksToMine, ulong chainHeight, ulong maxTries, ReserveScript reserveScript)
 {
     this.amountOfBlocksToMine = amountOfBlocksToMine;
     this.ChainHeight          = chainHeight;
     this.CurrentHeight        = chainHeight;
     this.MaxTries             = maxTries;
     this.ReserveScript        = reserveScript;
 }
        public PowMiningTestFixture()
        {
            this.Network       = Network.RegTest; // fast mining so use regtest
            this.Chain         = new ConcurrentChain(this.Network);
            this.Key           = new Key();
            this.ReserveScript = new ReserveScript(this.Key.ScriptPubKey);

            this.Block1         = this.PrepareValidBlock(this.Chain.Tip, 1, this.Key.ScriptPubKey);
            this.ChainedHeader1 = new ChainedHeader(this.Block1.Header, this.Block1.GetHash(), this.Chain.Tip);

            this.Block2         = this.PrepareValidBlock(this.ChainedHeader1, 2, this.Key.ScriptPubKey);
            this.ChainedHeader2 = new ChainedHeader(this.Block2.Header, this.Block2.GetHash(), this.ChainedHeader1);
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public List <uint256> GenerateBlocks(ReserveScript reserveScript, ulong amountOfBlocksToMine, ulong maxTries)
        {
            var context = new MineBlockContext(amountOfBlocksToMine, (ulong)this.chainIndexer.Height, maxTries, reserveScript);

            while (context.MiningCanContinue)
            {
                if (!this.ConsensusIsAtTip(context))
                {
                    continue;
                }

                if (!this.BuildBlock(context))
                {
                    continue;
                }

                if (!this.IsProofOfWorkAllowed(context))
                {
                    continue;
                }

                if (!this.MineBlock(context))
                {
                    break;
                }

                if (!this.ValidateMinedBlock(context))
                {
                    continue;
                }

                if (!this.ValidateAndConnectBlock(context))
                {
                    continue;
                }

                this.OnBlockMined(context);
            }

            return(context.Blocks);
        }
Exemplo n.º 6
0
        static async Task StartMiningAsync()
        {
            //var ibd = _fullNode.NodeService<IInitialBlockDownloadState>();
            //try
            //{
            //    Controller.LoadWallet();
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine(e.Message);
            //    if (!e.Message.StartsWith("No wallet file found"))
            //        throw;
            //    Controller.CreateWallet(new WalletCreateRequest
            //    { WalletName = _walletName, Passphrase = _passPhrase });
            //    Console.WriteLine($"Created a new wallet {_walletName} for mining.");

            //    await StartMiningAsync();

            //}
            // await Task.Delay(10000);
            var model = Controller.GetUsedReceiveAddresses(new Features.X1Wallet.Models.Api.Responses.GetAddressesRequest {
                Skip = 0, Take = 1
            });
            var address = model.PubKeyHashAddresses[0].Address;

            var script = new ReserveScript {
                ReserveFullNodeScript = address.GetScriptPubKey()
            };

            _ = Task.Run(() =>
            {
                _logger.LogInformation("Starting Miner...");

                while (!_fullNode.NodeLifetime.ApplicationStopping.IsCancellationRequested)
                {
                    _fullNode.NodeService <IPowMining>().GenerateBlocks(script, 1, 1000 * 1000);
                    _logger.LogInformation("Mining...");
                }
            }, _fullNode.NodeLifetime.ApplicationStopping);
        }
Exemplo n.º 7
0
        public static (HdAddress AddressUsed, List <uint256> BlockHashes) MineBlocks(CoreNode node, int numberOfBlocks, bool syncNode = true, string walletName = "mywallet", string walletPassword = "******", string accountName = "account 0")
        {
            Guard.NotNull(node, nameof(node));

            if (numberOfBlocks == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(numberOfBlocks), "Number of blocks must be greater than zero.");
            }

            SetMinerSecret(node, walletName, walletPassword, accountName);

            var script = new ReserveScript {
                ReserveFullNodeScript = node.MinerSecret.ScriptPubKey
            };
            var blockHashes = node.FullNode.Services.ServiceProvider.GetService <IPowMining>().GenerateBlocks(script, (ulong)numberOfBlocks, uint.MaxValue);

            if (syncNode)
            {
                WaitLoop(() => IsNodeSynced(node));
            }

            return(node.MinerHDAddress, blockHashes);
        }