Skip to content

Commit

Permalink
make use of the digest helper and also handle digest on every incomin…
Browse files Browse the repository at this point in the history
…g block if present
  • Loading branch information
Grigorov-Georgi committed Jan 14, 2025
1 parent 9707e0b commit 50652f1
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 57 deletions.
68 changes: 25 additions & 43 deletions src/main/java/com/limechain/grandpa/state/RoundState.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
import com.limechain.chain.lightsyncstate.LightSyncState;
import com.limechain.network.protocol.grandpa.messages.catchup.res.SignedVote;
import com.limechain.network.protocol.grandpa.messages.commit.Vote;
import com.limechain.network.protocol.warp.dto.ConsensusEngine;
import com.limechain.network.protocol.warp.dto.HeaderDigest;
import com.limechain.network.protocol.grandpa.messages.consensus.GrandpaConsensusMessage;
import com.limechain.storage.DBConstants;
import com.limechain.storage.KVRepository;
import com.limechain.storage.StateUtil;
import com.limechain.sync.warpsync.dto.AuthoritySetChange;
import com.limechain.sync.warpsync.dto.GrandpaDigestMessageType;
import com.limechain.sync.warpsync.scale.ForcedChangeReader;
import com.limechain.sync.warpsync.scale.ScheduledChangeReader;
import io.emeraldpay.polkaj.scale.ScaleCodecReader;
import com.limechain.sync.warpsync.dto.ForcedAuthoritySetChange;
import com.limechain.sync.warpsync.dto.ScheduledAuthoritySetChange;
import io.libp2p.core.crypto.PubKey;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
Expand Down Expand Up @@ -158,6 +155,7 @@ public void setLightSyncState(LightSyncState initState) {

/**
* Apply scheduled or forced authority set changes from the queue if present
*
* @param blockNumber required to determine if it's time to apply the change
*/
public boolean handleAuthoritySetChange(BigInteger blockNumber) {
Expand All @@ -183,44 +181,28 @@ public boolean handleAuthoritySetChange(BigInteger blockNumber) {

/**
* Handles grandpa consensus message
* @param headerDigests digest of the block header
*
* @param consensusMessage grandpa consensus message provided by any block header digest
*/
public void handleGrandpaConsensusMessage(HeaderDigest[] headerDigests) {
// Update authority set and set id
for (HeaderDigest digest : headerDigests) {
if (digest.getId() == ConsensusEngine.GRANDPA) {
ScaleCodecReader reader = new ScaleCodecReader(digest.getMessage());
GrandpaDigestMessageType type = GrandpaDigestMessageType.fromId(reader.readByte());

if (type == null) {
log.log(Level.SEVERE, "Could not get grandpa message type");
throw new IllegalStateException("Unknown grandpa message type");
}

switch (type) {
case SCHEDULED_CHANGE -> {
ScheduledChangeReader authorityChangesReader = new ScheduledChangeReader();
authoritySetChanges.add(authorityChangesReader.read(reader));
return;
}
case FORCED_CHANGE -> {
ForcedChangeReader authorityForcedChangesReader = new ForcedChangeReader();
authoritySetChanges.add(authorityForcedChangesReader.read(reader));
return;
}
case ON_DISABLED -> {
log.log(Level.SEVERE, "'ON DISABLED' grandpa message not implemented");
return;
}
case PAUSE -> {
log.log(Level.SEVERE, "'PAUSE' grandpa message not implemented");
return;
}
case RESUME -> {
log.log(Level.SEVERE, "'RESUME' grandpa message not implemented");
return;
}
}
public void handleGrandpaConsensusMessage(GrandpaConsensusMessage consensusMessage) {
switch (consensusMessage.getFormat()) {
case GRANDPA_SCHEDULED_CHANGE -> authoritySetChanges.add(new ScheduledAuthoritySetChange(
consensusMessage.getAuthorities(),
consensusMessage.getDelay()
));
case GRANDPA_FORCED_CHANGE -> authoritySetChanges.add(new ForcedAuthoritySetChange(
consensusMessage.getAuthorities(),
consensusMessage.getDelay()
));
//TODO: Implement later
case GRANDPA_ON_DISABLED -> {
log.log(Level.SEVERE, "'ON DISABLED' grandpa message not implemented");
}
case GRANDPA_PAUSE -> {
log.log(Level.SEVERE, "'PAUSE' grandpa message not implemented");
}
case GRANDPA_RESUME -> {
log.log(Level.SEVERE, "'RESUME' grandpa message not implemented");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public class GrandpaConsensusMessage {
private BigInteger delayStartBlockNumber;
private List<Authority> authorities;
private BigInteger disabledAuthority;
private long delay;
private BigInteger delay;
private GrandpaConsensusMessageFormat format;
}
4 changes: 2 additions & 2 deletions src/main/java/com/limechain/storage/block/BlockHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ private void handleBlock(Block block, Instant arrivalTime) {
log.fine(String.format("Updated epoch block config: %s", cm.getFormat().toString()));
});

//TODO
DigestHelper.getGrandpaConsensusMessage(header.getDigest())
.ifPresent(cm -> {
// roundState.
roundState.handleGrandpaConsensusMessage(cm);
log.fine(String.format("Updated grandpa set config: %s", cm.getFormat().toString()));
});

asyncExecutor.executeAndForget(() -> transactionProcessor.maintainTransactionPool(block));
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/com/limechain/sync/warpsync/WarpSyncState.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.limechain.sync.warpsync;

import com.limechain.chain.lightsyncstate.Authority;
import com.limechain.exception.global.RuntimeCodeException;
import com.limechain.exception.trie.TrieDecoderException;
import com.limechain.grandpa.state.RoundState;
Expand All @@ -12,10 +11,9 @@
import com.limechain.network.protocol.lightclient.pb.LightClientMessage;
import com.limechain.network.protocol.sync.BlockRequestField;
import com.limechain.network.protocol.sync.pb.SyncMessage.BlockData;
import com.limechain.network.protocol.warp.DigestHelper;
import com.limechain.network.protocol.warp.dto.BlockHeader;
import com.limechain.network.protocol.warp.dto.ConsensusEngine;
import com.limechain.network.protocol.warp.dto.DigestType;
import com.limechain.network.protocol.warp.dto.HeaderDigest;
import com.limechain.network.protocol.warp.dto.Justification;
import com.limechain.network.protocol.warp.scale.reader.BlockHeaderReader;
import com.limechain.network.protocol.warp.scale.reader.JustificationReader;
Expand All @@ -25,10 +23,6 @@
import com.limechain.storage.KVRepository;
import com.limechain.storage.block.SyncState;
import com.limechain.sync.JustificationVerifier;
import com.limechain.sync.warpsync.dto.AuthoritySetChange;
import com.limechain.sync.warpsync.dto.GrandpaDigestMessageType;
import com.limechain.sync.warpsync.scale.ForcedChangeReader;
import com.limechain.sync.warpsync.scale.ScheduledChangeReader;
import com.limechain.trie.decoded.Trie;
import com.limechain.trie.decoded.TrieVerifier;
import com.limechain.utils.LittleEndianUtils;
Expand All @@ -39,14 +33,11 @@
import lombok.Getter;
import lombok.Setter;
import lombok.extern.java.Log;
import org.javatuples.Pair;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.logging.Level;

Expand Down Expand Up @@ -303,7 +294,9 @@ private void updateSetData(BigInteger setChangeBlock) {
BlockHeader header = new BlockHeaderReader().read(new ScaleCodecReader(block.getHeader().toByteArray()));

syncState.finalizeHeader(header);
roundState.handleGrandpaConsensusMessage(header.getDigest());

DigestHelper.getGrandpaConsensusMessage(header.getDigest())
.ifPresent(roundState::handleGrandpaConsensusMessage);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.math.BigInteger;
import java.util.Comparator;
import java.util.List;

@Getter
@Setter
Expand All @@ -17,6 +18,10 @@ public class AuthoritySetChange {
private Authority[] authorities;
private BigInteger delay;

public AuthoritySetChange(List<Authority> authorities, BigInteger delay) {
this(authorities.toArray(new Authority[0]), delay);
}

// ForcedAuthoritySetChange has priority over ScheduledAuthoritySetChanges
public static Comparator<AuthoritySetChange> getComparator() {
return (c1, c2) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import com.limechain.chain.lightsyncstate.Authority;

import java.math.BigInteger;
import java.util.List;

public class ForcedAuthoritySetChange extends AuthoritySetChange {
public ForcedAuthoritySetChange(Authority[] authorities, BigInteger delay) {
super(authorities, delay);
}

public ForcedAuthoritySetChange(List<Authority> authorities, BigInteger delay) {
super(authorities, delay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import com.limechain.chain.lightsyncstate.Authority;

import java.math.BigInteger;
import java.util.List;

public class ScheduledAuthoritySetChange extends AuthoritySetChange {
public ScheduledAuthoritySetChange(Authority[] authorities, BigInteger delay) {
super(authorities, delay);
}

public ScheduledAuthoritySetChange(List<Authority> authorities, BigInteger delay) {
super(authorities, delay);
}
}

0 comments on commit 50652f1

Please sign in to comment.