-
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.
feat: add grandpa state with derivePrimary method
- Loading branch information
Georgi Grigorov
committed
Dec 18, 2024
1 parent
74ca155
commit 62a924c
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/com/limechain/grandpa/state/GrandpaState.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,46 @@ | ||
package com.limechain.grandpa.state; | ||
|
||
import com.limechain.chain.lightsyncstate.Authority; | ||
import com.limechain.network.protocol.grandpa.messages.catchup.res.SignedVote; | ||
import com.limechain.network.protocol.grandpa.messages.commit.Vote; | ||
import lombok.Getter; | ||
import org.bouncycastle.math.ec.rfc8032.Ed25519; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.math.BigInteger; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
// TODO: GrandpaState is not fully implemented | ||
/** | ||
* Represents the state information for the current round and authorities that are needed | ||
* for block finalization with GRANDPA. | ||
* Note: Intended for use only when the host is configured as an Authoring Node. | ||
*/ | ||
@Getter | ||
@Component | ||
public class GrandpaState { | ||
|
||
private static final long THRESHOLD_NUMERATOR = 2L; | ||
private static final long THRESHOLD_DENOMINATOR = 3L; | ||
|
||
private List<Authority> voters; | ||
private BigInteger setId; | ||
private BigInteger roundNumber; | ||
|
||
//TODO: This may not be the best place for those maps | ||
private Map<Ed25519, Vote> precommits = new ConcurrentHashMap<>(); | ||
private Map<Ed25519, Vote> prevotes = new ConcurrentHashMap<>(); | ||
private Map<Ed25519, SignedVote> pvEquivocations = new ConcurrentHashMap<>(); | ||
private Map<Ed25519, SignedVote> pcEquivocations = new ConcurrentHashMap<>(); | ||
|
||
public long getThreshold() { | ||
return (THRESHOLD_NUMERATOR * voters.size()) / THRESHOLD_DENOMINATOR; | ||
} | ||
|
||
public BigInteger derivePrimary() { | ||
var votersCount = BigInteger.valueOf(voters.size()); | ||
return roundNumber.remainder(votersCount); | ||
} | ||
} |