From 50652f138c9278fef946bdd82cf984b2d1a1be1f Mon Sep 17 00:00:00 2001 From: Grigorov-Georgi Date: Tue, 14 Jan 2025 17:58:11 +0200 Subject: [PATCH] make use of the digest helper and also handle digest on every incoming block if present --- .../limechain/grandpa/state/RoundState.java | 68 +++++++------------ .../consensus/GrandpaConsensusMessage.java | 2 +- .../limechain/storage/block/BlockHandler.java | 4 +- .../sync/warpsync/WarpSyncState.java | 15 ++-- .../sync/warpsync/dto/AuthoritySetChange.java | 5 ++ .../dto/ForcedAuthoritySetChange.java | 5 ++ .../dto/ScheduledAuthoritySetChange.java | 5 ++ 7 files changed, 47 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/limechain/grandpa/state/RoundState.java b/src/main/java/com/limechain/grandpa/state/RoundState.java index 0a2d65e0..81480e6d 100644 --- a/src/main/java/com/limechain/grandpa/state/RoundState.java +++ b/src/main/java/com/limechain/grandpa/state/RoundState.java @@ -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; @@ -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) { @@ -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"); } } } diff --git a/src/main/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessage.java b/src/main/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessage.java index a29e436a..136fa8ea 100644 --- a/src/main/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessage.java +++ b/src/main/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessage.java @@ -11,6 +11,6 @@ public class GrandpaConsensusMessage { private BigInteger delayStartBlockNumber; private List authorities; private BigInteger disabledAuthority; - private long delay; + private BigInteger delay; private GrandpaConsensusMessageFormat format; } diff --git a/src/main/java/com/limechain/storage/block/BlockHandler.java b/src/main/java/com/limechain/storage/block/BlockHandler.java index 5ae83553..1793a8d2 100644 --- a/src/main/java/com/limechain/storage/block/BlockHandler.java +++ b/src/main/java/com/limechain/storage/block/BlockHandler.java @@ -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)); diff --git a/src/main/java/com/limechain/sync/warpsync/WarpSyncState.java b/src/main/java/com/limechain/sync/warpsync/WarpSyncState.java index 10ebcb35..51374309 100644 --- a/src/main/java/com/limechain/sync/warpsync/WarpSyncState.java +++ b/src/main/java/com/limechain/sync/warpsync/WarpSyncState.java @@ -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; @@ -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; @@ -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; @@ -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; @@ -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); } } diff --git a/src/main/java/com/limechain/sync/warpsync/dto/AuthoritySetChange.java b/src/main/java/com/limechain/sync/warpsync/dto/AuthoritySetChange.java index 378705f4..8660340f 100644 --- a/src/main/java/com/limechain/sync/warpsync/dto/AuthoritySetChange.java +++ b/src/main/java/com/limechain/sync/warpsync/dto/AuthoritySetChange.java @@ -8,6 +8,7 @@ import java.math.BigInteger; import java.util.Comparator; +import java.util.List; @Getter @Setter @@ -17,6 +18,10 @@ public class AuthoritySetChange { private Authority[] authorities; private BigInteger delay; + public AuthoritySetChange(List authorities, BigInteger delay) { + this(authorities.toArray(new Authority[0]), delay); + } + // ForcedAuthoritySetChange has priority over ScheduledAuthoritySetChanges public static Comparator getComparator() { return (c1, c2) -> { diff --git a/src/main/java/com/limechain/sync/warpsync/dto/ForcedAuthoritySetChange.java b/src/main/java/com/limechain/sync/warpsync/dto/ForcedAuthoritySetChange.java index e2039dfd..30a618d4 100644 --- a/src/main/java/com/limechain/sync/warpsync/dto/ForcedAuthoritySetChange.java +++ b/src/main/java/com/limechain/sync/warpsync/dto/ForcedAuthoritySetChange.java @@ -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 authorities, BigInteger delay) { + super(authorities, delay); + } } diff --git a/src/main/java/com/limechain/sync/warpsync/dto/ScheduledAuthoritySetChange.java b/src/main/java/com/limechain/sync/warpsync/dto/ScheduledAuthoritySetChange.java index a96e438d..97e275bf 100644 --- a/src/main/java/com/limechain/sync/warpsync/dto/ScheduledAuthoritySetChange.java +++ b/src/main/java/com/limechain/sync/warpsync/dto/ScheduledAuthoritySetChange.java @@ -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 authorities, BigInteger delay) { + super(authorities, delay); + } }