## 5 Consensus Mechanism
### Smart Contract  
A smart contract governs the collaboration between the agents, in order to successfully exchange information according to the rules defined above and to combine information from path-messages that have reached their maximum length at different agents. The smart contract facilitates three basic procedures for the protocol. * It defines when path-messages are created and also defines a random path of agents for each one of the path-messages. * It validates any reports of malicious agent behavior and blacklist related nodes
* Handles all path-messages that reach their maximum length at the same time-step and calculates the required metrics to detect any ongoing DDoS attacks  
Every agent is identified by a blockchain identity. The smart contract permits access to its endpoints to a predefined set of such identities that corresponds to the agents that take part in the protocol. The workflow is designed in such a way that minimizes the interaction of the agents with the smart contract, and thus reduces performance overhead and the transaction fees in case of a public blockchain. There are two different functions that an agent may interact with the smart contract through, which are:  
* **path_message_validation0**: path message transition validation
* **path_message_submission0**: submission of finalized path messages in order to conclude whether there is an ongoing attack or not  
All agents follow a specific workflow regarding the handling of path-messages. The agents exchange and update path-messages, as it has been described in Section 4, but at specific points and under specific circumstances they may be required to interact with the smart-contract, as well. The main points of the workflow are:  
* The smart contract triggers the generation of new path-messages and it also sets the random path to be followed for each one of these. A random approach has been opted for path construction, in order to prevent agents from choosing the next node on their own. In such a case, malicious collaborating nodes would be able to set up paths consisting exclusively of them. As the mechanism is based on the fact that each agent checks data submitted from the previous agents in the path, this would enable malicious nodes to submit invalid data, while going undetected. The path is predetermined from the generation of the path-message and known to all agents it passes through. The randomization process is not related with keeping the path secret but with forcing agents to adhere to paths that they cannot set on their own. At each time period the smart contract generates a number (equal to the number of path messages to be created) of seeds (random values). Each seed value is hashed and the modulo of the result against the number of nodes indicates the ID of the agent to initialize the path-message. \[seed_{0}=hash(seed)\] (13) \[n_{0}=seed_{0}\mod n\] (14)Consequently, the ID of the next node is indicated by rehashing the hashed value and calculating its modulo against the number of the nodes. \[seed_{t}=hash(seed_{t-1}) \tag{15}\]  
\[n_{t}=seed_{t}\mod n \tag{16}\]  
If the next node \(n_{t}\) happens to have already been included in the path or is temporarily blacklisted, then node \(n_{t-1}\) rehashes the value until an appropriate node is selected. * When an agent starts a new path-message it adds its own traffic metrics, adds the identity of its own and of the subsequent agent to the list and signs the updated state, before forwarding the information to the next node. * When an agent receives a path-message from a previous node it has to check the validity of the information within the path message. The agent is required to validate the last \(k\) state transitions with respect to the validity of the signatures, the validity of the transitions according to the rules defined above, and the validity of the previous node IDs according to initial seed value. * If everything is okay then the agent has to update the state of the path message with its traffic information and sign the new state.