Example #1
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     [Perper("topic")] string topic,
                                     [PerperStream("outputStream")] IAsyncCollector <ISigned <object> > outputStream,
                                     ILogger logger)
        {
            var ipfs = new IpfsClient(ipfsGateway);

            await ipfs.PubSub.SubscribeAsync(topic, async message =>
            {
                try
                {
                    var item = IpfsJsonSettings.BytesToObject <ISigned <object> >(message.DataBytes);

                    var valueBytes = IpfsJsonSettings.ObjectToBytes(item.Value);

                    if (item.Signer.ValidateSignature(valueBytes, item.Signature))
                    {
                        logger.LogTrace("Received a '{type}' from '{topic}' on IPFS pubsub", item.GetType(), topic);
                        await outputStream.AddAsync(item);
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                }
            }, CancellationToken.None);

            await context.BindOutput(CancellationToken.None);
        }
Example #2
0
        public static async Task RunAsync([PerperStreamTrigger] PerperStreamContext context,
                                          [Perper("processor")] IPerperStream processor,
                                          ILogger logger, CancellationToken cancellationToken)
        {
            logger.LogInformation($"Starting pass-through consumer");
            await using var consumer =
                            await context.StreamActionAsync("NamedConsumer", typeof(Consumer), new { processor = processor.Subscribe() });

            await context.BindOutput(cancellationToken);
        }
Example #3
0
        public async Task Run([PerperStreamTrigger] PerperStreamContext context,
                              [Perper("input")] IAsyncEnumerable <IPerperStream> input,
                              [Perper("output")] IAsyncCollector <IPerperStream> output,
                              ILogger logger,
                              CancellationToken cancellationToken)
        {
            var agentInput = await input.FirstAsync(cancellationToken);

            var agentStream = await context.StreamFunctionAsync(typeof(AgentPingStream), new { input = agentInput.Subscribe() });

            await context.BindOutput(agentStream, cancellationToken);
        }
Example #4
0
        public static async Task RunAsync([PerperStreamTrigger] PerperStreamContext context,
                                          [Perper("delay")] TimeSpan delay,
                                          [Perper("data")] object data,
                                          [PerperStream("outputStream")] IAsyncCollector <object> outputStream,
                                          ILogger logger)
        {
            await Task.Delay(delay);

            logger.LogDebug("Sending a {type}", data.GetType());
            await outputStream.AddAsync(data);

            await context.BindOutput(CancellationToken.None);
        }
Example #5
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     CancellationToken cancellationToken)
        {
            ECParameters privateKey;
            ValidatorKey self;

            using (var dsa = ECDsa.Create())
            {
                privateKey = dsa.ExportParameters(true);
                self       = new ValidatorKey {
                    Key = dsa.ExportParameters(false)
                };
            }

            var ipfsGateway = "http://127.0.0.1:5001";

            await using var agentZeroStream = await context.StreamFunctionAsync(nameof (IpfsInput), new
            {
                ipfsGateway,
                topic = "apocryph-agent-0"
            });

            await using var _inputVerifierStream = await context.StreamFunctionAsync(nameof (StepSignatureVerifier), new
            {
                stepsStream = agentZeroStream,
            });

            await using var inputVerifierStream = await context.StreamFunctionAsync(nameof (IpfsLoader), new
            {
                ipfsGateway,
                hashStream = _inputVerifierStream
            });

            await using var validatorSetsStream = await context.StreamFunctionAsync(nameof (ValidatorSets), new
            {
                inputVerifierStream
            });

            await using var validatorSchedulerStream = await context.StreamActionAsync(nameof (ValidatorScheduler), new
            {
                validatorSetsStream,
                ipfsGateway,
                privateKey,
                self
            });

            await context.BindOutput(cancellationToken);
        }
