Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add Ronin Chain #62

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion core/chains/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
OSMO Token = "OSMO"
PLS Token = "PLS"
REGEN Token = "REGEN"
RONIN Token = "RONIN"
RUNE Token = "RUNE"
SEI Token = "SEI"
SOL Token = "SOL"
Expand Down Expand Up @@ -86,6 +87,8 @@ func (t Token) ChainName() string {
return "Pulsechain"
case REGEN:
return "Regen Network"
case RONIN:
return "Ronin Chain"
case RUNE:
return "Thorchain"
case SEI:
Expand All @@ -103,7 +106,9 @@ func (t Token) ChainName() string {
}
}

var Tokens = []Token{ADA, ALGO, APT, ATOM, AVAX, BLD, BNB, DOT, EGLD, GRT, HBAR, JUNO, MATIC, MINA, NEAR, OSMO, PLS, REGEN, RUNE, SEI, SOL, STARS, SUI, TIA}

var Tokens = []Token{ADA, ALGO, APT, ATOM, AVAX, BLD, BNB, DOT, EGLD, GRT, HBAR, JUNO, MATIC, MINA, NEAR, OSMO, PLS, REGEN, RONIN, RUNE, SEI, SOL, STARS, SUI, TIA}


// NewState returns a new fresh state.
func NewState() ChainState {
Expand Down Expand Up @@ -175,6 +180,8 @@ func newValues(token Token) (int, error) {
currVal, err = Pulsechain()
case REGEN:
currVal, err = Regen()
case RONIN:
currVal, err = Ronin()
case RUNE:
currVal, err = Thorchain()
case SEI:
Expand Down
193 changes: 193 additions & 0 deletions core/chains/ronin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package chains

import (
"encoding/json"
"fmt"
"io"
"log"
"math/big"
"net/http"
"sort"
"strings"

"github.com/xenowits/nakamoto-coefficient-calculator/core/utils"
)

type RoninResponse []struct {
Address string `json:"address"`
TotalStaked string `json:"totalStaked"`
Status string `json:"status"`
}

const MaxFinalityVotePercentage uint16 = 10_000
const MaxFinalityVoteThreshold = 22

const RoninValidatorQuery = `{
"query": "query ValidatorOrCandidates { ValidatorOrCandidates { address totalStaked status } }"
}`

func Ronin() (int, error) {

url := fmt.Sprintf("https://indexer.roninchain.com/query")

var totalStakes []*big.Int

// Create a new request using http
req, err := http.NewRequest("POST", url, strings.NewReader(RoninValidatorQuery))

// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return 0, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}

var response struct {
Data struct {
ValidatorOrCandidates RoninResponse
}
}
err = json.Unmarshal(body, &response)
if err != nil {
return 0, err
}

// loop through the validators stakes
for _, ele := range response.Data.ValidatorOrCandidates {
// skip validators that are no longer active
if ele.Status != "" {
continue
}
staked := new(big.Int)
staked, ok := staked.SetString(ele.TotalStaked, 10)
if !ok {
return 0, fmt.Errorf("failed to convert %s to big.Int", ele.TotalStaked)
}
totalStakes = append(totalStakes, staked)
}

// need to sort the stakes in descending order since they are in random order
sort.Slice(totalStakes, func(i, j int) bool {
res := (totalStakes[i]).Cmp(totalStakes[j])
return res == 1
})

// normalizes the finality vote weight
_ = normalizeFinalityVoteWeight(totalStakes, MaxFinalityVoteThreshold)

// convert []*big.Int to []big.Int
totalStakesResult := pointerSliceToSlice(totalStakes)

totalStake := utils.CalculateTotalVotingPowerBigNums(totalStakesResult)
fmt.Println("Total voting power:", new(big.Float).SetInt(totalStake))

// now we're ready to calculate the nakomoto coefficient
nakamotoCoefficient := utils.CalcNakamotoCoefficientBigNums(totalStake, totalStakesResult)
fmt.Println("The Nakamoto coefficient for Ronin is", nakamotoCoefficient)

return nakamotoCoefficient, nil
}

func pointerSliceToSlice(s []*big.Int) []big.Int {
var slice []big.Int
for _, v := range s {
slice = append(slice, *v)
}
return slice
}

// From: https://github.com/axieinfinity/ronin/blob/adc5849b5af046532755e5dae8f023c143d5494c/consensus/consortium/common/utils.go#L76
func normalizeFinalityVoteWeight(stakedAmounts []*big.Int, threshold int) []uint16 {
weights := make([]uint16, 0, len(stakedAmounts))

// The candidate list is too small, so weight is equal among the candidates
if len(stakedAmounts) <= threshold {
for range stakedAmounts {
weights = append(weights, MaxFinalityVotePercentage/uint16(len(stakedAmounts)))
}

return weights
}

cpyStakedAmounts := make([]*big.Int, len(stakedAmounts))
for i, stakedAmount := range stakedAmounts {
cpyStakedAmounts[i] = new(big.Int).Set(stakedAmount)
}

// Sort staked amount in descending order
for i := 0; i < len(cpyStakedAmounts)-1; i++ {
for j := i + 1; j < len(cpyStakedAmounts); j++ {
if cpyStakedAmounts[i].Cmp(cpyStakedAmounts[j]) < 0 {
cpyStakedAmounts[i], cpyStakedAmounts[j] = cpyStakedAmounts[j], cpyStakedAmounts[i]
}
}
}

totalStakedAmount := new(big.Int)
for _, stakedAmount := range cpyStakedAmounts {
totalStakedAmount.Add(totalStakedAmount, stakedAmount)
}
weightThreshold := new(big.Int).Div(totalStakedAmount, big.NewInt(int64(threshold)))

pointer := 0
sumOfUnchangedElements := totalStakedAmount
for {
sumOfChangedElements := new(big.Int)
shouldBreak := true
for cpyStakedAmounts[pointer].Cmp(weightThreshold) > 0 {
sumOfChangedElements.Add(sumOfChangedElements, cpyStakedAmounts[pointer])
shouldBreak = false
pointer++
}

if shouldBreak {
break
}

sumOfUnchangedElements = new(big.Int).Sub(sumOfUnchangedElements, sumOfChangedElements)
weightThreshold = new(big.Int).Div(
sumOfUnchangedElements,
new(big.Int).Sub(big.NewInt(int64(threshold)), big.NewInt(int64(pointer))),
)
}

for i, stakedAmount := range stakedAmounts {
if stakedAmount.Cmp(weightThreshold) > 0 {
stakedAmounts[i] = weightThreshold
}
}

totalStakedAmount.SetUint64(0)
for _, stakedAmount := range stakedAmounts {
totalStakedAmount.Add(totalStakedAmount, stakedAmount)
}

for _, stakedAmount := range stakedAmounts {
weight := new(big.Int).Mul(stakedAmount, big.NewInt(int64(MaxFinalityVotePercentage)))
weight.Div(weight, totalStakedAmount)

weights = append(weights, uint16(weight.Uint64()))
}

// Due to the imprecision of division, the remaining weight for the total to reach 100% is
// split equally across cnadidates. After this step, the total weight may still not reach
// 100% but the imprecision is neglectible (lower than the length of candidate list)
var totalFinalityWeight uint16
for _, weight := range weights {
totalFinalityWeight += weight
}
cutOffWeight := MaxFinalityVotePercentage - totalFinalityWeight
topUpWeight := cutOffWeight / uint16(len(weights))
for i := range weights {
weights[i] += topUpWeight
}

return weights
}