Commit 36f1ef57 authored by Benoit Lavenier's avatar Benoit Lavenier

[fix] Fix currency id/name in Dao

[enh] Add WotRemoteService.sendCertification() implementation
[enh] Add NetworkRemoveService.getDifficulties()
parent 3422479e
Pipeline #4508 passed with stage
in 34 seconds
......@@ -100,7 +100,7 @@ public class TransactionAction extends AbstractAction {
Currency currency = ServiceLocator.instance().getBlockchainRemoteService().getCurrencyFromPeer(peer);
ServiceLocator.instance().getCurrencyService().save(currency);
peer.setCurrency(currency.getCurrencyName());
peer.setCurrency(currency.getId());
ServiceLocator.instance().getPeerService().save(peer);
......@@ -123,7 +123,7 @@ public class TransactionAction extends AbstractAction {
}
Wallet wallet = new Wallet(
currency.getCurrencyName(),
currency.getId(),
null,
keypair.getPubKey(),
keypair.getSecKey());
......@@ -219,7 +219,7 @@ public class TransactionAction extends AbstractAction {
logTxSummary(wallet);
peers.stream().forEach(peer -> {
peer.setCurrency(currency.getCurrencyName());
peer.setCurrency(currency.getId());
peerService.save(peer);
log.debug(String.format("Send TX to [%s]...", peer));
......
......@@ -251,6 +251,10 @@ public class Configuration {
return applicationConfig.getOptionAsInt(ConfigurationOption.NETWORK_TIMEOUT.getKey());
}
public int getNetworkLargerTimeout() {
return Math.max(30000, getNetworkTimeout());
}
public int getNetworkMaxTotalConnections() {
return applicationConfig.getOptionAsInt(ConfigurationOption.NETWORK_MAX_CONNECTIONS.getKey());
}
......
......@@ -40,9 +40,11 @@ public interface CurrencyDao extends Bean, EntityDao<String, Currency> {
void remove(final Currency currency);
List<String> getCurrencyIds();
Set<String> getAllIds();
List<Currency> getCurrencies(long accountId);
List<Currency> getAll();
List<Currency> getAllByAccount(long accountId);
/**
* Return the value of the last universal dividend
......
......@@ -24,6 +24,7 @@ package org.duniter.core.client.dao;
import org.duniter.core.client.model.bma.EndpointApi;
import org.duniter.core.client.model.bma.NetworkPeers;
import org.duniter.core.client.model.bma.NetworkWs2pHeads;
import org.duniter.core.client.model.local.Peer;
import java.util.Collection;
......@@ -56,6 +57,14 @@ public interface PeerDao extends EntityDao<String, Peer> {
*/
List<NetworkPeers.Peer> getBmaPeersByCurrencyId(String currencyId, String[] pubkeys);
/**
* Get WS2p heads as BMA /network/ws2p/head format
* @param currencyId
* @param pubkeys use to filter on specific pubkeys. If null, not filtering
* @return
*/
List<NetworkWs2pHeads.Head> getWs2pPeersByCurrencyId(String currencyId, String[] pubkeys);
boolean isExists(String currencyId, String peerId);
Long getMaxLastUpTime(String currencyId);
......
......@@ -23,6 +23,7 @@ package org.duniter.core.client.dao.mem;
*/
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.duniter.core.client.dao.CurrencyDao;
import java.util.*;
......@@ -61,15 +62,18 @@ public class MemoryCurrencyDaoImpl implements CurrencyDao {
}
@Override
public List<String> getCurrencyIds() {
return ImmutableList.copyOf(currencies.keySet());
public Set<String> getAllIds() {
return ImmutableSet.copyOf(currencies.keySet());
}
@Override
public List<org.duniter.core.client.model.local.Currency> getCurrencies(long accountId) {
List<org.duniter.core.client.model.local.Currency> result = new ArrayList<>();
result.addAll(currencies.values());
return result;
public List<org.duniter.core.client.model.local.Currency> getAll() {
return ImmutableList.copyOf(currencies.values());
}
@Override
public List<org.duniter.core.client.model.local.Currency> getAllByAccount(long accountId) {
return ImmutableList.copyOf(currencies.values());
}
@Override
......
......@@ -26,6 +26,7 @@ import com.google.common.collect.ImmutableList;
import org.duniter.core.client.dao.PeerDao;
import org.duniter.core.client.model.bma.EndpointApi;
import org.duniter.core.client.model.bma.NetworkPeers;
import org.duniter.core.client.model.bma.NetworkWs2pHeads;
import org.duniter.core.client.model.local.Peer;
import org.duniter.core.client.model.local.Peers;
import org.duniter.core.util.Preconditions;
......@@ -121,6 +122,17 @@ public class MemoryPeerDaoImpl implements PeerDao {
return Peers.toBmaPeers(getPeersByCurrencyIdAndApiAndPubkeys(currencyId, null, pubkeys));
}
@Override
public List<NetworkWs2pHeads.Head> getWs2pPeersByCurrencyId(String currencyId, String[] pubkeys) {
Preconditions.checkNotNull(currencyId);
return getPeersByCurrencyIdAndApiAndPubkeys(currencyId, null, pubkeys)
.stream()
.map(Peers::toWs2pHead)
// Skip if no message
.filter(head -> head.getMessage() != null)
.collect(Collectors.toList());
}
@Override
public boolean isExists(final String currencyId, final String peerId) {
......
......@@ -23,6 +23,9 @@ package org.duniter.core.client.model;
*/
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.duniter.core.client.model.local.LocalEntity;
import java.io.Serializable;
/**
......@@ -36,6 +39,10 @@ public class BasicIdentity implements Serializable {
private static final long serialVersionUID = 8080689271400316984L;
public static final String PROPERTY_UID = "uid";
public static final String PROPERTY_PUBKEY = "pubkey";
public static final String PROPERTY_SIGNATURE = "signature";
private String pubkey;
private String signature;
......@@ -58,10 +65,14 @@ public class BasicIdentity implements Serializable {
this.signature = signature;
}
@JsonIgnore
@Deprecated
public String getSelf() {
return signature;
}
@JsonIgnore
@Deprecated
public void setSelf(String signature) {
this.signature = signature;
}
......
......@@ -51,6 +51,7 @@ public class BlockchainBlock implements Serializable {
public static final String PROPERTY_REVOKED = "revoked";
public static final String PROPERTY_EXCLUDED = "excluded";
public static final String PROPERTY_MEDIAN_TIME = "medianTime";
public static final String PROPERTY_ISSUER = "issuer";
private static final long serialVersionUID = -5598140972293452669L;
......
package org.duniter.core.client.model.bma;
/*
* #%L
* UCoin Java :: Core Client API
* %%
* Copyright (C) 2014 - 2016 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 java.io.Serializable;
public class BlockchainDifficulties implements Serializable {
private static final long serialVersionUID = -5631089862715942431L;
private Long block;
private DifficultyLevel[] levels;
public Long getBlock() {
return block;
}
public void setBlock(Long block) {
this.block = block;
}
public DifficultyLevel[] getLevels() {
return levels;
}
public void setLevels(DifficultyLevel[] levels) {
this.levels = levels;
}
public static class DifficultyLevel implements Serializable {
private static final long serialVersionUID = 1L;
private String uid;
private int level;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
}
......@@ -45,7 +45,7 @@ public class BlockchainMemberships extends BasicIdentity {
this.memberships = memberships;
}
public class Membership implements Serializable {
public static class Membership implements Serializable {
private static final long serialVersionUID = 1L;
private String version;
......
......@@ -35,6 +35,8 @@ public interface Protocol {
String TYPE_MEMBERSHIP = "Membership";
String TYPE_CERTIFICATION = "Certification";
String TYPE_TRANSACTION = "Transaction";
String TYPE_PEER = "Peer";
......
......@@ -113,7 +113,7 @@ public class Contact implements LocalEntity<Long>, Serializable {
public boolean hasIdentityForCurrency(String currencyId) {
return identities.stream()
.anyMatch(identity -> identity.getCurrencyId() != null
&& currencyId.equals(identity.getCurrencyId()));
.anyMatch(identity -> identity.getCurrency() != null
&& currencyId.equals(identity.getCurrency()));
}
}
......@@ -25,7 +25,6 @@ package org.duniter.core.client.model.local;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.duniter.core.client.model.Account;
import org.duniter.core.client.model.bma.BlockchainParameters;
/**
......@@ -33,7 +32,7 @@ import org.duniter.core.client.model.bma.BlockchainParameters;
*/
public class Currency implements LocalEntity<String>, Serializable {
private String currencyName;
private String id;
private Integer membersCount;
private String firstBlockSignature;
private Long lastUD;
......@@ -42,11 +41,11 @@ public class Currency implements LocalEntity<String>, Serializable {
public Currency() {
}
public Currency(String currencyName,
public Currency(String id,
String firstBlockSignature,
int membersCount,
BlockchainParameters parameters) {
this.currencyName = currencyName;
this.id = id;
this.firstBlockSignature = firstBlockSignature;
this.membersCount = membersCount;
this.parameters = parameters;
......@@ -54,12 +53,11 @@ public class Currency implements LocalEntity<String>, Serializable {
@JsonIgnore
public String getId() {
return currencyName;
return id;
}
public String getCurrencyName()
{
return currencyName;
public void setId(String id) {
this.id = id;
}
public Integer getMembersCount() {
......@@ -70,13 +68,6 @@ public class Currency implements LocalEntity<String>, Serializable {
return firstBlockSignature;
}
public void setId(String id) {
this.currencyName = id;
}
public void setCurrencyName(String currencyName) {
this.currencyName = currencyName;
}
public void setMembersCount(Integer membersCount) {
this.membersCount = membersCount;
......@@ -103,6 +94,6 @@ public class Currency implements LocalEntity<String>, Serializable {
}
public String toString() {
return currencyName;
return id;
}
}
\ No newline at end of file
......@@ -23,29 +23,23 @@ package org.duniter.core.client.model.local;
*/
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.duniter.core.client.model.BasicIdentity;
public class Identity extends BasicIdentity {
private static final long serialVersionUID = -7451079677730158794L;
public static final String PROPERTY_IS_MEMBER = "isMember";
public static final String PROPERTY_WAS_MEMBER = "wasMember";
private String timestamp = null;
private Boolean isMember = null;
private String currencyId;
/**
* The timestamp value of the signature date (a BLOCK_UID)
* @return
*/
public String getTimestamp() {
return timestamp;
}
private Boolean wasMember = null;
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
private String currency;
/**
* Indicate whether the certification is written in the blockchain or not.
......@@ -58,11 +52,35 @@ public class Identity extends BasicIdentity {
this.isMember = isMember;
}
public String getCurrencyId() {
return currencyId;
public Boolean getWasMember() {
return wasMember;
}
public void setWasMember(Boolean wasMember) {
this.wasMember = wasMember;
}
/**
* The timestamp value of the signature date (a BLOCK_UID)
* @return
*/
@JsonIgnore
public String getTimestamp() {
return timestamp;
}
@JsonIgnore
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
@JsonIgnore
public String getCurrency() {
return currency;
}
public void setCurrencyId(String currencyId) {
this.currencyId = currencyId;
@JsonIgnore
public void setCurrency(String currency) {
this.currency = currency;
}
}
package org.duniter.core.client.model;
package org.duniter.core.client.model.local;
/*
* #%L
......@@ -23,29 +23,20 @@ package org.duniter.core.client.model;
*/
import org.duniter.core.client.model.local.Identity;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class Member extends Identity {
public class Member extends Identity implements LocalEntity<String> {
private static final long serialVersionUID = 8448049949323699700L;
private String number;
private String hash;
public String getNumber() {
return number;
@JsonIgnore
public String getId() {
return getPubkey();
}
public void setNumber(String number) {
this.number = number;
@JsonIgnore
public void setId(String pubkey) {
setPubkey(pubkey);
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
}
......@@ -24,10 +24,7 @@ package org.duniter.core.client.model.local;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.duniter.core.client.model.bma.EndpointApi;
import org.duniter.core.client.model.bma.NetworkPeering;
import org.duniter.core.client.model.bma.NetworkPeers;
import org.duniter.core.client.model.bma.Protocol;
import org.duniter.core.client.model.bma.*;
import org.duniter.core.util.CollectionUtils;
import org.duniter.core.util.StringUtils;
......@@ -147,6 +144,13 @@ public final class Peers {
}).collect(Collectors.toList());
}
public static NetworkWs2pHeads.Head toWs2pHead(Peer peer) {
NetworkWs2pHeads.Head result = new NetworkWs2pHeads.Head();
// TODO : add implementation
return result;
}
public static Peer.PeerStatus getStatus(final Peer peer) {
return peer.getStats() != null &&
......
......@@ -50,6 +50,8 @@ public interface HttpService extends Service {
<T> T executeRequest(Peer peer, String absolutePath, Class<? extends T> resultClass);
<T> T executeRequest(Peer peer, String absolutePath, Class<? extends T> resultClass, int timeout) ;
String getPath(Peer peer, String... absolutePath);
String getPath(String... absolutePath);
......
......@@ -152,8 +152,12 @@ public class HttpServiceImpl implements HttpService, Closeable, InitializingBean
}
public <T> T executeRequest(Peer peer, String absolutePath, Class<? extends T> resultClass) {
return executeRequest(peer, absolutePath, resultClass, 0);
}
public <T> T executeRequest(Peer peer, String absolutePath, Class<? extends T> resultClass, int timeout) {
HttpGet httpGet = new HttpGet(peer.getUrl() + absolutePath);
return executeRequest(HttpClients.getThreadHttpClient(0), httpGet, resultClass);
return executeRequest(HttpClients.getThreadHttpClient(timeout), httpGet, resultClass);
}
public String getPath(Peer peer, String... absolutePath) {
......
......@@ -58,15 +58,29 @@ public abstract class BaseRemoteServiceImpl implements Service, InitializingBean
peerService = null;
}
@Deprecated
public <T> T executeRequest(Peer peer, String absolutePath, Class<? extends T> resultClass) {
return httpService.executeRequest(peer, absolutePath, resultClass);
}
@Deprecated
public <T> T executeRequest(Peer peer, String absolutePath, Class<? extends T> resultClass, int timeout) {
return httpService.executeRequest(peer, absolutePath, resultClass, timeout);
}
@Deprecated
public <T> T executeRequest(String currencyId, String absolutePath, Class<? extends T> resultClass) {
Peer peer = peerService.getActivePeerByCurrencyId(currencyId);
return httpService.executeRequest(peer, absolutePath, resultClass);
}
@Deprecated
public <T> T executeRequest(String currencyId, String absolutePath, Class<? extends T> resultClass, int timeout) {
Peer peer = peerService.getActivePeerByCurrencyId(currencyId);
return httpService.executeRequest(peer, absolutePath, resultClass, timeout);
}
@Deprecated
public <T> T executeRequest(HttpUriRequest request, Class<? extends T> resultClass) {
return httpService.executeRequest(request, resultClass);
}
......@@ -76,14 +90,17 @@ public abstract class BaseRemoteServiceImpl implements Service, InitializingBean
return httpService.getPath(peer, aPath);
}
@Deprecated
public String getPath(Peer peer, String aPath) {
return httpService.getPath(peer, aPath);
}
@Deprecated
public URIBuilder getURIBuilder(URI baseUri, String... path) {
return httpService.getURIBuilder(baseUri, path);
}
@Deprecated
public URIBuilder getURIBuilder(URL baseUrl, String... path) {
try {
return httpService.getURIBuilder(baseUrl.toURI(), path);
......
......@@ -23,6 +23,7 @@ package org.duniter.core.client.service.bma;
*/
import org.duniter.core.beans.Service;
import org.duniter.core.client.model.bma.BlockchainDifficulties;
import org.duniter.core.client.model.local.Identity;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.client.model.bma.BlockchainMemberships;
......@@ -49,6 +50,8 @@ public interface BlockchainRemoteService extends Service {
*/
BlockchainParameters getParameters(String currencyId, boolean useCache);
BlockchainParameters getParameters(Peer peer, boolean useCache);
/**
* get the blockchain parameters (currency parameters)
*
......@@ -84,6 +87,8 @@ public interface BlockchainRemoteService extends Service {
*/
Long getBlockDividend(String currencyId, long number) throws BlockNotFoundException;
Long getBlockDividend(Peer peer, long number) throws BlockNotFoundException;
/**
* Retrieve a block, by id (from 0 to current)
*
......@@ -124,6 +129,7 @@ public interface BlockchainRemoteService extends Service {
*
* @return
*/
BlockchainBlock getCurrentBlock(Peer peer, boolean useCache);
BlockchainBlock getCurrentBlock(String currencyId, boolean useCache);
/**
......@@ -131,15 +137,8 @@ public interface BlockchainRemoteService extends Service {
*
* @return
*/
BlockchainBlock getCurrentBlock(String currencyId);
/**
* Retrieve the current block
*
* @param peer the peer to use for request
* @return the last block
*/
BlockchainBlock getCurrentBlock(Peer peer);
BlockchainBlock getCurrentBlock(String currencyId);
/**
* Retrieve the currency data, from peer
......@@ -149,23 +148,20 @@ public interface BlockchainRemoteService extends Service {
*/
Currency getCurrencyFromPeer(Peer peer);
BlockchainParameters getBlockchainParametersFromPeer(Peer peer);
/**
* Retrieve the last emitted UD (or ud0 if not UD emitted yet)
*
* @param currencyId id of currency
* Retrieve personal difficulties (level, uid)
* @return
*/
long getLastUD(String currencyId);
BlockchainDifficulties getDifficulties(Peer peer);
BlockchainDifficulties getDifficulties(String currencyId);
/**
* Retrieve the last emitted UD, from a peer (or ud0 if not UD emitted yet)
* Retrieve the last emitted UD (or ud0 if not UD emitted yet)
*
* @param currencyId id of currency
* @return
*/
long getLastUD(Peer peer);
long getLastUD(String currencyId);
/**
* Check is a identity is not already used by a existing member
......@@ -193,7 +189,6 @@ public interface BlockchainRemoteService extends Service {
*/
void loadMembership(String currencyId, Identity identity, boolean checkLookupForNonMember);
BlockchainMemberships getMembershipByUid(String currencyId, String uid);
BlockchainMemberships getMembershipByPublicKey(String currencyId, String pubkey);
......@@ -220,18 +215,18 @@ public interface BlockchainRemoteService extends Service {
* @param startOffset
* @return
*/
Map<Integer, Long> getUDs(Peer peer, long startOffset);
Map<Integer, Long> getUDs(String currencyId, long startOffset);
/**
* Listening new block event
* @param currencyId
* @param listener
* @param autoReconnect
* @return
*/
WebsocketClientEndpoint addBlockListener(Peer peer, WebsocketClientEndpoint.MessageListener listener, boolean aut