-
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 verify slot winner and fixed network (#618)
Implement verify slot winner and fix network block announce pears
- Loading branch information
Showing
5 changed files
with
168 additions
and
8 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
32 changes: 32 additions & 0 deletions
32
src/main/java/com/limechain/babe/BlockProductionVerifier.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,32 @@ | ||
package com.limechain.babe; | ||
|
||
import com.limechain.babe.state.EpochState; | ||
import com.limechain.chain.lightsyncstate.Authority; | ||
import com.limechain.rpc.server.AppBean; | ||
import com.limechain.utils.LittleEndianUtils; | ||
import io.emeraldpay.polkaj.merlin.TranscriptData; | ||
import io.emeraldpay.polkaj.schnorrkel.Schnorrkel; | ||
import io.emeraldpay.polkaj.schnorrkel.VrfOutputAndProof; | ||
import lombok.extern.java.Log; | ||
|
||
import java.math.BigInteger; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
|
||
@Log | ||
public class BlockProductionVerifier { | ||
|
||
private EpochState epochState = AppBean.getBean(EpochState.class); | ||
|
||
public boolean verifySlotWinner(int authorityIndex, BigInteger epochIndex, byte[] randomness, BigInteger slotNumber, VrfOutputAndProof vrfOutputAndProof) { | ||
List<Authority> authorities = epochState.getCurrentEpochData().getAuthorities(); | ||
Authority verifyingAuthority = authorities.get(authorityIndex); | ||
TranscriptData transcriptData = Authorship.makeTranscript(randomness, slotNumber, epochIndex); | ||
BigInteger threshold = Authorship.calculatePrimaryThreshold(epochState.getCurrentEpochDescriptor().getConstant(), authorities, authorityIndex); | ||
var isBelowThreshold = LittleEndianUtils.fromLittleEndianByteArray(vrfOutputAndProof.getOutput()).compareTo(threshold) < 0; | ||
if (!isBelowThreshold) { | ||
log.log(Level.WARNING, "Block producer is not a winner of the slot"); | ||
} | ||
return Schnorrkel.getInstance().vrfVerify(new Schnorrkel.PublicKey(verifyingAuthority.getPublicKey()), transcriptData, vrfOutputAndProof) && isBelowThreshold; | ||
} | ||
} |
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
127 changes: 127 additions & 0 deletions
127
src/test/java/com/limechain/babe/BlockProductionVerifierTest.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,127 @@ | ||
package com.limechain.babe; | ||
|
||
import com.limechain.babe.state.EpochData; | ||
import com.limechain.babe.state.EpochDescriptor; | ||
import com.limechain.babe.state.EpochState; | ||
import com.limechain.chain.lightsyncstate.Authority; | ||
import io.emeraldpay.polkaj.merlin.TranscriptData; | ||
import io.emeraldpay.polkaj.schnorrkel.Schnorrkel; | ||
import io.emeraldpay.polkaj.schnorrkel.VrfOutputAndProof; | ||
import org.javatuples.Pair; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.math.BigInteger; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.*; | ||
|
||
class BlockProductionVerifierTest { | ||
|
||
@Mock | ||
private EpochState epochState; | ||
|
||
@Mock | ||
private EpochData currentEpochData; | ||
|
||
@Mock | ||
private EpochDescriptor epochDescriptor; | ||
|
||
@InjectMocks | ||
private BlockProductionVerifier blockProductionVerifier; | ||
|
||
@Mock | ||
private VrfOutputAndProof vrfOutputAndProof; | ||
|
||
private final BigInteger epochIndex = BigInteger.ONE; | ||
private final byte[] randomness = new byte[]{0x01, 0x02, 0x03}; | ||
private final BigInteger slotNumber = BigInteger.TEN; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
|
||
@Test | ||
void testVerifySlotWinner_AboveThresholdAndValidVrf() { | ||
try (MockedStatic<Schnorrkel> mockedSchnorrkel = mockStatic(Schnorrkel.class)) { | ||
Schnorrkel schnorrkelMock = mock(Schnorrkel.class); | ||
mockedSchnorrkel.when(Schnorrkel::getInstance).thenReturn(schnorrkelMock); | ||
|
||
when(schnorrkelMock.vrfVerify(any(Schnorrkel.PublicKey.class), any(TranscriptData.class), eq(vrfOutputAndProof))) | ||
.thenReturn(true); | ||
|
||
when(epochState.getCurrentEpochData()).thenReturn(currentEpochData); | ||
when(epochState.getCurrentEpochDescriptor()).thenReturn(epochDescriptor); | ||
when(epochDescriptor.getConstant()).thenReturn(new Pair<>(BigInteger.ZERO, BigInteger.valueOf(4))); | ||
when(currentEpochData.getAuthorities()).thenReturn(List.of( | ||
new Authority(new byte[]{0x01, 0x02, 0x03}, BigInteger.ONE), | ||
new Authority(new byte[32], BigInteger.ONE), | ||
new Authority(new byte[32], BigInteger.ONE) | ||
)); | ||
when(vrfOutputAndProof.getOutput()).thenReturn(new byte[]{0x01, 0x02}); | ||
|
||
boolean result = blockProductionVerifier.verifySlotWinner(0, epochIndex, randomness, slotNumber, vrfOutputAndProof); | ||
assertFalse(result); | ||
mockedSchnorrkel.verify(Schnorrkel::getInstance, times(1)); | ||
} | ||
} | ||
|
||
@Test | ||
void testVerifySlotWinner_BelowThresholdAndValidVrf() { | ||
try (MockedStatic<Schnorrkel> mockedSchnorrkel = mockStatic(Schnorrkel.class)) { | ||
Schnorrkel schnorrkelMock = mock(Schnorrkel.class); | ||
mockedSchnorrkel.when(Schnorrkel::getInstance).thenReturn(schnorrkelMock); | ||
|
||
when(schnorrkelMock.vrfVerify(any(Schnorrkel.PublicKey.class), any(TranscriptData.class), eq(vrfOutputAndProof))) | ||
.thenReturn(true); | ||
|
||
when(epochState.getCurrentEpochData()).thenReturn(currentEpochData); | ||
when(epochState.getCurrentEpochDescriptor()).thenReturn(epochDescriptor); | ||
when(epochDescriptor.getConstant()).thenReturn(new Pair<>(BigInteger.valueOf(3), BigInteger.valueOf(4))); | ||
when(currentEpochData.getAuthorities()).thenReturn(List.of( | ||
new Authority(new byte[]{0x01, 0x02, 0x03}, BigInteger.valueOf(1000)), | ||
new Authority(new byte[32], BigInteger.valueOf(500)), | ||
new Authority(new byte[32], BigInteger.valueOf(500)) | ||
)); | ||
when(vrfOutputAndProof.getOutput()).thenReturn(new byte[]{0x01, 0x02}); | ||
|
||
boolean result = blockProductionVerifier.verifySlotWinner(0, epochIndex, randomness, slotNumber, vrfOutputAndProof); | ||
assertTrue(result); | ||
mockedSchnorrkel.verify(Schnorrkel::getInstance, times(1)); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
void testVerifySlotWinner_InvalidVrfOutput() { | ||
try (MockedStatic<Schnorrkel> mockedSchnorrkel = mockStatic(Schnorrkel.class)) { | ||
Schnorrkel schnorrkelMock = mock(Schnorrkel.class); | ||
mockedSchnorrkel.when(Schnorrkel::getInstance).thenReturn(schnorrkelMock); | ||
|
||
when(schnorrkelMock.vrfVerify(any(Schnorrkel.PublicKey.class), any(TranscriptData.class), eq(vrfOutputAndProof))) | ||
.thenReturn(false); | ||
|
||
when(epochState.getCurrentEpochData()).thenReturn(currentEpochData); | ||
when(epochState.getCurrentEpochDescriptor()).thenReturn(epochDescriptor); | ||
when(epochDescriptor.getConstant()).thenReturn(new Pair<>(BigInteger.valueOf(3), BigInteger.valueOf(4))); | ||
when(currentEpochData.getAuthorities()).thenReturn(List.of( | ||
new Authority(new byte[]{0x01, 0x02, 0x03}, BigInteger.valueOf(1000)), | ||
new Authority(new byte[32], BigInteger.valueOf(500)), | ||
new Authority(new byte[32], BigInteger.valueOf(500)) | ||
)); | ||
when(vrfOutputAndProof.getOutput()).thenReturn(new byte[]{0x01, 0x02}); | ||
|
||
boolean result = blockProductionVerifier.verifySlotWinner(0, epochIndex, randomness, slotNumber, vrfOutputAndProof); | ||
assertFalse(result); | ||
mockedSchnorrkel.verify(Schnorrkel::getInstance, times(1)); | ||
} | ||
} | ||
} |