From 25c7b93e4428147393283c7b3e64351641dc54fe Mon Sep 17 00:00:00 2001 From: Grigorov-Georgi Date: Wed, 15 Jan 2025 16:59:32 +0200 Subject: [PATCH] refactor after pr --- .../java/com/limechain/grandpa/state/RoundState.java | 7 +------ .../messages/consensus/GrandpaConsensusMessage.java | 4 ++-- .../consensus/GrandpaConsensusMessageReader.java | 11 +++++++---- .../sync/warpsync/dto/AuthoritySetChange.java | 10 +++++----- .../sync/warpsync/dto/ForcedAuthoritySetChange.java | 6 +++--- .../warpsync/dto/ScheduledAuthoritySetChange.java | 2 +- .../consensus/GrandpaConsensusMessageReaderTest.java | 10 +++++----- 7 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/limechain/grandpa/state/RoundState.java b/src/main/java/com/limechain/grandpa/state/RoundState.java index 3be3d8c3..e91d36fa 100644 --- a/src/main/java/com/limechain/grandpa/state/RoundState.java +++ b/src/main/java/com/limechain/grandpa/state/RoundState.java @@ -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; } @@ -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( 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 878b8da2..8f18f9bd 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,7 +11,7 @@ public class GrandpaConsensusMessage { private GrandpaConsensusMessageFormat format; private List 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; } diff --git a/src/main/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReader.java b/src/main/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReader.java index 87931ba8..eb8ac2b2 100644 --- a/src/main/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReader.java +++ b/src/main/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReader.java @@ -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 { @@ -21,22 +22,24 @@ public GrandpaConsensusMessage read(ScaleCodecReader reader) { switch (format) { case GRANDPA_SCHEDULED_CHANGE -> { List 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 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; 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 e7f7d838..fee7d03a 100644 --- a/src/main/java/com/limechain/sync/warpsync/dto/AuthoritySetChange.java +++ b/src/main/java/com/limechain/sync/warpsync/dto/AuthoritySetChange.java @@ -13,16 +13,16 @@ public class AuthoritySetChange { private List authorities; - private Long delay; - private BigInteger applicationBlock; + private BigInteger delay; + private BigInteger applicationBlockNumber; - public AuthoritySetChange(List authorities, Long delay, BigInteger announceBlock) { + public AuthoritySetChange(List 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 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 325896ee..557e4de2 100644 --- a/src/main/java/com/limechain/sync/warpsync/dto/ForcedAuthoritySetChange.java +++ b/src/main/java/com/limechain/sync/warpsync/dto/ForcedAuthoritySetChange.java @@ -7,10 +7,10 @@ public class ForcedAuthoritySetChange extends AuthoritySetChange { public ForcedAuthoritySetChange(List authorities, - Long delay, - Long additionalOffset, + BigInteger delay, + BigInteger additionalOffset, BigInteger announceBlock) { - super(authorities, delay + additionalOffset, announceBlock); + super(authorities, delay.add(additionalOffset), announceBlock); } } 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 83457eaa..fd513f6a 100644 --- a/src/main/java/com/limechain/sync/warpsync/dto/ScheduledAuthoritySetChange.java +++ b/src/main/java/com/limechain/sync/warpsync/dto/ScheduledAuthoritySetChange.java @@ -6,7 +6,7 @@ import java.util.List; public class ScheduledAuthoritySetChange extends AuthoritySetChange { - public ScheduledAuthoritySetChange(List authorities, Long delay, BigInteger announceBlock) { + public ScheduledAuthoritySetChange(List authorities, BigInteger delay, BigInteger announceBlock) { super(authorities, delay, announceBlock); } } diff --git a/src/test/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReaderTest.java b/src/test/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReaderTest.java index 6dea32a9..1643407e 100644 --- a/src/test/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReaderTest.java +++ b/src/test/java/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReaderTest.java @@ -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 @@ -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 @@ -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 @@ -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()); } }