diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/NetworkPeerings.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/NetworkPeerings.java index 07a19749783467c0cfc2d49e98b4745bda23437a..fef4b095e0d787f43afc723177f78e66f5e157b7 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/NetworkPeerings.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/NetworkPeerings.java @@ -74,17 +74,19 @@ public class NetworkPeerings { for (; i < lines.length - 1; ) { line = lines[i++].trim(); NetworkPeering.Endpoint ep = Endpoints.parse(line); - Preconditions.checkNotNull(ep, "Unaparsable endpoint: " + line); + Preconditions.checkNotNull(ep, "Unable to parse endpoint: " + line); endpoints.add(ep); } result.setEndpoints(endpoints.toArray(new NetworkPeering.Endpoint[endpoints.size()])); result.setSignature(lines[lines.length - 1]); + result.setStatus("UP"); + return result; } catch(Exception e) { - throw new IOException(e.getMessage()); + throw new IOException(e.getMessage(), e); } } diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/EndpointSerializer.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/EndpointSerializer.java new file mode 100644 index 0000000000000000000000000000000000000000..278b7bff145c974502dc6fe55df8dccfa4d924dd --- /dev/null +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/EndpointSerializer.java @@ -0,0 +1,66 @@ +package org.duniter.core.client.model.bma.jackson; + +/* + * #%L + * Duniter4j :: Core Client API + * %% + * Copyright (C) 2014 - 2017 EIS + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import org.duniter.core.client.model.bma.Endpoints; +import org.duniter.core.client.model.bma.NetworkPeering; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +/** + * Created by blavenie on 17/10/18. + */ +public class EndpointSerializer extends JsonSerializer<NetworkPeering.Endpoint> { + + private static final Logger log = LoggerFactory.getLogger(EndpointSerializer.class); + + private boolean debug; + + public EndpointSerializer() { + this.debug = log.isDebugEnabled(); + } + + @Override + public void serialize(NetworkPeering.Endpoint endpoint, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) { + + try { + jsonGenerator.writeString(endpoint.toString()); + } catch(IOException e) { + // Unable to parse endpoint: continue (will skip this endpoint) + if (debug) { + log.warn(e.getMessage(), e); // link the exception + } + else { + log.debug(e.getMessage()); + } + } + } +} \ No newline at end of file diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java index 7c4d3addcc564d19bb087d998ea3b81b98d7001e..0309e327bc5361c3c8ef578e20346f5541b0f6de 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java @@ -61,6 +61,7 @@ public abstract class JacksonUtils extends SimpleModule { // Network module.addDeserializer(NetworkPeering.Endpoint.class, new EndpointDeserializer()); + module.addSerializer(NetworkPeering.Endpoint.class, new EndpointSerializer()); objectMapper.registerModule(module); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/NetworkRemoteServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/NetworkRemoteServiceImpl.java index 87613e29717b375d84ca663489ed5f25006c5d34..6071b7098cc3fec04941edfaffd8b76e95fdcc9f 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/NetworkRemoteServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/NetworkRemoteServiceImpl.java @@ -186,7 +186,7 @@ public class NetworkRemoteServiceImpl extends BaseRemoteServiceImpl implements N Preconditions.checkNotNull(peeringDocument); // http post /tx/process - HttpPost httpPost = new HttpPost(getPath(peer, URL_PEERING_PEERS)); + HttpPost httpPost = new HttpPost(getPath(peer, URL_PEERING)); if (log.isDebugEnabled()) { log.debug(String.format( diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/util/KnownBlocks.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/util/KnownBlocks.java new file mode 100644 index 0000000000000000000000000000000000000000..5f94d6de6c03c0822148880a20228271782baf8e --- /dev/null +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/util/KnownBlocks.java @@ -0,0 +1,45 @@ +package org.duniter.core.client.util; + +import org.duniter.core.client.model.bma.BlockchainBlock; +import org.duniter.core.exception.TechnicalException; + +public class KnownBlocks { + + + private KnownBlocks() { + // helper class + } + + public static BlockchainBlock getFirstBlock(String currencyId) { + + BlockchainBlock result = new BlockchainBlock(); + result.setNumber(0); + + // G1 currency + switch (currencyId) { + + case KnownCurrencies.G1 : + result.setCurrency("g1"); + result.setHash("000003D02B95D3296A4F06DBAC51775C4336A4DC09D0E958DC40033BE7E20F3D"); + result.setTime(1488987127L); + result.setMedianTime(1488987127L); + result.setIssuer("2ny7YAdmzReQxAayyJZsyVYwYhVyax2thKcGknmQy5nQ"); + result.setSignature("49OD/8pj0bU0Lg6HB4p+5TOcRbgtj8Ubxmhen4IbOXM+g33V/I56GfF+QbD9U138Ek04E9o0lSjaDIVI/BrkCw=="); + break; + + case KnownCurrencies.G1_TEST : + result.setCurrency("g1-test"); + result.setHash("0000DEFA598EA82BC8FF19BC56B49A686E63617DCC7304FAF7F0461FA34E0F9C"); + result.setTime(1496842431L); + result.setMedianTime(1496842431L); + result.setIssuer("3dnbnYY9i2bHMQUGyFp5GVvJ2wBkVpus31cDJA5cfRpj"); + result.setSignature("OQQJ8TVISMgpz8SmdVGHYAUQMDnHpXqeFal4+/q2hV37uyrpC8iF6d50Wgg2TMKhsB/9zelOXZgbuzutAOZ5AA=="); + break; + + default: + throw new TechnicalException(String.format("First block for currency %s not defined !", currencyId)); + } + + return result; + } +} diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/util/KnownCurrencies.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/util/KnownCurrencies.java new file mode 100644 index 0000000000000000000000000000000000000000..cf5941762b2c06bc5861fafef190ac3b7a3051c4 --- /dev/null +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/util/KnownCurrencies.java @@ -0,0 +1,9 @@ +package org.duniter.core.client.util; + +public interface KnownCurrencies { + + String G1 = "g1"; + + String G1_TEST = "g1-test"; + +} diff --git a/duniter4j-core-client/src/main/resources/i18n/duniter4j-core-client_en_GB.properties b/duniter4j-core-client/src/main/resources/i18n/duniter4j-core-client_en_GB.properties index c750c9582e9885cf8979e70f3a3f1a930eb8d975..6d8cde9d3611d881401ed1628a7c7b393b99313c 100644 --- a/duniter4j-core-client/src/main/resources/i18n/duniter4j-core-client_en_GB.properties +++ b/duniter4j-core-client/src/main/resources/i18n/duniter4j-core-client_en_GB.properties @@ -1,5 +1,5 @@ -duniter4j.client.authentication=Http request error (unauthorized or forbidden). -duniter4j.client.core.connect=Could not connect to Duniter node [%s] +duniter4j.client.authentication=Http request error\: unauthorized or forbidden +duniter4j.client.core.connect=Http request error {%s}\: could not connect duniter4j.client.core.emptyResponse=[%s] Empty Response duniter4j.client.core.invalidResponse=[%s] Invalid response duniter4j.client.core.invalidResponseContentType=[%s] Invalid response content-type\: expected [%s] but get [%s] diff --git a/duniter4j-core-client/src/main/resources/i18n/duniter4j-core-client_fr_FR.properties b/duniter4j-core-client/src/main/resources/i18n/duniter4j-core-client_fr_FR.properties index caf081d223158f35d326b2da8d6564b0c13f9c06..291be52ea0f81e0a0ad8acc937fffc59b0c74dcb 100644 --- a/duniter4j-core-client/src/main/resources/i18n/duniter4j-core-client_fr_FR.properties +++ b/duniter4j-core-client/src/main/resources/i18n/duniter4j-core-client_fr_FR.properties @@ -1,5 +1,5 @@ -duniter4j.client.authentication=Echec de la requete (Accès interdit ou non autorisé). -duniter4j.client.core.connect=Echec de la connection au noeud Duniter [%s] +duniter4j.client.authentication=Echec de la requete\: accès interdit ou non autorisé). +duniter4j.client.core.connect=Echec de la requete {%s}\: connection impossible duniter4j.client.core.emptyResponse=[%s] Réponse vide duniter4j.client.core.invalidResponse=[%s] Réponse non valide duniter4j.client.core.invalidResponseContentType=[%s] Type de réponse invalide\: attendu [%s] mais [%s] obtenu