Skip to content

Commit

Permalink
- Add the use of serializeCompactV2
Browse files Browse the repository at this point in the history
- Add the use of deserializeCompactV2
  • Loading branch information
nathanieliov committed Nov 26, 2024
1 parent 11d1a8b commit d9cab35
Show file tree
Hide file tree
Showing 3 changed files with 468 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package co.rsk.peg;

import static co.rsk.bitcoinj.core.StoredBlock.deserializeCompactLegacy;
import static co.rsk.bitcoinj.core.StoredBlock.deserializeCompactV2;

import co.rsk.bitcoinj.core.BtcBlock;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.bitcoinj.core.Sha256Hash;
Expand Down Expand Up @@ -308,17 +311,36 @@ public StoredBlock getStoredBlockAtMainChainDepth(int depth) throws BlockStoreEx
}

private byte[] storedBlockToByteArray(StoredBlock block) {
ByteBuffer byteBuffer = ByteBuffer.allocate(128);
block.serializeCompact(byteBuffer);
ByteBuffer byteBuffer = serializeBlock(block);
byte[] ba = new byte[byteBuffer.position()];
byteBuffer.flip();
byteBuffer.get(ba);
return ba;
}

private ByteBuffer serializeBlock(StoredBlock block) {
if (shouldUseLegacy12ByteChainworkFormat()) {
ByteBuffer byteBuffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE_LEGACY);
block.serializeCompactLegacy(byteBuffer);
return byteBuffer;
}

ByteBuffer byteBuffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE_V2);
block.serializeCompactV2(byteBuffer);
return byteBuffer;
}

private boolean shouldUseLegacy12ByteChainworkFormat() {
return !activations.isActive(ConsensusRule.RSKIP454);
}

private StoredBlock byteArrayToStoredBlock(byte[] ba) {
ByteBuffer byteBuffer = ByteBuffer.wrap(ba);
return StoredBlock.deserializeCompact(btcNetworkParams, byteBuffer);
if (ba.length == StoredBlock.COMPACT_SERIALIZED_SIZE_LEGACY) {
return deserializeCompactLegacy(btcNetworkParams, byteBuffer);
}

return deserializeCompactV2(btcNetworkParams, byteBuffer);
}

private void checkIfInitialized() {
Expand Down
4 changes: 2 additions & 2 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,10 @@ private InputStream getCheckpoints(NetworkParameters networkParameters, List<Btc
dataOutputStream.writeInt(0); // Number of signatures to read. Do this later.
digestOutputStream.on(true);
dataOutputStream.writeInt(checkpoints.size());
ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE_LEGACY);
for (BtcBlock block : checkpoints) {
storedBlock = storedBlock.build(block);
storedBlock.serializeCompact(buffer);
storedBlock.serializeCompactLegacy(buffer);
dataOutputStream.write(buffer.array());
buffer.position(0);
}
Expand Down
Loading

0 comments on commit d9cab35

Please sign in to comment.