-
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.
BEEFY consensus message scale classes (#786)
# Description - Added BeefyConsensusMessage and BeefyConsensusMessageFormat(enum class) classes - Implemented scale reading. - Introduced beefy consensus message processing in addBlockToTree() method (without implementation of the handler) Fixes #771 --------- Co-authored-by: Hristiyan Mitov <hristiyan.mitov@limechain.tech>
- Loading branch information
Showing
8 changed files
with
145 additions
and
41 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...n/java/com/limechain/network/protocol/beefy/messages/consensus/BeefyConsensusMessage.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.beefy.messages.consensus; | ||
|
||
import lombok.Data; | ||
|
||
import java.math.BigInteger; | ||
import java.util.List; | ||
|
||
@Data | ||
public class BeefyConsensusMessage { | ||
private BeefyConsensusMessageFormat format; | ||
private List<byte[]> authorityPublicKeys; | ||
private BigInteger authoritySetId; | ||
private BigInteger disabledAuthority; | ||
// The 32-byte Merkle Mountain Range (MMR) root payload hash. | ||
private byte[] mmrRootHash; | ||
} |
23 changes: 23 additions & 0 deletions
23
.../com/limechain/network/protocol/beefy/messages/consensus/BeefyConsensusMessageFormat.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.beefy.messages.consensus; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum BeefyConsensusMessageFormat { | ||
BEEFY_CHANGED_AUTHORITIES(1), BEEFY_ON_DISABLED(2), BEEFY_MMR_ROOT(3); | ||
|
||
private final int format; | ||
|
||
BeefyConsensusMessageFormat(int format) { | ||
this.format = format; | ||
} | ||
|
||
public static BeefyConsensusMessageFormat fromFormat(byte format) { | ||
for (BeefyConsensusMessageFormat messageFormat : values()) { | ||
if (messageFormat.getFormat() == format) { | ||
return messageFormat; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unknown beefy consensus message format: " + format); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
.../com/limechain/network/protocol/beefy/messages/consensus/BeefyConsensusMessageReader.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,42 @@ | ||
package com.limechain.network.protocol.beefy.messages.consensus; | ||
|
||
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 lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class BeefyConsensusMessageReader implements ScaleReader<BeefyConsensusMessage> { | ||
|
||
private static final BeefyConsensusMessageReader INSTANCE = new BeefyConsensusMessageReader(); | ||
public static final int ECDSA_PUBLIC_KEY_LENGTH = 33; | ||
|
||
public static BeefyConsensusMessageReader getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public BeefyConsensusMessage read(ScaleCodecReader reader) { | ||
|
||
BeefyConsensusMessage beefyConsensusMessage = new BeefyConsensusMessage(); | ||
BeefyConsensusMessageFormat format = BeefyConsensusMessageFormat.fromFormat(reader.readByte()); | ||
beefyConsensusMessage.setFormat(format); | ||
|
||
switch (format) { | ||
case BEEFY_CHANGED_AUTHORITIES -> { | ||
List<byte[]> authorityPublicKeys = new ListReader<>( | ||
rdr -> rdr.readByteArray(ECDSA_PUBLIC_KEY_LENGTH)).read(reader); | ||
beefyConsensusMessage.setAuthorityPublicKeys(authorityPublicKeys); | ||
beefyConsensusMessage.setAuthoritySetId(new UInt64Reader().read(reader)); | ||
} | ||
case BEEFY_ON_DISABLED -> beefyConsensusMessage.setDisabledAuthority(new UInt64Reader().read(reader)); | ||
case BEEFY_MMR_ROOT -> beefyConsensusMessage.setMmrRootHash(reader.readUint256()); | ||
} | ||
|
||
return beefyConsensusMessage; | ||
} | ||
} |
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
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
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
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
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