-
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.
Added Digest Helper and PreDigestReader (#590)
Added Digest Helper class for processing different types of header digests and PreDigestReader
- Loading branch information
Showing
11 changed files
with
188 additions
and
127 deletions.
There are no files selected for viewing
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/limechain/babe/predigest/scale/PreDigestReader.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,26 @@ | ||
package com.limechain.babe.predigest.scale; | ||
|
||
import com.limechain.babe.predigest.BabePreDigest; | ||
import com.limechain.babe.predigest.PreDigestType; | ||
import io.emeraldpay.polkaj.scale.ScaleCodecReader; | ||
import io.emeraldpay.polkaj.scale.ScaleReader; | ||
import io.emeraldpay.polkaj.scale.reader.UInt64Reader; | ||
|
||
import static io.emeraldpay.polkaj.schnorrkel.VrfOutputAndProof.OUTPUT_BYTE_LEN; | ||
import static io.emeraldpay.polkaj.schnorrkel.VrfOutputAndProof.PROOF_BYTE_LEN; | ||
|
||
public class PreDigestReader implements ScaleReader<BabePreDigest> { | ||
@Override | ||
public BabePreDigest read(ScaleCodecReader reader) { | ||
BabePreDigest preDigest = new BabePreDigest(); | ||
PreDigestType type = PreDigestType.getByValue(reader.readByte()); | ||
preDigest.setType(type); | ||
preDigest.setAuthorityIndex(reader.readUint32()); | ||
preDigest.setSlotNumber(new UInt64Reader().read(reader)); | ||
if (!PreDigestType.BABE_SECONDARY_PLAIN.equals(type)) { | ||
preDigest.setVrfOutput(reader.readByteArray(OUTPUT_BYTE_LEN)); | ||
preDigest.setVrfProof(reader.readByteArray(PROOF_BYTE_LEN)); | ||
} | ||
return preDigest; | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
src/main/java/com/limechain/network/protocol/warp/DigestHelper.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,50 @@ | ||
package com.limechain.network.protocol.warp; | ||
|
||
import com.limechain.babe.predigest.BabePreDigest; | ||
import com.limechain.babe.predigest.scale.PreDigestReader; | ||
import com.limechain.babe.state.EpochState; | ||
import com.limechain.network.protocol.warp.dto.ConsensusEngine; | ||
import com.limechain.network.protocol.warp.dto.DigestType; | ||
import com.limechain.network.protocol.warp.dto.HeaderDigest; | ||
import com.limechain.rpc.server.AppBean; | ||
import com.limechain.utils.scale.ScaleUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Helper class for processing different types of header digests | ||
*/ | ||
public class DigestHelper { | ||
private EpochState epochState; | ||
|
||
public DigestHelper() { | ||
this.epochState = AppBean.getBean(EpochState.class); | ||
} | ||
|
||
public void handleHeaderDigests(HeaderDigest[] headerDigests) { | ||
if (headerDigests == null || headerDigests.length == 0) { | ||
return; | ||
} | ||
updateEpochStateIfBabeConsensusMessageExists(headerDigests); | ||
handleBabePreRuntimeDigest(headerDigests); | ||
} | ||
|
||
private void updateEpochStateIfBabeConsensusMessageExists(HeaderDigest[] headerDigests) { | ||
Arrays.stream(headerDigests) | ||
.filter(headerDigest -> DigestType.CONSENSUS_MESSAGE.equals(headerDigest.getType()) && | ||
ConsensusEngine.BABE.equals(headerDigest.getId())) | ||
.findFirst() | ||
.map(HeaderDigest::getMessage) | ||
.ifPresent(message -> epochState.updateNextEpochBlockConfig(message)); | ||
} | ||
|
||
private Optional<BabePreDigest> handleBabePreRuntimeDigest(HeaderDigest[] headerDigests) { | ||
return Arrays.stream(headerDigests) | ||
.filter(headerDigest -> DigestType.PRE_RUNTIME.equals(headerDigest.getType()) && | ||
ConsensusEngine.BABE.equals(headerDigest.getId())) | ||
.findFirst() | ||
.map(HeaderDigest::getMessage) | ||
.map(message -> ScaleUtils.Decode.decode(message, new PreDigestReader())); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/limechain/babe/scale/PreDigestScaleTest.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,36 @@ | ||
package com.limechain.babe.scale; | ||
|
||
import com.limechain.babe.predigest.BabePreDigest; | ||
import com.limechain.babe.predigest.PreDigestType; | ||
import com.limechain.babe.predigest.scale.PreDigestReader; | ||
import com.limechain.babe.predigest.scale.PreDigestWriter; | ||
import com.limechain.utils.StringUtils; | ||
import com.limechain.utils.scale.ScaleUtils; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigInteger; | ||
|
||
import static io.emeraldpay.polkaj.schnorrkel.VrfOutputAndProof.OUTPUT_BYTE_LEN; | ||
import static io.emeraldpay.polkaj.schnorrkel.VrfOutputAndProof.PROOF_BYTE_LEN; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class PreDigestScaleTest { | ||
|
||
@Test | ||
public void testEncodeAndDecode_BABE_SECONDARY_VRF() { | ||
byte[] bytes = StringUtils.hexToBytes("0x03b7010000286f301100000000424dc7bc71c9b0f3a15b6aba389471fff57e154dbf3dc046f47513a38375ce4cfe421df446b2b7a0f5b1b2e69c810a6ba36787c0301e6636df3e45688116e005363dfe9922b3b7cc7b80e2fdab8b0b98dcfb4b96cc470fb35f753debda40aa0e"); | ||
BabePreDigest preDigest = ScaleUtils.Decode.decode(bytes, new PreDigestReader()); | ||
assertEquals(PreDigestType.BABE_SECONDARY_VRF, preDigest.getType()); | ||
assertEquals(439, preDigest.getAuthorityIndex()); | ||
assertEquals(BigInteger.valueOf(288386856), preDigest.getSlotNumber()); | ||
byte[] vrfOutput = preDigest.getVrfOutput(); | ||
byte[] vrfProof = preDigest.getVrfProof(); | ||
assertNotNull(vrfOutput); | ||
assertNotNull(vrfProof); | ||
assertEquals(OUTPUT_BYTE_LEN, vrfOutput.length); | ||
assertEquals(PROOF_BYTE_LEN, vrfProof.length); | ||
|
||
byte[] encode = ScaleUtils.Encode.encode(new PreDigestWriter(), preDigest); | ||
assertArrayEquals(bytes, encode); | ||
} | ||
} |
73 changes: 0 additions & 73 deletions
73
src/test/java/com/limechain/babe/scale/PreDigestWriterTest.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.