Skip to content

Commit 4a55c4e

Browse files
authored
Use CommandArgs instead of split parameters in methods arguments (#172)
1 parent 0aed8ca commit 4a55c4e

File tree

7 files changed

+63
-67
lines changed

7 files changed

+63
-67
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
1414
- Use no more `iexec-commons-poco` deprecated code in integration tests. (#163)
1515
- Fix Spring Security deprecations after Spring Boot 3.3.8 upgrade. (#169)
1616
- Harmonize YML internal variables to proper case. (#171)
17+
- Use `CommandArgs` instead of split parameters in methods arguments. (#172)
1718

1819
### Breaking API changes
1920

src/main/java/com/iexec/blockchain/command/generic/CommandEngine.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,22 @@ protected CommandEngine(
5050
* @return blockchain object ID if successful
5151
*/
5252
public String startBlockchainCommand(final A args, final boolean isPriority) {
53-
final String chainObjectId = args.getChainObjectId();
54-
final String messageDetails = String.format("[chainObjectId:%s, commandArgs:%s]", chainObjectId, args);
53+
final String messageDetails = String.format("chainObjectId:%s, commandArgs:%s", args.getChainObjectId(), args);
5554
if (!blockchainService.canSendBlockchainCommand(args)) {
56-
log.error("Starting blockchain command failed (failing on-chain checks) {}", messageDetails);
55+
log.error("Starting blockchain command failed (failing on-chain checks) [{}]", messageDetails);
5756
return "";
5857
}
5958

6059
if (!updaterService.updateToReceived(args)) {
61-
log.error("Starting blockchain command failed (failing update to received) {}", messageDetails);
60+
log.error("Starting blockchain command failed (failing update to received) [{}]", messageDetails);
6261
return "";
6362
}
6463
log.info("Received command {}", messageDetails);
6564

6665
final Runnable runnable = () -> triggerBlockchainCommand(args);
6766
queueService.addExecutionToQueue(runnable, isPriority);
6867

69-
return chainObjectId;
68+
return args.getChainObjectId();
7069
}
7170

7271
/**
@@ -77,33 +76,29 @@ public String startBlockchainCommand(final A args, final boolean isPriority) {
7776
* @param args input arguments for the blockchain command
7877
*/
7978
public void triggerBlockchainCommand(final A args) {
80-
String chainObjectId = args.getChainObjectId();
81-
if (!updaterService.updateToProcessing(chainObjectId, args.getCommandName())) {
82-
log.error("Triggering blockchain command failed (failing update" +
83-
" to processing) [chainObjectId:{}, commandArgs:{}]",
84-
chainObjectId, args);
79+
final String messageDetails = String.format("chainObjectId:%s, commandArgs:%s", args.getChainObjectId(), args);
80+
if (!updaterService.updateToProcessing(args)) {
81+
log.error("Triggering blockchain command failed (failing update to processing) [{}]", messageDetails);
8582
return;
8683
}
8784
int attempt = 0;
88-
log.info("Processing command [chainObjectId:{}, commandArgs:{}]",
89-
chainObjectId, args);
85+
log.info("Processing command [{}]", messageDetails);
9086
TransactionReceipt receipt = null;
9187
while (attempt < MAX_ATTEMPTS && receipt == null) {
9288
attempt++;
9389
try {
9490
receipt = blockchainService.sendBlockchainCommand(args);
9591
} catch (Exception e) {
96-
log.error("Something wrong happened while triggering command [chainObjectId:{}, commandArgs:{}, attempt:{}]",
97-
chainObjectId, args, attempt, e);
92+
log.error("Something wrong happened while triggering command [{}, attempt:{}]",
93+
messageDetails, attempt, e);
9894
}
9995
}
10096
if (receipt == null) {
10197
log.error("Triggering blockchain command failed " +
102-
"(received null receipt after blockchain send) " +
103-
"[chainObjectId:{}, commandArgs:{}, attempt:{}]",
104-
chainObjectId, args, attempt);
98+
"(received null receipt after blockchain send) [{}, attempt:{}]",
99+
messageDetails, attempt);
105100
}
106-
updaterService.updateToFinal(chainObjectId, args.getCommandName(), receipt);
101+
updaterService.updateToFinal(args, receipt);
107102
}
108103

109104
/**

src/main/java/com/iexec/blockchain/command/generic/CommandStorage.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ public CommandStorage(final MongoTemplate mongoTemplate) {
4141
}
4242

4343
/**
44-
* Locally set status to received and store blockchain command arguments for
45-
* future use.
44+
* Locally set status to received and store blockchain command arguments for future use.
4645
*
4746
* @param args input arguments for the blockchain command
48-
* @return true on successful update
47+
* @return true on successful update, false otherwise
4948
*/
5049
public boolean updateToReceived(final CommandArgs args) {
5150
final Command command = new Command();
@@ -66,14 +65,13 @@ public boolean updateToReceived(final CommandArgs args) {
6665
}
6766

6867
/**
69-
* Locally set status to processing just before sending the blockchain command
68+
* Locally set status to processing just before sending the blockchain command.
7069
*
71-
* @param chainObjectId blockchain object ID on which the blockchain command
72-
* is performed
73-
* @return true on successful update
70+
* @param args Command arguments containing on-chain object ID and command to perform
71+
* @return true on successful update, false otherwise
7472
*/
75-
public boolean updateToProcessing(final String chainObjectId, final CommandName commandName) {
76-
final Criteria criteria = createUpdateCriteria(chainObjectId, commandName, CommandStatus.RECEIVED);
73+
public boolean updateToProcessing(final CommandArgs args) {
74+
final Criteria criteria = createUpdateCriteria(args, CommandStatus.RECEIVED);
7775
final Update update = new Update();
7876
update.set(STATUS_FIELD_NAME, CommandStatus.PROCESSING);
7977
update.set("processingDate", Instant.now());
@@ -82,20 +80,17 @@ public boolean updateToProcessing(final String chainObjectId, final CommandName
8280
}
8381

8482
/**
85-
* Locally set status both to success or failure, when blockchain command
86-
* is completed.
83+
* Locally set status both to success or failure, when blockchain command is completed.
8784
*
88-
* @param chainObjectId blockchain object ID on which the blockchain command
89-
* is performed
90-
* @param receipt blockchain receipt
85+
* @param args Command arguments containing on-chain object ID and command to perform
86+
* @param receipt blockchain receipt
87+
* @return true on successful update, false otherwise
9188
*/
92-
public boolean updateToFinal(final String chainObjectId,
93-
final CommandName commandName,
94-
final TransactionReceipt receipt) {
89+
public boolean updateToFinal(final CommandArgs args, final TransactionReceipt receipt) {
9590
final CommandStatus finalStatus = receipt != null && receipt.isStatusOK() ? CommandStatus.SUCCESS : CommandStatus.FAILURE;
96-
log.info("Command final status with transaction receipt [chainObjectId]:{}, command:{}, status:{}, receipt:{}]",
97-
chainObjectId, commandName.name(), finalStatus, receipt);
98-
final Criteria criteria = createUpdateCriteria(chainObjectId, commandName, CommandStatus.PROCESSING);
91+
log.info("Command final status with transaction receipt [chainObjectId:{}, command:{}, status:{}, receipt:{}]",
92+
args.getChainObjectId(), args.getCommandName().name(), finalStatus, receipt);
93+
final Criteria criteria = createUpdateCriteria(args, CommandStatus.PROCESSING);
9994
final Update update = new Update();
10095
update.set(STATUS_FIELD_NAME, finalStatus);
10196
update.set("transactionReceipt", receipt);
@@ -107,14 +102,13 @@ public boolean updateToFinal(final String chainObjectId,
107102
/**
108103
* Creates a criteria, the rule to lookup for a specific entry in the Mongo collection.
109104
*
110-
* @param chainObjectId On-chain ID of the object to look for in collection
111-
* @param commandName Name of the command applied to the on-chain object
112-
* @param status Currently expected status for the on-chain object
105+
* @param args Command arguments containing on-chain object ID and command to perform
106+
* @param status Currently expected status for the on-chain object
113107
* @return A {@code Criteria} instance to use in {@code MongoTemplate} operations
114108
*/
115-
private Criteria createUpdateCriteria(final String chainObjectId, final CommandName commandName, final CommandStatus status) {
116-
return Criteria.where("chainObjectId").is(chainObjectId)
117-
.and("commandName").is(commandName)
109+
private Criteria createUpdateCriteria(final CommandArgs args, final CommandStatus status) {
110+
return Criteria.where("chainObjectId").is(args.getChainObjectId())
111+
.and("commandName").is(args.getCommandName())
118112
.and(STATUS_FIELD_NAME).is(status);
119113
}
120114

src/test/java/com/iexec/blockchain/chain/ChainConfigTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package com.iexec.blockchain.chain;
1818

19-
import jakarta.validation.*;
19+
import jakarta.validation.ConstraintViolation;
20+
import jakarta.validation.Validation;
21+
import jakarta.validation.ValidatorFactory;
2022
import org.junit.jupiter.params.ParameterizedTest;
2123
import org.junit.jupiter.params.provider.Arguments;
2224
import org.junit.jupiter.params.provider.MethodSource;
@@ -25,7 +27,7 @@
2527
import java.util.Set;
2628
import java.util.stream.Stream;
2729

28-
import static org.assertj.core.api.Assertions.*;
30+
import static org.assertj.core.api.Assertions.assertThat;
2931

3032
class ChainConfigTest {
3133
private static final int DEFAULT_CHAIN_ID = 1;
@@ -109,9 +111,9 @@ void shouldNotValidateChainId(int chainId) {
109111
static Stream<Arguments> invalidNodeAddresses() {
110112
return Stream.of(
111113
Arguments.of(null, "Node address must not be empty"),
112-
Arguments.of ("", "Node address must not be empty"),
113-
Arguments.of ("12345", "Node address must be a valid URL"),
114-
Arguments.of ("0xBF6B2B07e47326B7c8bfCb4A5460bef9f0Fd2002", "Node address must be a valid URL")
114+
Arguments.of("", "Node address must not be empty"),
115+
Arguments.of("12345", "Node address must be a valid URL"),
116+
Arguments.of("0xBF6B2B07e47326B7c8bfCb4A5460bef9f0Fd2002", "Node address must be a valid URL")
115117
);
116118
}
117119

@@ -140,7 +142,7 @@ static Stream<Arguments> invalidBlockTimes() {
140142
Arguments.of(Duration.ofSeconds(0), "Block time must be greater than 100ms"),
141143
Arguments.of(Duration.ofSeconds(25), "Block time must be less than 20s"),
142144
Arguments.of(Duration.ofSeconds(-1), "Block time must be greater than 100ms"),
143-
Arguments.of(null, "Block time must not be null")
145+
Arguments.of(null, "Block time must not be null")
144146
);
145147
}
146148

src/test/java/com/iexec/blockchain/command/generic/CommandStorageTests.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,52 +84,56 @@ void shouldNotSetReceivedSinceAlreadyPresent() {
8484
void shouldSetProcessing() {
8585
final TaskInitializeArgs args = getArgs();
8686
Assertions.assertTrue(updaterService.updateToReceived(args));
87-
Assertions.assertTrue(updaterService.updateToProcessing(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE));
87+
Assertions.assertTrue(updaterService.updateToProcessing(args));
8888
final CommandStatus status = updaterService.getStatusForCommand(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE).orElseThrow();
8989
Assertions.assertEquals(CommandStatus.PROCESSING, status);
9090
}
9191

9292
@ParameterizedTest
9393
@EnumSource(value = CommandStatus.class, names = "RECEIVED", mode = EnumSource.Mode.EXCLUDE)
9494
void shouldNotSetProcessingSinceBadStatus(final CommandStatus status) {
95+
final TaskInitializeArgs args = getArgs();
9596
final Command command = createCommand(status);
9697
mongoTemplate.insert(command);
97-
Assertions.assertFalse(updaterService.updateToProcessing(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE));
98+
Assertions.assertFalse(updaterService.updateToProcessing(args));
9899
}
99100

100101
@Test
101102
void shouldSetFinalSuccess() {
103+
final TaskInitializeArgs args = getArgs();
102104
final TransactionReceipt receipt = new TransactionReceipt();
103105
receipt.setStatus("0x1");
104106
final Command command = createCommand(CommandStatus.PROCESSING);
105107
mongoTemplate.insert(command);
106108

107-
Assertions.assertTrue(updaterService.updateToFinal(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE, receipt));
109+
Assertions.assertTrue(updaterService.updateToFinal(args, receipt));
108110
final CommandStatus status = updaterService.getStatusForCommand(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE).orElseThrow();
109111
Assertions.assertEquals(CommandStatus.SUCCESS, status);
110112
}
111113

112114
@Test
113115
void shouldSetFinalFailure() {
116+
final TaskInitializeArgs args = getArgs();
114117
final TransactionReceipt receipt = new TransactionReceipt();
115118
receipt.setStatus("0x0");
116119
final Command command = createCommand(CommandStatus.PROCESSING);
117120
mongoTemplate.insert(command);
118121

119-
Assertions.assertTrue(updaterService.updateToFinal(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE, receipt));
122+
Assertions.assertTrue(updaterService.updateToFinal(args, receipt));
120123
final CommandStatus status = updaterService.getStatusForCommand(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE).orElseThrow();
121124
Assertions.assertEquals(CommandStatus.FAILURE, status);
122125
}
123126

124127
@ParameterizedTest
125128
@EnumSource(value = CommandStatus.class, names = "PROCESSING", mode = EnumSource.Mode.EXCLUDE)
126129
void shouldNotSetFinalSinceBadStatus(final CommandStatus status) {
130+
final TaskInitializeArgs args = getArgs();
127131
final TransactionReceipt receipt = new TransactionReceipt();
128132
final Command taskInitialize = new Command();
129133
taskInitialize.setStatus(status);
130134
mongoTemplate.insert(taskInitialize);
131135

132-
Assertions.assertFalse(updaterService.updateToFinal(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE, receipt));
136+
Assertions.assertFalse(updaterService.updateToFinal(args, receipt));
133137
}
134138

135139
private Command createCommand(final CommandStatus status) {

src/test/java/com/iexec/blockchain/command/task/finalize/TaskFinalizeServiceTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,30 @@ void shouldNotFinalizeTaskSinceCannotUpdate() {
106106
@Test
107107
void triggerFinalizeTask() throws Exception {
108108
final TransactionReceipt receipt = mock(TransactionReceipt.class);
109-
when(updaterService.updateToProcessing(CHAIN_TASK_ID, CommandName.TASK_FINALIZE)).thenReturn(true);
109+
when(updaterService.updateToProcessing(args)).thenReturn(true);
110110
when(blockchainService.sendBlockchainCommand(args)).thenReturn(receipt);
111111

112112
taskFinalizeService.triggerBlockchainCommand(args);
113-
verify(updaterService).updateToFinal(CHAIN_TASK_ID, CommandName.TASK_FINALIZE, receipt);
113+
verify(updaterService).updateToFinal(args, receipt);
114114
}
115115

116116
@Test
117117
void shouldNotTriggerFinalizeTaskSinceCannotUpdate() {
118118
final TransactionReceipt receipt = mock(TransactionReceipt.class);
119-
when(updaterService.updateToProcessing(CHAIN_TASK_ID, CommandName.TASK_FINALIZE)).thenReturn(false);
119+
when(updaterService.updateToProcessing(args)).thenReturn(false);
120120

121121
taskFinalizeService.triggerBlockchainCommand(args);
122-
verify(updaterService, never()).updateToFinal(CHAIN_TASK_ID, CommandName.TASK_FINALIZE, receipt);
122+
verify(updaterService, never()).updateToFinal(args, receipt);
123123
verifyNoInteractions(blockchainService);
124124
}
125125

126126
@Test
127127
void shouldNotTriggerFinalizeTaskSinceReceiptIsNull() throws Exception {
128-
when(updaterService.updateToProcessing(CHAIN_TASK_ID, CommandName.TASK_FINALIZE)).thenReturn(true);
128+
when(updaterService.updateToProcessing(args)).thenReturn(true);
129129
when(blockchainService.sendBlockchainCommand(args)).thenReturn(null);
130130

131131
taskFinalizeService.triggerBlockchainCommand(args);
132-
verify(updaterService).updateToFinal(CHAIN_TASK_ID, CommandName.TASK_FINALIZE, null);
132+
verify(updaterService).updateToFinal(args, null);
133133
}
134134
// endregion
135135

src/test/java/com/iexec/blockchain/command/task/initialize/TaskInitializeServiceTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,30 @@ void shouldNotInitializeTaskSinceCannotUpdate() {
108108
@Test
109109
void triggerInitializeTask() throws Exception {
110110
final TransactionReceipt receipt = mock(TransactionReceipt.class);
111-
when(updaterService.updateToProcessing(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE)).thenReturn(true);
111+
when(updaterService.updateToProcessing(args)).thenReturn(true);
112112
when(blockchainCheckerService.sendBlockchainCommand(args)).thenReturn(receipt);
113113

114114
taskInitializeService.triggerBlockchainCommand(args);
115-
verify(updaterService).updateToFinal(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE, receipt);
115+
verify(updaterService).updateToFinal(args, receipt);
116116
}
117117

118118
@Test
119119
void shouldNotTriggerInitializeTaskSinceCannotUpdate() {
120120
final TransactionReceipt receipt = mock(TransactionReceipt.class);
121-
when(updaterService.updateToProcessing(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE)).thenReturn(false);
121+
when(updaterService.updateToProcessing(args)).thenReturn(false);
122122

123123
taskInitializeService.triggerBlockchainCommand(args);
124-
verify(updaterService, never()).updateToFinal(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE, receipt);
124+
verify(updaterService, never()).updateToFinal(args, receipt);
125125
verifyNoInteractions(blockchainCheckerService);
126126
}
127127

128128
@Test
129129
void shouldNotTriggerInitializeTaskSinceReceiptIsNull() throws Exception {
130-
when(updaterService.updateToProcessing(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE)).thenReturn(true);
130+
when(updaterService.updateToProcessing(args)).thenReturn(true);
131131
when(blockchainCheckerService.sendBlockchainCommand(args)).thenReturn(null);
132132

133133
taskInitializeService.triggerBlockchainCommand(args);
134-
verify(updaterService).updateToFinal(CHAIN_TASK_ID, CommandName.TASK_INITIALIZE, null);
134+
verify(updaterService).updateToFinal(args, null);
135135
}
136136
// endregion
137137

0 commit comments

Comments
 (0)