Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduled and Forced Authority Set Change Handling #686

Merged
merged 11 commits into from
Jan 16, 2025
Prev Previous commit
Next Next commit
refactor
Grigorov-Georgi committed Jan 15, 2025
commit 39c71c74f10cb70223bead4e7b70675c35a4792c
14 changes: 9 additions & 5 deletions src/main/java/com/limechain/grandpa/state/RoundState.java
Original file line number Diff line number Diff line change
@@ -164,8 +164,7 @@ public boolean handleAuthoritySetChange(BigInteger blockNumber) {
boolean updated = false;
while (changeSetData != null) {

// Too early to add the new authority set
if (changeSetData.getDelay().compareTo(blockNumber) > 0) {
if (changeSetData.getApplicationBlock().compareTo(blockNumber) > 0) {
break;
}

@@ -184,15 +183,18 @@ public boolean handleAuthoritySetChange(BigInteger blockNumber) {
*
* @param consensusMessage grandpa consensus message provided by any block header digest
*/
public void handleGrandpaConsensusMessage(GrandpaConsensusMessage consensusMessage) {
public void handleGrandpaConsensusMessage(GrandpaConsensusMessage consensusMessage, BigInteger currentBlockNumber) {
switch (consensusMessage.getFormat()) {
case GRANDPA_SCHEDULED_CHANGE -> authoritySetChanges.add(new ScheduledAuthoritySetChange(
consensusMessage.getAuthorities(),
consensusMessage.getDelay()
consensusMessage.getDelay(),
currentBlockNumber
));
case GRANDPA_FORCED_CHANGE -> authoritySetChanges.add(new ForcedAuthoritySetChange(
consensusMessage.getAuthorities(),
consensusMessage.getDelay()
consensusMessage.getDelay(),
consensusMessage.getAdditionalOffset(),
currentBlockNumber
));
//TODO: Implement later
case GRANDPA_ON_DISABLED -> {
@@ -205,5 +207,7 @@ public void handleGrandpaConsensusMessage(GrandpaConsensusMessage consensusMessa
log.log(Level.SEVERE, "'RESUME' grandpa message not implemented");
}
}

log.fine(String.format("Updated grandpa set config: %s", consensusMessage.getFormat().toString()));
}
}
Original file line number Diff line number Diff line change
@@ -8,9 +8,10 @@

@Data
public class GrandpaConsensusMessage {
private BigInteger delayStartBlockNumber;
private GrandpaConsensusMessageFormat format;
private List<Authority> authorities;
private BigInteger disabledAuthority;
private BigInteger delay;
private GrandpaConsensusMessageFormat format;
private Long delay;
// this is denoted as 'm' in the polkadot spec
private Long additionalOffset;
}
Original file line number Diff line number Diff line change
@@ -7,36 +7,38 @@
import io.emeraldpay.polkaj.scale.reader.ListReader;
import io.emeraldpay.polkaj.scale.reader.UInt64Reader;

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

public class GrandpaConsensusMessageReader implements ScaleReader<GrandpaConsensusMessage> {

@Override
public GrandpaConsensusMessage read(ScaleCodecReader reader) {

GrandpaConsensusMessage grandpaConsensusMessage = new GrandpaConsensusMessage();
GrandpaConsensusMessageFormat format = GrandpaConsensusMessageFormat.fromFormat(reader.readByte());
grandpaConsensusMessage.setFormat(format);

switch (format) {
case GRANDPA_SCHEDULED_CHANGE -> {
List<Authority> authorities = reader.read(new ListReader<>(new AuthorityReader()));
long delay = reader.readUint32();

grandpaConsensusMessage.setAuthorities(authorities);
grandpaConsensusMessage.setDelay(BigInteger.valueOf(delay));
grandpaConsensusMessage.setDelay(delay);
}
case GRANDPA_FORCED_CHANGE -> {
long delayStartBlockNumber = reader.readUint32();
long additionalOffset = reader.readUint32();
List<Authority> authorities = reader.read(new ListReader<>(new AuthorityReader()));
long delay = reader.readUint32();
grandpaConsensusMessage.setDelayStartBlockNumber(BigInteger.valueOf(delayStartBlockNumber));

grandpaConsensusMessage.setAuthorities(authorities);
grandpaConsensusMessage.setDelay(BigInteger.valueOf(delay));
grandpaConsensusMessage.setDelay(delay);
grandpaConsensusMessage.setAdditionalOffset(additionalOffset);
}
case GRANDPA_ON_DISABLED -> grandpaConsensusMessage.setDisabledAuthority(new UInt64Reader().read(reader));
case GRANDPA_PAUSE, GRANDPA_RESUME -> grandpaConsensusMessage.setDelay(
BigInteger.valueOf(reader.readUint32())
);
case GRANDPA_PAUSE, GRANDPA_RESUME -> grandpaConsensusMessage.setDelay(reader.readUint32());
}

return grandpaConsensusMessage;
}
}
5 changes: 1 addition & 4 deletions src/main/java/com/limechain/storage/block/BlockHandler.java
Original file line number Diff line number Diff line change
@@ -122,10 +122,7 @@ private void handleBlock(Block block, Instant arrivalTime) {
});

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

asyncExecutor.executeAndForget(() -> transactionProcessor.maintainTransactionPool(block));
}
Original file line number Diff line number Diff line change
@@ -296,7 +296,7 @@ private void updateSetData(BigInteger setChangeBlock) {
syncState.finalizeHeader(header);

DigestHelper.getGrandpaConsensusMessage(header.getDigest())
.ifPresent(roundState::handleGrandpaConsensusMessage);
.ifPresent(cm -> roundState.handleGrandpaConsensusMessage(cm, header.getBlockNumber()));
}
}

Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import com.limechain.grandpa.state.RoundState;
import com.limechain.network.protocol.grandpa.messages.consensus.GrandpaConsensusMessage;
import com.limechain.network.protocol.warp.DigestHelper;
import com.limechain.network.protocol.warp.dto.BlockHeader;
import com.limechain.network.protocol.warp.dto.WarpSyncFragment;
import com.limechain.rpc.server.AppBean;
import com.limechain.storage.block.SyncState;
@@ -74,10 +75,10 @@ public void handle(WarpSyncMachine sync) {
}

private void handleAuthorityChanges(WarpSyncFragment fragment) {
Optional<GrandpaConsensusMessage> grandpaConsensusMessage =
DigestHelper.getGrandpaConsensusMessage(fragment.getHeader().getDigest());
BlockHeader header = fragment.getHeader();

grandpaConsensusMessage.ifPresent(roundState::handleGrandpaConsensusMessage);
DigestHelper.getGrandpaConsensusMessage(header.getDigest())
.ifPresent(cm -> roundState.handleGrandpaConsensusMessage(cm, header.getBlockNumber()));

log.log(Level.INFO, "Verified justification. Block hash is now at #"
+ syncState.getLastFinalizedBlockNumber() + ": "
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.limechain.sync.warpsync.dto;

import com.limechain.chain.lightsyncstate.Authority;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@@ -12,14 +11,17 @@

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class AuthoritySetChange {

private Authority[] authorities;
private BigInteger delay;
private Long delay;
private BigInteger applicationBlock;

public AuthoritySetChange(List<Authority> authorities, BigInteger delay) {
this(authorities.toArray(new Authority[0]), delay);
public AuthoritySetChange(List<Authority> authorities, Long delay, BigInteger announceBlock) {
this.authorities = authorities.toArray(new Authority[0]);
this.delay = delay;
this.applicationBlock = announceBlock.add(BigInteger.valueOf(delay));
}

// ForcedAuthoritySetChange has priority over ScheduledAuthoritySetChanges
georg-getz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -6,11 +6,11 @@
import java.util.List;

public class ForcedAuthoritySetChange extends AuthoritySetChange {
public ForcedAuthoritySetChange(Authority[] authorities, BigInteger delay) {
super(authorities, delay);
}
public ForcedAuthoritySetChange(List<Authority> authorities,
Long delay,
Long additionalOffset,
BigInteger announceBlock) {

public ForcedAuthoritySetChange(List<Authority> authorities, BigInteger delay) {
super(authorities, delay);
super(authorities, delay + additionalOffset, announceBlock);
}
}
Original file line number Diff line number Diff line change
@@ -6,11 +6,7 @@
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);
public ScheduledAuthoritySetChange(List<Authority> authorities, Long delay, BigInteger announceBlock) {
super(authorities, delay, announceBlock);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ void testScheduledChangeInput() {
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_SCHEDULED_CHANGE, message.getFormat());
assertNotNull(message.getAuthorities());
assertEquals(1, message.getAuthorities().size());
assertEquals(BigInteger.valueOf(768L), message.getDelay());
assertEquals(768L, message.getDelay());
}

@Test
@@ -32,11 +32,11 @@ void testForcedChangeInput() {
GrandpaConsensusMessage message = reader.read(new ScaleCodecReader(input));
assertNotNull(message);
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_FORCED_CHANGE, message.getFormat());
assertEquals(BigInteger.valueOf(3), message.getDelayStartBlockNumber());
assertEquals(3, message.getAdditionalOffset());
assertNotNull(message.getAuthorities());

assertEquals(1, message.getAuthorities().size());
assertEquals(BigInteger.valueOf(768L), message.getDelay());
assertEquals(768L, message.getDelay());
}

@Test
@@ -56,7 +56,7 @@ void testPauseInput() {
GrandpaConsensusMessage message = reader.read(new ScaleCodecReader(input));
assertNotNull(message);
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_PAUSE, message.getFormat());
assertEquals(BigInteger.valueOf(20L), message.getDelay());
assertEquals(20L, message.getDelay());
}

@Test
@@ -66,6 +66,6 @@ void testResumeInput() {
GrandpaConsensusMessage message = reader.read(new ScaleCodecReader(input));
assertNotNull(message);
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_RESUME, message.getFormat());
assertEquals(BigInteger.valueOf(25L), message.getDelay());
assertEquals(25L, message.getDelay());
}
}