Exemple #1
0
        public static AgentContext <object> Run([PerperWorkerTrigger] object workerContext,
                                                [Perper("state")] object state,
                                                [Perper("sender")] AgentCapability sender,
                                                [Perper("message")] object message)
        {
            var context = new AgentContext <object>(state);

            if (message is InitMessage)
            {
                context.SampleStore("0", "1");
                context.SubscribeUserInput(new ValidatorKey
                {
                    Key = new ECParameters
                    {
                        Curve = ECCurve.NamedCurves.nistP521,
                        Q     = new ECPoint
                        {
                            // Valid signed object: {"$type":"Apocryph.FunctionApp.Model.Signed`1[[Apocryph.FunctionApp.Model.Commit, Apocryph.FunctionApp]], Apocryph.FunctionApp","Value":{"For":{"Bytes":"AXESIAqgZa22xsCD2XY3SIz4RWk3M4kbUVw93vb+tik8+Wb2"}},"Signer":{"Key":{"X":"ADBRFw9bcnJGVJsrZmvG90YGv2iighBCuc6EtJzktGpfhqaGx6yPBUHgWiby/JwFsWMzSGUm39UxkARSNj/x0AJW","Y":"AbtoMEZ8kF4jc25HYb4VJHYlwU3Pv9BEzJyQMkngj+/z2WDcaVwwAo4zoUz0ls+wXeMoh3qWoM/d7jr61W3B1DjQ"}},"Signature":{"Bytes":"AY7MGR4IM+Qe6Z4wPXi3Ajna7u56B0iw77SZRrMkqPH5OfWAsT78nrWqpk1CbV63p9a+A0SKRs4/7RktyOrARRkDACBxFrlGbGtRrcH4TEvBPi6UR4L39TmBSelNKEQWA24ttGVwCH8NWYyTLFZmWlebx1owzboaXT0bp2ZVmyNb+Ch+"}}
                            X = Convert.FromBase64String("ADBRFw9bcnJGVJsrZmvG90YGv2iighBCuc6EtJzktGpfhqaGx6yPBUHgWiby/JwFsWMzSGUm39UxkARSNj/x0AJW"),
                            Y = Convert.FromBase64String("AbtoMEZ8kF4jc25HYb4VJHYlwU3Pv9BEzJyQMkngj+/z2WDcaVwwAo4zoUz0ls+wXeMoh3qWoM/d7jr61W3B1DjQ"),
                        }
                    }
                });
                context.AddReminder(TimeSpan.FromSeconds(5), "0");
            }
            else if (sender.AgentId == "Reminder" && message is string key)
            {
                context.SampleRestore(key);
            }
            else if (sender.AgentId == "Sample" && message is Tuple <string, object> data)
            {
                if (data.Item2 is string item)
                {
                    context.AddReminder(TimeSpan.FromSeconds(5), item);
                }
                else
                {
                    context.SampleStore(data.Item1, (int.Parse(data.Item1) + 1).ToString());
                    context.AddReminder(TimeSpan.FromSeconds(5), "0");
                }
            }
            else if (sender.AgentId == "IpfsInput")
            {
                Console.WriteLine(message);
            }
            return(context);
        }
        public Task <AgentContext <OrganizationState> > Run(object state, AgentCapability self, object message)
        {
            var organizationState    = state as OrganizationState ?? new OrganizationState();
            var context              = new AgentContext <OrganizationState>(organizationState, self);
            var distributeCapability = context.IssueCapability(new[] { typeof(DistributeCapabilitiesMessage) });

            switch (message)
            {
            case AgentRootInitMessage _:

                var memberFactoryInitMessage = new InitWetonomyAgentMessage(distributeCapability);
                context.CreateAgent("memberAndGroupFactory", "MemberAndGroupFactoryAgent", memberFactoryInitMessage, null);
                break;

            case DistributeCapabilitiesMessage DistributeCapabilitiesMessage:
                context.State.AgentToCapabilities.Add(DistributeCapabilitiesMessage.Id, DistributeCapabilitiesMessage.AgentCapabilities);

                switch (DistributeCapabilitiesMessage.Id)
                {
                // -------- Members, Groups & Voting --------
                case "memberAndGroupFactory":
                    //var createFirstMemberMsg = new CreateGroupMessage("FirstGroup");
                    //var creatGroupMessage = DistributeCapabilitiesMessage.AgentCapabilities["MintTokenMessage"];
                    //context.SendMessage(creatGroupMessage, createFirstMemberMsg, null);

                    var creatMemberCap = DistributeCapabilitiesMessage.AgentCapabilities["CreateMemberMessage"];
                    for (int i = 0; i < 5; i++)
                    {
                        var createMemberMsg = new CreateMemberMessage("MemberDebt" + i);
                        context.SendMessage(creatMemberCap, createMemberMsg, null);
                    }


                    var votingInitMessage = new InitWetonomyAgentMessage(distributeCapability);
                    context.CreateAgent("centralVoting", "VotingAgent", votingInitMessage, null);
                    break;

                case "centralVoting":
                    //var votingInitMessage = new InitWetonomyAgentMessage(distributeCapability);
                    //context.CreateAgent("centralVoting", "VotingAgent", votingInitMessage, null);
                    var moneyTokenManagerInitMessage = new InitWetonomyAgentMessage(distributeCapability);
                    context.CreateAgent("moneyTokenManager", "TokenManagerAgent", moneyTokenManagerInitMessage, null);
                    break;

                // -------- Token Flow --------

                case "moneyTokenManager":
                    var mintCapability   = DistributeCapabilitiesMessage.AgentCapabilities["MintTokenMessage"];
                    var moneyTokenMinter = new TokenActionAgentInitMessage(
                        mintCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>(),
                        null,
                        new HashSet <string>()
                    {
                        "debtTokenBurner", "allowanceTokenBurner"
                    });
                    context.CreateAgent("moneyTokenMinter", "TokenMinterAgent", moneyTokenMinter, null);
                    break;

                case "moneyTokenMinter":

                    var cashTokenManagerInitMessage      = new InitWetonomyAgentMessage(distributeCapability);
                    var debtTokenManagerInitMessage      = new InitWetonomyAgentMessage(distributeCapability);
                    var allowanceTokenManagerInitMessage = new InitWetonomyAgentMessage(distributeCapability);

                    context.CreateAgent("cashTokenManager", "TokenManagerAgent", cashTokenManagerInitMessage, null);
                    context.CreateAgent("debtTokenManager", "TokenManagerAgent", debtTokenManagerInitMessage, null);
                    context.CreateAgent("allowanceTokenManager", "TokenManagerAgent", allowanceTokenManagerInitMessage, null);
                    break;

                case "cashTokenManager":
                    var splitCapability    = DistributeCapabilitiesMessage.AgentCapabilities["TransferTokenMessage"];
                    var tokenSplitterAgent = new TokenActionAgentInitMessage(
                        splitCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("cashTokenManager", typeof(TokensTransferedNotification)), new UniformSplitterStrategy() }
                    },
                        new List <IAgentTokenKey>()
                    {
                        new SingleAngentTokenKey("cashTokenBurnerForDebt"), new SingleAngentTokenKey("cashTokenBurnerForAllowance")
                    });
                    context.CreateAgent("cashTokenSplitter", "TokenSplitterAgent", tokenSplitterAgent, null);


                    var burnCapability         = DistributeCapabilitiesMessage.AgentCapabilities["BurnTokenMessage"];
                    var cashTokenBurnerForDebt = new TokenActionAgentInitMessage(
                        burnCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("cashTokenManager", typeof(TokensTransferedNotification)), new SelfBurnStrategy() }
                    });
                    context.CreateAgent("cashTokenBurnerForDebt", "TokenBurnerAgent", cashTokenBurnerForDebt, null);


                    var cashTokenBurnerForAllowance = new TokenActionAgentInitMessage(
                        burnCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("cashTokenManager", typeof(TokensTransferedNotification)), new SelfBurnStrategy() }
                    });
                    context.CreateAgent("cashTokenBurnerForAllowance", "TokenBurnerAgent", cashTokenBurnerForAllowance, null);
                    break;

                case "debtTokenManager":
                    var debtBurnCapability = DistributeCapabilitiesMessage.AgentCapabilities["BurnTokenMessage"];
                    var debtTokenBurner    = new TokenActionAgentInitMessage(
                        debtBurnCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("cashTokenBurnerForDebt", typeof(TokensBurnedTriggerer)), new SequentialBurnStrategy() }
                    },
                        null,
                        new HashSet <string>()
                    {
                        "cashTokenBurnerForDebt", "debtTokenMinter"
                    });
                    context.CreateAgent("debtTokenBurner", "TokenBurnerAgent", debtTokenBurner, null);

                    var debtMintCapability = DistributeCapabilitiesMessage.AgentCapabilities["MintTokenMessage"];
                    var debtTokenMinter    = new TokenActionAgentInitMessage(
                        debtMintCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("AgentRoot", typeof(MockMessageTrigger)), new MintForAgentOnBurnerStrategy() }
                    });
                    context.CreateAgent("debtTokenMinter", "TokenMinterAgent", debtTokenMinter, null);
                    break;

                case "allowanceTokenManager":

                    //Should add minter
                    var allowanceBurnCapability = DistributeCapabilitiesMessage.AgentCapabilities["BurnTokenMessage"];
                    var allowanceTokenBurner    = new TokenActionAgentInitMessage(
                        allowanceBurnCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        //{ new AgentTriggerPair("cashTokenBurnerForAllowance" ,typeof(TokensBurnedTriggerer)), new SequentialBurnStrategy()}
                    },
                        null,
                        new HashSet <string>()
                    {
                        "cashTokenBurnerForAllowance"
                    });
                    context.CreateAgent("allowanceTokenBurner", "TokenBurnerAgent", allowanceTokenBurner, null);
                    break;

                case "debtTokenMinter":
                    var debtTokenMinterCapability = new AgentCapability("debtTokenMinter", typeof(MockMessageTrigger));

                    for (int i = 0; i < 5; i++)
                    {
                        var mintTokenMessage = new MockMessageTrigger("AgentRoot", 100, new SingleAngentTokenKey("MemberDebt" + i));
                        context.SendMessage(debtTokenMinterCapability, mintTokenMessage, null);
                    }
                    break;

                case "debtTokenBurner":

                    var cap1            = context.State.AgentToCapabilities["moneyTokenMinter"]["AddTriggerToActionMessage"];
                    var debtBurnTrigger = new AddTriggerToActionMessage(new AgentTriggerPair("debtTokenBurner", typeof(TokensBurnedTriggerer)), new SingleMintAfterBurnStrategy());
                    context.SendMessage(cap1, debtBurnTrigger, null);
                    break;

                case "allowanceTokenBurner":

                    var cap2 = context.State.AgentToCapabilities["moneyTokenMinter"]["AddTriggerToActionMessage"];
                    var allowanceBurnTrigger = new AddTriggerToActionMessage(new AgentTriggerPair("allowanceTokenBurner", typeof(TokensBurnedTriggerer)), new SingleMintAfterBurnStrategy());
                    context.SendMessage(cap2, allowanceBurnTrigger, null);

                    // Imitate user action
                    var mintSingleKey = new SingleAngentTokenKey(self.Issuer);
                    context.AddReminder(TimeSpan.FromSeconds(5), new MintTokenMessage(100, mintSingleKey));
                    break;
                }
                break;

            // Imitate user action
            case MintTokenMessage mintCashToken:
                var cashTokenManager = context.State.AgentToCapabilities["cashTokenManager"]["MintTokenMessage"];
                context.SendMessage(cashTokenManager, mintCashToken, null);
                break;

            case TokensMintedNotification tokensMintedNotification:
                var transferCashTokenManager = context.State.AgentToCapabilities["cashTokenManager"]["TransferTokenMessage"];
                context.SendMessage(transferCashTokenManager, new TransferTokenMessage(100, new SingleAngentTokenKey(self.Issuer), new SingleAngentTokenKey("cashTokenSplitter")), null);
                break;
            }
            return(Task.FromResult(context));
        }
