-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented functionality for reading grandpa consensus message
- Loading branch information
Oleksandr
committed
Dec 19, 2024
1 parent
be7cbd0
commit 2722477
Showing
6 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...va/com/limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.limechain.network.protocol.grandpa.messages.consensus; | ||
|
||
import com.limechain.chain.lightsyncstate.Authority; | ||
import lombok.Data; | ||
|
||
import java.math.BigInteger; | ||
import java.util.List; | ||
|
||
@Data | ||
public class GrandpaConsensusMessage { | ||
private long bestFinalizedBlock; | ||
private List<Authority> authorities; | ||
private BigInteger disabledAuthority; | ||
private long delay; | ||
private GrandpaConsensusMessageFormat format; | ||
} |
23 changes: 23 additions & 0 deletions
23
.../limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.limechain.network.protocol.grandpa.messages.consensus; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum GrandpaConsensusMessageFormat { | ||
GRANDPA_SCHEDULED_CHANGE(1), GRANDPA_FORCED_CHANGE(2), GRANDPA_ON_DISABLED(3), GRANDPA_PAUSE(4), GRANDPA_RESUME(5); | ||
|
||
private final int format; | ||
|
||
GrandpaConsensusMessageFormat(int format) { | ||
this.format = format; | ||
} | ||
|
||
public static GrandpaConsensusMessageFormat fromFormat(byte format) { | ||
for (GrandpaConsensusMessageFormat messageFormat : values()) { | ||
if (messageFormat.getFormat() == format) { | ||
return messageFormat; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unknown grandpa consensus message format: " + format); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../limechain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.limechain.network.protocol.grandpa.messages.consensus; | ||
|
||
import com.limechain.chain.lightsyncstate.Authority; | ||
import com.limechain.chain.lightsyncstate.scale.AuthorityReader; | ||
import io.emeraldpay.polkaj.scale.ScaleCodecReader; | ||
import io.emeraldpay.polkaj.scale.ScaleReader; | ||
import io.emeraldpay.polkaj.scale.reader.ListReader; | ||
import io.emeraldpay.polkaj.scale.reader.UInt64Reader; | ||
|
||
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(delay); | ||
} | ||
case GRANDPA_FORCED_CHANGE -> { | ||
long bestFinalizedBlock = reader.readUint32(); | ||
List<Authority> authorities = reader.read(new ListReader<>(new AuthorityReader())); | ||
long delay = reader.readUint32(); | ||
grandpaConsensusMessage.setBestFinalizedBlock(bestFinalizedBlock); | ||
grandpaConsensusMessage.setAuthorities(authorities); | ||
grandpaConsensusMessage.setDelay(delay); | ||
} | ||
case GRANDPA_ON_DISABLED -> grandpaConsensusMessage.setDisabledAuthority(new UInt64Reader().read(reader)); | ||
case GRANDPA_PAUSE, GRANDPA_RESUME -> grandpaConsensusMessage.setDelay(reader.readUint32()); | ||
} | ||
return grandpaConsensusMessage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...echain/network/protocol/grandpa/messages/consensus/GrandpaConsensusMessageReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.limechain.network.protocol.grandpa.messages.consensus; | ||
|
||
import com.limechain.utils.StringUtils; | ||
import io.emeraldpay.polkaj.scale.ScaleCodecReader; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigInteger; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class GrandpaConsensusMessageReaderTest { | ||
|
||
private final GrandpaConsensusMessageReader reader = new GrandpaConsensusMessageReader(); | ||
|
||
@Test | ||
public void testScheduledChangeInput() { | ||
String hexWithPrefix = "0x0104010101010101010101010101010101010101010101010101010101010101010101020000000000000003000000"; | ||
byte[] input = StringUtils.hexToBytes(hexWithPrefix); | ||
GrandpaConsensusMessage message = reader.read(new ScaleCodecReader(input)); | ||
assertNotNull(message); | ||
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_SCHEDULED_CHANGE, message.getFormat()); | ||
assertNotNull(message.getAuthorities()); | ||
assertEquals(1, message.getAuthorities().size()); | ||
assertEquals(768L, message.getDelay()); | ||
} | ||
|
||
@Test | ||
public void testForcedChangeInput() { | ||
String hexWithPrefix = "0x020300000004010101010101010101010101010101010101010101010101010101010101010101020000000000000003000000"; | ||
byte[] input = StringUtils.hexToBytes(hexWithPrefix); | ||
GrandpaConsensusMessage message = reader.read(new ScaleCodecReader(input)); | ||
assertNotNull(message); | ||
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_FORCED_CHANGE, message.getFormat()); | ||
assertEquals(3, message.getBestFinalizedBlock()); | ||
assertNotNull(message.getAuthorities()); | ||
|
||
assertEquals(1, message.getAuthorities().size()); | ||
assertEquals(768L, message.getDelay()); | ||
} | ||
|
||
@Test | ||
public void testOnDisabledInput() { | ||
String hexWithPrefix = "0x0315cd5b0700000000"; | ||
byte[] input = StringUtils.hexToBytes(hexWithPrefix); | ||
GrandpaConsensusMessage message = reader.read(new ScaleCodecReader(input)); | ||
assertNotNull(message); | ||
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_ON_DISABLED, message.getFormat()); | ||
assertEquals(BigInteger.valueOf(123456789L), message.getDisabledAuthority()); | ||
} | ||
|
||
@Test | ||
public void testPauseInput() { | ||
String hexWithPrefix = "0x0414000000"; | ||
byte[] input = StringUtils.hexToBytes(hexWithPrefix); | ||
GrandpaConsensusMessage message = reader.read(new ScaleCodecReader(input)); | ||
assertNotNull(message); | ||
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_PAUSE, message.getFormat()); | ||
assertEquals(20L, message.getDelay()); | ||
} | ||
|
||
@Test | ||
public void testResumeInput() { | ||
String hexWithPrefix = "0x0519000000"; | ||
byte[] input = StringUtils.hexToBytes(hexWithPrefix); | ||
GrandpaConsensusMessage message = reader.read(new ScaleCodecReader(input)); | ||
assertNotNull(message); | ||
assertEquals(GrandpaConsensusMessageFormat.GRANDPA_RESUME, message.getFormat()); | ||
assertEquals(25L, message.getDelay()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters