Exemplo n.º 1
0
        public async Task ParentChainServerClientTest()
        {
            string dir = @"/tmp/ServerClientsTest";

            _mock.ClearDirectory(dir);
            try
            {
                GlobalConfig.InvertibleChainHeight = 0;
                var port    = 50053;
                var address = "127.0.0.1";

                var parentChainId = _mock.MockParentChainServer(port, address, dir);
                var parimpl       = _mock.MockParentChainBlockInfoRpcServerImpl();
                parimpl.Init(parentChainId);
                var sideimpl = _mock.MockSideChainBlockInfoRpcServerImpl();
                sideimpl.Init(Hash.Generate());
                var serverManager = _mock.ServerManager(parimpl, sideimpl);
                serverManager.Init(dir);
                // create client, main chain is client-side
                var manager = _mock.MinerClientManager();
                int t       = 1000;
                // for client

                GrpcRemoteConfig.Instance.ParentChain = new Dictionary <string, Uri>
                {
                    {
                        parentChainId.DumpHex(), new Uri {
                            Address = address,
                            Port    = port
                        }
                    }
                };
                GrpcLocalConfig.Instance.ClientToParentChain = true;
                manager.Init(dir, t);

                GrpcLocalConfig.Instance.WaitingIntervalInMillisecond = 10;

                Thread.Sleep(t / 2);
                var result = manager.TryGetParentChainBlockInfo();
                Assert.NotNull(result);
                Assert.Equal(GlobalConfig.GenesisBlockHeight, result.Height);
                Assert.Equal(1, result.IndexedBlockInfo.Count);
                Assert.True(result.IndexedBlockInfo.Keys.Contains(GlobalConfig.GenesisBlockHeight));
                //Assert.True(await manager.UpdateParentChainBlockInfo(result));
                var chainManagerBasic = _mock.MockChainManager().Object;
                await chainManagerBasic.UpdateCurrentBlockHeightAsync(result.ChainId, result.Height);

                _mock.GetTimes++;

                Thread.Sleep(t);
                result = manager.TryGetParentChainBlockInfo();
                Assert.NotNull(result);
                Assert.Equal(GlobalConfig.GenesisBlockHeight + 1, result.Height);
                Assert.Equal(1, result.IndexedBlockInfo.Count);
                Assert.True(result.IndexedBlockInfo.Keys.Contains(GlobalConfig.GenesisBlockHeight + 1));
                //Assert.True(await manager.UpdateParentChainBlockInfo(result));
                await chainManagerBasic.UpdateCurrentBlockHeightAsync(result.ChainId, result.Height);

                _mock.GetTimes++;

                Thread.Sleep(t);
                result = manager.TryGetParentChainBlockInfo();
                Assert.NotNull(result);
                Assert.Equal(GlobalConfig.GenesisBlockHeight + 2, result.Height);
                Assert.Equal(1, result.IndexedBlockInfo.Count);
                Assert.True(result.IndexedBlockInfo.Keys.Contains(GlobalConfig.GenesisBlockHeight + 2));
                manager.CloseClientToParentChain();
            }
            finally
            {
                Directory.Delete(Path.Combine(dir), true);
            }
        }