public async Task InvalidStartAndEndingFiltersRaiseError()
    {
        await using var fx = await TestTopicMessage.CreateAsync(_network);

        await Task.Delay(5000); // give the beta net time to sync

        using var cts = new CancellationTokenSource();
        var capture = new TopicMessageCapture(1);

        await using var mirror = _network.NewMirror();
        var aoe = await Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() =>
        {
            cts.CancelAfter(500);
            await mirror.SubscribeTopicAsync(new SubscribeTopicParams
            {
                Topic             = fx.TestTopic.Record.Topic,
                Starting          = DateTime.UtcNow.AddDays(-1),
                Ending            = DateTime.UtcNow.AddDays(-2),
                MessageWriter     = capture,
                CancellationToken = cts.Token
            });
        });

        Assert.Equal("Ending", aoe.ParamName);
        Assert.StartsWith("The ending filter date is less than the starting filter date, no records can be returned.", aoe.Message);
        Assert.Empty(capture.CapturedList);
    }
Beispiel #2
0
    public static async Task <TopicMessage[]> CaptureOrTimeoutAsync(MirrorClient mirror, Address topic, int expectedCount, int timeoutInMiliseconds)
    {
        using var cts = new CancellationTokenSource();
        var capture       = new TopicMessageCapture(expectedCount);
        var subscribeTask = mirror.SubscribeTopicAsync(new SubscribeTopicParams
        {
            Topic             = topic,
            Starting          = DateTime.UtcNow.AddHours(-1),
            MessageWriter     = capture,
            CancellationToken = cts.Token
        });

        cts.CancelAfter(timeoutInMiliseconds);
        await subscribeTask;

        return(capture.CapturedList.ToArray());
    }
    public async Task InvalidTopicIdRaisesError()
    {
        await using var mirror = _network.NewMirror();
        var capture = new TopicMessageCapture(1);
        var mex     = await Assert.ThrowsAsync <MirrorException>(async() =>
        {
            await mirror.SubscribeTopicAsync(new SubscribeTopicParams
            {
                Topic             = _network.Payer,
                MessageWriter     = capture,
                CancellationToken = new CancellationTokenSource(2500).Token
            });
        });

        Assert.Equal(MirrorExceptionCode.InvalidTopicAddress, mex.Code);
        Assert.StartsWith("The address exists, but is not a topic.", mex.Message);
        Assert.Empty(capture.CapturedList);
    }
    public async Task NonExistantTopicIdRaisesError()
    {
        await using var mirror = _network.NewMirror();
        var capture = new TopicMessageCapture(1);
        var me      = await Assert.ThrowsAsync <MirrorException>(async() =>
        {
            await mirror.SubscribeTopicAsync(new SubscribeTopicParams
            {
                Topic             = new Address(0, 1, 100),
                MessageWriter     = capture,
                CancellationToken = new CancellationTokenSource(2500).Token
            });
        });

        Assert.Equal(MirrorExceptionCode.TopicNotFound, me.Code);
        Assert.StartsWith("The topic with the specified address does not exist.", me.Message);
        Assert.Empty(capture.CapturedList);
    }
    public async Task MissingTopicIdRaisesError()
    {
        await using var mirror = _network.NewMirror();
        var capture = new TopicMessageCapture(1);

        var ane = await Assert.ThrowsAsync <ArgumentNullException>(async() =>
        {
            await mirror.SubscribeTopicAsync(new SubscribeTopicParams
            {
                Topic         = null,
                MessageWriter = capture
            });
        });

        Assert.Equal("Topic", ane.ParamName);
        Assert.StartsWith("Topic address is missing. Please check that it is not null.", ane.Message);
        Assert.Empty(capture.CapturedList);
    }
    public async Task CanCaptureATestTopic()
    {
        try
        {
            await using var fx = await TestTopicMessage.CreateAsync(_network);

            Assert.Equal(ResponseCode.Success, fx.Record.Status);
            Assert.Equal(1ul, fx.Record.SequenceNumber);
            Assert.False(fx.Record.RunningHash.IsEmpty);

            await Task.Delay(5000); // give the beta net time to sync

            var capture = new TopicMessageCapture(1);
            await using var mirror = _network.NewMirror();
            using var cts          = new CancellationTokenSource();
            var subscribeTask = mirror.SubscribeTopicAsync(new SubscribeTopicParams
            {
                Topic             = fx.TestTopic.Record.Topic,
                Starting          = DateTime.UtcNow.AddHours(-1),
                MessageWriter     = capture,
                CancellationToken = cts.Token
            });
            cts.CancelAfter(500);
            await subscribeTask;
            if (capture.CapturedList.Count == 0)
            {
                _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RETURN TOPIC IN ALLOWED TIME");
            }
            else
            {
                var message = capture.CapturedList[0];
                Assert.Equal(fx.TestTopic.Record.Topic, message.Topic);
                Assert.Equal(1ul, message.SequenceNumber);
                Assert.Equal(fx.Record.RunningHash.ToArray(), message.RunningHash.ToArray());
                Assert.Equal(fx.Message.ToArray(), message.Messsage.ToArray());
                Assert.Null(message.SegmentInfo);
            }
        }
        catch (MirrorException mex) when(mex.Code == MirrorExceptionCode.TopicNotFound)
        {
            _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RECEIVE TOPIC CREATE IN ALLOWED TIME");
            return;
        }
    }
