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

Create a ConfigServerClient instance and use it #700

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
- Create `iexec-task-api` to access task feedback API. (#695)
- Move `notification` package from `iexec-commons-poco` to `iexec-core-library`. (#697 #698)
- Move `PublicConfiguration` class from `iexec-common` to `iexec-core-library`. (#699)
- Create `ConfigServerClient` instance and use it. (#700)

### Bug Fixes

Expand Down
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
version=8.4.1
iexecCommonVersion=8.4.0
iexecCommonVersion=8.4.0-NEXT-SNAPSHOT
iexecCommonsPocoVersion=3.2.0
iexecBlockchainAdapterVersion=8.4.0
iexecResultVersion=8.4.0
iexecSmsVersion=8.5.1

nexusUser
nexusPassword
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 IEXEC BLOCKCHAIN TECH
* Copyright 2021-2024 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,6 @@
import com.iexec.blockchain.api.BlockchainAdapterApiClient;
import com.iexec.blockchain.api.BlockchainAdapterApiClientBuilder;
import com.iexec.blockchain.api.BlockchainAdapterService;
import com.iexec.common.config.PublicChainConfig;
import feign.Logger;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -64,15 +63,4 @@ public BlockchainAdapterApiClient blockchainAdapterClient() {
public BlockchainAdapterService blockchainAdapterService(BlockchainAdapterApiClient blockchainAdapterClient) {
return new BlockchainAdapterService(blockchainAdapterClient, Duration.ofSeconds(WATCH_PERIOD_SECONDS), MAX_ATTEMPTS);
}

@Bean
public PublicChainConfig publicChainConfig(BlockchainAdapterApiClient apiClient) {
return apiClient.getPublicChainConfig();
}

@Bean
public int getChainId(PublicChainConfig publicChainConfig) {
return publicChainConfig.getChainId();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2024 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.iexec.core.configuration;

import com.iexec.common.config.ConfigServerClient;
import com.iexec.common.config.ConfigServerClientBuilder;
import com.iexec.common.config.PublicChainConfig;
import feign.Logger;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.context.annotation.Bean;

@Data
@ConstructorBinding
@ConfigurationProperties(prefix = "config-server")
public class ConfigServerClientConfig {

private final String protocol;
private final String host;
private final int port;

public String getUrl() {
return buildHostUrl(protocol, host, port);
}

private String buildHostUrl(String protocol, String host, int port) {
return protocol + "://" + host + ":" + port;
}
jbern0rd marked this conversation as resolved.
Show resolved Hide resolved

@Bean
public ConfigServerClient configServerClient() {
return ConfigServerClientBuilder.getInstance(
Logger.Level.NONE, getUrl());
}

@Bean
public PublicChainConfig publicChainConfig(ConfigServerClient apiClient) {
return apiClient.getPublicChainConfig();
}

@Bean
public int getChainId(PublicChainConfig publicChainConfig) {
return publicChainConfig.getChainId();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 IEXEC BLOCKCHAIN TECH
* Copyright 2022-2024 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,6 +38,7 @@ public class PublicConfigurationService {
private final WorkerConfiguration workerConfiguration;
private final ResultRepositoryConfiguration resultRepoConfig;
private final BlockchainAdapterClientConfig blockchainAdapterClientConfig;
private final ConfigServerClientConfig configServerClientConfig;

private PublicConfiguration publicConfiguration = null;
/**
Expand All @@ -51,19 +52,22 @@ public PublicConfigurationService(ChainConfig chainConfig,
CredentialsService credentialsService,
WorkerConfiguration workerConfiguration,
ResultRepositoryConfiguration resultRepoConfig,
BlockchainAdapterClientConfig blockchainAdapterClientConfig) {
BlockchainAdapterClientConfig blockchainAdapterClientConfig,
ConfigServerClientConfig configServerClientConfig) {
this.chainConfig = chainConfig;
this.credentialsService = credentialsService;
this.workerConfiguration = workerConfiguration;
this.resultRepoConfig = resultRepoConfig;
this.blockchainAdapterClientConfig = blockchainAdapterClientConfig;
this.configServerClientConfig = configServerClientConfig;
}

@PostConstruct
void buildPublicConfiguration() {
this.publicConfiguration = PublicConfiguration.builder()
.workerPoolAddress(chainConfig.getPoolAddress())
.blockchainAdapterUrl(blockchainAdapterClientConfig.getUrl())
jbern0rd marked this conversation as resolved.
Show resolved Hide resolved
.configServerUrl(configServerClientConfig.getUrl())
.schedulerPublicAddress(credentialsService.getCredentials().getAddress())
.resultRepositoryURL(resultRepoConfig.getResultRepositoryURL())
.askForReplicatePeriod(workerConfiguration.getAskForReplicatePeriod())
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ blockchain-adapter:
name: ${IEXEC_CORE_CHAIN_ADAPTER_USERNAME:admin}
password: ${IEXEC_CORE_CHAIN_ADAPTER_PASSWORD:whatever}

config-server:
protocol: ${IEXEC_CONFIG_SERVER_PROTOCOL:http}
host: ${IEXEC_CONFIG_SERVER_HOST:localhost}
port: ${IEXEC_CONFIG_SERVER_PORT:13020}
jbern0rd marked this conversation as resolved.
Show resolved Hide resolved

resultRepository:
protocol: ${IEXEC_RESULT_REPOSITORY_PROTOCOL:http}
host: ${IEXEC_RESULT_REPOSITORY_HOST:localhost}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 IEXEC BLOCKCHAIN TECH
* Copyright 2021-2024 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,55 +16,19 @@

package com.iexec.core.chain.adapter;

import com.iexec.common.config.PublicChainConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.time.Duration;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@Slf4j
@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = BlockchainAdapterClientConfig.class)
@Testcontainers
class BlockchainAdapterClientConfigTests {
private static final int WIREMOCK_PORT = 8080;

@Autowired
private PublicChainConfig chainConfig;

@Container
static final GenericContainer<?> wmServer = new GenericContainer<>("wiremock/wiremock:3.3.1")
.withClasspathResourceMapping("wiremock", "/home/wiremock", BindMode.READ_ONLY)
.withExposedPorts(WIREMOCK_PORT);

@DynamicPropertySource
static void registerProperties(DynamicPropertyRegistry registry) {
registry.add("blockchain-adapter.protocol", () -> "http");
registry.add("blockchain-adapter.host", () -> "localhost");
registry.add("blockchain-adapter.port", () -> wmServer.getMappedPort(WIREMOCK_PORT));
}

@Test
void checkChainConfigInitialization() {
PublicChainConfig expectedConfig = PublicChainConfig.builder()
.chainId(255)
.chainNodeUrl("http://localhost:8545")
.blockTime(Duration.ofSeconds(5L))
.sidechain(true)
.build();
assertThat(chainConfig).isEqualTo(expectedConfig);
void checkURL() {
final BlockchainAdapterClientConfig blockchainAdapterClientConfig = new BlockchainAdapterClientConfig("http", "localhost", 8080, "", "");
assertThat(blockchainAdapterClientConfig.getUrl()).isEqualTo("http://localhost:8080");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2024 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.iexec.core.configuration;

import com.iexec.common.config.PublicChainConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.time.Duration;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@Slf4j
@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = ConfigServerClientConfig.class)
@Testcontainers
class ConfigServerClientConfigTests {
private static final int WIREMOCK_PORT = 8080;

@Autowired
private PublicChainConfig chainConfig;

@Autowired
private int getChainId;

@Container
static final GenericContainer<?> wmServer = new GenericContainer<>("wiremock/wiremock:3.3.1")
.withClasspathResourceMapping("wiremock", "/home/wiremock", BindMode.READ_ONLY)
.withExposedPorts(WIREMOCK_PORT);

@DynamicPropertySource
static void registerProperties(DynamicPropertyRegistry registry) {
registry.add("config-server.protocol", () -> "http");
registry.add("config-server.host", () -> "localhost");
registry.add("config-server.port", () -> wmServer.getMappedPort(WIREMOCK_PORT));
}

@Test
void checkChainConfigInitialization() {
PublicChainConfig expectedConfig = PublicChainConfig.builder()
.chainId(255)
.chainNodeUrl("http://localhost:8545")
.blockTime(Duration.ofSeconds(5L))
.sidechain(true)
.build();
assertThat(chainConfig).isEqualTo(expectedConfig);
}

@Test
void checkChainIdInitialization() {
assertThat(getChainId).isEqualTo(255);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class PublicConfigurationServiceTests {
private ResultRepositoryConfiguration resultRepoConfig;
@Mock
private BlockchainAdapterClientConfig blockchainAdapterClientConfig;
@Mock
private ConfigServerClientConfig configServerClientConfig;

@InjectMocks
private PublicConfigurationService publicConfigurationService;
Expand Down