From 5a868ca3c329aa1e8cddaca714c2f592900ab8ad Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 14 Feb 2024 09:49:32 -0300 Subject: [PATCH 01/12] feat: create reputationValue attribute --- src/main/java/reputation/node/models/Node.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index 68c204b..8859c24 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -78,6 +78,7 @@ public class Node implements NodeTypeService, ILedgerSubscriber { private boolean isAverageEvaluationZero = false; private boolean useCredibility; private boolean useLatestCredibility; + private double reputationValue; private NodeCredibility nodeCredibility; private CsvWriterService csvWriter; private String[] csvData = new String[6]; @@ -1240,4 +1241,12 @@ public boolean isUseLatestCredibility() { public void setUseLatestCredibility(boolean useLatestCredibility) { this.useLatestCredibility = useLatestCredibility; } + + public double getReputationValue() { + return reputationValue; + } + + public void setReputationValue(double reputationValue) { + this.reputationValue = reputationValue; + } } From efe8c6768916122a27d69fdd6f2e5114438cdfb6 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 14 Feb 2024 10:03:06 -0300 Subject: [PATCH 02/12] feat: implements CheckNodeReputationTask for disturbing nodes --- .../node/tasks/CheckNodeReputationTask.java | 61 ++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/main/java/reputation/node/tasks/CheckNodeReputationTask.java b/src/main/java/reputation/node/tasks/CheckNodeReputationTask.java index ffba96c..615974d 100644 --- a/src/main/java/reputation/node/tasks/CheckNodeReputationTask.java +++ b/src/main/java/reputation/node/tasks/CheckNodeReputationTask.java @@ -1,11 +1,68 @@ package reputation.node.tasks; +import dlt.client.tangle.hornet.model.transactions.Transaction; +import java.util.List; +import java.util.TimerTask; +import reputation.node.models.Node; +import reputation.node.reputation.IReputation; + /** * Task para verificar de tempos em tempos a reputação do próprio nó. * * @author Allan Capistrano * @version 1.0.0 */ -public class CheckNodeReputationTask { - // TODO: Ideia, no nó do tipo perturbador, quando ele alcançar uma boa reputação, mudar o seu comportamento aqui, através do método defineConduct(). +public class CheckNodeReputationTask extends TimerTask { // TODO: Renomear classe para ChangeDisturbingNodeBehaviorTask + + private static final double REPUTATION_THRESHOLD = 0.9; + + private final Node node; + private final IReputation reputation; + private boolean changeBehaviorFlag = true; + + /** + * Método construtor. + * @param node Node - O nó que verificará a própria reputação. + * @param reputation IReputation - Objeto para calcular a reputação. + */ + public CheckNodeReputationTask(Node node, IReputation reputation) { + this.node = node; + this.reputation = reputation; + } + + @Override + public void run() { + /* Somente se um nó do tipo perturbador. */ + if ( + this.node.getNodeType() + .getNode() + .getConductType() + .toString() + .equals("DISTURBING") + ) { + List evaluationTransactions = + this.node.getLedgerConnector() + .getLedgerReader() + .getTransactionsByIndex(this.node.getNodeType().getNodeId(), false); + + double reputationValue = + this.reputation.calculate( + evaluationTransactions, + this.node.isUseLatestCredibility() + ); + + if (this.changeBehaviorFlag && reputationValue > REPUTATION_THRESHOLD) { + this.node.getNodeType().getNode().defineConduct(); + this.changeBehaviorFlag = false; + } + + /** + * Quando o nó alcançar uma reputação negativa, ele está habilitado a + * poder alterar novamente seu comportamento. + */ + if (reputationValue <= 0) { + this.changeBehaviorFlag = true; + } + } + } } From 869437bd64030f98fe5acd0d87200ef768d92f3a Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 14 Feb 2024 10:04:05 -0300 Subject: [PATCH 03/12] chore: renaming class --- ...utationTask.java => ChangeDisturbingNodeBehaviorTask.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/main/java/reputation/node/tasks/{CheckNodeReputationTask.java => ChangeDisturbingNodeBehaviorTask.java} (89%) diff --git a/src/main/java/reputation/node/tasks/CheckNodeReputationTask.java b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java similarity index 89% rename from src/main/java/reputation/node/tasks/CheckNodeReputationTask.java rename to src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java index 615974d..4d0221c 100644 --- a/src/main/java/reputation/node/tasks/CheckNodeReputationTask.java +++ b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java @@ -12,7 +12,7 @@ * @author Allan Capistrano * @version 1.0.0 */ -public class CheckNodeReputationTask extends TimerTask { // TODO: Renomear classe para ChangeDisturbingNodeBehaviorTask +public class ChangeDisturbingNodeBehaviorTask extends TimerTask { // TODO: Renomear classe para ChangeDisturbingNodeBehaviorTask private static final double REPUTATION_THRESHOLD = 0.9; @@ -25,7 +25,7 @@ public class CheckNodeReputationTask extends TimerTask { // TODO: Renomear class * @param node Node - O nó que verificará a própria reputação. * @param reputation IReputation - Objeto para calcular a reputação. */ - public CheckNodeReputationTask(Node node, IReputation reputation) { + public ChangeDisturbingNodeBehaviorTask(Node node, IReputation reputation) { this.node = node; this.reputation = reputation; } From 186eba2fb0ed727355bfdebe705d3a3e84cad4fc Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 14 Feb 2024 10:04:50 -0300 Subject: [PATCH 04/12] docs: improving class comment --- .../reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java index 4d0221c..fbd05fd 100644 --- a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java +++ b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java @@ -7,7 +7,7 @@ import reputation.node.reputation.IReputation; /** - * Task para verificar de tempos em tempos a reputação do próprio nó. + * Task para alterar o comportamento de um nó do tipo Perturbador. * * @author Allan Capistrano * @version 1.0.0 From dba634da95ef80c783ecf779a1e67cc92fd723d4 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 14 Feb 2024 10:07:27 -0300 Subject: [PATCH 05/12] feat: add log message --- src/main/java/reputation/node/models/Node.java | 2 +- .../tasks/ChangeDisturbingNodeBehaviorTask.java | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index 8859c24..bab669d 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -985,7 +985,7 @@ private void changeMaliciousNodeBehavior() { logger.info( String.format( - "Changing Malicious behavior to '%s'.", + "Changing Malicious node behavior to '%s'.", this.getNodeType().getNode().getConductType().toString() ) ); diff --git a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java index fbd05fd..8af5531 100644 --- a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java +++ b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java @@ -3,6 +3,7 @@ import dlt.client.tangle.hornet.model.transactions.Transaction; import java.util.List; import java.util.TimerTask; +import java.util.logging.Logger; import reputation.node.models.Node; import reputation.node.reputation.IReputation; @@ -12,7 +13,7 @@ * @author Allan Capistrano * @version 1.0.0 */ -public class ChangeDisturbingNodeBehaviorTask extends TimerTask { // TODO: Renomear classe para ChangeDisturbingNodeBehaviorTask +public class ChangeDisturbingNodeBehaviorTask extends TimerTask { private static final double REPUTATION_THRESHOLD = 0.9; @@ -20,6 +21,10 @@ public class ChangeDisturbingNodeBehaviorTask extends TimerTask { // TODO: Renom private final IReputation reputation; private boolean changeBehaviorFlag = true; + private static final Logger logger = Logger.getLogger( + ChangeDisturbingNodeBehaviorTask.class.getName() + ); + /** * Método construtor. * @param node Node - O nó que verificará a própria reputação. @@ -54,10 +59,17 @@ public void run() { if (this.changeBehaviorFlag && reputationValue > REPUTATION_THRESHOLD) { this.node.getNodeType().getNode().defineConduct(); this.changeBehaviorFlag = false; + + logger.info( + String.format( + "Changing Disturbing node behavior to '%s'.", + this.node.getNodeType().getNode().getConductType().toString() + ) + ); } /** - * Quando o nó alcançar uma reputação negativa, ele está habilitado a + * Quando o nó alcançar uma reputação negativa, ele está habilitado a * poder alterar novamente seu comportamento. */ if (reputationValue <= 0) { From ef6fff911d431e9cc7509c8b4d19e04d83b23129 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 14 Feb 2024 10:13:30 -0300 Subject: [PATCH 06/12] feat: create changeDisturbingNodeBehaviorTaskTime property --- src/main/resources/OSGI-INF/blueprint/blueprint.xml | 2 ++ src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 21fb60d..dc7774f 100644 --- a/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -37,6 +37,7 @@ + @@ -68,6 +69,7 @@ + diff --git a/src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg b/src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg index 770ba8f..d6050b7 100644 --- a/src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg +++ b/src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg @@ -16,6 +16,8 @@ checkNodesServicesTaskTime=45 # Tempo máximo (segundos) de espera da resposta do nós para a requisição de pretação de serviço. # Obs: Tem que ser menor do que o 'checkNodesServicesTaskTime'. waitNodesResponsesTaskTime=30 +# Tempo (segundos) para verificar a reputação e alterar o comportamento do nó do tipo Perbubador. +changeDisturbingNodeBehaviorTaskTime=30 # Determina se deseja usar (true) ou não (false) a credibilidade no sistema. useCredibility=true # Determina se é para usar (true) ou não (false) a credibilidade mais recente para o cálculo da reputação. From 431a5f27bae5592f055e5e4c1511003a52348e4a Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 14 Feb 2024 10:15:22 -0300 Subject: [PATCH 07/12] feat: using ChangeDisturbingNodeBehaviorTask --- .../java/reputation/node/models/Node.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index bab669d..535a047 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -40,6 +40,7 @@ import reputation.node.reputation.credibility.NodeCredibility; import reputation.node.services.NodeTypeService; import reputation.node.tangle.LedgerConnector; +import reputation.node.tasks.ChangeDisturbingNodeBehaviorTask; import reputation.node.tasks.CheckDevicesTask; import reputation.node.tasks.CheckNodesServicesTask; import reputation.node.tasks.RequestDataTask; @@ -63,6 +64,7 @@ public class Node implements NodeTypeService, ILedgerSubscriber { private int waitDeviceResponseTaskTime; private int checkNodesServicesTaskTime; private int waitNodesResponsesTaskTime; + private int changeDisturbingNodeBehaviorTaskTime; private List devices; private List nodesWithServices; private LedgerConnector ledgerConnector; @@ -689,6 +691,19 @@ private void createTasks() { 0, this.checkNodesServicesTaskTime * 1000 ); + new Timer() + .scheduleAtFixedRate( + new ChangeDisturbingNodeBehaviorTask( + this, + new ReputationUsingKMeans( + this.kMeans, + this.nodeCredibility, + this.getNodeType().getNodeId() + ) + ), + 0, + this.changeDisturbingNodeBehaviorTaskTime * 1000 + ); } /** @@ -1249,4 +1264,15 @@ public double getReputationValue() { public void setReputationValue(double reputationValue) { this.reputationValue = reputationValue; } + + public int getChangeDisturbingNodeBehaviorTaskTime() { + return changeDisturbingNodeBehaviorTaskTime; + } + + public void setChangeDisturbingNodeBehaviorTaskTime( + int changeDisturbingNodeBehaviorTaskTime + ) { + this.changeDisturbingNodeBehaviorTaskTime = + changeDisturbingNodeBehaviorTaskTime; + } } From 6b8f2cc7333a9a65e214cc70251418933b7a9084 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sat, 24 Feb 2024 15:34:18 -0300 Subject: [PATCH 08/12] chore: passing useCredibility to calculate reputation method --- .../node/tasks/ChangeDisturbingNodeBehaviorTask.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java index 8af5531..08394c6 100644 --- a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java +++ b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java @@ -53,7 +53,8 @@ public void run() { double reputationValue = this.reputation.calculate( evaluationTransactions, - this.node.isUseLatestCredibility() + this.node.isUseLatestCredibility(), + this.node.isUseCredibility() ); if (this.changeBehaviorFlag && reputationValue > REPUTATION_THRESHOLD) { From 06e9984afd654f66e1cc3ca695a391620352fdcd Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sat, 24 Feb 2024 16:26:56 -0300 Subject: [PATCH 09/12] fix: checking node type instead of conduct type --- .../node/tasks/ChangeDisturbingNodeBehaviorTask.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java index 08394c6..7487d9a 100644 --- a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java +++ b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java @@ -38,13 +38,7 @@ public ChangeDisturbingNodeBehaviorTask(Node node, IReputation reputation) { @Override public void run() { /* Somente se um nó do tipo perturbador. */ - if ( - this.node.getNodeType() - .getNode() - .getConductType() - .toString() - .equals("DISTURBING") - ) { + if (this.node.getNodeType().getType().toString().equals("DISTURBING")) { List evaluationTransactions = this.node.getLedgerConnector() .getLedgerReader() From 1ca6df4ae1b8e1a87b0c69fe08ec0397a473b952 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sat, 24 Feb 2024 16:36:35 -0300 Subject: [PATCH 10/12] chore: removing node type check --- .../ChangeDisturbingNodeBehaviorTask.java | 52 ++++++++----------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java index 7487d9a..ab137a0 100644 --- a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java +++ b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java @@ -37,39 +37,33 @@ public ChangeDisturbingNodeBehaviorTask(Node node, IReputation reputation) { @Override public void run() { - /* Somente se um nó do tipo perturbador. */ - if (this.node.getNodeType().getType().toString().equals("DISTURBING")) { - List evaluationTransactions = - this.node.getLedgerConnector() - .getLedgerReader() - .getTransactionsByIndex(this.node.getNodeType().getNodeId(), false); + List evaluationTransactions = + this.node.getLedgerConnector() + .getLedgerReader() + .getTransactionsByIndex(this.node.getNodeType().getNodeId(), false); - double reputationValue = - this.reputation.calculate( - evaluationTransactions, - this.node.isUseLatestCredibility(), - this.node.isUseCredibility() - ); + double reputationValue = + this.reputation.calculate( + evaluationTransactions, + this.node.isUseLatestCredibility(), + this.node.isUseCredibility() + ); - if (this.changeBehaviorFlag && reputationValue > REPUTATION_THRESHOLD) { - this.node.getNodeType().getNode().defineConduct(); - this.changeBehaviorFlag = false; + logger.info( + String.format("Disturbing node reputation: %f", reputationValue) + ); - logger.info( - String.format( - "Changing Disturbing node behavior to '%s'.", - this.node.getNodeType().getNode().getConductType().toString() - ) - ); - } + if (this.changeBehaviorFlag && reputationValue > REPUTATION_THRESHOLD) { + this.node.getNodeType().getNode().defineConduct(); + this.changeBehaviorFlag = false; + } - /** - * Quando o nó alcançar uma reputação negativa, ele está habilitado a - * poder alterar novamente seu comportamento. - */ - if (reputationValue <= 0) { - this.changeBehaviorFlag = true; - } + /** + * Quando o nó alcançar uma reputação negativa, ele está habilitado a + * poder alterar novamente seu comportamento. + */ + if (reputationValue <= 0) { + this.changeBehaviorFlag = true; } } } From daabaec589cbfe93cf100329e848bafafee7e1e0 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sat, 24 Feb 2024 16:36:43 -0300 Subject: [PATCH 11/12] feat: adding node type checking --- .../java/reputation/node/models/Node.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index fff086f..203650e 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -693,19 +693,22 @@ private void createTasks() { 0, this.checkNodesServicesTaskTime * 1000 ); - new Timer() - .scheduleAtFixedRate( - new ChangeDisturbingNodeBehaviorTask( - this, - new ReputationUsingKMeans( - this.kMeans, - this.nodeCredibility, - this.getNodeType().getNodeId() - ) - ), - 0, - this.changeDisturbingNodeBehaviorTaskTime * 1000 - ); + /* Somente se um nó do tipo perturbador. */ + if (this.getNodeType().getType().toString().equals("DISTURBING")) { + new Timer() + .scheduleAtFixedRate( + new ChangeDisturbingNodeBehaviorTask( + this, + new ReputationUsingKMeans( + this.kMeans, + this.nodeCredibility, + this.getNodeType().getNodeId() + ) + ), + 0, + this.changeDisturbingNodeBehaviorTaskTime * 1000 + ); + } } /** From 1056d315dd05d5f364f3f25475196dd64a9eac9e Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sun, 25 Feb 2024 15:22:17 -0300 Subject: [PATCH 12/12] fix: changeMaliciousNodeBehavior method using node type instead of conduct type --- src/main/java/reputation/node/models/Node.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index 203650e..bdd1d9a 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -994,13 +994,7 @@ private int getLastEvaluation( * Altera o comportamento do nó, caso seja um nó malicioso. */ private void changeMaliciousNodeBehavior() { - if ( - this.getNodeType() - .getNode() - .getConductType() - .toString() - .equals("MALICIOUS") - ) { + if (this.getNodeType().getType().toString().equals("MALICIOUS")) { this.getNodeType().getNode().defineConduct(); logger.info(