Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Fail.cs
2using System.Text;
3using System.Threading.Tasks;
5using Waher.Events;
11
13{
17 public class Fail : ContractAction
18 {
22 public Fail()
23 : base()
24 {
25 }
26
30 public override string LocalName => nameof(Fail);
31
36 public override IStateMachineNode Create()
37 {
38 return new Fail();
39 }
40
47 public override async Task Execute(EvaluationArguments Arguments, string Reason, string ContractId)
48 {
49 string TokenContractId = Arguments.Token?.CreationContract ?? Arguments.Machine.DefinitionContractId;
51 bool TerminateStateMachine;
52
53 if (string.IsNullOrEmpty(ContractId))
54 {
55 ContractId = TokenContractId;
56 TerminateStateMachine = true;
57 }
58 else
59 TerminateStateMachine = false;
60
61 KeyValuePair<Contract, IqResultEventArgs> P = await Arguments.Legal.GetContract(ContractId);
62 Contract = P.Key;
63 if (Contract is null)
64 {
65 if (string.IsNullOrEmpty(P.Value?.ErrorText))
66 throw new StateMachineException(Arguments.Machine, "Contract not found: " + ContractId);
67 else
68 {
69 throw new StateMachineException(Arguments.Machine, "Unable to get contract " + ContractId +
70 ": " + P.Value.ErrorText);
71 }
72 }
73
74 if (!TerminateStateMachine)
75 {
76 bool ReferencesTokenContract = false;
77 string ContractReferenceId = null;
78
79 if (!(Contract.Parameters is null))
80 {
81 foreach (Parameter P2 in Contract.Parameters)
82 {
83 if (P2 is ContractReferenceParameter ContractReference)
84 {
85 ContractReferenceId = ContractReference.Value;
86
87 if (ContractReferenceId == TokenContractId)
88 {
89 ReferencesTokenContract = true;
90 break;
91 }
92 }
93 }
94 }
95
96 if (!ReferencesTokenContract)
97 {
98 throw new GenericException("Contract does not reference the token creation contract, so the token state machine is prohibited from failing the contract.",
99 null, null, null, null, null, null, null,
100 new KeyValuePair<string, object>("ContractId", ContractId),
101 new KeyValuePair<string, object>("Reason", Reason),
102 new KeyValuePair<string, object>("TokenContractId", TokenContractId),
103 new KeyValuePair<string, object>("ContractReferenceId", ContractReferenceId));
104 }
105 }
106
107 Arguments.Profiler?.Event(Reason);
108
109 if (TerminateStateMachine)
110 await StateMachine.GoToState(string.Empty, Arguments);
111 else
112 {
113 XmppAddress ContractAddress = new XmppAddress(ContractId);
114
115 if (XmppServerModule.Legal?.IsComponentDomain(ContractAddress.Domain, true) ?? false)
116 {
117 await NeuroFeaturesProcessor.RejectContract(Contract,
118 ContractId == TokenContractId, Reason, true, Arguments.Legal,
119 Arguments.EDaler);
120 }
121 else
122 {
123 StringBuilder sb = new StringBuilder();
124
125 sb.Append("<failContract xmlns='");
127 sb.Append("' contractId='");
128 sb.Append(XML.Encode(ContractId));
129 sb.Append("' reason='");
130 sb.Append(XML.Encode(Reason));
131 sb.Append("'/>");
132
133 await Arguments.Legal.Server.SendMessage(string.Empty, string.Empty,
134 Arguments.Legal.MainDomain, new XmppAddress(ContractAddress.Domain),
135 string.Empty, sb.ToString());
136 }
137 }
138
139 if (TerminateStateMachine)
140 Arguments.Profiler?.Stop();
141 }
142 }
143}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
Generic exception, with meta-data for logging.
XmppAddress MainDomain
Main/principal domain address
Definition: Component.cs:87
bool IsComponentDomain(CaseInsensitiveString Domain, bool IncludeAlternativeDomains)
Checks if a domain is the component domain, or optionally, an alternative component domain.
Definition: Component.cs:124
XmppServer Server
XMPP Server.
Definition: Component.cs:97
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
override string ToString()
object.ToString()
Definition: XmppAddress.cs:190
CaseInsensitiveString Domain
Domain
Definition: XmppAddress.cs:97
static readonly XmppAddress Empty
Empty address.
Definition: XmppAddress.cs:31
Task< bool > SendMessage(string Type, string Id, string From, string To, string Language, string ContentXml)
Sends a Message stanza to a recipient.
Definition: XmppServer.cs:3862
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
CaseInsensitiveString CreationContract
ID of contract that details the creation of the token.
Definition: Token.cs:298
Fail the contract used to create the token, associated with the state-machine.
Definition: Fail.cs:18
Fail()
Fail the contract used to create the token, associated with the state-machine.
Definition: Fail.cs:22
override async Task Execute(EvaluationArguments Arguments, string Reason, string ContractId)
Evaluates the action node
Definition: Fail.cs:47
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: Fail.cs:36
Contains information required for evaluating script in a state-machine.
StateMachine Machine
Reference to state-machine definition.
Class representing a state machine.
Definition: StateMachine.cs:43
static async Task GoToState(string StateId, EvaluationArguments Arguments)
Goes to a new state.
CaseInsensitiveString DefinitionContractId
ID of Definition Contract
Definition: StateMachine.cs:82
Service Module hosting the XMPP broker and its components.
NamespaceSet
Namespace versions
Definition: NamespaceSet.cs:7