Ejemplo n.º 1
0
        public async Task Mine()
        {
            var chain = await _mock.CreateChain();

            // create miner
            var keypair     = new KeyPairGenerator().Generate();
            var minerconfig = _mock.GetMinerConfig(chain.Id, 10, keypair.GetAddress().DumpByteArray());

            ChainConfig.Instance.ChainId    = chain.Id.DumpHex();
            NodeConfig.Instance.NodeAccount = keypair.GetAddressHex();
            var txPool = _mock.CreateTxPool();

            txPool.Start();

            var txs = CreateTx(chain.Id);

            foreach (var tx in txs)
            {
                await txPool.AddTransactionAsync(tx);
            }

            var manager = _mock.MinerClientManager();
            var miner   = _mock.GetMiner(minerconfig, txPool, manager);

            GrpcLocalConfig.Instance.ClientToSideChain            = false;
            GrpcLocalConfig.Instance.WaitingIntervalInMillisecond = 10;
            NodeConfig.Instance.ECKeyPair = keypair;
            miner.Init();

            var block = await miner.Mine();

            Assert.NotNull(block);
            Assert.Equal(GlobalConfig.GenesisBlockHeight + 1, block.Header.Index);

            byte[]  uncompressedPrivKey = block.Header.P.ToByteArray();
            Address addr = Address.FromRawBytes(uncompressedPrivKey);

            Assert.Equal(minerconfig.CoinBase, addr);

            ECKeyPair  recipientKeyPair = ECKeyPair.FromPublicKey(uncompressedPrivKey);
            ECVerifier verifier         = new ECVerifier(recipientKeyPair);

            Assert.True(verifier.Verify(block.Header.GetSignature(), block.Header.GetHash().DumpByteArray()));
        }