Beispiel #7
0
        public async Task ReturnLimitIsEnforced()
        {
            await using var fx = await TestTopicMessage.CreateAsync(_network);

            await fx.TestTopic.Client.SubmitMessageAsync(fx.TestTopic.Record.Topic, fx.Message, fx.TestTopic.ParticipantPrivateKey);

            await fx.TestTopic.Client.SubmitMessageAsync(fx.TestTopic.Record.Topic, fx.Message, fx.TestTopic.ParticipantPrivateKey);

            await Task.Delay(8000); // give the beta net time to sync

            var capture = new TopicMessageCapture(10);

            await using var mirror = _network.NewMirror();
            using var cts          = new CancellationTokenSource();
            try
            {
                var subscribeTask = mirror.SubscribeTopicAsync(new SubscribeTopicParams
                {
                    Topic             = fx.TestTopic.Record.Topic,
                    Starting          = DateTime.UtcNow.AddHours(-1),
                    MessageWriter     = capture,
                    CancellationToken = cts.Token,
                    MaxCount          = 2
                });
                cts.CancelAfter(10000);
                await subscribeTask;
                if (capture.CapturedList.Count == 0)
                {
                    _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RETURN TOPIC IN ALLOWED TIME");
                }
                else
                {
                    Assert.Equal(2, capture.CapturedList.Count);
                }
            }
            catch (MirrorException mex) when(mex.Code == MirrorExceptionCode.TopicNotFound)
            {
                _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RECEIVE TOPIC CREATE IN ALLOWED TIME");
                return;
            }
        }
    public async Task CanSubmitBogusSegmentedMessageMetadata()
    {
        await using var fx = await TestTopic.CreateAsync(_network);

        var parentTx     = fx.Client.CreateNewTxId();
        var submitParams = new SubmitMessageParams
        {
            Topic             = fx.Record.Topic,
            Segment           = Encoding.ASCII.GetBytes(Generator.String(120, 199)),
            ParentTxId        = parentTx,
            Index             = 100,
            TotalSegmentCount = 200,
            Signatory         = fx.ParticipantPrivateKey
        };
        var receipt = await fx.Client.SubmitMessageAsync(submitParams);

        Assert.Equal(ResponseCode.Success, receipt.Status);
        Assert.Equal(1ul, receipt.SequenceNumber);
        Assert.False(receipt.RunningHash.IsEmpty);
        Assert.Equal(3ul, receipt.RunningHashVersion);

        var info = await fx.Client.GetTopicInfoAsync(fx.Record.Topic);

        Assert.Equal(fx.Memo, info.Memo);
        Assert.NotEmpty(info.RunningHash.ToArray());
        Assert.Equal(1UL, info.SequenceNumber);
        Assert.True(info.Expiration > DateTime.MinValue);
        Assert.Equal(new Endorsement(fx.AdminPublicKey), info.Administrator);
        Assert.Equal(new Endorsement(fx.ParticipantPublicKey), info.Participant);
        Assert.True(info.AutoRenewPeriod > TimeSpan.MinValue);
        Assert.Equal(fx.TestAccount.Record.Address, info.RenewAccount);
        AssertHg.NotEmpty(info.Ledger);

        await Task.Delay(7000); // give the beta net time to sync

        try
        {
            await using var mirror = _network.NewMirror();
            var topicMessages = await TopicMessageCapture.CaptureOrTimeoutAsync(mirror, fx.Record.Topic, 1, 7000);

            if (topicMessages.Length == 0)
            {
                _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RETURN TOPIC IN ALLOWED TIME");
            }
            else
            {
                var topicMessage = topicMessages[0];
                Assert.Equal(submitParams.Topic, topicMessage.Topic);
                Assert.Equal(1ul, topicMessage.SequenceNumber);
                Assert.Equal(receipt.RunningHash.ToArray(), topicMessage.RunningHash.ToArray());
                Assert.Equal(submitParams.Segment.ToArray(), topicMessage.Messsage.ToArray());
                Assert.NotNull(topicMessage.SegmentInfo);
                Assert.Equal(parentTx, topicMessage.SegmentInfo.ParentTxId);
                Assert.Equal(100, topicMessage.SegmentInfo.Index);
                Assert.Equal(200, topicMessage.SegmentInfo.TotalSegmentCount);
            }
        }
        catch (MirrorException mex) when(mex.Code == MirrorExceptionCode.TopicNotFound)
        {
            _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RECEIVE TOPIC CREATE IN ALLOWED TIME");
            return;
        }
    }
    public async Task ReturnLimitIsEnforced()
    {
        await using var fx = await TestTopicMessage.CreateAsync(_network);

        await fx.TestTopic.Client.SubmitMessageAsync(fx.TestTopic.Record.Topic, fx.Message, fx.TestTopic.ParticipantPrivateKey);

        await fx.TestTopic.Client.SubmitMessageAsync(fx.TestTopic.Record.Topic, fx.Message, fx.TestTopic.ParticipantPrivateKey);

        await fx.TestTopic.Client.SubmitMessageAsync(fx.TestTopic.Record.Topic, fx.Message, fx.TestTopic.ParticipantPrivateKey);

        await fx.TestTopic.Client.SubmitMessageAsync(fx.TestTopic.Record.Topic, fx.Message, fx.TestTopic.ParticipantPrivateKey);

        // Wait for enough messages to be available
        // in the mirror node's database.
        for (int waitTries = 0; waitTries < 20; waitTries++)
        {
            try
            {
                var captured = await TopicMessageCapture.CaptureOrTimeoutAsync(_network.NewMirror(), fx.TestTopic.Record.Topic, 4, 5000);

                if (captured.Length > 2)
                {
                    break;
                }
            }
            catch (MirrorException ex) when(ex.Code == MirrorExceptionCode.TopicNotFound)
            {
                _network.Output?.WriteLine("Mirror Node is slow, can not find topic just yet.");
                await Task.Delay(1000);
            }
        }

        // Now we can try the real test on the limits.
        var capture = new TopicMessageCapture(10);

        await using var mirror = _network.NewMirror();
        using var cts          = new CancellationTokenSource();
        try
        {
            var subscribeTask = mirror.SubscribeTopicAsync(new SubscribeTopicParams
            {
                Topic             = fx.TestTopic.Record.Topic,
                Starting          = DateTime.UtcNow.AddHours(-1),
                MessageWriter     = capture,
                CancellationToken = cts.Token,
                MaxCount          = 2
            });
            cts.CancelAfter(10000);
            await subscribeTask;
            if (capture.CapturedList.Count == 0)
            {
                _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RETURN TOPIC IN ALLOWED TIME");
            }
            else
            {
                Assert.Equal(2, capture.CapturedList.Count);
            }
        }
        catch (MirrorException mex) when(mex.Code == MirrorExceptionCode.TopicNotFound)
        {
            _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RECEIVE TOPIC CREATE IN ALLOWED TIME");
            return;
        }
    }
