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

Add serialization for btc tx to avoid passing boolean, and reuse it when possible #2734

Conversation

julia-zack
Copy link
Contributor

Also add serialization for rsk tx hash to reuse it when possible

@julia-zack julia-zack requested a review from a team as a code owner September 17, 2024 19:11
Copy link
Contributor

@apancorb apancorb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactor, looking very good :)

Comment on lines 85 to 93
private static BtcTransaction deserializeBtcTransactionFromRLPData(byte[] rlpData, NetworkParameters networkParameters, boolean txHasInputs) {
if (!txHasInputs) {
BtcTransaction tx = new BtcTransaction(networkParameters);
tx.parseNoInputs(rlpData);
return tx;
}

return new BtcTransaction(networkParameters, rlpData);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static BtcTransaction deserializeBtcTransactionFromRLPData(byte[] rlpData, NetworkParameters networkParameters, boolean txHasInputs) {
if (!txHasInputs) {
BtcTransaction tx = new BtcTransaction(networkParameters);
tx.parseNoInputs(rlpData);
return tx;
}
return new BtcTransaction(networkParameters, rlpData);
}
private static BtcTransaction deserializeBtcTransactionFromRLPData(
byte[] rlpData, NetworkParameters networkParameters, boolean hasInputs) {
if (hasInputs) {
return new BtcTransaction(networkParameters, rlpData);
}
BtcTransaction tx = new BtcTransaction(networkParameters);
tx.parseNoInputs(rlpData);
return tx;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i prefer to have the less used case inside the if, to improve readability.
(Currently, we only use this deserialization having inputs)

Comment on lines 71 to 104
public static byte[] serializeRskTxsWaitingForSignatures(
SortedMap<Keccak256, BtcTransaction> rskTxWaitingForSignaturesMap) {
public static byte[] serializeRskTxsWaitingForSignatures(SortedMap<Keccak256, BtcTransaction> rskTxWaitingForSignaturesMap) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Google Java Style Guide enforces a 100-character line limit, is there any reason why this is being changed across the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad, sorry

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines 95 to 115
public static BtcTransaction deserializeSvpFundTx(byte[] data, NetworkParameters networkParameters) {
return deserializeBtcTransaction(data, networkParameters, true);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a serialize method that complements this one?


// Act
byte[] serializedSvpFundTransaction = BridgeSerializationUtils.serializeBtcTransaction(svpFundTx);
BtcTransaction deserializedSvpFundTransaction = BridgeSerializationUtils.deserializeSvpFundTx(serializedSvpFundTransaction, NETWORK_PARAMETERS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we really need this method that simply acts as a wrapper. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats one of the points of this refactor, having specific methods when deserializing txs we know they have inputs to avoid passing the boolean.
We need to keep the whole txHasInputs thing since its part of bitcoin, but is not the cleanest thing to do 😄

@Test
void serializeAndDeserializeBtcTransaction_withValidDataAndInputs_shouldReturnEqualResults() {
// Arrange
BtcTransaction fundTx = new BtcTransaction(NETWORK_PARAMETERS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fundTx might give the impression this is something related to svp, what about we call it prevTx here as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree! Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Comment on lines -75 to +130
int n = 0;

int n = 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was moved to be closer from where its being used

@marcos-iov marcos-iov force-pushed the feat/refactor_bridge_serialization_utils branch from 5a1e6c4 to 44c4644 Compare September 18, 2024 19:02
Copy link

@marcos-iov marcos-iov merged commit 98ed61b into feature/powpeg_validation_protocol-phase2 Sep 18, 2024
6 checks passed
@marcos-iov marcos-iov deleted the feat/refactor_bridge_serialization_utils branch September 18, 2024 19:29
@marcos-iov marcos-iov restored the feat/refactor_bridge_serialization_utils branch September 19, 2024 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants