Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/disturbing behavior #13

Merged
merged 13 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions src/main/java/reputation/node/models/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -63,6 +64,7 @@ public class Node implements NodeTypeService, ILedgerSubscriber {
private int waitDeviceResponseTaskTime;
private int checkNodesServicesTaskTime;
private int waitNodesResponsesTaskTime;
private int changeDisturbingNodeBehaviorTaskTime;
private List<Device> devices;
private List<Transaction> nodesWithServices;
private LedgerConnector ledgerConnector;
Expand All @@ -78,6 +80,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];
Expand Down Expand Up @@ -690,6 +693,22 @@ private void createTasks() {
0,
this.checkNodesServicesTaskTime * 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
);
}
}

/**
Expand Down Expand Up @@ -975,18 +994,12 @@ 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(
String.format(
"Changing Malicious behavior to '%s'.",
"Changing Malicious node behavior to '%s'.",
this.getNodeType().getNode().getConductType().toString()
)
);
Expand Down Expand Up @@ -1242,4 +1255,23 @@ public boolean isUseLatestCredibility() {
public void setUseLatestCredibility(boolean useLatestCredibility) {
this.useLatestCredibility = useLatestCredibility;
}

public double getReputationValue() {
return reputationValue;
}

public void setReputationValue(double reputationValue) {
this.reputationValue = reputationValue;
}

public int getChangeDisturbingNodeBehaviorTaskTime() {
return changeDisturbingNodeBehaviorTaskTime;
}

public void setChangeDisturbingNodeBehaviorTaskTime(
int changeDisturbingNodeBehaviorTaskTime
) {
this.changeDisturbingNodeBehaviorTaskTime =
changeDisturbingNodeBehaviorTaskTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package reputation.node.tasks;

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;

/**
* Task para alterar o comportamento de um nó do tipo Perturbador.
*
* @author Allan Capistrano
* @version 1.0.0
*/
public class ChangeDisturbingNodeBehaviorTask extends TimerTask {

private static final double REPUTATION_THRESHOLD = 0.9;

private final Node node;
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.
* @param reputation IReputation - Objeto para calcular a reputação.
*/
public ChangeDisturbingNodeBehaviorTask(Node node, IReputation reputation) {
this.node = node;
this.reputation = reputation;
}

@Override
public void run() {
List<Transaction> evaluationTransactions =
this.node.getLedgerConnector()
.getLedgerReader()
.getTransactionsByIndex(this.node.getNodeType().getNodeId(), false);

double reputationValue =
this.reputation.calculate(
evaluationTransactions,
this.node.isUseLatestCredibility(),
this.node.isUseCredibility()
);

logger.info(
String.format("Disturbing node reputation: %f", reputationValue)
);

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;
}
}
}
11 changes: 0 additions & 11 deletions src/main/java/reputation/node/tasks/CheckNodeReputationTask.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/resources/OSGI-INF/blueprint/blueprint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<property name="waitDeviceResponseTaskTime" value="${waitDeviceResponseTaskTime}" />
<property name="checkNodesServicesTaskTime" value="${checkNodesServicesTaskTime}" />
<property name="waitNodesResponsesTaskTime" value="${waitNodesResponsesTaskTime}" />
<property name="changeDisturbingNodeBehaviorTaskTime" value="${changeDisturbingNodeBehaviorTaskTime}" />
<property name="deviceManager" ref="deviceManagerService" />
<property name="ledgerConnector" ref="ledgerConnector" />
<property name="useCredibility" value="${useCredibility}" />
Expand Down Expand Up @@ -68,6 +69,7 @@
<cm:property name="waitDeviceResponseTaskTime" value="10" />
<cm:property name="checkNodesServicesTaskTime" value="45" />
<cm:property name="waitNodesResponsesTaskTime" value="30" />
<cm:property name="changeDisturbingNodeBehaviorTaskTime" value="30" />
<cm:property name="useCredibility" value="true" />
<cm:property name="useLatestCredibility" value="true" />
<cm:property name="debugModeValue" value="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down