Beispiel #10
0
        public async Task CanSubmitLargeSegmentedMessageWithTwoSegments()
        {
            await using var fx = await TestTopic.CreateAsync(_network);

            var segmentSize   = Generator.Integer(100, 200);
            var expectedCount = 2;
            var message       = Encoding.ASCII.GetBytes(Generator.Code(3 * segmentSize / 2));
            var receipts      = await fx.Client.SubmitLargeMessageAsync(fx.Record.Topic, message, segmentSize, fx.ParticipantPrivateKey);

            Assert.Equal(expectedCount, receipts.Length);
            for (int i = 0; i < expectedCount; i++)
            {
                var receipt = receipts[i];
                Assert.Equal(ResponseCode.Success, receipt.Status);
                Assert.Equal((ulong)(i + 1), receipt.SequenceNumber);
                Assert.False(receipt.RunningHash.IsEmpty);
                Assert.Equal(3ul, receipt.RunningHashVersion);
            }

            var info = await fx.Client.GetTopicInfoAsync(fx.Record.Topic);

            Assert.Equal(fx.Memo, info.Memo);
            Assert.NotEqual(ReadOnlyMemory <byte> .Empty, info.RunningHash);
            Assert.Equal((ulong)expectedCount, info.SequenceNumber);
            Assert.True(info.Expiration > DateTime.MinValue);
            Assert.Equal(new Endorsement(fx.AdminPublicKey), info.Administrator);
            Assert.Equal(new Endorsement(fx.ParticipantPublicKey), info.Participant);
            Assert.True(info.AutoRenewPeriod > TimeSpan.MinValue);
            Assert.Equal(fx.TestAccount.Record.Address, info.RenewAccount);

            await Task.Delay(10000); // give the beta net time to sync

            try
            {
                await using var mirror = _network.NewMirror();
                var topicMessages = await TopicMessageCapture.CaptureOrTimeoutAsync(mirror, fx.Record.Topic, expectedCount, 7000);

                if (topicMessages.Length == 0)
                {
                    _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RETURN TOPIC IN ALLOWED TIME");
                }
                else
                {
                    var pointerIndex         = 0;
                    var reconstitutedMessage = new byte[message.Length];
                    Assert.Equal(expectedCount, topicMessages.Length);
                    for (int i = 0; i < topicMessages.Length; i++)
                    {
                        var topicMessage = topicMessages[i];
                        Assert.Equal(fx.Record.Topic, topicMessage.Topic);
                        Assert.Equal((ulong)i + 1, topicMessage.SequenceNumber);
                        Assert.Equal(receipts[i].RunningHash.ToArray(), topicMessage.RunningHash.ToArray());
                        Assert.NotNull(topicMessage.SegmentInfo);
                        Assert.Equal(receipts[0].Id, topicMessage.SegmentInfo.ParentTxId);
                        Assert.Equal(i + 1, topicMessage.SegmentInfo.Index);
                        Assert.Equal(expectedCount, topicMessage.SegmentInfo.TotalSegmentCount);
                        topicMessage.Messsage.ToArray().CopyTo(reconstitutedMessage, pointerIndex);
                        pointerIndex += topicMessage.Messsage.Length;
                    }
                    Assert.Equal(message.ToArray(), reconstitutedMessage);
                }
            }
            catch (MirrorException mex) when(mex.Code == MirrorExceptionCode.TopicNotFound)
            {
                _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RECEIVE TOPIC CREATE IN ALLOWED TIME");
                return;
            }
        }
        public async Task CanSubmitTwoSegmentedMessage()
        {
            await using var fx = await TestTopic.CreateAsync(_network);

            var submitParams = new SubmitMessageParams[2];
            var receipt      = new SubmitMessageReceipt[2];

            submitParams[0] = new SubmitMessageParams
            {
                Topic             = fx.Record.Topic,
                Segment           = Encoding.ASCII.GetBytes(Generator.String(120, 199)),
                Index             = 1,
                TotalSegmentCount = 2,
                Signatory         = fx.ParticipantPrivateKey
            };
            receipt[0] = await fx.Client.SubmitMessageAsync(submitParams[0]);

            Assert.Equal(ResponseCode.Success, receipt[0].Status);
            Assert.Equal(1ul, receipt[0].SequenceNumber);
            Assert.False(receipt[0].RunningHash.IsEmpty);
            Assert.Equal(3ul, receipt[0].RunningHashVersion);
            var txId = receipt[0].Id;

            submitParams[1] = new SubmitMessageParams
            {
                Topic             = fx.Record.Topic,
                Segment           = Encoding.ASCII.GetBytes(Generator.String(120, 199)),
                ParentTxId        = txId,
                Index             = 2,
                TotalSegmentCount = 2,
                Signatory         = fx.ParticipantPrivateKey
            };
            receipt[1] = await fx.Client.SubmitMessageAsync(submitParams[1]);

            Assert.Equal(ResponseCode.Success, receipt[1].Status);
            Assert.Equal(2ul, receipt[1].SequenceNumber);
            Assert.False(receipt[1].RunningHash.IsEmpty);
            Assert.Equal(3ul, receipt[1].RunningHashVersion);

            var info = await fx.Client.GetTopicInfoAsync(fx.Record.Topic);

            Assert.Equal(fx.Memo, info.Memo);
            Assert.NotEqual(ReadOnlyMemory <byte> .Empty, info.RunningHash);
            Assert.Equal(2UL, info.SequenceNumber);
            Assert.True(info.Expiration > DateTime.MinValue);
            Assert.Equal(new Endorsement(fx.AdminPublicKey), info.Administrator);
            Assert.Equal(new Endorsement(fx.ParticipantPublicKey), info.Participant);
            Assert.True(info.AutoRenewPeriod > TimeSpan.MinValue);
            Assert.Equal(fx.TestAccount.Record.Address, info.RenewAccount);

            await Task.Delay(7000); // give the beta net time to sync

            try
            {
                await using var mirror = _network.NewMirror();
                var topicMessages = await TopicMessageCapture.CaptureOrTimeoutAsync(mirror, fx.Record.Topic, submitParams.Length, 7000);

                if (topicMessages.Length == 0)
                {
                    _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RETURN TOPIC IN ALLOWED TIME");
                }
                else
                {
                    Assert.Equal(2, topicMessages.Length);
                    for (int i = 0; i < topicMessages.Length; i++)
                    {
                        var topicMessage = topicMessages[i];
                        Assert.Equal(fx.Record.Topic, topicMessage.Topic);
                        Assert.Equal((ulong)i + 1, topicMessage.SequenceNumber);
                        Assert.Equal(receipt[i].RunningHash.ToArray(), topicMessage.RunningHash.ToArray());
                        Assert.Equal(submitParams[i].Segment.ToArray(), topicMessage.Messsage.ToArray());
                        Assert.NotNull(topicMessage.SegmentInfo);
                        Assert.Equal(txId, topicMessage.SegmentInfo.ParentTxId);
                        Assert.Equal(i + 1, topicMessage.SegmentInfo.Index);
                        Assert.Equal(submitParams.Length, topicMessage.SegmentInfo.TotalSegmentCount);
                    }
                }
            }
            catch (MirrorException mex) when(mex.Code == MirrorExceptionCode.TopicNotFound)
            {
                _network.Output?.WriteLine("INDETERMINATE TEST - MIRROR NODE DID NOT RECEIVE TOPIC CREATE IN ALLOWED TIME");
                return;
            }
        }