Exemplo n.º 1
0
        public async Task Can_Receive_Query_Response_On_Observer()
        {
            var recipientPeerId = PeerIdHelper.GetPeerId();

            var hp            = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));
            var lastDeltaHash = hp.ComputeMultiHash(ByteUtil.GenerateRandomByteArray(32));

            var collection = new List <DeltaIndex>();

            //// this matches the fake mock
            for (uint x = 0; x < 10; x++)
            {
                var delta = new Delta
                {
                    PreviousDeltaDfsHash = lastDeltaHash.Digest.ToByteString()
                };

                var index = new DeltaIndex
                {
                    Height = 10,
                    Cid    = delta.ToByteString()
                };

                collection.Add(index);
                lastDeltaHash = hp.ComputeMultiHash(ByteUtil.GenerateRandomByteArray(32));
            }

            var deltaHistoryResponse = new PeerDeltaHistoryResponse(recipientPeerId, collection);

            _peerDeltaHistoryRequest.DeltaHistoryResponseMessageStreamer.OnNext(deltaHistoryResponse);
            var response = await _peerDeltaHistoryRequest.DeltaHistoryAsync(recipientPeerId).ConfigureAwait(false);

            response.DeltaCid.Count.Should().Be(10);
        }
Exemplo n.º 2
0
        private byte[] PrepareEd25519PrecompileCall(HashProvider hashProvider, FfiWrapper cryptoContext, IPrivateKey signingPrivateKey, IPrivateKey otherPrivateKey)
        {
            var data    = Encoding.UTF8.GetBytes("Testing testing 1 2 3");
            var message = hashProvider.ComputeMultiHash(data).Digest;

            var signingContext = Encoding.UTF8.GetBytes("Testing testing 1 2 3 context");
            var context        = hashProvider.ComputeMultiHash(signingContext).Digest;

            ISignature signature      = cryptoContext.Sign(signingPrivateKey, message, context);
            var        signatureBytes = signature.SignatureBytes;
            var        publicKeyBytes = otherPrivateKey.GetPublicKey().Bytes;

            // below verify check is not needed but allows for greater confidence for any person in the future
            // that would approach and debug test when it goes wrong
            // =====================================================
            cryptoContext.Verify(signature, message, context)
            .Should().BeTrue("signature generated with private key should verify with corresponding public key");

            // =====================================================

            // save message hash in memory at position 0x00
            string pushToMemoryAt0 = "7f" + message.ToHexString(false) + "600052";

            // save first 32 bytes of the sig in memory starting from position 0x20
            string pushToMemoryAt32 = "7f" + signatureBytes.AsSpan(0, 32).ToHexString(false) + "602052";

            // save remaining 32 bytes of the sig in memory starting from position 0x40
            string pushToMemoryAt64 = "7f" + signatureBytes.AsSpan(32, 32).ToHexString(false) + "604052";

            // save context bytes in memory starting from position 0x60 (but this should be changed if context is smaller)
            string pushToMemoryAt96 = "7f" + context.ToHexString(false) + "606052";

            // save public key bytes in memory starting from position 0x80 (but this should be changed if context is smaller)
            string pushToMemoryAt128 = "7f" + publicKeyBytes.ToHexString(false) + "608052";

            // address of the precompile within Catalyst
            string addressCode = GetEd25519PrecompileAddressAsHex();

            var byteCode = Bytes.FromHexString(
                pushToMemoryAt0 +
                pushToMemoryAt32 +
                pushToMemoryAt64 +
                pushToMemoryAt96 +
                pushToMemoryAt128 +

                // PUSH1 32 PUSH1 0 PUSH1 160 PUSH1 0 PUSH20 address GAS STATICCALL
                // make a call to precompile and pass [0,160) bytes of memory as an input
                // and store result at [0,1) of memory array
                // allow precompile to use all the gas required
                "6001600060a0600073" +
                addressCode +
                "45fa00");

            TestContext.WriteLine(byteCode.ToHexString());
            return(byteCode);
        }
Exemplo n.º 3
0
        public void DeltasDao_Deltas_Should_Be_Convertible()
        {
            var previousHash = _hashProvider.ComputeMultiHash(Encoding.UTF8.GetBytes("previousHash"));

            var original = DeltaHelper.GetDelta(_hashProvider, previousHash);

            var messageDao  = original.ToDao <Delta, DeltaDao>(_mapperProvider);
            var reconverted = messageDao.ToProtoBuff <DeltaDao, Delta>(_mapperProvider);

            original.Should().Be(reconverted);
        }
Exemplo n.º 4
0
        public TestCycleEventProvider(ILogger logger = null)
        {
            Scheduler = new TestScheduler();

            var schedulerProvider = Substitute.For <ICycleSchedulerProvider>();

            schedulerProvider.Scheduler.Returns(Scheduler);

            var dateTimeProvider  = Substitute.For <IDateTimeProvider>();
            var deltaHashProvider = Substitute.For <IDeltaHashProvider>();

            var hashingAlgorithm = HashingAlgorithm.GetAlgorithmMetadata("blake2b-256");
            var hashingProvider  = new HashProvider(hashingAlgorithm);

            dateTimeProvider.UtcNow.Returns(_ => Scheduler.Now.DateTime);

            _cycleEventsProvider = new CycleEventsProvider(
                CycleConfiguration.Default, dateTimeProvider, schedulerProvider, deltaHashProvider,
                logger ?? Substitute.For <ILogger>());

            deltaHashProvider.GetLatestDeltaHash(Arg.Any <DateTime>())
            .Returns(ci => hashingProvider.ComputeMultiHash(
                         BitConverter.GetBytes(((DateTime)ci[0]).Ticks /
                                               (int)_cycleEventsProvider.Configuration.CycleDuration.Ticks)));

            _deltaUpdatesSubscription = PhaseChanges.Subscribe(p => CurrentPhase = p);
        }
        public async Task Can_Process_DeltaHeightRequest_Correctly()
        {
            var fakeContext = Substitute.For <IChannelHandlerContext>();
            var deltaHistoryRequestMessage = new DeltaHistoryRequest();

            var channeledAny = new ObserverDto(fakeContext,
                                               deltaHistoryRequestMessage.ToProtocolMessage(PeerIdHelper.GetPeerId(),
                                                                                            CorrelationId.GenerateCorrelationId()
                                                                                            )
                                               );

            var observableStream = new[] { channeledAny }.ToObservable(_testScheduler);

            _deltaHistoryRequestObserver.StartObserving(observableStream);
            _testScheduler.Start();

            var response = new DeltaHistoryResponse();
            var hp            = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));
            var lastDeltaHash = hp.ComputeMultiHash(ByteUtil.GenerateRandomByteArray(32));

            for (uint x = 0; x < 10; x++)
            {
                var delta = new Delta
                {
                    PreviousDeltaDfsHash = lastDeltaHash.Digest.ToByteString()
                };

                var index = new DeltaIndex
                {
                    Height = 10,
                    Cid    = delta.ToByteString()
                };

                response.DeltaIndex.Add(index);
                lastDeltaHash = hp.ComputeMultiHash(ByteUtil.GenerateRandomByteArray(32));
            }

            await fakeContext.Channel.ReceivedWithAnyArgs(1)
            .WriteAndFlushAsync(response.ToProtocolMessage(PeerIdHelper.GetPeerId(), CorrelationId.GenerateCorrelationId())).ConfigureAwait(false);

            _subbedLogger.ReceivedWithAnyArgs(1);
        }