Skip to content

Commit

Permalink
Merge pull request #2457 from HenrikJannsen/add-ExcludeForHash-for-me…
Browse files Browse the repository at this point in the history
…tadata

Add exclude for hash for metadata
  • Loading branch information
djing-chan authored Jul 22, 2024
2 parents 47592b4 + c320a7f commit 8b32afd
Show file tree
Hide file tree
Showing 50 changed files with 876 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public static OracleNodeService.Config from(com.typesafe.config.Config config) {
private final String signatureBase64;
private final String profileId;
private final boolean staticPublicKeysProvided;
private AuthorizedOracleNode authorizedOracleNode;
@Nullable
private Scheduler startupScheduler, scheduler;

Expand Down Expand Up @@ -185,14 +184,24 @@ public CompletableFuture<Boolean> initialize() {
KeyPair keyPair = identity.getNetworkIdWithKeyPair().getKeyPair();
byte[] authorizedPublicKeyEncoded = authorizedPublicKey.getEncoded();
String authorizedPublicKeyAsHex = Hex.encode(authorizedPublicKeyEncoded);
authorizedOracleNode = new AuthorizedOracleNode(networkId,
AuthorizedOracleNode authorizedOracleNode = new AuthorizedOracleNode(networkId,
profileId,
authorizedPublicKeyAsHex,
bondUserName,
signatureBase64,
staticPublicKeysProvided);
bisq1BridgeService.setAuthorizedOracleNode(authorizedOracleNode);

// Can be removed once there are no pre 2.1.0 versions out there anymore
AuthorizedOracleNode oldVersion = new AuthorizedOracleNode(0,
networkId,
profileId,
authorizedPublicKeyAsHex,
bondUserName,
signatureBase64,
staticPublicKeysProvided);
bisq1BridgeService.setAuthorizedOracleNodeOldVersion(oldVersion);

// We only self-publish if we are a root oracle
if (staticPublicKeysProvided) {
AuthorizedBondedRole authorizedBondedRole = new AuthorizedBondedRole(profileId,
Expand All @@ -206,6 +215,7 @@ public CompletableFuture<Boolean> initialize() {
staticPublicKeysProvided);

// TODO deactivate republishing until issues are resolved

// Repeat 3 times at startup to republish to ensure the data gets well distributed
/* startupScheduler = Scheduler.run(() -> publishMyAuthorizedData(authorizedOracleNode, authorizedBondedRole, keyPair))
.repeated(1, 10, TimeUnit.SECONDS, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public static Bisq1BridgeService.Config from(com.typesafe.config.Config config)
@Setter
private AuthorizedOracleNode authorizedOracleNode;
@Setter
private AuthorizedOracleNode authorizedOracleNodeOldVersion;
@Setter
private Identity identity;

@Nullable
Expand Down Expand Up @@ -338,8 +340,17 @@ private void processAuthorizeAccountAgeRequest(AuthorizeAccountAgeRequest reques
if (date == requestDate) {
persistableStore.getAccountAgeRequests().add(request);
persist();
AuthorizedAccountAgeData data = new AuthorizedAccountAgeData(profileId, requestDate, staticPublicKeysProvided);
AuthorizedAccountAgeData data = new AuthorizedAccountAgeData(profileId,
requestDate,
staticPublicKeysProvided);
publishAuthorizedData(data);

// Can be removed once there are no pre 2.1.0 versions out there anymore
AuthorizedAccountAgeData oldVersion = new AuthorizedAccountAgeData(0,
profileId,
requestDate,
staticPublicKeysProvided);
publishAuthorizedData(oldVersion);
} else {
log.warn("Date of account age for {} is not matching the date from the users request. " +
"Date from bridge service call: {}; Date from users request: {}",
Expand Down Expand Up @@ -384,8 +395,17 @@ private void processAuthorizeSignedWitnessRequest(AuthorizeSignedWitnessRequest
if (date == witnessSignDate) {
persistableStore.getSignedWitnessRequests().add(request);
persist();
AuthorizedSignedWitnessData data = new AuthorizedSignedWitnessData(request.getProfileId(), request.getWitnessSignDate(), staticPublicKeysProvided);
AuthorizedSignedWitnessData data = new AuthorizedSignedWitnessData(request.getProfileId(),
request.getWitnessSignDate(),
staticPublicKeysProvided);
publishAuthorizedData(data);

// Can be removed once there are no pre 2.1.0 versions out there anymore
AuthorizedSignedWitnessData oldVersion = new AuthorizedSignedWitnessData(0,
request.getProfileId(),
request.getWitnessSignDate(),
staticPublicKeysProvided);
publishAuthorizedData(oldVersion);
} else {
log.warn("Date of signed witness for {} is not matching the date from the users request. " +
"Date from bridge service call: {}; Date from users request: {}",
Expand Down Expand Up @@ -439,6 +459,25 @@ private void processBondedRoleRegistrationRequest(BondedRoleRegistrationRequest
} else {
publishAuthorizedData(data);
}

// Can be removed once there are no pre 2.1.0 versions out there anymore
AuthorizedBondedRole oldVersion = new AuthorizedBondedRole(0,
profileId,
request.getAuthorizedPublicKey(),
bondedRoleType,
bondUserName,
signatureBase64,
request.getAddressByTransportTypeMap(),
request.getNetworkId(),
Optional.of(authorizedOracleNodeOldVersion),
false);
if (request.isCancellationRequest()) {
authorizedBondedRolesService.getAuthorizedBondedRoleStream()
.filter(authorizedBondedRole -> authorizedBondedRole.equals(oldVersion))
.forEach(this::removeAuthorizedData);
} else {
publishAuthorizedData(oldVersion);
}
} else {
log.warn("RequestBondedRole failed. {}", bondedRoleVerificationDto.getErrorMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public CompletableFuture<Boolean> initialize() {
log.info("initialize");
marketPriceByCurrencyMapPin = marketPriceRequestService.getMarketPriceByCurrencyMap().addObserver(() -> {
if (!marketPriceRequestService.getMarketPriceByCurrencyMap().isEmpty()) {
publishAuthorizedData(new AuthorizedMarketPriceData(new TreeMap<>(marketPriceRequestService.getMarketPriceByCurrencyMap()), staticPublicKeysProvided));
publishAuthorizedData(new AuthorizedMarketPriceData(new TreeMap<>(marketPriceRequestService.getMarketPriceByCurrencyMap()),
staticPublicKeysProvided));

// Can be removed once there are no pre 2.1.0 versions out there anymore
publishAuthorizedData(new AuthorizedMarketPriceData(0,
new TreeMap<>(marketPriceRequestService.getMarketPriceByCurrencyMap()),
staticPublicKeysProvided));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ private void processAuthorizeTimestampRequest(AuthorizeTimestampRequest request)
// to republish it.
date = persistableStore.getTimestampsByProfileId().get(profileId);
}
publishAuthorizedData(new AuthorizedTimestampData(profileId, date, staticPublicKeysProvided));
AuthorizedTimestampData authorizedTimestampData = new AuthorizedTimestampData(profileId, date, staticPublicKeysProvided);
publishAuthorizedData(authorizedTimestampData);

// Can be removed once there are no pre 2.1.0 versions out there anymore
AuthorizedTimestampData oldVersion = new AuthorizedTimestampData(0, profileId, date, staticPublicKeysProvided);
publishAuthorizedData(oldVersion);
}

private CompletableFuture<Boolean> publishAuthorizedData(AuthorizedDistributedData data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public CompletableFuture<Boolean> initialize() {
KeyPair keyPair = keyBundleService.getOrCreateKeyBundle(defaultKeyId).getKeyPair();

// TODO deactivate republishing until issues are resolved

// Repeat 3 times at startup to republish to ensure the data gets well distributed
/* startupScheduler = Scheduler.run(() -> publishMyBondedRole(authorizedBondedRole, keyPair, authorizedPrivateKey, authorizedPublicKey))
.repeated(1, 10, TimeUnit.SECONDS, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bisq.bonded_roles.AuthorizedPubKeys;
import bisq.bonded_roles.BondedRoleType;
import bisq.bonded_roles.oracle.AuthorizedOracleNode;
import bisq.common.annotation.ExcludeForHash;
import bisq.common.application.DevMode;
import bisq.common.proto.ProtoResolver;
import bisq.common.proto.UnresolvableProtobufMessageException;
Expand All @@ -43,8 +44,14 @@
@EqualsAndHashCode
@Getter
public final class AuthorizedBondedRole implements AuthorizedDistributedData {
private static final int VERSION = 1;

@EqualsAndHashCode.Exclude
@ExcludeForHash(excludeOnlyInVersions = {1, 2, 3})
private final MetaData metaData = new MetaData(TTL_100_DAYS, HIGHEST_PRIORITY, getClass().getSimpleName(), MAX_MAP_SIZE_100);
@EqualsAndHashCode.Exclude
@ExcludeForHash
private final int version;
private final String profileId;
private final String authorizedPublicKey;
private final BondedRoleType bondedRoleType;
Expand All @@ -54,6 +61,12 @@ public final class AuthorizedBondedRole implements AuthorizedDistributedData {
private final NetworkId networkId;
// The oracle node which did the validation and publishing
private final Optional<AuthorizedOracleNode> authorizingOracleNode;

// ExcludeForHash from version 1 on to not treat data from different oracle nodes with different staticPublicKeysProvided value as duplicate data.
// We add version 2 and 3 for extra safety...
// Once no pre version 2.0.5 nodes are expected anymore in the network we can remove the parameter
// and use default `@ExcludeForHash` instead.
@ExcludeForHash(excludeOnlyInVersions = {1, 2, 3})
@EqualsAndHashCode.Exclude
private final boolean staticPublicKeysProvided;

Expand All @@ -66,6 +79,29 @@ public AuthorizedBondedRole(String profileId,
NetworkId networkId,
Optional<AuthorizedOracleNode> authorizingOracleNode,
boolean staticPublicKeysProvided) {
this(VERSION,
profileId,
authorizedPublicKey,
bondedRoleType,
bondUserName,
signatureBase64,
addressByTransportTypeMap,
networkId,
authorizingOracleNode,
staticPublicKeysProvided);
}

public AuthorizedBondedRole(int version,
String profileId,
String authorizedPublicKey,
BondedRoleType bondedRoleType,
String bondUserName,
String signatureBase64,
Optional<AddressByTransportTypeMap> addressByTransportTypeMap,
NetworkId networkId,
Optional<AuthorizedOracleNode> authorizingOracleNode,
boolean staticPublicKeysProvided) {
this.version = version;
this.profileId = profileId;
this.authorizedPublicKey = authorizedPublicKey;
this.bondedRoleType = bondedRoleType;
Expand Down Expand Up @@ -96,7 +132,8 @@ public bisq.bonded_roles.protobuf.AuthorizedBondedRole.Builder getBuilder(boolea
.setBondUserName(bondUserName)
.setSignatureBase64(signatureBase64)
.setNetworkId(networkId.toProto(serializeForHash))
.setStaticPublicKeysProvided(staticPublicKeysProvided);
.setStaticPublicKeysProvided(staticPublicKeysProvided)
.setVersion(version);
addressByTransportTypeMap.ifPresent(e -> builder.setAddressByTransportTypeMap(e.toProto(serializeForHash)));
authorizingOracleNode.ifPresent(oracleNode -> builder.setAuthorizingOracleNode(oracleNode.toProto(serializeForHash)));
return builder;
Expand All @@ -108,7 +145,9 @@ public bisq.bonded_roles.protobuf.AuthorizedBondedRole toProto(boolean serialize
}

public static AuthorizedBondedRole fromProto(bisq.bonded_roles.protobuf.AuthorizedBondedRole proto) {
return new AuthorizedBondedRole(proto.getProfileId(),
return new AuthorizedBondedRole(
proto.getVersion(),
proto.getProfileId(),
proto.getAuthorizedPublicKey(),
BondedRoleType.fromProto(proto.getBondedRoleType()),
proto.getBondUserName(),
Expand All @@ -120,7 +159,8 @@ public static AuthorizedBondedRole fromProto(bisq.bonded_roles.protobuf.Authoriz
proto.hasAuthorizingOracleNode() ?
Optional.of(AuthorizedOracleNode.fromProto(proto.getAuthorizingOracleNode())) :
Optional.empty(),
proto.getStaticPublicKeysProvided());
proto.getStaticPublicKeysProvided()
);
}

public static ProtoResolver<DistributedData> getResolver() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package bisq.bonded_roles.market_price;

import bisq.bonded_roles.AuthorizedPubKeys;
import bisq.common.annotation.ExcludeForHash;
import bisq.common.application.DevMode;
import bisq.common.currency.Market;
import bisq.common.currency.MarketRepository;
Expand All @@ -44,16 +45,35 @@
@EqualsAndHashCode
@Getter
public final class AuthorizedMarketPriceData implements AuthorizedDistributedData {
private static final int VERSION = 1;
public static final long TTL = TimeUnit.MINUTES.toMillis(10);

@EqualsAndHashCode.Exclude
@ExcludeForHash(excludeOnlyInVersions = {1, 2, 3})
private final MetaData metaData = new MetaData(TTL, DEFAULT_PRIORITY, getClass().getSimpleName());
@EqualsAndHashCode.Exclude
@ExcludeForHash
private final int version;
// We need deterministic sorting or the map, so we use a treemap
private final TreeMap<Market, MarketPrice> marketPriceByCurrencyMap;

// ExcludeForHash from version 1 on to not treat data from different oracle nodes with different staticPublicKeysProvided value as duplicate data.
// We add version 2 and 3 for extra safety...
// Once no pre version 2.0.5 nodes are expected anymore in the network we can remove the parameter
// and use default `@ExcludeForHash` instead.
@ExcludeForHash(excludeOnlyInVersions = {1, 2, 3})
@EqualsAndHashCode.Exclude
private final boolean staticPublicKeysProvided;

public AuthorizedMarketPriceData(TreeMap<Market, MarketPrice> marketPriceByCurrencyMap, boolean staticPublicKeysProvided) {
public AuthorizedMarketPriceData(TreeMap<Market, MarketPrice> marketPriceByCurrencyMap,
boolean staticPublicKeysProvided) {
this(VERSION, marketPriceByCurrencyMap, staticPublicKeysProvided);
}

public AuthorizedMarketPriceData(int version,
TreeMap<Market, MarketPrice> marketPriceByCurrencyMap,
boolean staticPublicKeysProvided) {
this.version = version;
this.marketPriceByCurrencyMap = marketPriceByCurrencyMap;
this.staticPublicKeysProvided = staticPublicKeysProvided;

Expand All @@ -72,7 +92,8 @@ public bisq.bonded_roles.protobuf.AuthorizedMarketPriceData.Builder getBuilder(b
.putAllMarketPriceByCurrencyMap(marketPriceByCurrencyMap.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey().getMarketCodes(),
e -> e.getValue().toProto(serializeForHash))))
.setStaticPublicKeysProvided(staticPublicKeysProvided);
.setStaticPublicKeysProvided(staticPublicKeysProvided)
.setVersion(version);
}

@Override
Expand All @@ -85,7 +106,11 @@ public static AuthorizedMarketPriceData fromProto(bisq.bonded_roles.protobuf.Aut
.filter(e -> MarketRepository.findAnyMarketByMarketCodes(e.getKey()).isPresent())
.collect(Collectors.toMap(e -> MarketRepository.findAnyMarketByMarketCodes(e.getKey()).orElseThrow(),
e -> MarketPrice.fromProto(e.getValue())));
return new AuthorizedMarketPriceData(new TreeMap<>(map), proto.getStaticPublicKeysProvided());
return new AuthorizedMarketPriceData(
proto.getVersion(),
new TreeMap<>(map),
proto.getStaticPublicKeysProvided()
);
}

public static ProtoResolver<DistributedData> getResolver() {
Expand Down
Loading

0 comments on commit 8b32afd

Please sign in to comment.