Skip to content

Commit

Permalink
Keep bitcoin impl for deserializeBtcTransaction method, but create me…
Browse files Browse the repository at this point in the history
…thods to deserialize btc txs with and without inputs.
  • Loading branch information
julia-zack committed Sep 18, 2024
1 parent e468239 commit 5a1e6c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
21 changes: 15 additions & 6 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ public static byte[] serializeBtcTransaction(BtcTransaction btcTransaction) {
return RLP.encodeElement(btcTransaction.bitcoinSerialize());
}

public static BtcTransaction deserializeBtcTransaction(
public static BtcTransaction deserializeBtcTransactionWithInputs(byte[] serializedTx, NetworkParameters networkParameters) {
return deserializeBtcTransaction(serializedTx, networkParameters, true);
}

public static BtcTransaction deserializeBtcTransactionWithoutInputs(byte[] serializedTx, NetworkParameters networkParameters) {
return deserializeBtcTransaction(serializedTx, networkParameters, false);
}

private static BtcTransaction deserializeBtcTransaction(
byte[] serializedTx,
NetworkParameters networkParameters,
boolean txHasInputs) {
Expand All @@ -84,9 +92,14 @@ public static BtcTransaction deserializeBtcTransaction(

RLPElement rawTxElement = RLP.decode2(serializedTx).get(0);
byte[] rawTx = rawTxElement.getRLPData();

return deserializeBtcTransactionFromRawTx(rawTx, networkParameters, txHasInputs);
}

private static BtcTransaction deserializeBtcTransactionWithInputsFromRawTx(byte[] rawTx, NetworkParameters networkParameters) {
return deserializeBtcTransactionFromRawTx(rawTx, networkParameters, true);
}

private static BtcTransaction deserializeBtcTransactionFromRawTx(
byte[] rawTx,
NetworkParameters networkParameters,
Expand All @@ -101,10 +114,6 @@ private static BtcTransaction deserializeBtcTransactionFromRawTx(
return new BtcTransaction(networkParameters, rawTx);
}

public static BtcTransaction deserializeSvpFundTx(byte[] serializedTx, NetworkParameters networkParameters) {
return deserializeBtcTransaction(serializedTx, networkParameters, true);
}

public static byte[] serializeRskTxWaitingForSignatures(
Map.Entry<Keccak256, BtcTransaction> rskTxWaitingForSignaturesEntry) {
byte[][] serializedRskTxWaitingForSignaturesEntry =
Expand Down Expand Up @@ -181,7 +190,7 @@ private static Map.Entry<Keccak256, BtcTransaction> deserializeRskTxWaitingForSi

RLPElement btcTxRLPElement = rlpList.get(index * 2 + 1);
byte[] btcRawTx = btcTxRLPElement.getRLPData();
BtcTransaction btcTx = deserializeBtcTransactionFromRawTx(btcRawTx, networkParameters, true);
BtcTransaction btcTx = deserializeBtcTransactionWithInputsFromRawTx(btcRawTx, networkParameters);

return new AbstractMap.SimpleEntry<>(rskTxHash, btcTx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void serializeAndDeserializeBtcTransaction_withValidDataAndInputs_shouldReturnEq

// Act
byte[] serializedBtcTransaction = BridgeSerializationUtils.serializeBtcTransaction(btcTx);
BtcTransaction deserializedBtcTransaction = BridgeSerializationUtils.deserializeBtcTransaction(serializedBtcTransaction, MAINNET_PARAMETERS, true);
BtcTransaction deserializedBtcTransaction = BridgeSerializationUtils.deserializeBtcTransactionWithInputs(serializedBtcTransaction, MAINNET_PARAMETERS);

// Assert
assertNotNull(serializedBtcTransaction);
Expand All @@ -126,7 +126,7 @@ void serializeAndDeserializeBtcTransaction_withValidDataAndWithoutInputs_shouldR

// Act
byte[] serializedBtcTransaction = BridgeSerializationUtils.serializeBtcTransaction(btcTx);
BtcTransaction deserializedBtcTransaction = BridgeSerializationUtils.deserializeBtcTransaction(serializedBtcTransaction, MAINNET_PARAMETERS, false);
BtcTransaction deserializedBtcTransaction = BridgeSerializationUtils.deserializeBtcTransactionWithoutInputs(serializedBtcTransaction, MAINNET_PARAMETERS);

// Assert
assertNotNull(serializedBtcTransaction);
Expand All @@ -139,8 +139,8 @@ void serializeAndDeserializeBtcTransaction_withValidDataAndWithoutInputs_shouldR
@EmptySource
void deserializeBtcTransaction_withInvalidData_shouldReturnNull(byte[] data) {
// Act
BtcTransaction deserializedTxWithInputs = BridgeSerializationUtils.deserializeBtcTransaction(data, MAINNET_PARAMETERS, true);
BtcTransaction deserializedTxWithoutInputs = BridgeSerializationUtils.deserializeBtcTransaction(data, MAINNET_PARAMETERS, false);
BtcTransaction deserializedTxWithInputs = BridgeSerializationUtils.deserializeBtcTransactionWithInputs(data, MAINNET_PARAMETERS);
BtcTransaction deserializedTxWithoutInputs = BridgeSerializationUtils.deserializeBtcTransactionWithoutInputs(data, MAINNET_PARAMETERS);

// Assert
assertNull(deserializedTxWithInputs);
Expand All @@ -161,7 +161,7 @@ void serializeAndDeserializeSvpFundTransaction_withValidData_shouldReturnEqualRe

// Act
byte[] serializedSvpFundTransaction = BridgeSerializationUtils.serializeBtcTransaction(svpFundTx);
BtcTransaction deserializedSvpFundTransaction = BridgeSerializationUtils.deserializeSvpFundTx(serializedSvpFundTransaction, MAINNET_PARAMETERS);
BtcTransaction deserializedSvpFundTransaction = BridgeSerializationUtils.deserializeBtcTransactionWithInputs(serializedSvpFundTransaction, MAINNET_PARAMETERS);

// Assert
assertNotNull(serializedSvpFundTransaction);
Expand All @@ -174,7 +174,7 @@ void serializeAndDeserializeSvpFundTransaction_withValidData_shouldReturnEqualRe
@EmptySource
void deserializeSvpFundTransaction_withInvalidData_shouldReturnNull(byte[] data) {
// Act
BtcTransaction result = BridgeSerializationUtils.deserializeSvpFundTx(data, MAINNET_PARAMETERS);
BtcTransaction result = BridgeSerializationUtils.deserializeBtcTransactionWithInputs(data, MAINNET_PARAMETERS);

// Assert
assertNull(result);
Expand Down

0 comments on commit 5a1e6c4

Please sign in to comment.