コード例 #1
0
ファイル: MinerLifetime.cs プロジェクト: wyk125/AElf
        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()));
        }
コード例 #2
0
ファイル: MinerLifetime.cs プロジェクト: wyk125/AElf
        public async Task MineWithIndexingSideChain()
        {
            GlobalConfig.InvertibleChainHeight = 0;
            string dir   = @"/tmp/minerpems";
            var    chain = await _mock.CreateChain();

            var keyPair     = new KeyPairGenerator().Generate();
            var minerConfig = _mock.GetMinerConfig(chain.Id, 10, keyPair.GetAddress().DumpByteArray());

            NodeConfig.Instance.ECKeyPair   = keyPair;
            NodeConfig.Instance.NodeAccount = keyPair.GetAddressHex();
            ChainConfig.Instance.ChainId    = chain.Id.DumpHex();
            var pool = _mock.CreateTxPool();

            pool.Start();

            try
            {
                int    sidePort   = 50054;
                int    parentPort = 50055;
                string address    = "127.0.0.1";
                _mock.ClearDirectory(dir);
                GrpcRemoteConfig.Instance.ParentChain = null;
                var sideChainId = _mock.MockSideChainServer(sidePort, address, dir);
                //var parentChainId = _mock.MockParentChainServer(parentPort, address, dir);
                var parimpl = _mock.MockParentChainBlockInfoRpcServerImpl();
                //parimpl.Init(parentChainId);
                var sideimpl = _mock.MockSideChainBlockInfoRpcServerImpl();
                sideimpl.Init(sideChainId);
                var serverManager = _mock.ServerManager(parimpl, sideimpl);
                serverManager.Init(dir);

                var manager = _mock.MinerClientManager();
                int t       = 1000;
                GrpcRemoteConfig.Instance.ChildChains = new Dictionary <string, Uri>
                {
                    {
                        sideChainId.DumpHex(), new Uri {
                            Address = address,
                            Port    = sidePort
                        }
                    }
                };

                GrpcLocalConfig.Instance.ClientToSideChain   = true;
                GrpcLocalConfig.Instance.ClientToParentChain = false;
                manager.Init(dir, t);
                var miner = _mock.GetMiner(minerConfig, pool, manager);
                miner.Init();

                //Thread.Sleep(t/2);
                var block = await miner.Mine();

                Assert.NotNull(block);
                Assert.NotNull(block.Body.IndexedInfo);
                int count = block.Body.IndexedInfo.Count;
                Assert.Equal(1, count);
                Assert.Equal(GlobalConfig.GenesisBlockHeight, block.Body.IndexedInfo[0].Height);
                Assert.Equal(GlobalConfig.GenesisBlockHeight + 1, block.Header.Index);

                Thread.Sleep(t);
                block = await miner.Mine();

                Assert.NotNull(block);
                Assert.NotNull(block.Body.IndexedInfo);
                count = block.Body.IndexedInfo.Count;
                Assert.Equal(1, count);
                Assert.Equal(GlobalConfig.GenesisBlockHeight + 1, block.Body.IndexedInfo[0].Height);
                Assert.Equal(GlobalConfig.GenesisBlockHeight + 2, block.Header.Index);

                Thread.Sleep(t);
                block = await miner.Mine();

                Assert.NotNull(block);
                Assert.NotNull(block.Body.IndexedInfo);
                count = block.Body.IndexedInfo.Count;
                Assert.Equal(1, count);
                Assert.Equal(GlobalConfig.GenesisBlockHeight + 2, block.Body.IndexedInfo[0].Height);
                Assert.Equal(GlobalConfig.GenesisBlockHeight + 3, block.Header.Index);

                manager.CloseClientsToSideChain();
            }
            finally
            {
                Directory.Delete(Path.Combine(dir), true);
            }
        }