Ejemplo n.º 1
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     [PerperStream("dataStream")] IAsyncEnumerable <object> dataStream,
                                     [PerperStream("outputStream")] IAsyncCollector <IHashed <object> > outputStream,
                                     ILogger logger)
        {
            var ipfs = new IpfsClient(ipfsGateway);

            await dataStream.ForEachAsync(async item =>
            {
                try
                {
                    var jToken = IpfsJsonSettings.JTokenFromObject(item);
                    var cid    = await ipfs.Dag.PutAsync(jToken, cancel: CancellationToken.None);
                    var hash   = new Hash {
                        Bytes = cid.ToArray()
                    };

                    logger.LogDebug("Saved {json} as {hash} in ipfs", jToken.ToString(Formatting.None), hash);

                    await outputStream.AddAsync(Hashed.Create(item, hash));
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                }
            }, CancellationToken.None);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     [PerperStream("hashStream")] IAsyncEnumerable <Hash> hashStream,
                                     [PerperStream("outputStream")] IAsyncCollector <IHashed <object> > outputStream,
                                     ILogger logger)
        {
            var ipfs  = new IpfsClient(ipfsGateway);
            var state = await context.FetchStateAsync <State>() ?? new State();

            async Task processHash(Hash hash, CancellationToken cancellationToken)
            {
                if (state.ResolvedHashes.Contains(hash))
                {
                    return;
                }

                state.ResolvedHashes.Add(hash);
                var jToken = await ipfs.Dag.GetAsync(Cid.Read(hash.Bytes), cancellationToken);

                var item = IpfsJsonSettings.ObjectFromJToken <object>(jToken);

                if (item is IAgentStep agentStep)
                {
                    await processHash(agentStep.Previous, cancellationToken);
                }

                var hashedType = typeof(Hashed <>).MakeGenericType(item.GetType());
                var hashed     = (IHashed <object>)Activator.CreateInstance(hashedType, item, hash);

                await outputStream.AddAsync(hashed);
            };

            await hashStream.ForEachAsync(async hash =>
            {
                try
                {
                    // NOTE: Currently blocks other items on the stream and does not process them
                    await processHash(hash, CancellationToken.None);

                    await context.UpdateStateAsync(state);
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                }
            }, CancellationToken.None);
        }
Ejemplo n.º 4
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     [Perper("topic")] string topic,
                                     [PerperStream("dataStream")] IAsyncEnumerable <ISigned <object> > dataStream,
                                     ILogger logger)
        {
            var ipfs = new IpfsClient(ipfsGateway);

            await dataStream.ForEachAsync(async item =>
            {
                try
                {
                    var bytes = IpfsJsonSettings.ObjectToBytes(item);

                    await ipfs.PubSub.PublishAsync(topic, bytes, CancellationToken.None);
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                }
            }, CancellationToken.None);
        }