Skip to content

Commit

Permalink
refactor after pr
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigorov-Georgi committed Jan 15, 2025
1 parent 6645e79 commit 25c7b93
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 26 deletions.
7 changes: 1 addition & 6 deletions src/main/java/com/limechain/grandpa/state/RoundState.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public boolean handleAuthoritySetChange(BigInteger blockNumber) {
boolean updated = false;
while (changeSetData != null) {

if (changeSetData.getApplicationBlock().compareTo(blockNumber) > 0) {
if (changeSetData.getApplicationBlockNumber().compareTo(blockNumber) > 0) {
break;
}

Expand All @@ -178,11 +178,6 @@ public boolean handleAuthoritySetChange(BigInteger blockNumber) {
return updated;
}

/**
* Handles grandpa consensus message
*
* @param consensusMessage grandpa consensus message provided by any block header digest
*/
public void handleGrandpaConsensusMessage(GrandpaConsensusMessage consensusMessage, BigInteger currentBlockNumber) {
switch (consensusMessage.getFormat()) {
case GRANDPA_SCHEDULED_CHANGE -> authoritySetChanges.add(new ScheduledAuthoritySetChange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class GrandpaConsensusMessage {
private GrandpaConsensusMessageFormat format;
private List<Authority> authorities;
private BigInteger disabledAuthority;
private Long delay;
private BigInteger delay;
// this is denoted as 'm' in the polkadot spec
private Long additionalOffset;
private BigInteger additionalOffset;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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> {
Expand All @@ -21,22 +22,24 @@ public GrandpaConsensusMessage read(ScaleCodecReader reader) {
switch (format) {
case GRANDPA_SCHEDULED_CHANGE -> {
List<Authority> authorities = reader.read(new ListReader<>(new AuthorityReader()));
long delay = reader.readUint32();
BigInteger delay = BigInteger.valueOf(reader.readUint32());

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

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

return grandpaConsensusMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
public class AuthoritySetChange {

private List<Authority> authorities;
private Long delay;
private BigInteger applicationBlock;
private BigInteger delay;
private BigInteger applicationBlockNumber;

public AuthoritySetChange(List<Authority> authorities, Long delay, BigInteger announceBlock) {
public AuthoritySetChange(List<Authority> authorities, BigInteger delay, BigInteger announceBlockNumber) {
this.authorities = authorities;
this.delay = delay;
this.applicationBlock = announceBlock.add(BigInteger.valueOf(delay));
this.applicationBlockNumber = announceBlockNumber.add(delay);
}

// ForcedAuthoritySetChange has priority over ScheduledAuthoritySetChanges
// ForcedAuthoritySetChange has priority over ScheduledAuthoritySetChange
public static Comparator<AuthoritySetChange> getComparator() {
return (c1, c2) -> {

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

public class ForcedAuthoritySetChange extends AuthoritySetChange {
public ForcedAuthoritySetChange(List<Authority> authorities,
Long delay,
Long additionalOffset,
BigInteger delay,
BigInteger additionalOffset,
BigInteger announceBlock) {

super(authorities, delay + additionalOffset, announceBlock);
super(authorities, delay.add(additionalOffset), announceBlock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;

public class ScheduledAuthoritySetChange extends AuthoritySetChange {
public ScheduledAuthoritySetChange(List<Authority> authorities, Long delay, BigInteger announceBlock) {
public ScheduledAuthoritySetChange(List<Authority> authorities, BigInteger delay, BigInteger announceBlock) {
super(authorities, delay, announceBlock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void testScheduledChangeInput() {
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_SCHEDULED_CHANGE, message.getFormat());
assertNotNull(message.getAuthorities());
assertEquals(1, message.getAuthorities().size());
assertEquals(768L, message.getDelay());
assertEquals(BigInteger.valueOf(768L), message.getDelay());
}

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

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

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

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

0 comments on commit 25c7b93

Please sign in to comment.