Example #6
0
        public static async Task Run([PerperStreamTrigger(RunOnStartup = true)] PerperStreamContext context,
                                     CancellationToken cancellationToken)
        {
            var keys         = new List <(ECParameters, ValidatorKey)>();
            var validatorSet = new ValidatorSet();

            for (var i = 0; i < 1; i++)
            {
                using var dsa = ECDsa.Create(ECCurve.NamedCurves.nistP521);
                var privateKey = dsa.ExportParameters(true);
                var publicKey  = new ValidatorKey {
                    Key = dsa.ExportParameters(false)
                };
                keys.Add((privateKey, publicKey));
                validatorSet.Weights.Add(publicKey, 10);
            }

            var ipfsGateway = "http://127.0.0.1:5001";

            await using var _validatorSetsStream = await context.StreamFunctionAsync("TestDataGenerator", new
            {
                delay = TimeSpan.FromSeconds(20),
                data  = validatorSet
            });

            await using var validatorSetsStream = await context.StreamFunctionAsync(nameof (IpfsSaver), new
            {
                ipfsGateway,
                dataStream = _validatorSetsStream
            });

            await using var validatorLauncherStreams = new AsyncDisposableList();
            foreach (var(privateKey, self) in keys)
            {
                validatorLauncherStreams.Add(
                    await context.StreamActionAsync(nameof(ValidatorLauncher), new
                {
                    agentId  = "0",
                    services = new [] { "Sample", "IpfsInput" },
                    validatorSetsStream,
                    ipfsGateway,
                    privateKey,
                    self
                }));
            }

            await context.BindOutput(cancellationToken);
        }
Example #7
0
        public static async Task RunLauncher([PerperStreamTrigger] PerperStreamContext context,
                                             [Perper("agentId")] string agentId,
                                             [Perper("ipfsGateway")] string ipfsGateway,
                                             [Perper("commandsStream")] object[] commandsStream,
                                             CancellationToken cancellationToken)
        {
            var topic = "apocryph-agentInput-" + agentId;

            await using var ipfsStream = await context.StreamFunctionAsync(nameof (IpfsInput), new
            {
                ipfsGateway,
                topic
            });

            await using var transformedStream = await context.StreamFunctionAsync(nameof (IpfsInputSubscriber), new
            {
                ipfsStream,
                commandsStream
            });

            await context.BindOutput(transformedStream, cancellationToken);
        }
Example #8
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("count")] int count,
                                     [Perper("output")] IAsyncCollector <IPerperStream> output,
                                     ILogger logger, CancellationToken cancellationToken)
        {
            var state = await context.FetchStateAsync <Data>() ?? new Data
            {
                Value = 0
            };

            for (var i = state.Value; i < count; i++)
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);

                logger.LogInformation($"Generator Generator generates: {i}");
                var stream = await context.StreamFunctionAsync("NamedGenerator-" + i, typeof(Generator), new { count = 10, tag = "first" ?? "xx-" + i }, typeof(Data));

                await output.AddAsync(stream, cancellationToken);

                state.Value = i;
                await context.UpdateStateAsync(state);
            }
            await context.BindOutput(cancellationToken);
        }
Example #9
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("agentId")] string agentId,
                                     [Perper("services")] string[] services,
                                     [Perper("validatorSetsStream")] object[] validatorSetsStream,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     [Perper("privateKey")] ECParameters privateKey,
                                     [Perper("self")] ValidatorKey self,
                                     CancellationToken cancellationToken)
        {
            var genesisMessage = ("", (object)new InitMessage());

            await using var validatorSchedulerStream = await context.StreamActionAsync(nameof (PBFTConsensus), new
            {
                agentId,
                services,
                validatorSetsStream,
                genesisMessage,
                ipfsGateway,
                privateKey,
                self
            });

            await context.BindOutput(cancellationToken);
        }
Example #10
0
 public async Task Run([PerperStreamTrigger] PerperStreamContext context,
                       [Perper("streams")] List <IPerperStream> streams,
                       CancellationToken cancellationToken)
 {
     await context.BindOutput(streams, cancellationToken);
 }
