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

[2] Add exclude for hash annotation #7151

Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions common/src/main/java/bisq/common/ExcludeForHash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcludeForHash {
}
119 changes: 119 additions & 0 deletions common/src/main/java/bisq/common/ExcludeForHashAwareProto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common;


import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;

import java.io.IOException;
import java.io.OutputStream;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Borrowed from Bisq2, but as we want to reduce risk of breaking things, specially for the DAO we pack
* the support for ExcludeForHash annotations into a dedicated interface.
* Classes which use ExcludeForHash need to implement that interface.
*/
public interface ExcludeForHashAwareProto extends Proto {

////////////////////////////////////////////////////////////////////////////////
// Override Proto methods
////////////////////////////////////////////////////////////////////////////////

default Message toProtoMessage() {
return completeProto();
}

default byte[] serialize() {
return resolveProto(false).toByteArray();
}

default byte[] serializeForHash() {
return resolveProto(true).toByteArray();
}


////////////////////////////////////////////////////////////////////////////////
// API
////////////////////////////////////////////////////////////////////////////////

Message.Builder getBuilder(boolean serializeForHash);

Message toProto(boolean serializeForHash);

default Message completeProto() {
return toProto(false);
}

default <T extends Message> T resolveProto(boolean serializeForHash) {
//noinspection unchecked
return (T) resolveBuilder(getBuilder(serializeForHash), serializeForHash).build();
}

default <B extends Message.Builder> B resolveBuilder(B builder, boolean serializeForHash) {
return serializeForHash ? clearAnnotatedFields(builder) : builder;
}

default int getSerializedSize() {
return resolveProto(false).getSerializedSize();
}

default void writeDelimitedTo(OutputStream outputStream) throws IOException {
completeProto().writeDelimitedTo(outputStream);
}

default Set<String> getExcludedFields() {
return Arrays.stream(getClass().getDeclaredFields())
.peek(field -> field.setAccessible(true))
.filter(field -> field.isAnnotationPresent(ExcludeForHash.class))
.map(Field::getName)
.collect(Collectors.toSet());
}

/**
* Requires that the name of the java fields is the same as the name of the proto definition.
*
* @param builder The builder we transform by clearing the ExcludeForHash annotated fields.
* @return Builder with the fields annotated with ExcludeForHash cleared.
*/
default <B extends Message.Builder> B clearAnnotatedFields(B builder) {
Set<String> excludedFields = getExcludedFields();
if (!excludedFields.isEmpty()) {
getLogger().error("Clear fields in builder annotated with @ExcludeForHash: {}", excludedFields);
}
for (Descriptors.FieldDescriptor fieldDesc : builder.getAllFields().keySet()) {
if (excludedFields.contains(fieldDesc.getName())) {
builder.clearField(fieldDesc);
}
}
return builder;
}

private Logger getLogger() {
return LoggerFactory.getLogger(getClass().getSimpleName());
}
}
2 changes: 2 additions & 0 deletions common/src/main/java/bisq/common/Proto.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ default byte[] serialize() {
return toProtoMessage().toByteArray();
}

// If the class implements ExcludedFieldsProto this method will be overwritten so that
// fields annotated with ExcludeForHash will be excluded.
default byte[] serializeForHash() {
return serialize();
}
Expand Down
Loading