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

[1] Refactor Proto interface #7150

Merged
merged 2 commits into from
Jun 8, 2024
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
8 changes: 8 additions & 0 deletions common/src/main/java/bisq/common/Proto.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@
*/
public interface Proto {
Message toProtoMessage();

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

default byte[] serializeForHash() {
return serialize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public abstract class AccountingNode implements DaoSetupService, DaoStateListene
///////////////////////////////////////////////////////////////////////////////////////////

public static Sha256Hash getSha256Hash(AccountingBlock block) {
return Sha256Hash.of(block.toProtoMessage().toByteArray());
return Sha256Hash.of(block.serializeForHash());
}

@Nullable
public static Sha256Hash getSha256Hash(Collection<AccountingBlock> blocks) {
long ts = System.currentTimeMillis();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
for (AccountingBlock accountingBlock : blocks) {
outputStream.write(accountingBlock.toProtoMessage().toByteArray());
outputStream.write(accountingBlock.serializeForHash());
}
Sha256Hash hash = Sha256Hash.of(outputStream.toByteArray());
// 2833 blocks takes about 23 ms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public static SecretKey createSecretKey() {
}

public static byte[] getEncryptedVotes(VoteWithProposalTxIdList voteWithProposalTxIdList, SecretKey secretKey) throws CryptoException {
byte[] bytes = voteWithProposalTxIdList.toProtoMessage().toByteArray();
byte[] bytes = voteWithProposalTxIdList.serialize();
byte[] encrypted = Encryption.encrypt(bytes, secretKey);
log.info("EncryptedVotes: " + Utilities.bytesAsHexString(encrypted));
return encrypted;
}

public static byte[] getEncryptedMeritList(MeritList meritList, SecretKey secretKey) throws CryptoException {
byte[] bytes = meritList.toProtoMessage().toByteArray();
byte[] bytes = meritList.serialize();
return Encryption.encrypt(bytes, secretKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class BlindVotePayload implements PersistableNetworkPayload, Consen
protected final byte[] hash; // 20 byte

public BlindVotePayload(BlindVote blindVote) {
this(blindVote, Hash.getRipemd160hash(blindVote.toProtoMessage().toByteArray()));
this(blindVote, Hash.getRipemd160hash(blindVote.serializeForHash()));
}

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static Coin getFee(DaoStateService daoStateService, int chainHeight) {
}

public static byte[] getHashOfPayload(Proposal payload) {
final byte[] bytes = payload.toProtoMessage().toByteArray();
final byte[] bytes = payload.serializeForHash();
return Hash.getSha256Ripemd160hash(bytes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ProposalPayload implements PersistableNetworkPayload, ConsensusCrit
protected final byte[] hash; // 20 byte

public ProposalPayload(Proposal proposal) {
this(proposal, Hash.getRipemd160hash(proposal.toProtoMessage().toByteArray()));
this(proposal, Hash.getRipemd160hash(proposal.serializeForHash()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class VoteRevealConsensus {
public static byte[] getHashOfBlindVoteList(List<BlindVote> blindVotes) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
blindVotes.forEach(blindVote -> {
byte[] data = blindVote.toProtoMessage().toByteArray();
byte[] data = blindVote.serializeForHash();
try {
outputStream.write(data);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private boolean maybeUpdateHashChain(int blockHeight) {
.collect(Collectors.toList());

// We use MyBlindVoteList to get the serialized bytes from the blindVotes list
byte[] serializedBlindVotes = new MyBlindVoteList(blindVotes).toProtoMessage().toByteArray();
byte[] serializedBlindVotes = new MyBlindVoteList(blindVotes).serializeForHash();

byte[] prevHash;
if (blindVoteStateBlockChain.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private boolean maybeUpdateHashChain(int blockHeight) {
.collect(Collectors.toList());

// We use MyProposalList to get the serialized bytes from the proposals list
byte[] serializedProposals = new MyProposalList(proposals).toProtoMessage().toByteArray();
byte[] serializedProposals = new MyProposalList(proposals).serializeForHash();

byte[] prevHash;
if (proposalStateBlockChain.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private Role(String uid,
this.link = link;
this.bondedRoleType = bondedRoleType;

hash = Hash.getSha256Ripemd160hash(toProtoMessage().toByteArray());
hash = Hash.getSha256Ripemd160hash(serializeForHash());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/filter/FilterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ private ECKey toECKey(String privKeyString) {
}

private Sha256Hash getSha256Hash(Filter filter) {
byte[] filterData = filter.toProtoMessage().toByteArray();
byte[] filterData = filter.serializeForHash();
return Sha256Hash.of(filterData);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/offer/OfferPayloadBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public OfferPayloadBase(String id,

public byte[] getHash() {
if (this.hash == null) {
this.hash = Hash.getSha256Hash(this.toProtoMessage().toByteArray());
this.hash = Hash.getSha256Hash(serializeForHash());
}
return this.hash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public byte[] getHash() {
if (this.hash == null && this.offerFeePaymentTxId != null) {
// A proto message can be created only after the offerFeePaymentTxId is
// set to a non-null value; now is the time to cache the payload hash.
this.hash = Hash.getSha256Hash(this.toProtoMessage().toByteArray());
this.hash = Hash.getSha256Hash(serializeForHash());
}
return this.hash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
public class ProcessModel implements ProtocolModel<TradingPeer> {

public static byte[] hashOfPaymentAccountPayload(PaymentAccountPayload paymentAccountPayload) {
return Hash.getRipemd160hash(checkNotNull(paymentAccountPayload).toProtoMessage().toByteArray());
return Hash.getRipemd160hash(checkNotNull(paymentAccountPayload).serializeForHash());
}

// Transient/Immutable (net set in constructor so they are not final, but at init)
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/bisq/core/filter/TestFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static Filter createFilter(PublicKey ownerPublicKey, String signerPubKeyA
}

public static Filter signFilter(Filter unsignedFilter, ECKey signerKey) {
byte[] filterData = unsignedFilter.toProtoMessage().toByteArray();
byte[] filterData = unsignedFilter.serializeForHash();
Sha256Hash hash = Sha256Hash.of(filterData);

ECKey.ECDSASignature ecdsaSignature = signerKey.sign(hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ private String printPersistableNetworkPayloadMap(Map<ByteArray, PersistableNetwo
* @return Hash of data
*/
public static byte[] get32ByteHash(NetworkPayload data) {
return Hash.getSha256Hash(data.toProtoMessage().toByteArray());
return Hash.getSha256Hash(data.serializeForHash());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ private ProtectedStorageEntry getProtectedStorageEntryForAdd(Capabilities requir
Message messageMock = mock(Message.class);
when(messageMock.toByteArray()).thenReturn(Sig.getPublicKeyBytes(ownerKeys.getPublic()));
when(protectedStoragePayload.toProtoMessage()).thenReturn(messageMock);
when(protectedStoragePayload.serialize()).thenReturn(new byte[]{});
when(protectedStoragePayload.serializeForHash()).thenReturn(new byte[]{});

// Entry stub
ProtectedStorageEntry stub = mock(ProtectedStorageEntry.class);
Expand Down
2 changes: 2 additions & 0 deletions p2p/src/test/java/bisq/network/p2p/storage/TestState.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ static MailboxStoragePayload buildMailboxStoragePayload(PublicKey senderKey, Pub
when(payloadMock.getOwnerPubKey()).thenReturn(receiverKey);
when(payloadMock.getSenderPubKeyForAddOperation()).thenReturn(senderKey);
when(payloadMock.toProtoMessage()).thenReturn(messageMock);
when(payloadMock.serialize()).thenReturn(new byte[]{});
when(payloadMock.serializeForHash()).thenReturn(new byte[]{});

return payloadMock;
}
Expand Down
Loading