Skip to content
New issue

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

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

Already on GitHub? # to your account

BEEFY consensus message scale classes #786

Merged
merged 4 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 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(33)).read(reader);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you create a constant for that 33, it feels like a magic number right now.

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;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each digest is added separately in the header. This means we can have more than one digest per type. With the current implementation we use findFirst() and that would not work. I suggest we rework the getBabeConsensusMessage, getGrandpaConsensusMessage, getBeefyConsensusMessage so that they return a list of scale decoded messages instead of a single option.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.limechain.babe.consensus.scale.BabeConsensusMessageReader;
import com.limechain.babe.predigest.BabePreDigest;
import com.limechain.babe.predigest.scale.PreDigestReader;
import com.limechain.network.protocol.beefy.messages.consensus.BeefyConsensusMessage;
import com.limechain.network.protocol.beefy.messages.consensus.BeefyConsensusMessageReader;
import com.limechain.network.protocol.grandpa.messages.consensus.GrandpaConsensusMessage;
import com.limechain.network.protocol.grandpa.messages.consensus.GrandpaConsensusMessageReader;
import com.limechain.network.protocol.warp.dto.BlockHeader;
Expand Down Expand Up @@ -43,6 +45,15 @@ public static Optional<GrandpaConsensusMessage> getGrandpaConsensusMessage(Heade
.map(message -> ScaleUtils.Decode.decode(message, GrandpaConsensusMessageReader.getInstance()));
}

public static Optional<BeefyConsensusMessage> getBeefyConsensusMessage(HeaderDigest[] headerDigests) {
return Arrays.stream(headerDigests)
.filter(headerDigest -> DigestType.CONSENSUS_MESSAGE.equals(headerDigest.getType()) &&
ConsensusEngine.BEEFY.equals(headerDigest.getId()))
.findFirst()
.map(HeaderDigest::getMessage)
.map(message -> ScaleUtils.Decode.decode(message, BeefyConsensusMessageReader.getInstance()));
}

public static Optional<BabePreDigest> getBabePreRuntimeDigest(HeaderDigest[] headerDigests) {
return Arrays.stream(headerDigests)
.filter(headerDigest -> DigestType.PRE_RUNTIME.equals(headerDigest.getType()) &&
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/limechain/storage/block/BlockHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ public void addBlockToTree(Block block, Instant arrivalTime) {
));

grandpaSetState.handleAuthoritySetChange(header.getBlockNumber());

asyncExecutor.executeAndForget(() -> DigestHelper.getBeefyConsensusMessage(header.getDigest())
.ifPresent(cm -> {
}
//Todo: handleBeefyConsensusMessage
));
}
}

Expand Down
Loading