Skip to content

Commit

Permalink
fix: Implement a fix for polkaj's Hash256 and Hash512 serialization i…
Browse files Browse the repository at this point in the history
…ssues. (#721)

# Description
The Hash512 and Hash256 classes don't work with java's default
serialization logic. Considering they are nothing but wrapper around a
byte array field it is adequate to only serialize/deserialize the byte
array value of each instance. This PR implements changes needed in
Fruzhin after the update in polkaj

Fixes #717
  • Loading branch information
Zurcusa authored Jan 27, 2025
1 parent 1101e04 commit 6a1c285
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
Binary file removed libs/polkaj-common-types-0.5.0-SNAPSHOT.jar
Binary file not shown.
Binary file added libs/polkaj-common-types-0.5.4-SNAPSHOT.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ public void saveAuthoritySetId() {
}

public BigInteger fetchAuthoritiesSetId() {
return repository.find(DBConstants.SET_ID, BigInteger.ONE);
return repository.find(DBConstants.SET_ID, BigInteger.ZERO);
}

public void saveLatestRound() {
repository.save(DBConstants.LATEST_ROUND, roundCache.getLatestRound(setId));
}

public BigInteger fetchLatestRound() {
return repository.find(DBConstants.LATEST_ROUND, BigInteger.ONE);
public GrandpaRound fetchLatestRound() {
return repository.find(DBConstants.LATEST_ROUND, new GrandpaRound());
}

public void savePreVotes(BigInteger roundNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.limechain.network.ConnectionManager;
import com.limechain.network.protocol.transaction.scale.TransactionReader;
import com.limechain.rpc.server.AppBean;
import com.limechain.state.AbstractState;
import com.limechain.sync.SyncMode;
import com.limechain.sync.warpsync.WarpSyncState;
import com.limechain.transaction.TransactionProcessor;
import com.limechain.transaction.dto.ExtrinsicArray;
Expand Down Expand Up @@ -87,6 +89,11 @@ private void handleResponderStreamMessage(byte[] message, Stream stream) {
return;
}

if (!SyncMode.HEAD.equals(AbstractState.getSyncMode())) {
log.fine("Skipping transaction message before we reach head of chain.");
return;
}

if (isHandshake(message)) {
handleHandshake(peerId, stream);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void initialize() {

setArrivalTime(genesisBlockHash, Instant.now());
setHeader(genesisBlockHeader);
db.save(BlockStateHelper.headerHashKey(genesisBlockHeader.getBlockNumber()), genesisBlockHash.getBytes());
db.save(BlockStateHelper.headerHashKey(genesisBlockHeader.getBlockNumber()), genesisBlockHash);
setBlockBody(genesisBlockHash, new BlockBody(new ArrayList<>()));

//set the latest finalized head to the genesis header
Expand Down Expand Up @@ -783,7 +783,7 @@ && getHighestFinalizedNumber().compareTo(header.getBlockNumber()) >= 0) {
}

handleFinalizedBlock(hash);
db.save(BlockStateHelper.finalizedHashKey(round, setId), hash.getBytes());
db.save(BlockStateHelper.finalizedHashKey(round, setId), hash);
setHighestRoundAndSetID(round, setId);

if (round.compareTo(BigInteger.ZERO) > 0) {
Expand Down Expand Up @@ -849,7 +849,7 @@ public Hash256 getFinalizedHash(final BigInteger round, final BigInteger setId)
throw new HeaderNotFoundException("Header not found in database");
}

return new Hash256((byte[]) foundHash.get());
return (Hash256) foundHash.get();
}

/**
Expand Down Expand Up @@ -934,7 +934,7 @@ public void handleFinalizedBlock(final Hash256 currentFinalizedHash) {
Instant arrivalTime = blockTree.getArrivalTime(subchainHash);
setArrivalTime(subchainHash, arrivalTime);

db.save(BlockStateHelper.headerHashKey(block.getHeader().getBlockNumber()), subchainHash.getBytes());
db.save(BlockStateHelper.headerHashKey(block.getHeader().getBlockNumber()), subchainHash);

// Delete from the unfinalizedBlockMap and delete reference to in-memory trie
unfinalizedBlocks.remove(subchainHash);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/limechain/sync/state/SyncState.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ private void loadFromDatabase() {
@Override
public void persistState() {
repository.save(DBConstants.LAST_FINALIZED_BLOCK_NUMBER, lastFinalizedBlockNumber);
repository.save(DBConstants.LAST_FINALIZED_BLOCK_HASH, lastFinalizedBlockHash.getBytes());
repository.save(DBConstants.STATE_ROOT, stateRoot.getBytes());
repository.save(DBConstants.LAST_FINALIZED_BLOCK_HASH, lastFinalizedBlockHash);
repository.save(DBConstants.STATE_ROOT, stateRoot);
}

public void finalizeHeader(BlockHeader header) {
Expand Down

0 comments on commit 6a1c285

Please sign in to comment.