Exemple #3
0
        public Task <AgentContext <VotingState> > Run(object state, AgentCapability self, object message)
        {
            var context = new AgentContext <VotingState>(state as VotingState, self);

            switch (message)
            {
            case InitWetonomyAgentMessage initMsg:
                var distributeCapabilityMessage = new DistributeCapabilitiesMessage
                {
                    Id = self.Issuer,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { "AddVoteMessage", context.IssueCapability(new[] { typeof(AddVoteMessage <T>) }) },
                        { "AddDecisionMessage", context.IssueCapability(new[] { typeof(AddDecisionMessage) }) },
                        //{"ForwardMessage", context.IssueCapability(new[]{ typeof(ForwardMessage) })}
                    }
                };
                context.SendMessage(initMsg.CreatorAgentCapability, distributeCapabilityMessage, null);
                break;

            case AddVoteMessage <V> addVoteMessage:
                context.State.DecisionsVotes[addVoteMessage.DecisionId].Add(addVoteMessage.Sender, addVoteMessage.Vote);
                context.MakePublication(new NewVotePublication <V>(addVoteMessage.DecisionId, addVoteMessage.Vote));
                break;

            case AddDecisionMessage addDecisionMessage:
                var decision = new Decision <T>(
                    context.State.nonce.ToString(),
                    addDecisionMessage.Executable,
                    addDecisionMessage.ActionMessage,
                    addDecisionMessage.Start,
                    addDecisionMessage.Finale);

                context.State.Decisions.Add(context.State.nonce.ToString(), decision);
                context.State.DecisionsVotes.Add(context.State.nonce.ToString(), new Dictionary <string, V>());

                context.MakePublication(
                    new NewDecisionPublication(context.State.nonce.ToString(), addDecisionMessage.ActionMessage)
                    );

                context.AddReminder(decision.Finale - DateTime.Now, new FinalizeDecision(context.State.nonce.ToString()));
                //Just Temporary solution
                context.State.nonce++;
                break;

            case FinalizeDecision finalizeDecisionMessage:
                var             dec   = context.State.Decisions[finalizeDecisionMessage.DecisionId];
                IEnumerable <V> votes =
                    context.State.DecisionsVotes[finalizeDecisionMessage.DecisionId]
                    .Select(pair => pair.Value);

                T decisionEvaluation = context.State.VotingStategy.MakeDecision(votes);

                dec.Evaluation = decisionEvaluation;
                dec.State      = DecisionState.Finalized;


                if (dec.Executable)
                {
                    //Executes only if T is bool, not sure if we need execution when T isn't bool
                    if (decisionEvaluation is bool check && check)
                    {
                        // should discuss how we store Capabilities
                        context.ForwardMessage(null, dec.DecisionActionMessage, null);
                    }
                }
                break;
            }

            return(Task.FromResult(context));
        }