Example #11
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("agentId")] string agentId,
                                     [Perper("services")] string[] services,
                                     [Perper("commandsStream")] object[] commandsStream,
                                     [Perper("genesisMessage")] object genesisMessage,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     CancellationToken cancellationToken)
        {
            await using var genesisMessageStream = await context.StreamFunctionAsync(nameof (TestDataGenerator), new
            {
                delay = TimeSpan.FromSeconds(10),
                data  = genesisMessage
            });

            /* await using var agentZeroStream = await context.StreamFunctionAsync(nameof(IpfsInput), new
             * {
             *  ipfsGateway,
             *  topic = 0 //"apocryph-agent-0"
             * });
             *
             * await using var _inputVerifierStream = await context.StreamFunctionAsync(nameof(StepSignatureVerifier), new
             * {
             *  validatorSetsStream, // TODO: Should give agent 0's validator set instead !!
             *  stepsStream = agentZeroStream,
             * });
             *
             * await using var inputVerifierStream = await context.StreamFunctionAsync(nameof(IpfsLoader), new
             * {
             *  ipfsGateway,
             *  hashStream = _inputVerifierStream
             * });
             *
             * await using var validatorSetsStream = await context.StreamFunctionAsync(nameof(ValidatorSets), new
             * {
             *  inputVerifierStream
             * });
             *
             * await using var subscriptionCommandExecutorStream = await context.StreamFunctionAsync(nameof(SubscriptionCommandExecutor), new
             * {
             *  ipfsGateway,
             *  commandsStream,
             *  validatorSetsStream
             * }); */

            await using var reminderCommandExecutorStream = await context.StreamFunctionAsync(nameof (ReminderCommandExecutor), new
            {
                commandsStream
            });

            await using var serviceFilterStreams = new AsyncDisposableList();
            await using var serviceStreams       = new AsyncDisposableList();
            foreach (var serviceName in services)
            {
                var functionName           = "Service" + serviceName;
                var filteredCommandsStream = await context.StreamFunctionAsync(nameof(ServiceCommandFilter), new
                {
                    commandsStream,
                    serviceName
                });

                serviceFilterStreams.Add(filteredCommandsStream);
                var serviceStream = await context.StreamFunctionAsync(functionName, new
                {
                    commandsStream = filteredCommandsStream,
                    agentId,
                    ipfsGateway
                });

                serviceStreams.Add(serviceStream);
            }

            var outputStream = new []
            {
                reminderCommandExecutorStream,
                genesisMessageStream,
                // subscriptionCommandExecutorStream
            }.Concat(serviceStreams).ToArray();


            await context.BindOutput(outputStream, cancellationToken);
        }
Example #12
0
        public async Task Run([PerperStreamTrigger] PerperStreamContext context,
                              [Perper("slotGossips")] IPerperStream slotGossips,
                              [Perper("chains")] IDictionary <Guid, Chain> chains,
                              [Perper("output")] IAsyncCollector <int> output,
                              CancellationToken cancellationToken)
        {
            await using var peering = context.DeclareStream("Peering", typeof(PeeringStream));
            var gossips = peering;
            var queries = peering;
            var reports = peering;
            var hashes  = peering;

            await using var salts = context.DeclareStream("Salts", typeof(SaltsStream));

            await using var hashRegistry = await context.StreamFunctionAsync("HashRegistry", typeof(HashRegistryStream), new
            {
                filter = typeof(Block),
                input  = hashes.Subscribe()
            }, typeof(HashRegistryEntry));

            await context.StreamActionAsync("DummyStream", new
            {
                hashRegistry = hashRegistry.Subscribe() // HACK: Ensure hash registry is started up before anything else
            });

            await using var chain = await context.StreamFunctionAsync("Chain", typeof(ChainStream), new
            {
                chains,
                gossips,
                queries,
                hashRegistry,
                salts       = salts.Subscribe(),
                slotGossips = slotGossips.Subscribe()
            });

            // HACK: Create an empty stream for the global IBC
            await using var validator = await context.StreamFunctionAsync("DummyStream", new
            {
                peering      = peering.Subscribe(),     // HACK: Ensure peering is started before it starts receiving streams
                hashRegistry = hashRegistry.Subscribe() // HACK: Ensure hash registry is started up
            });

            await using var ibc = await context.StreamFunctionAsync("IBC-global", typeof(IBCStream), new
            {
                chain     = chain.Subscribe(),
                validator = validator.Subscribe(),
                gossips   = gossips.Subscribe(),
                nodes     = new Dictionary <Guid, Node?[]>(),
                node      = default(Node?)
            });

            await using var filter = await context.StreamFunctionAsync("Filter-global", typeof(FilterStream), new
            {
                ibc     = ibc.Subscribe(),
                gossips = gossips.Subscribe(),
                hashRegistry,
                chains
            });

            await context.StreamFunctionAsync(salts, new
            {
                chains,
                hashRegistry = hashRegistry,
                filter       = filter.Subscribe()
            });

            await context.StreamFunctionAsync(peering, new
            {
                factory = chain.Subscribe(),
                initial = new List <IPerperStream>()
                {
                    filter
                },
            });

            await using var reportsStream = await context.StreamActionAsync(typeof(ReportsStream), new
            {
                hashRegistry = hashRegistry,
                chain        = chain.Subscribe(),
                nodes        = new Dictionary <Guid, Node?[]>(),
                filter       = filter.Subscribe(),
                reports      = reports.Subscribe()
            });

            await context.BindOutput(cancellationToken);
        }