Skip to content

Commit

Permalink
feat: Added BeefyConsensusMessage and BeefyConsensusMessageFormat, im…
Browse files Browse the repository at this point in the history
…plemented scale reading. Introduced beefy consensus message processing in addBlockToTree() method (without implementation of the handler).
  • Loading branch information
Hristiyan Mitov committed Feb 20, 2025
1 parent 4f6dcf8 commit fa338e5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
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);
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;
}
}
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

0 comments on commit fa338e5

Please sign in to comment.