Esempio n. 1
0
        public async Task CanSuspendNetwork()
        {
            var systemAddress = await _network.GetSystemFreezeAdminAddress();

            if (systemAddress is null)
            {
                _network.Output?.WriteLine("TEST SKIPPED: No access to System Freeze Administrator Account.");
                return;
            }
            await using var client = _network.NewClient();
            var suspendParameters = new SuspendNetworkParams
            {
                Starting = TimeSpan.FromSeconds(5),
                Duration = TimeSpan.FromSeconds(60)
            };
            var receipt = await client.SuspendNetworkAsync(suspendParameters, ctx => ctx.Payer = systemAddress);

            Assert.Equal(ResponseCode.Success, receipt.Status);

            await Task.Delay(TimeSpan.FromSeconds(30));

            var pex = await Assert.ThrowsAsync <PrecheckException>(async() =>
            {
                await client.GetAccountInfoAsync(_network.Payer);
            });

            Assert.Equal(ResponseCode.PlatformNotActive, pex.Status);
            Assert.StartsWith("Transaction Failed Pre-Check: PlatformNotActive", pex.Message);

            await Task.Delay(TimeSpan.FromSeconds(50));

            var info = await client.GetAccountInfoAsync(_network.Payer);

            Assert.Equal(_network.Payer, info.Address);
        }
        internal static SuspendNetworkParams SuspendNetworkParams(SuspendNetworkParams suspendParameters)
        {
            if (suspendParameters.Duration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(suspendParameters.Duration), "The duration of the suspension must be greater than zero.");
            }
            if (suspendParameters.Duration.TotalHours > 24)
            {
                throw new ArgumentOutOfRangeException(nameof(suspendParameters.Duration), "The duration of suspension must not exceed 24 hours.");
            }
            if (suspendParameters.Starting.TotalHours > 24)
            {
                throw new ArgumentOutOfRangeException(nameof(suspendParameters.Starting), "The starting wait must not exceed 24 hours.");
            }
            var now  = DateTime.UtcNow;
            var then = now.Add(suspendParameters.Starting).Add(suspendParameters.Duration);

            if (then < now)
            {
                throw new ArgumentOutOfRangeException(nameof(suspendParameters.Starting), "The combination of Starting wait and Duration has already passed.");
            }
            if (suspendParameters.UpdateFile.IsNullOrNone())
            {
                if (!suspendParameters.UpdateFileHash.IsEmpty)
                {
                    throw new ArgumentOutOfRangeException(nameof(suspendParameters.UpdateFile), "The the the hash of the file contents is specified, an address for the update file must be included.");
                }
            }
            else if (suspendParameters.UpdateFileHash.IsEmpty)
            {
                throw new ArgumentOutOfRangeException(nameof(suspendParameters.UpdateFileHash), "The an update file address is specified, the hash of the file contents must be included.");
            }
            return(suspendParameters);
        }
Esempio n. 3
0
        public async Task CanSuspendNetworkWithUpdateFile()
        {
            await using var client = _network.NewClient();
            using var sha384       = new SHA384Managed();
            var systemAddress = await _network.GetSystemFreezeAdminAddress();

            if (systemAddress is null)
            {
                _network.Output?.WriteLine("TEST SKIPPED: No access to System Freeze Administrator Account.");
                return;
            }

            var specialFileAddress = new Address(0, 0, 150);
            var contents           = await client.GetFileContentAsync(specialFileAddress);

            var contentHash = sha384.ComputeHash(contents.ToArray());

            var suspendParameters = new SuspendNetworkParams
            {
                Starting       = TimeSpan.FromSeconds(5),
                Duration       = TimeSpan.FromSeconds(60),
                UpdateFile     = specialFileAddress,
                UpdateFileHash = contentHash
            };

            var receipt = await client.SuspendNetworkAsync(suspendParameters, ctx => ctx.Payer = systemAddress);

            Assert.Equal(ResponseCode.Success, receipt.Status);

            await Task.Delay(20000);

            var pex = await Assert.ThrowsAsync <PrecheckException>(async() =>
            {
                await client.GetAccountInfoAsync(_network.Payer);
            });

            Assert.Equal(ResponseCode.PlatformNotActive, pex.Status);
            Assert.StartsWith("Transaction Failed Pre-Check: PlatformNotActive", pex.Message);

            await Task.Delay(50000);

            var info = await client.GetAccountInfoAsync(_network.Payer);

            Assert.Equal(_network.Payer, info.Address);
        }